Reputation: 53
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?
Upvotes: 2
Views: 2142
Reputation: 31
There is multiple functions to do that but right now (2022) the recommended method is to use the call
function like this:
function sendETH(address payable _recipient) public payable {
// pass the value you want in Wei
(bool wasSent, bytes memory data) = _recipient.call{value: 1 ether}("");
require(wasSent, "Failed to send Ether");
}
Upvotes: 0
Reputation: 144
Are you sure that the contract has enough ether available to send?
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.
Upvotes: 3
Reputation: 1845
I just checked your code in remix and it works, i just used solidity compiler version 0.5
pragma solidity ^0.5;
contract toMyself{
address owner;
constructor() public{
owner = msg.sender;
}
function Send(uint x)public payable{
msg.sender.transfer(x);
}
}
maybe its because of the no amount in the contract. Secondly when you use Send the uint value should be in wei.
For the domination units http://ethdocs.org/en/latest/ether.html
Upvotes: 2
Reputation: 839
I am just clarifying @Fernando's answer here.
function Send(uint x) public payable {
owner.transfer(x);
}
here x amount of wei will be sent to owner's account form contract's balance. For this to happen your contract needs to hold at least x amount of wei. Not the account that is calling Send
function. Note: here Send
function need not be marked as payable
.
Now in case of
function Send() public payable {
owner.transfer(msg.value);
}
caller of Send
function will send some amount of ether/wei
along with the request. We can retrieve that amount using msg.value
. Then transfer it to the owner's account. Here the contract itself does not need to hold any amount of ether. Note: here Send
function has to be marked as payable
.
Upvotes: 1