【问题标题】:Calling withdraw method with web3 ethereum使用 web3 ethereum 调用提取方法
【发布时间】:2021-04-26 17:12:18
【问题描述】:

我正在从使用元掩码的 react.js 应用程序中以solidity 编写的合约调用提款方法。交易失败,我不知道为什么。我认为这与汽油费有关,因为当我更改它时,我会遇到不同的错误。这是反应代码:

     contract.methods
      .withdrawDividends()
      .send({
        from: userAddress,
        value: availableToWithdraw,
      })
      .on("transactionHash", function (hash: string) {
        console.log(hash);
      })
      .on(
        "confirmation",
        function (confirmationNumber: number, receipt: unknown) {
          console.log(confirmationNumber);
          console.log(receipt);
        }
      )
      .on("receipt", function (receipt: unknown) {
        // receipt example
        console.log(receipt);
      })
      .on("error", function (error: unknown, receipt: unknown) {
        // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
        console.log(error);
        console.log(receipt);
      });

这是我的solidity方法:

function withdrawDividends() public {
    User storage user = users[_msgSender()];

    uint256 totalAmount = getUserDividends(_msgSender());

    require(totalAmount > 0, "User has no dividends");

    user.checkpoint = block.timestamp;

    (bool success, ) = _msgSender().call.value(totalAmount)("");
    require(success, "Transfer failed.");

    emit Withdrawn(_msgSender(), totalAmount);

}

该方法没有崩溃,但我在元掩码中看到以下错误:

我做错了什么?是不是和气体有关? 提前致谢

【问题讨论】:

  • getUserDividends(_msgSender()) 返回的值是否超过 0?
  • 是的,如果我不发送任何价值和气体:28500000,它可以工作,但提款是 0

标签: solidity web3


【解决方案1】:

如果你想让你的函数接受 ETH 值,它需要有 payable 修饰符。

在你的情况下:

function withdrawDividends() public payable {

您可以在docs 中阅读有关payable 修饰符的更多信息。它链接到 receive 函数,这是一个略有不同的主题。但它也涵盖了payable 修饰符。

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 2021-08-28
    • 2018-12-28
    • 1970-01-01
    • 2021-09-24
    • 2019-01-10
    • 1970-01-01
    • 2018-10-30
    • 2018-06-22
    相关资源
    最近更新 更多