首页 > 解决方案 > 如何在 web3.js 上打印 solidity revert me 消息?

问题描述

我正在尝试编写一个拍卖平台。当合约发送一个还原函数时,我想在屏幕上显示它。我正在使用 web3.js 1.5 版。

这是相关的solidity代码;

function bid() public payable {
    require(
        now <= auctionEndTime,
        "Auction already ended."
    );

    require(
        //I want to print this error on screen.
        msg.value > highestBid,
        "There already is a higher bid."
    );

    if (highestBid != 0) {
        pendingReturns[highestBidder] += highestBid;
    }
    highestBidder = msg.sender;
    highestBid = msg.value;
    emit HighestBidIncreased(msg.sender, msg.value);
}

这是出价和获取错误的 javascript 代码;

async bid(amount) {
      if(this.state.auction!=='undefined'){
        try{
          await this.state.auction.methods.bid().send({value: this.state.web3.utils.toWei(amount).toString(), from: this.state.account})
        } catch (e) {
        console.log(e)
        }
      }
    }

如果我出价低于最高出价,这就是我在控制台上得到的;

{代码:-32603,消息:Error: [ethjs-query] while formatting outputs from…/task_queues.js:93:5)","name":"RuntimeError"}}}}',堆栈: Error: Error: [ethjs-query] while formatting outpu…/task_queues.js:93:5)","name":"RuntimeError"}}}}'}

我想要的是打印“已经有更高的出价”。在控制台上,但我无法让它工作。我阅读了 web3 文档和元掩码文档,但没有运气。谢谢您的回答。

标签: javascriptjsonethereumsolidityweb3js

解决方案


推荐阅读