Solidity

在 ethers 中呼叫具有多個參數的函式返回錯誤:缺少參數

  • August 10, 2022

我正在將我的 dApp 前端的一部分從 usingMoralis更改為 using ethers.js。出於某種原因,我無法弄清楚如何以正確的方式為函式呼叫提供多個參數。

考慮這個使用預言機進行鏈上轉換的合約函式:

   function getUniversalConversionRate(uint256 coinAmount,address priceFeedAddress) 
   public view returns (uint256) {
       uint256 coinPrice = getUniversalPrice(priceFeedAddress);
       uint256 coinAmountInUsdCents = (coinPrice * coinAmount) /10000000000000000;
       return coinAmountInUsdCents;}

這是它的 ABI:

   {
       "inputs": [
           {"internalType": "uint256","name": "coinAmount","type": "uint256"},
           {"internalType": "address","name": "priceFeedAddress","type": "address"}],
       "name": "getUniversalConversionRate",
       "outputs": [{"internalType": "uint256","name": "","type": "uint256"}],
       "stateMutability": "view",
       "type": "function"},

這就是我與之互動的方式:

 const provider = new ethers.providers.JsonRpcProvider("https://rpc...", 80001);
 const signer = new ethers.Wallet("0x...", provider); //saving space
 const administrativeSidechainContract = new ethers.Contract(mainContractAddress, abi, signer)
 
 async function updateUI() {
 const ethPriceData = await administrativeSidechainContract.getUniversalConversionRate({
      coinAmount: "1", priceFeedAddress: "0x...." })
 console.log(ethPriceData)}
 updateUI()

我提供了兩個正確的論點,並且在具有相同輸入的另一個框架中工作,但是ethers我得到Error: missing argument: passed to contract (count=1, expectedCount=2, code=MISSING_ARGUMENT, version=contracts/5.6.0) 了我做錯了什麼?

你傳遞這樣的參數:

const ethPriceData = await administrativeSidechainContract.getUniversalConversionRate("1", "0x....")

如果你想在函式呼叫中發送乙太幣、設置 gasLimit 等,你可以這樣做:

const ethPriceData = await administrativeSidechainContract.getUniversalConversionRate("1", "0x....", {value: 100000000000})

引用自:https://ethereum.stackexchange.com/questions/133311