I'm learning solidity on remix. I was following some tutorial video on youtube and I've got stuck trying to build 2 contracts. contract ERC20Token has function which increases balances. And Mycontract instantiates and call the mint function. I did it the same way as the guy on youtube did, but I ran into error saying when I call buyToken function. The error is saying
transact to MyContract.buyToken errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
Please help me out.
Here is the code.
pragma solidity 0.5.1;
contract ERC20Token{
string public name;
mapping(address => uint256) public balances;
function mint() public {
balances[tx.origin] ++;
}
}
contract MyContract {
address payable wallet;
address public token;
constructor(address payable _wallet, address _token) public {
wallet = _wallet;
token = _token;
}
function buyToken() public payable {
ERC20Token _token = ERC20Token(address(token));
_token.mint();
wallet.transfer(msg.value);
}
}
First, you need to deploy the ERC20Token
.
In my case, it was deployed to the address 0xcD6a42782d230D7c13A74ddec5dD140e55499Df9
.
The address of the ERC20Token
is important for the next step.
Then you deploy the MyContract
. Don't forget to pass the constructor arguments:
_wallet
is any valid address in the Remix JS VM emulator (from the Account
select box)
For the purpose of this answer, I chose the address 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c
, that you will see in the third step as well.
_token
is the token contract address from the previous step
Finally, you can execute the buyToken()
function of the MyContract
.
You can see that before the function execution, the sender has 89 ETH, and is about to send 50 ETH with the transaction.
After it has been successfully executed, the sender now has 39 ETH, and the wallet
has received the 50 ETH because of this line:
wallet.transfer(msg.value);
And as you can see in the screenshot, the transaction was successful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With