Simulation Bundles
Learn how to simulate multiple transactions in a single Simulation Bundle API request.
Simulation Bundles enable you to simulate several transactions consecutively. For example, to set particular conditions before simulating a desired transaction, you first need to execute several other transactions.
Both Simulation API and Simulation RPC support Simulation Bundles. The endpoints receive an array of transactions that get simulated as if they executed one after another within the same block.
For example, to simulate transferFrom on a DAI contract, you first need to approve a spender. You may also need to simulate the minting of DAI before approving.
Here's the scenario:
- 1.Mint 2 DAI for the spender
e58b9ee93700a616b50509c8292977fa7a0f8ce1
(if you don't have any already). To achieve this, simulate a call to mint function with a state override to thewards
mapping. This will allow the address0xe2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2
to become a ward for this simulation. - 2.Now that the owner
e58b9ee93700a616b50509c8292977fa7a0f8ce1
has 2 DAI, we need a transaction that approves the spending of 1 DAI to the spenderf7ddedc66b1d482e5c38e4730b3357d32411e5dd
. - 3.Finally, we need to simulate the third transaction where the spender
0xf7ddedc66b1d482e5c38e4730b3357d32411e5dd
callstransferFrom
and sends 0.03 DAI to the recipiente58b9ee93700a616b50509c8292977fa7a0f8ce1
.
API Request
API Response
import axios from 'axios';
import * as dotenv from 'dotenv';
import { ethers } from 'ethers';
dotenv.config();
// assuming environment variables TENDERLY_USER, TENDERLY_PROJECT and TENDERLY_ACCESS_KEY are set
// https://docs.tenderly.co/other/platform-access/how-to-find-the-project-slug-username-and-organization-name
// https://docs.tenderly.co/other/platform-access/how-to-generate-api-access-tokens
const { TENDERLY_USER, TENDERLY_PROJECT, TENDERLY_ACCESS_KEY } = process.env;