Solidity

如何在測試合約時傳遞地址數組

  • January 24, 2022

我已經寫了契約…

contract TimeLock is TimelockController {

   
   uint256 minDelay = block.timestamp + 4 minutes;


   constructor(
       address[] memory proposers,
       address[] memory executors
   ) TimelockController( minDelay, proposers, executors ) {}

   //rest of contract's code
}

現在,我想用 mocha 和 javascript 測試它,所以我寫了一個測試……

beforeEach(async function(){
   //const date = new Date();
   TimeLock = await ethers.getContractFactory('TimeLock');
   [proposer, executor, sendTo, admin, ...addrs] = await ethers.getSigners();
   timelock = await TimeLock.deploy(proposer.address, executor.address);
   await timelock.deployed();
});

問題是,在測試我的契約時,我無法弄清楚如何通過 javascript 傳遞地址數組,以我過去的知識,我正在這樣做……

timelock = await TimeLock.deploy(proposer.address, executor.address);

這就是我的契約通過建構子獲取地址的方式……

constructor(
           address[] memory proposers,
           address[] memory executors
       ) TimelockController( minDelay, proposers, executors ) {}

通過提供上述地址,我得到了這個錯誤……

1) "before each" hook for "deploys the contract":
    Error: invalid value for array (argument="value", value="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", code=INVALID_ARGUMENT, version=contracts/5.5.0)

誰能幫我如何通過數組發送地址,我做錯了嗎?

您的合約建構子需要兩個數組作為參數。您的程式碼發送兩個地址而不是數組。

會是這樣的

timelock = await TimeLock.deploy([proposer.address], [executor.address]);

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