0

Using this smart contract, I want to transfer from one account to another.

Here is my simple smart contract :

pragma solidity ^0.5.0;
contract Subtract {
  address payable public minter;
  uint public balance;
  event Transfer(address payable indexed _from, address payable indexed _to, uint256 _value);
  mapping (address => uint) public balances;
  constructor() public {
    minter = msg.sender;
  }
  function showSender() public view returns (address)
  {
    return (msg.sender);
  }
  function sendcoin(address payable receiver, uint amount) public {
    receiver.transfer(amount);
  }
}

I got 10 new accounts(each having 100 ethers) from the command:

ganache-cli

Then, in another terminal, in truffle console, I created an account using:

web3.eth.personal.newAccount()

The address of new account was:

'0x4FcE397EFfd8a68f033407769f9f444652534Bb7'

Now to send amount, I used the following command:

Subtract.deployed().then(function(instance) { return instance.sendcoin("0x4FcE397EFfd8a68f033407769f9f444652534Bb7",1e+5)});

Then I got the following error:

Error: Returned error: VM Exception while processing transaction: revert
at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:318:1)
at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:208:1)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/~/web3-providers-http/src/index.js:96:1)
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:112:1
at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth/~/web3-core-requestmanager/src/index.js:140:1
at Object.ErrorResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth/~/web3-core-helpers/src/errors.js:29:1)

I could not understand the error. Can someone please explain what is happening? Please give a possible solution.

Thank you.

  • Do you have 100000 wei in your contract? – goodvibration May 26 at 5:49
  • I don't know. How do I check it? I thought that the amount is transferred from (msg.sender). I think I am wrong. It would be very kind of you if you could tell me how to check if my contract has 10^5 wei. If not there, then how do I transfer it.- @goodvibration – Shailesh Mishra May 26 at 5:56
  • The amount in your code is transferred from the contract to receiver. The caller (msg.sender) will only pay for the gas (a little more than 21,000 wei in this case). Therefore you need to have this amount in your contract before the function is called. – goodvibration May 26 at 6:04
  • Thanks a lot for the clarification. But, how do I add the amount to my contract?- @goodvibration – Shailesh Mishra May 26 at 6:21
  • There are various ways, but the simplest one I suppose, would be to add function() external payable {} in your contract, recompile it, redeploy it, and then call await web3.eth.sendTransaction({from: YourWalletAddress, to: YourContractAddress, value: 100000});. – goodvibration May 26 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.