Solidity

使用 calldata 類型時如何通過另一個合約的介面呼叫函式

  • January 30, 2020

我已經聲明了以下介面:

interface Test {
   function airDropMultiple(
       address[] calldata recipients,
       uint[] calldata tokenAmount
   )
       external
       returns (bool);
}

我試圖這樣稱呼它:

Test(ContractAddress).airDropMultiple(
   [testWalletAddress[4], testWalletAddress[3]],
   [10**18, 10**19]
);

我收到以下編譯器錯誤:

Invalid implicit conversion from address[2] memory to address[] memory requested.

我如何從這個契約呼叫這個介面到目標契約?

一般錯誤:

Invalid implicit conversion from type[N] memory to type[] memory requested.

嘗試將靜態數組發送到需要動態數組的函式時發生。

為了在您的特定情況下修復它,您應該執行以下操作:

address[] memory recipients = new address[](2);
uint[] memory tokenAmounts = new uint[](2);
(recipients[0], recipients[1]) = (testWalletAddress[4], testWalletAddress[3]);
(tokenAmounts[0], tokenAmounts[1]) = (10**18, 10**19);
Test(ContractAddress).airDropMultiple(recipients, tokenAmounts);

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