web3js调用合约报错

web3js 版本: 4.0.1-rc.1 合约:

// SPDX-License-Identifier: GPL-3.0
pragma solidity = 0.7.0;
pragma experimental ABIEncoderV2;

contract ToDoList {
    struct toDo {
        uint id;
        string name;
        bool isOK;
    }

    toDo[] private toDoList;

    toDo private lastToDo;

    function addToDo(string memory _name) public {
        toDo memory newToDo = toDo({id: toDoList.length+1, name: _name, isOK: false});
        lastToDo = newToDo;
    }

    function setToDo(string memory _name) public {
        toDo memory newToDo = toDo({id: toDoList.length+1, name: _name, isOK: false});
        toDoList.push(newToDo);
    }

    function getToDoList() public view returns (toDo[] memory) {
        return toDoList;
    }

    function getLastToDo() public view returns (toDo memory) {
        return lastToDo;
    }

    function getToDoStatus(uint _id) public {
        for (uint i = 0; i < toDoList.length; i++) {
            if (toDoList[i].id == _id) {
                toDoList[i].isOK = true;
            }
        }
    }
}

调用:

/**
 * 调用合约添加数据
 */
export const callingAContract = async (toDo: string) => {
  const contract = new web3.eth.Contract(toDoABI, address);
  contract.methods.addToDo(toDo).send({from: store.state.address[0],value:1000000000000000, gas: 1010000}).then((res) => {
    console.log(res)
  })
}

报错:

MetaMask - RPC Error: [ethjs-query] while formatting outputs from RPC '{"value":{"code":-32603,"data":{"message":"VM Exception while processing transaction: revert","stack":"RuntimeError: VM Exception while processing transaction: revert\n    at EIP1559FeeMarketTransaction.fillFromResult (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:12745)\n    at Miner.<anonymous> (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:36703)\n    at async Miner.<anonymous> (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:35116)\n    at async Miner.mine (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:39680)\n    at async Blockchain.mine (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:60063)\n    at async Promise.all (index 0)\n    at async TransactionPool.emit (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\node_modules\\emittery\\index.js:303:3)","code":-32000,"name":"RuntimeError","data":{"hash":"0x0a5696026bc3994182a6293e8110b537f538d5369a98f2cef106cb2117bcdcbe","programCounter":16,"result":"0x0a5696026bc3994182a6293e8110b537f538d5369a98f2cef106cb2117bcdcbe","reason":null,"message":"revert"}}}}' {code: -32603, message: `[ethjs-query] while formatting outputs from RPC '{…2117bcdcbe","reason":null,"message":"revert"}}}}'`}
请先 登录 后评论
  • 0 关注
  • 0 收藏,1705 浏览
  • IronMan 提出于 2023-10-08 10:03