3

I'm trying to send transactions to the polygon network using ethers.js. After submitting the transaction, i await tx.wait(), but it fails to resolve 50% of the time. I saw other people were having similar issues but was due to their gas price being too low. I currently have this code:

const getWallet = (): Wallet => {
  return new ethers.Wallet(PRIVATE_KEY, HTTP_PROVIDER);
};
const wallet = getWallet();
const gasPrice = 50000000000;
const swap1 = await wallet.sendTransaction({
      data: tx1.data,
      chainId: tx1.chainId,
      from: tx1.from,
      gasLimit: 350449, 
      gasPrice: gasPrice, 
      value: '0x' + new BigNumber(tx1.value).toString(16),
      to: tx1.to,
      nonce: nonce,
    });

I've always returned an object that has the tx hash, but when I go to look up that hash on polyscan it never appears. After waiting sometimes up to an hour tx.wait() still does not resolve and the transaction still doesn't appear in polyscan.

I cancel my script, bump the gasPrice up by 20% and try to run it again w/ the same nonce (hoping to replace it). At that point I'm usually told the replacement gas fee is too low.

Can someone explain what I'm doing wrong here?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dan Ramos
  • 1,092
  • 2
  • 19
  • 35

1 Answers1

1

Maybe you need instance the provider first using new ethers.providers, in this example i use JsonRpcProvider.

And Alternatively to wait(),you can execute another function to wait receipt using ethers.provider.waitForTransaction(hash,confirmations,timeout).

You can do something like this:

const ethers = require("ethers");
const provider = new ethers.providers.JsonRpcProvider(HTTP_PROVIDER);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const swap1 = await wallet.sendTransaction({//});
const receipt = provider.waitForTransaction(swap1.hash, 1, 150000).then(() => {//});
Flavio
  • 159
  • 1
  • 3