Skip to main content
In this tutorial, you’ll learn how to use the Tenderly SDK to simulate bundled transactions. A bundled transaction is a collection of multiple transactions that are simulated as a unit. Bundled transactions are often used in DeFi protocols and other applications to simulate the process of performing complex operations that involve multiple contract interactions.
Transactions from a bundle are simulated on the same blocks.
We’ll simulate the usage of Uniswap for swapping a particular amount of DAI for WETH, and show the execution status of simulated transactions, and calculate the total gas used. Here are the transactions needed to achieve this:
  1. Simulate minting of 2 DAI so the swapper has some assets to swap. Alternatively, if you have DAI, you can skip this and just do transaction 2.
  2. Approve UniswapV3Router to use DAI.
  3. Do the swap. Call UniswapV2Router.exactInputSingle to perform the swap.
For the 1st transaction to run successfully, the sender needs to be a ward of DAI stablecoin. Since most of us aren’t, we’ll see how to use State Overrides to “become a ward” within the context of the Bundled Simulation.

Prerequisites

Step 1: Set up the project

Create a new npm project, install dependencies, and configure Typescript by pasting the following commands into your terminal:
example

Step 2: Call the Simulation API

Copy the complete runnable example from below and paste it into the swap.ts file. Transactions are determined by functions mint2DaiTx, approveUniswapV3RouterTx, and swapSomeDaiForWethTx:
example.ts
Further down in the tutorial, you’ll find detailed explanations of what the code is doing.

Step 3: Run the script

example

Understanding the code

First, we’re creating a new instance of the Tenderly SDK:
example.ts
To call the Simulation API and perform a bundled simulation, we’re using Tenderly.simulator.simulateBundle() and passing an object with the following fields:
  • blockNumber - the block number on which you wish to execute the simulation.
  • transactions - an array holding three transactions: minting of 2 DAI, approving the Uniswap Router, and swapping DAI for WETH.
  • overrides - an object making fakeWardAddress a ward of DAI within the simulation.
example.ts

Understanding State Overrides

We’re calling the DAI stablecoin mint function. During the simulation, the DAI contract has to believe you (the sender) are one of the wards. Otherwise, it will reject to mint. We need the following condition to hold for DAI contract’s state for a successful minting initiated by fakeWardAddressEOA:
example.ts
To enable this, we’re passing overrides to the SDK, where we’re overriding the DAI wards storage variable, so fakeDaiWardAddress becomes a ward during the simulation.
example.ts
Note that the overrides apply to the entire bundle and are visible to all simulations within the bundle.
Now, you have a better understanding of how to use the Tenderly SDK to perform complex simulation scenarios and display essential results.