Contract-Invocation

ethers - 呼叫具有特定簽名者的重載函式

  • November 2, 2021

將特定簽名者連接到Ethers中的合約對像很容易:

// assuming a signer called 'user' and a contract called 'Foo' with function 'bar'
await Foo.connect(user).bar();

這裡也有關於有時在 Ethers 中發生函式重載的問題的明確指導,以 ERC721 介面為例:

// assuming an ERC721 nft contract called 'NFT'
await NFT['safeTransferFrom(address,address,uint256)'](
 sender.address, 
 recipient.address, 
 ethers.BigNumber.from("1")
);
// or
await NFT['safeTransferFrom(address,address,uint256,bytes)'](
 sender.address,
 recipient.address,
 ethers.BigNumber.from("1"),
 "0x"
);

但是如何將特定簽名者連接到重載函式?

這花了相當多的時間,但最終發現:

await NFT.connect(sender)['safeTransferFrom(address,address,uint256)'](
 sender.address, 
 recipient.address, 
 ethers.BigNumber.from("1")
);

是獲勝組合。不要放在.後面connect(sender)

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