cosmjs icon indicating copy to clipboard operation
cosmjs copied to clipboard

How to calculate the transaction fee.

Open sheriff-rango opened this issue 3 years ago • 1 comments

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.

sheriff-rango avatar Jul 18 '22 10:07 sheriff-rango

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.

webmaster128 avatar Aug 04 '22 10:08 webmaster128