profoundwatcher
profoundwatcher

Reputation: 45

Ethers js tx.wait() never returns?

I'm writing a program that will send multiple TXs to the blockchain in a few steps.

  1. Send each transaction
  2. Wait for each transaction
  3. Check each transaction

I've checked and the transactions are sending fine. I've checked to make sure they're being added to the array and that it's getting to the wait function properly, but it never returns anything. No error, no transaction replaced and no confirmation of success or failure.

Any idea what I can change?

let buyTransactions = []

    for (i = 0; i < buyConfig.numberWallets; i ++){
        let buyContract = mintContract.connect(signers[i])
        let buyTransaction = await buyContract[buyConfig.mintFunction](...buyConfig.inputs, settings)
        buyTransactions.push(buyTransaction)
        win.webContents.send("console", "<span> Transaction Sent For : " + wallets[i] + " </span> <br/>")
    }

    console.log(buyTransactions)

    let buyTransactionsWaited = []
    console.log(i)
  
    for (i = 0; i < buyTransactions.length; i++) {
        console.log(i)
      try{
        let buyTransaction = await buyTransactions[i].wait()
        buyTransactionsWaited.push(buyTransaction)
        console.log("done")
      }
      catch{
        win.webContents.send("console", "<span style='color: red'> Transaction Failed : </span> <br/>")
      }
    }
    console.log(buyTransactions)

    for (i = 0; i < buyTransactionsWaited.length; i++){
      if (buyTransactionsWaited[i]['status'] == 1){
        win.webContents.send("console", "<span style='color: green> Transaction Successful : " + buyTransactionsWaited[i]['transactionHash'] + " </span> <br/>")
      }else {
        win.webContents.send("console", "<span style='color: red> Transaction Failed : " + buyTransactionsWaited[i]['transactionHash'] + " </span> <br/>")
      }
    }

Upvotes: 0

Views: 1312

Answers (1)

kshitij chaurasiya
kshitij chaurasiya

Reputation: 177

You can extend the below code for your use case, in the below example I'm sending ether from my account to the endUserAddress:

const signer = new ethers.providers.Web3Provider(window.ethereum).getSigner();
const gasFeeData = await signer.getFeeData();
const nounce = await signer.getTransactionCount(address);

const transaction = { 
            to: endUserAddress,
            maxFeePerGas: gasFeeData.maxFeePerGas,
            maxPriorityFeePerGas: gasFeeData.maxPriorityFeePerGas,
            chainId: (await signer.getNetwork()).chainId,
            data: null,
            nonce: nounce,
            gasLimit: ethers.utils.hexlify(21000),
            value: ethers.utils.parseEther(amount).toHexString(),
            type: 2
}
const unsignedTx = await ethers.utils.resolveProperties(transaction);
const signedMessage = await signer.signTransaction(unsignedTx);
const txn_receipt = await signer.sendTransaction(signedMessage);
let transaction = await signer.waitForTransaction(txn_receipt.hash);

For calling smart contract you have to modify the transaction object properties like below:

 transaction.to = getContractHash(); 
 transaction.value = null;
 transaction.data = new web3.eth.Contract(getContractABI(), getContractHash()).methods.<your method>(parameters).encodeABI();
 transaction.gasLimit = ethers.utils.hexlify(gaslimit);

Here getContractHash(), and getContractABI() will just simply return the Deployed smart contract hash and its ABI.

Upvotes: 0

Related Questions