sei-protocol / sei-js

SeiJS is a monorepo that contains multiple NPM libraries for writing applications that interact with the Sei network.
43 stars 37 forks source link

[BUG] calculateFee calculates wrongly #173

Closed Mikssxed closed 4 months ago

Mikssxed commented 4 months ago

SeiJS package & version @sei-js/cosmjs: 1.0.5 @sei-js/proto: 4.0.5 @cosmjs/stargate: 0.32.3

Chain ID testnet

Describe the bug While running calculate fee like this while trying to get max value to send

const fee = calculateFee(balance, "0.1usei");

It calculates amount for example 4500 usei, balance was 45000usei then I am sending it, after substracting 45000 usei from balance and sendAmount there is 40500 usei then i calculate fee once again with balance = 40500 in sendAmount

const send = await signingClient.sendTokens(
        activeAccount.address,
        receiver,
        [sendAmount],
        fee,
      );

but after this I got this error Log: out of gas in location: ReadPerByte; gasWanted: 45000, gasUsed: 45019: out of gas]

To Reproduce Described up

Expected behavior Calculate correct amount of fee and let me send transaction

besated commented 4 months ago

The first param in calculateFee is actually gas limit, not the amount. Amount isn't used to calculate the fee needed for the transaction. You just need gas limit and price and it's calculated like this: gas limit * gas price.

In your case, you're passing in a gas limit of 45000 and a gas price of 0.1usei resulting in a gas cost of 4500usei. The error log is because the amount of gas required for a bank send is ~90k, so you will need to pass in a gas limit higher than that.