On BSC, usually when your transaction to a contract fails such a tx uses about 30k gas when you do it through a wallet. But when a transaction made with an ethers.js script fails, it uses all the gas up to the gasLimit. Is there anything I can do to make my script behave correctly?
What should be: https://bscscan.com/tx/0xb36a1ae9d8294db8e1e1faf23c5c8d98d5e1cbcf3ae3bd57dc7f1fcb648b9041
What happens: https://bscscan.com/tx/0x864e489c1e678d536b07d84bccd609d07002d6d1a66a6a0ff2a368ab53bc47eb
const ethers = require("ethers");
const purchaseAmount = ethers.utils.parseEther("1");
const recepient = "";
const privateKey = "";
const sender = "";
const provider = new ethers.providers.WebSocketProvider("wss://bsc-ws-node.nariox.org:443");
const wallet = new ethers.Wallet(privateKey, provider);
const Tx = require('ethereumjs-tx')
async function sendEths() {
let txCount = await provider.getTransactionCount(sender);
let tx = {
nonce: ethers.utils.hexlify(txCount),
to: recepient,
value: purchaseAmount,
gasLimit: 140000,
gasPrice: ethers.utils.parseUnits("5", "gwei"),
};
console.log("Sending transactions");
let receipt = await wallet.sendTransaction(tx);
console.log("Transaction receipt");
console.log(receipt);
process.exit();
};
sendEths();