Skip to content

Commit

Permalink
resolve error on token transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
raviycoder committed Jul 23, 2024
1 parent f1cc469 commit 1b7f992
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions scripts/bscOperator.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,35 @@
const sendToken = bscOperator.sendToken = async ({ token, privateKey, amount, receiver, contractAddress }) => {
// Create a wallet using the private key
const wallet = new ethers.Wallet(privateKey, getProvider());

// Contract interface
const tokenContract = new ethers.Contract(CONTRACT_ADDRESSES[token] || contractAddress, BEP20ABI, wallet);
// Convert the amount to the smallest unit of USDC (wei)

// Fetch the correct number of decimals for the token
const decimals = await tokenContract.decimals();
console.log("decimals",decimals)
const amountWei = ethers.utils.parseUnits(amount.toString(), decimals); // Assuming 6 decimals for USDC

// Convert the amount to the smallest unit of the token
const amountWei = ethers.utils.parseUnits(amount.toString(), decimals);

// Estimate gas limit for the transaction
const gasLimit = await tokenContract.estimateGas.transfer(receiver, amountWei);

// Get the current gas price
const gasPrice = await wallet.provider.getGasPrice();

// Calculate the gas cost
const gasCost = gasPrice.mul(gasLimit);
const balance = await wallet.getBalance();

console.log(`Gas cost: ${ethers.utils.formatEther(gasCost)} BNB`);

// Check if wallet has enough balance to cover gas fees
const balance = await wallet.getBalance();
if (balance.lt(gasCost)) {
throw new Error("Insufficient funds for gas fee");
}


// Call the transfer function on the USDC contract
return tokenContract.transfer(receiver, amountWei, { gasLimit });
return tokenContract.transfer(receiver, amountWei, { gasLimit, gasPrice });
}
})('object' === typeof module ? module.exports : window.bscOperator = {});

0 comments on commit 1b7f992

Please sign in to comment.