Virtual TestNets are live! ⚡️ Test and stage dapps on real-time production data.
Get started →

All Products

Simulations
Gas Estimation

Precise Gas Estimation Using Simulation API

The Simulation API allows you to simulate transactions and obtain the estimated gas usage needed to successfully execute the transactions.

Set estimate_gas: true in the request to get gas estimates.

Example

Make sure to store the access key securely in a .env file.

example.ts
import axios from 'axios';
import * as dotenv from 'dotenv';
 
dotenv.config();
 
const { TENDERLY_ACCOUNT_SLUG, TENDERLY_PROJECT_SLUG, TENDERLY_ACCESS_KEY } = process.env;
 
const simulateTransaction = async () => {
  const { data } = await axios.post(
    `https://api.tenderly.co/api/v1/account/${TENDERLY_ACCOUNT_SLUG}/project/${TENDERLY_PROJECT_SLUG}/simulate`,
    {
      // Simulation Configuration
      save: true,
      save_if_fails: false,
      estimate_gas: true,
      simulation_type: 'quick',
      network_id: '1',
      // Standard EVM Transaction object (sample values)
      from: '0xdc6bdc37b2714ee601734cf55a05625c9e512461',
      to: '0x6b175474e89094c44da98b954eedeac495271d0f',
      input:
        '0x095ea7b3000000000000000000000000f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1000000000000000000000000000000000000000000000000000000000000012b',
      gas: 8000000,
      gas_price: 0,
      value: 0,
    },
    {
      headers: {
        'X-Access-Key': TENDERLY_ACCESS_KEY as string,
      },
    },
  );
 
  console.log({ data });
};
 
simulateTransaction();

This calculates the maximum gas needed for the transaction. After a successful simulation, check the gas_used field in the transaction structure.

Result

example.json
"transaction": {
    ...
    "gas": 8000000,
    "gas_price": 0,
    "gas_fee_cap": 0,
    "gas_tip_cap": 0,
    "cumulative_gas_used": 0,
    "gas_used": 51534, // Gas estimates
    "effective_gas_price": 0,
    ...
 }