Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/actions/estimateFee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Keypair, PublicKey } from '@solana/web3.js';
import { Transaction } from '@metaplex-foundation/mpl-core';
import BN from 'bn.js';
import { Connection } from '../Connection';

interface Params {
connection: Connection;
feePayer: PublicKey;
txs: Transaction[];
signers?: Keypair[];
}

type Lamports = BN;

type EstimateFee = (params: Params) => Promise<Lamports>;

export const estimateFee: EstimateFee = async (params) => {
const { connection, feePayer, txs, signers = [] } = params;

const {
blockhash,
feeCalculator: { lamportsPerSignature },
} = await connection.getRecentBlockhash();

const tx = Transaction.fromCombined(txs, {
feePayer,
recentBlockhash: blockhash,
});

if (signers.length) {
tx.partialSign(...signers);
}

const { numRequiredSignatures } = tx.compileMessage().header;

return new BN(numRequiredSignatures * lamportsPerSignature);
};