【问题标题】:How would I set a manual gas limit in web3? To get the smart contract to execute?如何在 web3 中设置手动气体限制?让智能合约执行?
【发布时间】:2022-01-12 14:00:43
【问题描述】:

我在执行智能合约时遇到问题。我认为由于错误代码必须是气体限制问题。虽然我不知道我会将代码放在哪里以便它正确执行。有什么建议吗?

Solidity 代码如下: pragma solidity ^0.8.0;

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

Javascript 代码如下:

const  App = () => {

  const [currentAccount, setCurrentAccount] = useState("")
  const contractAddress = "contractAddress"
  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)}
}
 const connectWallet = async () => { 
    try { 
      const {ethereum} = window; 
      if(!ethereum) {alert("Get Metamask extenstion")
    return 
  } else { 
  const accounts = await ethereum.request({method: 'eth_accounts'})
  console.log("Connected", accounts[0])
  window.alert("Wallet is Connected")
  setCurrentAccount(accounts[0])
  const ethBlance = await Web3.eth.getBalance(currentAccount)
  console.log(ethBlance)
  }
    }catch(err) {
      console.log(err)
    }
  }

  const wave = async () => { 
    try { 
    const {ethereum} = window;

    if(ethereum) { 
      const provider = new ethers.providers.Web3Provider(ethereum);
      const signer = provider.getSigner(); 
      const wavePortalContract = new ethers.Contract(contractAddress, contractABI, signer);

      let count = await wavePortalContract.getTotalWaves( ); 

      const waveTxn = await wavePortalContract.wave({gas:10000000 }, {gasPrice:80000000000});
      console.log("Mining....", waveTxn.hash)

      await waveTxn.wait( );
      console.log("Mined-- ", waveTxn.hash)

      count = await await wavePortalContract.getTotalWaves( );
      console.log("Retrieved total wave count.. ", count.toNumber())
      }else { 
      console.log("Eth Object doesn't exist!")
    }
    } catch(err) {console.log(`${err} hello world`) }
}

  useEffect(() => {
    checkIfWalletConnected();
  }, [])

我收到的错误代码:

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)

【问题讨论】:

  • 尝试将gas和gas price放在同一个对象中

标签: javascript solidity web3


【解决方案1】:

在调用合约 Abi 方法时,您可以在第二个参数中传递一个额外的气体相关参数的对象(或者如果您的方法中没有任何参数,则首先传递一个对象)。

所以wave 函数的调用应该如下所示:

const waveTxn = await wavePortalContract.wave({gasLimit:30000});

检查以下文档中的所有其他参数。

https://docs.ethers.io/v5/api/contract/contract/#contract-functionsSend

【讨论】:

  • 它仍然为我返回相同的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 2019-09-12
  • 2022-11-23
  • 2018-07-10
  • 2018-07-07
  • 2021-09-16
相关资源
最近更新 更多