[英]Web3.js returned error: execution reverted
I'm new to blockchain programming and web3 and I don't really understand the error.我是区块链编程和 web3 的新手,我不太了解这个错误。 I'm trying to call a function from my smart contract (on BSC test net)我正在尝试从我的智能合约中调用一个函数(在 BSC 测试网上)
Function is called getRandom
函数称为getRandom
app.js应用程序.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错误
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
}
I'm just trying to get the response from my smart contract's function.我只是想从我的智能合约功能中获得响应。
I think there is a logical error in your getRandom
method.我认为您的getRandom
方法存在逻辑错误。 Here is a working code for the same problem.这是相同问题的工作代码。
And if you just want to retrive a random number from the contract then you can just use call
method.如果你只想从合约中检索一个随机数,那么你可以使用call
方法。
Check here for the basic understanding of how send
and call
method works: How to save and retrieve data on Ethereum blockchain with Solidity and Web.js在这里查看send
和call
方法如何工作的基本理解: 如何使用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.