Skip to main content

Method Signature

align.blockchain.transactions.estimateGas(
  from: string,
  to: string,
  amount: string,
  network: Network,
  data?: string
): Promise<GasEstimate>

Parameters

from
string
required
Sender wallet address
to
string
required
Recipient address
amount
string
required
Amount in native token (e.g., "0.1")
network
string
required
Network: ethereum, polygon, base, arbitrum
data
string
Optional encoded contract call data (for contract interactions)

Returns

gasLimit
string
Estimated gas units required
gasPrice
string
Current gas price in wei
maxFeePerGas
string
EIP-1559 max fee per gas
maxPriorityFeePerGas
string
EIP-1559 priority fee
estimatedCost
string
Total estimated cost in native token

Examples

import Align from "@tolbel/align";

const align = new Align({
  apiKey: process.env.ALIGN_API_KEY!,
  environment: "sandbox",
});

const estimate = await align.blockchain.transactions.estimateGas(
  "0xSenderAddress...",
  "0xRecipientAddress...",
  "0.1",
  "polygon"
);

console.log(`Gas Limit: ${estimate.gasLimit}`);
console.log(`Estimated Cost: ${estimate.estimatedCost} MATIC`);

Show Fee to User Before Send

const estimate = await align.blockchain.transactions.estimateGas(
  wallet.address,
  recipient,
  sendAmount,
  "polygon"
);

// Display to user
console.log(`
  Transaction Summary:
  - Sending: ${sendAmount} MATIC
  - Network Fee: ${estimate.estimatedCost} MATIC
  - Total: ${parseFloat(sendAmount) + parseFloat(estimate.estimatedCost)} MATIC
`);

// Check if user has enough
const balance = await align.blockchain.wallets.getBalance(
  wallet.address,
  "polygon"
);
const totalNeeded = parseFloat(sendAmount) + parseFloat(estimate.estimatedCost);

if (parseFloat(balance) < totalNeeded) {
  throw new Error(`Insufficient balance. Need ${totalNeeded}, have ${balance}`);
}
Gas estimates include a buffer for safety. Unused gas is returned to the sender.