How to Simulate Transactions with Tenderly SDK
In this tutorial, you’ll learn how to run a transaction simulation using the Tenderly SDK. Simulations allow you to test the behavior of your transactions without sending them on-chain. Learn more about Tenderly’s Transaction Simulator here.
Prerequisites
- Node.js installed on your machine
- Tenderly account with an API key
- Project set up in the Tenderly Dashboard
Use this link to sign up for Tenderly.
Step 1: Initialize a new Node.js project
Create a new directory for your project, navigate to it in the terminal, and initialize a new Node.js project:
mkdir tenderly-simulation
cd tenderly-simulation
npm init -y
Step 2: Install Tenderly SDK
Install the Tenderly SDK npm package as a dependency in your Node.js project:
npm install @tenderly/sdk
Step 3: Import the Tenderly SDK and configure it
Create a new file named index.js
in your project directory, and add the following code to import the Tenderly SDK and configure it with your Tenderly account details:
const { Network, Tenderly } = require('@tenderly/sdk');
const tenderly = new Tenderly({
accountName: 'your-account-name',
projectName: 'your-project-name',
accessKey: 'your-access-key',
network: Network.MAINNET, // Replace with the appropriate network
});
Make sure to replace your-account-name
, your-project-name
, and your-access-key
with your actual Tenderly account information.
Follow these guides to obtain the configuration details.
Step 4: Define the transaction details
Next, define the transaction details for the simulation.
Add the following code to index.js
:
const transactionParameters = {
from: '0x0000000000000000000000000000000000000000',
to: '0x1f98431c8ad98523631ae4a59f267346ea31f984',
input: '0xa1671295000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000152649ea73beab28c5b49b26eb48f7ead6d4c8980000000000000000000000000000000000000000000000000000000000000bb8',
value: '0',
gas: 8000000,
gas_price: '0',
};
const simulation = {
transaction: transactionParameters,
blockNumber: 18813657, // simulation's target block number
//overrides: {...} // optional state overrides field
};
Replace the from
, to
, input
, value
, gas
, and gas_price
fields with your desired transaction details. The override
field is optional and allows you to override the state of specific contracts in the simulation.
Make sure that the specified block number actually exists on the selected network.
Step 5: Run the transaction simulation
Now, use the Tenderly SDK’s simulateTransaction
method to run the transaction simulation.
Add the following code to index.js
:
(async () => {
try {
const simulationResult = await tenderly.simulator.simulateTransaction(simulation);
console.log('Simulation result:', simulationResult);
} catch (error) {
console.error('Error running simulation:', error);
}
})();
This code runs an asynchronous function that calls the simulateTransaction
method and logs the simulation result to the console.
Step 6: Execute the script
Save the index.js
file and run the script using the following command:
node index.js
If the transaction simulation is successful, you’ll see the simulation result printed in the console.
That’s it! You’ve now learned how to run a transaction simulation using the Tenderly SDK.