ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.87k stars 1.82k forks source link

discussions tx.wait() return error: not an array #2433

Closed RoeiBenZeev closed 1 week ago

RoeiBenZeev commented 2 years ago

const buyAction = async (tokenToBuyWith, tokenToPurchase) => { console.log("In buy action") try { const amountIn = data.BNBAmount; const amount = await router.getAmountOut(amountIn, tokenToBuyWith, tokenToPurchase, { 'gasLimit': data.gasLimit, 'gasPrice': data.gasPrice }); const amountOutMin = (amount.mul(100 - ${data.Slippage}).div(100));

    console.log(
        clc.green.inverse(`Start to buy \n`)
        +
        `Buying Token
    =================
    tokenIn: ${(amountIn * 1e-18).toString()} ${tokenToBuyWith} (BNB)
    tokenOut: ${amountOutMin.toString()} ${tokenToPurchase}
  `);

    const tx = await router.swapExactTokensForTokens(
        amountIn,
        amountOutMin,
        [tokenToBuyWith, tokenToPurchase],
        data.recipient,
        Math.floor(Date.now() / 1000) + 60 * 10, //10 minutes
        {
            'gasLimit': data.gasLimit,
            'gasPrice': data.gasPrice,
            "value": BigNumber.from(0)
        });

    const receipt = await tx.wait();
    console.log(`Transaction receipt : https://www.bscscan.com/tx/${receipt.logs[1].transactionHash}`);
    console.log(receipt);
} catch (err) {
    let error = JSON.parse(JSON.stringify(err));
    console.log(`Error caused by :
    {
    reason : ${error.reason},
    transactionHash : ${error.transactionHash}
    message : ${error}
    }`);
    console.log(error);
}

}

this is the code im running to swap BNB to WBNB on bsc.

my code fail on tx.wait() and throwing and Error: not an array

im hard to find what is wrong with the code / the parameters im sending.

ricmoo commented 2 years ago

I don't quite understand. The tx.wait() returns a TransactionReceipt, and if the transaction failed (mined, but with a status of 0) throws an error.

It should never return an Array. What would you expect the Array to be of?

mastilver commented 2 years ago

not an array is the error being thrown by ethers.js

I'm getting the same error (also on BSC), here's the full stack

Error: not an array
    at Object.logs (/usr/src/app/node_modules/@ethersproject/providers/lib/formatter.js:408:23)
    at Function.Formatter.check (/usr/src/app/node_modules/@ethersproject/providers/lib/formatter.js:373:40)
    at Formatter.receipt (/usr/src/app/node_modules/@ethersproject/providers/lib/formatter.js:325:32)
    at JsonRpcProvider.<anonymous> (/usr/src/app/node_modules/@ethersproject/providers/lib/base-provider.js:1707:70)
    at step (/usr/src/app/node_modules/@ethersproject/providers/lib/base-provider.js:48:23)
    at Object.next (/usr/src/app/node_modules/@ethersproject/providers/lib/base-provider.js:29:53)
    at fulfilled (/usr/src/app/node_modules/@ethersproject/providers/lib/base-provider.js:20:58)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
mastilver commented 2 years ago

what is happening is that the RPC node returns an empty body (null) if the transaction receipt is not available

We are not using wait() so our temporary fix / hack will be different (you can always replace wait by calling getTransactionReceipt until you get an answer)

export async function getTransactionReceipt(hash: string): Promise<TransactionReceipt | null> {
  try {
    return await bscProvider()
          .getTransactionReceipt(hash)
  } catch (err) {
    // When the transaction is not present, the RPC returns an empty body
    if (err.code === 'SERVER_ERROR' && err.body === null) {
      return null
    }

    throw err
  }
}
ricmoo commented 1 week ago

Closing older issues. But if this is still happening in v6, please re-open or start a new issue.

Thanks! :)