【问题标题】:Why does this function fail to withdraw ether from a smart contract?为什么此功能无法从智能合约中提取以太币?
【发布时间】:2018-12-28 02:16:08
【问题描述】:

我使用了以下代码,我认为它可以提取任何存入智能合约的以太币。你能帮忙解释一下为什么这可能行不通吗?

function withdraw() public {
        require(owner == msg.sender);
        msg.sender.transfer(address(this).balance);
}

我在提取以太币时遇到问题,想知道代码的哪一部分阻止我这样做。

【问题讨论】:

  • 您能否发布整个智能合约以及您用来调用合约withdraw() 函数的代码?
  • 你得到什么样的错误?

标签: ethereum solidity smartcontracts erc20


【解决方案1】:
 function withdraw(uint amount) public payable{
    require(address(this).balance >= amount);
    msg.sender.transfer(amount);   
}

使用此代码

【讨论】:

  • OP 似乎希望从合约中提取所有资金
猜你喜欢
  • 2018-06-29
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多