Solidity
執行 interface.encodeFunctionData 時出現多個匹配函式錯誤
我有openzeppelin的標準
ERC721
契約。下面是返回
bytes data
將用於某些目的的輔助函式。async function getErc721SafeTransferFromTransaction(tokenInstance, args) { const txData = tokenInstance.interface.encodeFunctionData("safeTransferFrom",args); return txData; }
當我使用已部署的
ERC721
實例並[from,to,tokenId]
作為參數呼叫該函式時,出現以下錯誤。Error: multiple matching functions (argument="name", value="safeTransferFrom", code=INVALID_ARGUMENT, version=abi/5.6.4) at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:261:28) at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:273:20) at Logger.throwArgumentError (node_modules/@ethersproject/logger/src.ts/index.ts:277:21) at Interface.getFunction (node_modules/@ethersproject/abi/src.ts/interface.ts:210:24) at Interface.encodeFunctionData (node_modules/@ethersproject/abi/src.ts/interface.ts:373:37) at getErc721SafeTransferFromTransaction (test/utils/helper.js:30:44) at processTicksAndRejections (node:internal/process/task_queues:95:5) at runNextTicks (node:internal/process/task_queues:64:3) at listOnTimeout (node:internal/timers:533:9) at processTimers (node:internal/timers:507:7)
似乎 ethers.js 有問題,
safeTransferFrom
因為 openzeppelin 的標準 ERC721 有重載safeTransferFrom
方法,一個有data
參數,一個沒有。我該如何解決這個問題並獲取編碼數據?
您缺少參數,因為它有 2 個名稱相同但參數長度不同的函式。
- 如果你想在沒有數據的情況下打電話。
const txData = tokenInstance.interface.encodeFunctionData("safeTransferFrom(address, address, uint256)",args);
- 如果你想用數據打電話。
const txData = tokenInstance.interface.encodeFunctionData("safeTransferFrom(address, address, uint256, bytes)",args);