团结:给自己寄钱

时间:2019-02-10 09:51:13

标签: solidity remix

我有一个来自Solidity的问题,我的IDE是使用Remix,我想给自己寄钱。

我的代码:

pragma solidity ^0.4.24;
contract toMyself{

    address owner;

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

但是当我按下“发送”按钮时,它将向我显示一条消息,例如:

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

我该如何解决?

3 个答案:

答案 0 :(得分:2)

  1. 您确定合同中有足够的醚可以发送吗?

  2. 您不喜欢切换

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

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

那么您将发送给智能合约的任何东西发送给所有者了吗?

此外,您还可以通过以下方式将刚刚发送到msg.sender的数量发送回去:

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

但这将最终变得毫无用处并浪费了一些气体。

答案 1 :(得分:1)

我只是在这里澄清@Fernando的答案。

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

这里x的wei金额将被发送到所有者的帐户形成合同的余额。为此,您的合同需要至少持有x wei。不是正在调用Send函数的帐户。 注意:这里的Send功能不必标记为payable

现在是

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

调用者将随请求一起发送一定数量的ether/wei。我们可以使用msg.value来获取该金额。然后将其转移到所有者的帐户。在这里,合约本身不需要持有任何数量的以太。 注意:这里的Send功能必须标记为payable

答案 2 :(得分:1)

我刚刚在混音中检查了您的代码,它可以正常工作,我只是使用了Solidity编译器版本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);
}
}

可能是因为合同中没有金额。其次,当您使用Send时,uint值应该在wei中。

对于统治单位 http://ethdocs.org/en/latest/ether.html

相关问题