How to calculate spendTx fees?

How can we calculate fees for a spend transaction with JS SDK?

Hey Kira :slight_smile:

(BaseGas + (byte_size(SpendTx) * GasPerByte)) * GasPrice

Regards,
Pegah

You can check the implementation in one of the SDKs, here is how the fee is calculated in Python:

If I may ask, what are you working on that requires you to calculate the fee? I am asking because there are already SDKs for Javascript, Python, Golang, Elixir and a community maintained Java one that implement already the fee calculation.

I’m trying to implement something like, if the fee for given transaction exceeds a certain threshold then don’t send that transaction.

Also, Is the fees calculated from SDK implementation the minimum possible fees for that transaction?

Or you could also use the TxBuilder:

TxBuilder.calculateMinFee(
  'spendTx', {
    params: {
      senderId: STUB_ADDRESS,
      recipientId: STUB_ADDRESS,
      amount: MAX_UINT256,
      ttl: MAX_UINT256,
      nonce: MAX_UINT256,
    },
  },
);

and the transaction builder can be accessed like:

import { TxBuilder } from '@aeternity/aepp-sdk/es';

I’m using the Node.js bundle. I think TxBuilder is not exposed in it.

yes, the fee is the minimum possible fee **

** there is the GAS_PRICE element that is decided in 2 places, one by consensus[1] is 1e6 and one by configuration[2] where the default is 1e9, the node will accept tx using the max( [1], [2] ) as mininum gas price; the SDKs use the [2] as default (as for then node)

Hey, you can import TxBuilder from bundle like:
import { TxBuilder } from '@aeternity/aepp-sdk'

Is the value 1e6 for consensus a constant or can it increase at the time of congestions (like in the case ether the gas price can be too high and I don’t want to send transaction in those cases) and is there any way of getting current consensus GAS_PRICE?

The 1e6 is a constant, cannot be changed and it is described in the protocol specs. It is the minimum value for GAS_PRICE for the node.
What may change (on the miner side) is the minimum gas price that a miner will accept to include a transaction ( it is 1e9 by default ) but there is no way to get that information since a mining node may not even be exposed to the network