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



我是区块链编程和web3的新手,我真的不明白这个错误。我正试图从我的智能合约(在BSC测试网上)调用一个函数

函数称为getRandom

app.js

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)
}
})();

误差

Error: Returned error: execution reverted
at Object.ErrorResponse (C:DeFi projectnode_modulesweb3-core-helpersliberrors.js:28:19)
at C:DeFi projectnode_modulesweb3-core-requestmanagerlibindex.js:302:36
at XMLHttpRequest.request.onreadystatechange (C:DeFi projectnode_modulesweb3-providers-httplibindex.js:98:13)
at XMLHttpRequestEventTarget.dispatchEvent (C:DeFi projectnode_modulesxhr2-cookiesdistxml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (C:DeFi projectnode_modulesxhr2-cookiesdistxml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (C:DeFi projectnode_modulesxhr2-cookiesdistxml-http-request.js:318:14)
at IncomingMessage.<anonymous> (C:DeFi projectnode_modulesxhr2-cookiesdistxml-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
}

我只是想从我的智能合约的功能中得到响应。

我认为您的getRandom方法存在逻辑错误。下面是相同问题的工作代码:

如果你只是想从合约中检索一个随机数,那么你可以使用call方法。

检查这里sendcall方法如何工作的基本理解:如何使用Solidity和Web.js保存和检索以太坊区块链上的数据

// 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));
}
}
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

相关内容

  • 没有找到相关文章

最新更新