跳转到主要内容
Simulation API 允许您模拟交易并获取成功执行该交易所需的预计 gas 用量。 在请求中设置 estimate_gas: true 即可获取 gas 估算。
注意:您可以使用 tenderly_estimateGastenderly_estimateGasBundle RPC 方法获取 100% 精确的 gas 估算。
示例 请务必将访问密钥安全地存储在 .env 文件中。
example.ts

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();
此操作会计算交易所需的最大 gas 用量。模拟成功后,请检查 transaction 结构中的 gas_used 字段。 结果
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,
    ...
 }