simulateExecution - SDK

Simulates a single transaction and the resulting and returns list of decoded traces and logs that occurred during the transaction simulation.

Don’t have an API key?

Start using this method in your app today. Get started for free

Description

Simulates the asset changes resulting from a single transaction.

Returns list of asset changes that occurred during the transaction simulation. Note that this method does not run the transaction on the blockchain.

Parameters

NameTypeDescription
transactionobjectThe transaction to simulate.
blockIdentifier?stringOptional block identifier to simulate the transaction in.

transaction object parameters

PropertyTypeDescription
data?stringThe data associated with the transaction.
from?stringThe address the transaction is sent from.
gas?stringThe gas provided for the transaction execution, as a hex string.
gasPrice?stringThe gas price to use as a hex string.
tostringThe address the transaction is directed to.
valuestringThe value associated with the transaction as a hex string.

Response

PropertyTypeDescription
Promise<SimulateExecutionResponse>objectReturns the transaction simulation response.

SimulateExecutionResponse object parameters

PropertyTypeDescription
callsarray of objectAn array of traces generated during simulation that represent the execution of the transaction along with the decoded calls if available.
logsarray of objectAn array of logs emitted during simulation along with the decoded logs if available.

calls array property parameters

PropertyTypeDescription
decoded?stringA decoded version of the call. Provided on a best-effort basis.
errorstringOptional error field.
gasstringGas provided for call as a hex string.
gasUsedstringGas used during the call as a hex string.
inputstringCall data.
fromstringFrom address of the transaction.
outputstringReturn data.
typestringThe type of call.
valuestringAmount of value transfer as a hex string.
tostringTo address of the transaction.

logs array property parameters

PropertyTypeDescription
decoded?stringA decoded version of the log. Provided on a best-effort basis.
addressstringThe address of the contract that generated the log.
datastringThe data included the log.
topicsarray of stringsAn array of topics in the log.

Example Request and Response

Prerequisite: You will need to install the Alchemy SDK before making requests with it.

The commands for installing it using npm or yarn are given below:

$npm install alchemy-sdk@latest

Request

index.js
1// Imports the Alchemy SDK
2const { Alchemy, Network } = require("alchemy-sdk");
3
4// Configures the Alchemy SDK
5const config = {
6 apiKey: "alchemy-replit", // Replace with your API key
7 network: Network.ETH_MAINNET, // Replace with your network
8};
9
10// Creates an Alchemy object instance with the config to use for making requests
11const alchemy = new Alchemy(config);
12
13const main = async () => {
14 // define the transaction hash
15 const trnx = "0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b";
16
17 //Call the method to display the transaction based on the transaction hash
18 const response = await alchemy.transact.simulateExecution(trnx)
19
20 //Logging the response to the console
21 console.log(response)
22}
23
24main();

Response

json
1{
2 calls: [
3 {
4 type: 'CALL',
5 from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
6 to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
7 value: '0xf3dbb76162000',
8 gas: '0x6fb0',
9 gasUsed: '0x53a0',
10 input: '0x68656c6c6f21',
11 output: '0x'
12 }
13 ],
14 logs: []
15}

Use Cases

Here are some potential use cases for the simulateExecution method:

  • Testing: You can use simulateExecution to test the logic of your smart contract without incurring the cost of actually executing the transaction on the blockchain.

  • Gas estimation: The method can also be used to estimate the amount of gas required to execute a transaction. This can be useful in determining the appropriate gas limit to set for a transaction to ensure it is processed efficiently.

  • Debugging: When you encounter errors while interacting with a smart contract, simulateExecution can help you debug the issue. By simulating the execution of the transaction, you can identify the specific line of code that is causing the error.

Related Methods

Here are the methods related to simulateExecution:

  • simulateExecutionBundle: Simulates a list of transactions in sequence and returns list of decoded traces and logs that occurred for each transaction during simulation.