Search code examples
solidityremix

Solidity: Send some money to myself


I have a question from Solidity, and my IDE is use Remix, I want to send some money to my self.

My Code:

pragma solidity ^0.4.24;
contract toMyself{

    address owner;

    function toMyself()public{
        owner = msg.sender;
    }
    function Send(uint x)public payable{
        owner.transfer(x);
    }
}

But when I press the Send button, it will show me a message like:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?

How can I fix it?


Solution

    1. Are you sure that the contract has enough ether available to send?

    2. Don't you prefer switching

    function Send(uint x)public payable{
        owner.transfer(x);
    }
    

    to

    function Send()public payable{
        owner.transfer(msg.value);
    }
    

    So you send to the owner whatever is coming into the smart contract?

    Also, you can send back whichever quantity has just been sent to the msg.sender in this way:

    function SendBack() public payable{
        msg.sender.transfer(msg.value);
    }
    

    But this will end being useless and wasting some gas.