How to Simulate Transactions with Tenderly SDK
Learn how to execute a simple transaction simulation using the 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.
- Node.js installed on your machine
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
npm install @tenderly/sdk
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 { Tenderly, Network } = 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.Next, define the transaction details for the simulation.
Add the following code to
index.js
:const transactionParameters = {
from: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
to: "0x742d35Cc6634C0532925a3b844Bc454e4438f44f",
input: "0x606060...",
value: "1000000000000000000",
gas: 21000,
gas_price: "20000000000",
};
const simulationDetails = {
transaction: transactionParameters,
blockNumber: 12000000,
override: {}, // Optional state override
};
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.
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(simulationDetails);
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.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.