solidity transaction error, The called function should be payable if you send value and the value you send should be less than your current balance
Asked Answered
B

11

23

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);
    
      }
  }

enter image description here

Bibby answered 23/4, 2020 at 13:42 Comment(1)
a bit late this comment, are you sending ether when calling buyToken?Ausgleich
P
5

First, you need to deploy the ERC20Token.

enter image description here

In my case, it was deployed to the address 0xcD6a42782d230D7c13A74ddec5dD140e55499Df9.

enter image description here

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

enter image description here


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.

enter image description here

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);

enter image description here

And as you can see in the screenshot, the transaction was successful.

enter image description here

Piscator answered 24/8, 2021 at 1:10 Comment(0)
C
4

You should use payable in your constructor as shown below:

constructor(address payable _wallet) public payable{
     ...
}
Cassandracassandre answered 22/11, 2020 at 17:15 Comment(1)
it is working only in constructor why can't we use in another function function enter() public payable { }Flintshire
S
2

Because your current balance is 0. You can try to use Low level interactions to send some balance to the contract address. Let me know if that works!

enter image description here

Scend answered 9/7, 2020 at 2:14 Comment(1)
I am having this issue, and this doesn't work. I get the same error when deploying with any amount of wei or ether.Plea
U
1

An answer I didn't see anywhere else is the need to just refresh the page/browser if you are running Remix via browser

Uncalledfor answered 3/6, 2021 at 21:53 Comment(0)
T
1

Check your ENVIRONMENT if you are using remix and make sure you chose Injected web3for cool guys who using this environment!

Trapezoid answered 23/8, 2021 at 9:37 Comment(0)
C
0

Check your balance by using balanceOf() function like, balanceOf(owner). Because, the token you are creating is your balance , if you don't have something you can't buy things. The smart contract that you have write here having the balance as of the no of tokens you own. I'm also have error in my smart contract too like this and found out this after a day.

To overcome this do the following,

  • Add programs to add and subtract balance from the balance of the individual both the buyer and owner.
  • If you want give some entry-level balance for new comers to start buy things.
Carothers answered 18/6, 2021 at 10:22 Comment(0)
M
0

Make sure you deploy ERC20Token first. Then next to the deployed ERC20Token is a clipboard to copy the address of the ER20Token. Copy that address into the _token field of MyContract, and copy one of the wallet addresses from the accounts too, into the _wallet field.

Switch account, so you see a different wallet address, and then click the MyContract buyToken button, and you should no longer have an error.

Your code is right, but you are going wrong with wallet/token addresses.

Miracidium answered 23/8, 2021 at 20:19 Comment(0)
E
0

Try going with O wei, since you're using a test environment

Elan answered 24/10, 2021 at 5:11 Comment(0)
D
0

This Error also comes when your contract does not have anything to accept ether. Make sure have a look on that. receive() function should exist.

Diamante answered 4/3, 2022 at 10:37 Comment(0)
C
0

change this line of code : wallet.transfer(msg.value); to this wallet.transfer(payable(msg.value));

Cis answered 6/3, 2022 at 15:9 Comment(1)
Can you add exactly how this solves the issue? - A reference or brief explanation would be helpful.Averyaveryl
B
0

To anyone who comes here looking for an answer, but the above isn't it, if the function you're calling does not need to receive eth or any value of any sort, i.e the function is not payable and the action to be carried out would be on a front end then Remix can't carry out that function. This was the case for me, but if i'm wrong someone correct me.

Borzoi answered 11/7, 2022 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.