Metamask: Ethernaut lvl1: Fallback – how to set msg.value when calling contribute()?

Metamask and Ethernaut: Setting msg.value for contribute()

As you begin your Ethernaut journey, it’s important to understand how to interact with the Ethereum blockchain using Metamask. In this article, we’ll look at setting msg.value for the contribute() function when it’s called from within a contract.

Understanding the contribute() function

Before we dive into the solution, let’s quickly review what the contribute() function does:

Pragma solidity ^0,8.0;

Contract Ethernaut {

Function contribute() public payable {

// ...

}

}

In this example, we’ve defined an Ethernaut contract with an inplace modifier that sets the behavior of the contract.

The problem: Insufficient msg.value

Metamask: Ethernaut lvl1 : Fallback - how to set msg.value when calling contribute()?

When we call contribute() inside a contract using the from or contract callback methods (e.g. contract.contribute()), we need to make sure that msg.value is set correctly before sending the amount. However, for security and debugging reasons, this value is not specified explicitly.

Solution: Set msg.value via fallback function

To solve this problem, we can create a fallback function in the contract that sets msg.value. Here is an example:

Pragma Solid ^0,8.0;

Contract Ethernaut {

// ...

reserve() public paid {

msg.sender.transfer(0);

}

}

In this backup function:

  • We call the function “transfer()” on a contract instance (“msg.sender”) with an amount of 0.
  • This is done to set the variable “msg.value” which will be used when calling “contribute()” within the contract.

Calling contribute() with the fallback function

Now that we have created the backup function, you can call it from your Ethernaut level 1 node:

const ethernaut = await ethers.getContractInstance("Ethernaut");

await ethernaut.contribute();

Example use case:

Suppose we want to test a scenario where “contribute()” is called with an amount of 10 ETH. We can create the backup function and call it from our Ethernaut node:

const ethernaut = await ethers.getContractInstance("Ethernaut");

//Create a contract instance

const contract = new Ether.Contract("Ethernaut", Ethernaut.abi, "nonces/Etherernaut.sol", {

from: "",

});

//Define the fallback function

contract.fallback() public payable {

// Set msg.value to 10 ETH

msg.sender.transfer(ethernaut.address, 10);

}

await contract.contribution();

In this example, we have created a “fallback()” function that sets the variable “msg.value” to 10 ETH when called. We then call contribute() from our Ethernaut node using the fallback function.

Conclusion

By setting msg.value via a fallback function in your contract, you can interact with the contribute() function without having to manually pass an amount of Ether as a parameter. This is just one example of how you can use Metamask and Ethernaut to create more robust and secure interactions with the Ethereum blockchain.

Socials:

Leave a Reply

Your email address will not be published. Required fields are marked *