Method Signature
align.blockchain.transactions.estimateGas(
from: string,
to: string,
amount: string,
network: Network,
data?: string
): Promise<GasEstimate>
Parameters
Amount in native token (e.g., "0.1")
Network: ethereum, polygon, base, arbitrum
Optional encoded contract call data (for contract interactions)
Returns
Estimated gas units required
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`);
const estimate = await align.blockchain.transactions.estimateGas(
from,
to,
"0.1",
"polygon"
);
console.log("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.