0

How to calculate the transaction fee using the library @solana/web3.js? I found the interface here, but I don't know how to use it

vlabster
  • 33
  • 4

1 Answers1

1

Given a message, you can use the getFeeForMessage() API on Connection.

const transaction = new Transaction(/* ... */);

const response = await connection.getFeeForMessage(
  transaction.compileMessage(),
  'confirmed',
);
const feeInLamports = response.value;
steveluscher
  • 4,144
  • 24
  • 42
  • See also, the tests for `getFeeForMessage`: https://github.com/solana-labs/solana/blob/ebe3d2d59d34f8d3c4d04ed42b4e8f958b2a6829/web3.js/test/connection.test.ts#L2319-L2349 – steveluscher Mar 01 '22 at 22:34