Solidity:给我自己寄点钱

Solidity: Send some money to myself

我有一个来自 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?

我该如何解决?

  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);
}

但这将结束无用并浪费一些气体。

我只是在这里澄清@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.

我刚刚在 remix 中检查了你的代码并且它有效,我只是使用了 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