Solidity

錯誤:缺少參數:在契約建構子中

  • March 10, 2022

記錄我在處理 Chainlink 合約時收到的此錯誤的解決方案:

錯誤資訊

Error: missing argument:  in Contract constructor (count=1, expectedCount=2, code=MISSING_ARGUMENT, version=contracts/5.5.0)...

reason: 'too many arguments:  in Contract constructor',
 code: 'UNEXPECTED_ARGUMENT',
 count: 1,
 expectedCount: 0

我的解決方案

安全帽出現此問題。合約建構子中的參數數量需要與部署腳本的 .deploy() 方法中的參數數量相匹配。

正確實施

// CONTRACT FILE

pragma solidity ^0.8.0;

import "hardhat/console.sol";
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";

contract APIConsumer is ChainlinkClient {
   using Chainlink for Chainlink.Request;

  constructor() { // <——— Has 0 arguments
       console.log("Hey!");
   }
}

// DEPLOYMENT SCRIPT

async function main() {
   const APIConsumer = await ethers.getContractFactory("APIConsumer");
   const apiConsumer = await APIConsumer.deploy();  // <——— Must also have 0 arguments

   await apiConsumer.deployed();

   console.log(`APIConsumer deployed to: ${apiConsumer.address}`);
}

在 hardhat 合約建構子中的參數數量需要與部署腳本的 .deploy() 方法中的參數數量相匹配。

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