cosmjs
cosmjs copied to clipboard
How to calculate the transaction fee.
I am building dApp running on Juno Chain using @cosmjs/cosmwasm-stargate.
I want to send the whole JUNO tokens from my wallet to the other.
But when I inputted the amount as the balance of my wallet, I got the error "insufficient funds".
I think it is related to the transaction fee.
So I would like to get the amount, calculated by subtracting the transaction fee from the balance.
Any comment will be helpful for me.
Bank send transactions have a relatively stable gas usage. I get simulation results of like 71507. So it you use a gas limit of 100_000 you should be safe. Now take a gas price as configued in Keplr, let's say 0.0025 µJUNO/gas. Then you do
import { calculateFee } from "@cosmjs/stargate";
const fee = calculateFee(100_000, "0.0025ujuno");
console.log(fee) // { amount: [ { amount: '250', denom: 'ujuno' } ], gas: '100000' }
If you now subtract this fee.amount from the balance and use this fee, your account should be cleared.