0

I was using token contract to transfer erc20 tokens to an address and it was working perfectly fine. but since i have migrated to ethers v6. everything has messed up. All the methods are either replaced or renamed.

const contract = new ethers.Contract(tokenAddress, erc20Abi, ether)
const decimals = await contract.decimals()
const signer = new ethers.Wallet(fromPrivateKey, ether);
const amount = ethers.parseUnits(quantity.toString(), decimals);
const contractSigner = contract.connect(signer)
const gasFee = await contractSigner.estimateGas.transfer(to, amount)
const gasLimit = gasFee.toNumber()

const tx = await contractSigner.transfer(to, amount, {
  gasPrice: enhancePrice,
  gasLimit: gasLimit,
});
return tx.hash;

both of these lines

const gasFee = await contractSigner.estimateGas.transfer(to, amount)


const tx = await contractSigner.transfer(to, amount, {
  gasPrice: enhancePrice,
  gasLimit: gasLimit,
});

are now giving error that neither property transfer or nor estimateGas exists on contractSigner. i have checked the migration list as well can't seem to find anything there as well

Ethers v6 migration docs

Can someone help?

1 Answer 1

0

In ethers.js v6, estimateGas() should be called like this:

const estimatedGas = await contractSigner.functionName.estimateGas(arg1, arg2, ...);

The revised portion of the code would be:

const gasFee = await contractSigner.transfer.estimateGas(to, amount);
const gasLimit = parseInt(gasFee)
console.log(gasLimit)

const tx = await contractSigner.transfer(to, amount, {
    gasLimit: gasLimit,
});

const receipt = await tx.wait();

console.log(receipt.hash)
3
  • like i mentioned in my question that contractSigner neither recognizes transfer property not estimateGas so contractSigner.transfer.estimateGas(to, amount) is not working nor is const tx = await contractSigner.transfer(to, amount, { gasLimit: gasLimit, }); Commented Apr 19 at 6:07
  • In your question, you were using contractSigner.estimateGas.transfer. Please try using contractSigner.transfer.estimateGas. Commented Apr 19 at 6:44
  • Also, make sure that you’re using the correct ABI while creating the contract instance. Commented Apr 19 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.