Ethers.js – How to Call an Overloaded Function with a Specific Signer

contract-invocationethers.jsfunction-override

Connecting a specific signer to a contract object in Ethers is easy:

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

There's also clear guidance on the issues that sometimes happen with function overloading in Ethers here, to take an example from the ERC721 interface:

// 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"
);

But how do you connect a specific signer to an overloaded function?

Best Answer

This took a fair amount of playing around, but finally discovered that:

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

is the winning combination. Don't put a . after connect(sender).