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.

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
yarn add alchemy-sdk@latest

Request

// Imports the Alchemy SDK
const { Alchemy, Network } = require("alchemy-sdk");

// Configures the Alchemy SDK
const config = {
    apiKey: "alchemy-replit", // Replace with your API key
    network: Network.ETH_MAINNET, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);

const main = async () => {
    // define the transaction hash
    const trnx = "0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b";

    //Call the method to display the transaction based on the transaction hash
    const response = await alchemy.transact.simulateExecution(trnx)

    //Logging the response to the console
    console.log(response)
}

main();

Response

{
  calls: [
    {
      type: 'CALL',
      from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
      to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
      value: '0xf3dbb76162000',
      gas: '0x6fb0',
      gasUsed: '0x53a0',
      input: '0x68656c6c6f21',
      output: '0x'
    }
  ],
  logs: []
}

Code Sandbox

You can test out the simulateExecution method using the code sandbox below:

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.
ReadMe