【问题标题】:The called function should be payable error in withdraw function被调用函数应在withdraw函数中支付错误
【发布时间】:2021-05-05 00:02:30
【问题描述】:

我有这样的功能可以从合约中转移代币,但我应该在哪里添加应付账款时出现错误,我真的不明白提款是否会提取以太币或代币?

function withdrawBalances() public nonReentrant {
            uint share = _Balances[msg.sender];
           _Balances[msg.sender] = 0;
            msg.sender.transfer(share);
        }

.withdrawBalances 错误:VM 错误:还原。 revert 事务已恢复到初始状态。注意:如果您发送值并且您发送的值应该小于您当前的余额,则调用的函数应该是应付的。调试事务以获取更多信息。

【问题讨论】:

    标签: solidity


    【解决方案1】:

    当您在调用不可支付的函数时尝试发送一些 Ether 时会发生此错误。将 Ether 值设置为 0 以避免这种情况。您还可以使用payable关键字允许函数接受以太币。

    function withdrawBalances() public payable {
         ...
    }
    

    【讨论】:

    • 如何将以太币设置为 0?在这个函数中?
    • 默认为0。您目前如何发送交易?
    • 'uint share = _WithdrawBalances[msg.sender]; _WithdrawBalances[msg.sender] = 0; msg.sender.transfer(share);'
    • 我不明白 ehters 退出合约和代币之间的区别。我如何提取以太币和代币?
    • 让我重新表述我之前的问题。你是如何调用withdrawBalances 函数的?这就是问题所在。使用 transfer 将转移 ETH。要转移代币,您需要获取一个合约实例,例如'ERC20 instance = ERC20(
      )' 并调用合约的函数(transferFrom 或 transfer)。
    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 2019-09-21
    • 2021-07-24
    • 2018-09-24
    • 1970-01-01
    • 2017-06-08
    • 2020-04-24
    • 1970-01-01
    相关资源
    最近更新 更多