Metamask Ed Ethenaut: setting of msg.value to contribute ()
When you start with Ethenout’s path, you need to understand how to interact with the Ethereum Blockchain using Metamasco. In this article, when calling from the contract, we are creating msg.value 'for the function to contribute ()
.
Understanding of the function contribution ()
Before immersing yourself, let’s briefly explore the solution, which “contributes ()”.
Solidity `
Pragma of solidity ^0.8.0;
Ethenaut contract {
The function contributes () Payable public {
// …
}
}
`
In this example, we defined the "Ethenout" contract with the modifier "Inplace", which establishes the behavior of the contract.
Problem: insufficientmsg.value
When called "contributions ()" from the contract using "or" contractual "methods" or "contract" methods (e.g. contract.contribute ()), we must ensure that the ‘msg.value’ is set correctly before sending the amount. However, this value is not explicitly provided due to the safety and debut restrictions.
Solution: Setting MSG.Value 'by falsification function
To solve this problem, we can create a backup space function that setsmsg.value ‘. Here is an example:
Solidity `
Pragma of solidity ^0.8.0;
Ethenaut contract {
// …
Faltback () Payable public {
msg.sender.transfer (0);
}
}
`
In this function of the backup space:
- We call the transmission function ()at the request of the contract (
msg.sender ‘) with a number of 0.
- This is done to set the variable
msg.value 'which is used to call')
from within the contract.
Call customize ()
with backup space
Now that we have created a Fallback function, you can call it from the level 1 Ethenaut node:
`Javascript
Const Ethertout = Abvets Ether.GetContractsance (“Ethernaut”);
Wait for Ethenaut.Contribute ();
`
Example of use:
Suppose we want to test the scenario in which "contributes () with many 10 ETH. We can create a backup space function and call it from our Ethernaut node ::
`Javascript
Const Ethertout = Abvets Ether.GetContractsance (“Ethernaut”);
// Create an instance of a contract
Const Contract = New Ethers.Contract (“Ethenaut”, Ethenaut.abi, “Nonsc/Etereneutiut.sol”, {
From: “”,
});
// define the function of the backup space
Contract.fallback () public due to {{
// MSG.Value tax on 10 ETH
Msg.sender.transfer (Ethernaut.address, 10);
}
Expect a contract.
`
In this example, we have createdFaltback ()which sets the variable
msg.valueto 10 ETH. So we call
adaptation ()from our Ethenout node using the Fallback function.
Conclusion
By settingmsg.value ‘through the function of commitment in the contract, you will be able to interact with” customizing () “without having to manually deliver the amount of ether as a parameter. This is just an example of how you can use Metamask Ed Ethenaut to create more robust and safer interactions with the Ethereum blockchain.