0

我正在尝试构建一个智能合约,您可以在其中向我发送关于 Eth 区块链的信息,尽管每次我从前端调用智能合约。但是每次调用合同时都会出错。

Javascript代码如下:

const  App = () => {

  const [currentAccount, setCurrentAccount] = useState("")
  const contractAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3"
  const contractABI = abi.abi

const checkIfWalletConnected = async () => { 
  let web3
  try { 
    const {ethereum} = window; 

if(!ethereum) { 
  window.alert("Make sure you have metamask !");
  return;
}else {
  web3 = new Web3(window.ethereum)
  console.log("We have the ethereum object", ethereum)}

const accounts = await ethereum.request({method: 'eth_accounts'})
  if(accounts.length !== 0) { 
    const account = accounts[0]
  
    console.log("Found an Authorized account:", account, );
    setCurrentAccount(account)

  } else { 
    window.alert("NO authorized account found")
  }


  const EthBalance = await web3.eth.getBalance(accounts[0])
  console.log(EthBalance)






 }catch(err) {console.log(err)}
  
}
 

坚固代码:

import 'hardhat/console.sol';

contract WavePort { 
//TrackWaves
uint totalWaves; //State Variable, Permanetly Stored In Contract Storage

constructor() {
    console.log("Yo, I am I contract");
    }


//Function Increments Waves On The Blockchain/ Immutable Never Decreasing 
function wave() public { 
    totalWaves += 1;
    console.log("%s has waved!", msg.sender);
}

//Function Returns Us the The Total Waves To Be Viewed
function getTotalWaves()public view returns(uint256) { 
    console.log("We have %d total waves", totalWaves);
    return totalWaves;
}
                

}

inpage.js:1 MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'}
(anonymous) @ inpage.js:1
(anonymous) @ inpage.js:17
_runReturnHandlers @ inpage.js:17
_processRequest @ inpage.js:17
await in _processRequest (async)
_handle @ inpage.js:17
handle @ inpage.js:17
_rpcRequest @ inpage.js:1
(anonymous) @ inpage.js:1
request @ inpage.js:1
(anonymous) @ web3-provider.ts:96
send @ web3-provider.ts:169
(anonymous) @ json-rpc-provider.ts:560
(anonymous) @ json-rpc-provider.ts:2
push../node_modules/@ethersproject/providers/lib.esm/json-rpc-provider.js.__awaiter @ json-rpc-provider.ts:2
perform @ json-rpc-provider.ts:535
(anonymous) @ base-provider.ts:1405
fulfilled @ base-provider.ts:2
Promise.then (async)
step @ base-provider.ts:2
fulfilled @ base-provider.ts:2
Promise.then (async)
step @ base-provider.ts:2
(anonymous) @ base-provider.ts:2
push../node_modules/@ethersproject/providers/lib.esm/base-provider.js.__awaiter @ base-provider.ts:2
call @ base-provider.ts:1398
(anonymous) @ index.ts:118
fulfilled @ index.ts:2
Promise.then (async)
step @ index.ts:2
(anonymous) @ index.ts:2
push../node_modules/@ethersproject/abstract-signer/lib.esm/index.js.__awaiter @ index.ts:2
call @ index.ts:115
(anonymous) @ index.ts:389
fulfilled @ index.ts:2
Promise.then (async)
step @ index.ts:2
(anonymous) @ index.ts:2
push../node_modules/@ethersproject/contracts/lib.esm/index.js.__awaiter @ index.ts:2
(anonymous) @ index.ts:371
wave @ App.js:101
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:4070
executeDispatch @ react-dom.development.js:8243
processDispatchQueueItemsInOrder @ react-dom.development.js:8275
processDispatchQueue @ react-dom.development.js:8288
dispatchEventsForPlugins @ react-dom.development.js:8299
(anonymous) @ react-dom.development.js:8508
batchedEventUpdates$1 @ react-dom.development.js:22396
batchedEventUpdates @ react-dom.development.js:3745
dispatchEventForPluginEventSystem @ react-dom.development.js:8507
attemptToDispatchEvent @ react-dom.development.js:6005
dispatchEvent @ react-dom.development.js:5924
unstable_runWithPriority @ scheduler.development.js:468
runWithPriority$1 @ react-dom.development.js:11276
discreteUpdates$1 @ react-dom.development.js:22413
discreteUpdates @ react-dom.development.js:3756
dispatchDiscreteEvent @ react-dom.development.js:5889
App.js:120 Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32000,"message":"execution reverted"}, method="call", transaction={"from":"0xD49a9a33F180D1e35A30F0ae2Dbfe5716a740Ebc","to":"0x5FbDB2315678afecb367f032d93F642f64180aa3","data":"0x9a2cdc08","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.5.1)

    Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":-32000,"message":"execution reverted"}, method="call", transaction={"from":"0xD49a9a33F180D1e35A30F0ae2Dbfe5716a740Ebc","to":"0x5FbDB2315678afecb367f032d93F642f64180aa3","data":"0x9a2cdc08","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.5.1) 

我认为添加一组气体和 gasPricelimit 会起作用。

module.exports = {
  solidity: "0.8.4",
  networks: {
    rinkeby: {
      url: "My_URL",
      accounts: ['PrivateKey'],
      gas: 21000000,
      gasPrice: 80000000000
    }
  }
};

我尝试添加 gas 和 gasPrice 限制,但无济于事?有什么建议么?

4

0 回答 0