【问题标题】:Where do the ethers come from when sending ethers with .send() function in ethereum contract在以太坊合约中使用 .send() 函数发送以太币时,以太币从何而来
【发布时间】:2017-08-22 10:55:55
【问题描述】:

我正在学习 here 之后的以太坊代币合约。我对下面的代码感到困惑:

function sell(uint amount) returns (uint revenue){
if (balanceOf[msg.sender] < amount ) throw;        // checks if the sender has enough to sell
balanceOf[this] += amount;                         // adds the amount to owner's balance
balanceOf[msg.sender] -= amount;                   // subtracts the amount from seller's balance
revenue = amount * sellPrice;
if (!msg.sender.send(revenue)) {                   // sends ether to the seller: it's important
    throw;                                         // to do this last to prevent recursion attacks
} else {
    Transfer(msg.sender, this, amount);             // executes an event reflecting on the change
    return revenue;                                 // ends function and returns
}

}

msg.sender.send(revenue) 表示将以太币发送给卖家。我的问题是:

要发送的以太币是来自 msg.sender 还是来自令牌合约? 我认为它们来自 msg.sender。然而 msg.sender 实际上是代表卖家的,对吧?这使得卖家向自己发送以太币。我可以知道如何理解这一点。然后如何让合约自动将以太币发送回用户帐户?

谢谢!

【问题讨论】:

    标签: token ethereum solidity


    【解决方案1】:

    我做了一些测试来弄清楚这个问题。我发现发送到目标地址的以太币来自令牌合约实例地址。

    之前我很困惑,因为我不明白合约实例在构建后如何获得以太币。现在我知道,当账户调用合约的关键字payable标记的方法时,合约实例会获得以太币。当调用发生时,以太币同时被发送到合约地址。在demo token code 中,buy() 方法负责将以太币发送到合约地址。

    我是学习以太坊合约的新手。我所意识到的可能仍然存在一些错误。请让我知道是否有。欣赏!

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多