From 86154f268b43aa31137f2513719f579e7ebe2ff9 Mon Sep 17 00:00:00 2001 From: raviycoder <135521192+raviycoder@users.noreply.github.com> Date: Mon, 5 Aug 2024 20:54:57 +0530 Subject: [PATCH] solve transaction issue in bsc wallet send again --- scripts/bscOperator.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/scripts/bscOperator.js b/scripts/bscOperator.js index 7f06adc..ab4ffda 100644 --- a/scripts/bscOperator.js +++ b/scripts/bscOperator.js @@ -336,18 +336,33 @@ const provider = getProvider(); const signer = new ethers.Wallet(privateKey, provider); - // Creating and sending the transaction object for BNB + // Estimate gas limit + const gasLimit = await provider.estimateGas({ + to: receiver, + value: ethers.utils.parseUnits(amount.toString(), 'ether'), + }); + + const gasPrice = await provider.getGasPrice(); + const maxPriorityFeePerGas = ethers.utils.parseUnits("2", "gwei"); + const maxFeePerGas = gasPrice.add(maxPriorityFeePerGas); + + // Ensure maxPriorityFeePerGas is less than or equal to maxFeePerGas + if (maxPriorityFeePerGas.gt(maxFeePerGas)) { + throw new Error("Max priority fee per gas cannot be higher than max fee per gas"); + } + + // Creating and sending the transaction object return await signer.sendTransaction({ to: receiver, - value: ethers.utils.parseUnits(amount.toString(), "ether"), - gasLimit: ethers.utils.hexlify(21000), // Standard gas limit for BNB transfer + value: ethers.utils.parseUnits(amount.toString(), 'ether'), + gasLimit: gasLimit, + maxPriorityFeePerGas: maxPriorityFeePerGas, + maxFeePerGas: maxFeePerGas, nonce: await signer.getTransactionCount(), - maxPriorityFeePerGas: ethers.utils.parseUnits("2", "gwei"), }); } catch (e) { - console.error("BNB Transaction Error:", e); - throw new Error("Failed to send BNB transaction"); - } + throw new Error(e.message); + } };