2

I created a few code below

pragma solidity ^0.4.19;

contract SendAndTransferExample {

    function SimpleSendToAccount() public returns(bool) {
       return msg.sender.send(10000000000000000000);
    }

    function SimpleTransferToAccount() public {
       msg.sender.transfer(10000000000000000000);
    }
}

I executed both functions using JavaScript vm and that result always same like that. first result when I executed send() function enter image description here and below result when I executed trasfer() function enter image description here

transact to SendAndTransferExample.SimpleTransferToAccount errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value.  Debug the transaction to get more information. 

I have no idea what was wrong. Seombody help me please.

  • It looks like you are trying to transfer ETH from the contract to the msg.sender. Does the contract have any ETH in it to send? As the error message suggests, you could add a constructor function or another payable function which allows the contract to accept ETH, which it could then send. – Shawn Tabrizi Sep 6 '18 at 5:36
  • I tried to put ETH at contract and I got some error. VM error: revert. revert The transaction has been reverted to the initial state. Note: The constructor should be payable if you send value. Debug the transaction to get more information. – graychocobo Sep 6 '18 at 5:47
  • And how can I add constructor or payable function which allows that? – graychocobo Sep 6 '18 at 5:49
2

Try this for your contract:

pragma solidity ^0.4.24;

contract SendAndTransferExample {

    constructor() public payable { }

    function SimpleSendToAccount() public returns(bool) {
       return msg.sender.send(10000000000000000000);
    }

    function SimpleTransferToAccount() public {
       msg.sender.transfer(10000000000000000000);
    }

    function() public payable { }
}

Here I have added 2 functions:

  1. A constructor() function which runs only once, right when the contract is created. You can use this function to initialize variables, like who the owner of the contract is and other global values. In this case, we made the function payable, which means that it can accept ETH. So during the contract creation, you can pass ETH as msg.value and the upon contract creation, the ETH will be accepted into the contract as part of its balance.

  2. A fallback function (function()) which runs whenever you call the contract, and none of the other functions match the function identifier, or when no data was supplied. This means that you can simply send ETH to the contract, and because the fallback function is also payable, it will accept that ETH.

Using either of these functions, you should be able to send to your contract enough ETH such that your other functions will work.

You should spend some time reading the solidity documentation which goes over both of these topics and much more!

1

It will be something like that:

function send(address _receiver) payable {
_receiver.send(msg.value);

}

Your method should payable to send ether.

N.B. address.send(value) and address.transfer(value) both are same except send doesn't throw exception on failure and consumes all of your transaction fees and transfer throw exception.

  • This is not accurate. A function does NOT need to be payable to send ether. Only to receive ether. – Shawn Tabrizi Sep 6 '18 at 6:06
  • To perform send or transfer in smart contract, the method should receive ether from method invoker which is here msg.value. – Smalik Sep 6 '18 at 6:14
  • You are making assumptions about what the user's function is trying to do. Here you have built a function which forwards ETH to another address. In this case, your function makes sense, but that is not necessarily what the user is trying to do, and the statement you make is not true in general. – Shawn Tabrizi Sep 6 '18 at 6:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.