首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Web3.js返回的错误:恢复执行

Web3.js返回的错误:恢复执行
EN

Stack Overflow用户
提问于 2021-08-13 22:51:10
回答 1查看 1.5K关注 0票数 4

我对区块链编程和web3并不熟悉,我也不太明白这个错误。我试图从我的智能合同中调用一个函数(在BSC测试网上)

函数称为getRandom

app.js

代码语言:javascript
运行
复制
const Web3 = require("web3");
const web3 = new Web3("https://speedy-nodes-nyc.moralis.io/redacted/bsc/testnet");
const key = "28e........................8"
const contract = new web3.eth.Contract([
    {
        "inputs": [],
        "name": "getRandom",
        "outputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    }
], "0x22191A37B2AB83e7A81758ecd0E120cf080153B1")

const account = web3.eth.accounts.privateKeyToAccount('0x' + key);
web3.eth.accounts.wallet.add(account);

(async () => {
    try {
        const gasPrice = await web3.eth.getGasPrice();
        const gasEstimate = await contract.methods.getRandom().estimateGas({from: account.address});

        console.log(contract.methods.getRandom().send({from: account.address, gasPrice: gasPrice, gas: gasEstimate}))
    }
    catch (e) {
        console.log(e)
    }
})();

错误

代码语言:javascript
运行
复制
Error: Returned error: execution reverted
    at Object.ErrorResponse (C:\DeFi project\node_modules\web3-core-helpers\lib\errors.js:28:19)
    at C:\DeFi project\node_modules\web3-core-requestmanager\lib\index.js:302:36
    at XMLHttpRequest.request.onreadystatechange (C:\DeFi project\node_modules\web3-providers-http\lib\index.js:98:13)
    at XMLHttpRequestEventTarget.dispatchEvent (C:\DeFi project\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (C:\DeFi project\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (C:\DeFi project\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (C:\DeFi project\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  data: null
}

我只是想从我的智能合同的功能中得到回应。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-12 13:56:41

我认为在getRandom方法中有一个逻辑错误。下面是相同问题的工作代码。

如果您只想从契约中检索一个随机数,那么只需使用call方法即可。

在这里查看sendcall方法如何工作的基本理解:How to save and retrieve data on Ethereum blockchain with Solidity and Web.js

代码语言:javascript
运行
复制
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.4.17;

/**
 * @title Storage
 * @dev Store & retrieve value in a variable
 */
contract Storage {

    uint256 number;
    /**
     * @dev Store value in variable
     * @param num value to store
     */
    function store(uint256 num) public {
        number = num;
    }

    /**
     * @dev Return value 
     * @return value of 'number'
     */
    function retrieve() public view returns (uint256){
        return number;
    }
    
    /**
     * @dev Return value 
     * @return value of 'number'
     */
    function getRandom() public view returns (uint){
        return uint(keccak256(block.difficulty, now));
    }
}
代码语言:javascript
运行
复制
const Web3 = require("web3");
const web3 = new Web3("https://data-seed-prebsc-1-s1.binance.org:8545");
const key = "YOUR_WALLET_KEY_HERE"
const contract = new web3.eth.Contract([
    {
        "constant": true,
        "inputs": [],
        "name": "retrieve",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "num",
                "type": "uint256"
            }
        ],
        "name": "store",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getRandom",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
], "0xc48a2E92F3F2066153a3eDDf5071bc0C1Ece0172")

const account = web3.eth.accounts.privateKeyToAccount('0x' + key);
web3.eth.accounts.wallet.add(account);

(async () => {
    try {
        const gasPrice = await web3.eth.getGasPrice();
        const value = Web3.utils.toHex('5');
        const gasEstimate = await contract.methods.store(value).estimateGas({from: account.address});
        console.log(await contract.methods.store(value).send({from: account.address, gasPrice: gasPrice, gas: gasEstimate}));
        console.log(await contract.methods.retrieve().call({from: account.address, gasPrice: gasPrice, gas: gasEstimate}));
        console.log(await contract.methods.getRandom().call({from: account.address, gasPrice: gasPrice, gas: gasEstimate}));
    }
    catch (e) {
        console.log(e)
    }
})();


Output - 

store method
{
    blockHash: '0xac3bed99e723736a09eca7f802d5791a29a7035a6097fd5479121cfbf686dcd8',
    blockNumber: 20996462,
    contractAddress: null,
    cumulativeGasUsed: 782688,
    from: '0x62aacdd74659506d72ec55e9bc2342798e5929fb',
    gasUsed: 41383,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    status: true,
    to: '0xc48a2e92f3f2066153a3eddf5071bc0c1ece0172',
    transactionHash: '0xc6706afddc18cdd3ccb9b9077a779aea1f72130bf33862b8c2c50714770139e3',
    transactionIndex: 7,
    type: '0x0',
    events: {}
}

retrieve method
5

getRandom method
5554097233048177814328041552358545131308099309008757005048758322173278412271
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68779051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档