simulateAssetChangesBundle - SDK

Simulates the asset changes resulting from a list of transactions simulated in sequence. Returns a list of asset changes for each transaction during simulation.

Don’t have an API key?

Start using this method in your app today.

Description

Simulates the asset changes resulting from a list of transactions simulated in sequence.

Returns a list of asset changes for each transaction during simulation.

Parameters

NameTypeDescription
transactionsarrayTransactions list of max 3 transactions to simulate.
blockIdentifier?stringOptional block identifier to simulate the transaction in.

transactions array 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<SimulateAssetChangesResponse[]>array of objectReturns the transactions response.

SimulateAssetChangesResponse object parameters

PropertyTypeDescription
changesarrayAn array of asset changes that resulted from the transaction.
error?objectOptional error field that is present if an error occurred. The available parameter in this error response is:

message: string The error message.
gasUsed?stringThe amount of gas used by the transaction represented as a hex string. The field is undefined if an error occurred.

changes array property parameters

PropertyTypeDescription
amount?stringThe amount as an integer string. This value is calculated by applying the decimals field to the rawAmount field. Only available on TRANSFER changes for NATIVE and ERC20 assets, or ERC721/ERC1155 disapprove changes (field set to '0').
assetTypestringThe type of asset from the transaction.
changeTypestringThe type of change from the transaction.
contractAddress?stringThe contract address of the asset. Only applicable to ERC20, ERC721, ERC1155, NFT and SPECIAL_NFT transactions.
decimals?numberThe number of decimals used by the ERC20 token. Set to 0 for APPROVE changes. Field is undefined if it's not defined in the contract and not available from other sources.
fromstringThe from address.
logo?stringURL for the logo of the asset, if available. Only applicable to ERC20 transactions.
name?stringThe name of the asset transferred, if available.
rawAmountstringThe raw amount as an integer string. Only available on TRANSFER changes for NATIVE and ERC20 assets, or ERC721/ERC1155 disapprove changes (field set to '0').
symbol?stringThe symbol of the asset transferred if available.
tostringThe to address.
tokenIdstringThe token id of the asset transferred. Only applicable to ERC721, ERC1155 and SPECIAL_NFT NFTs.

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

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 trnxs = 
      [   
        {
          tr1: "0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b",
          tr2: "0xf02c1c8e6114b1dbe8937a39260b5b0a374432bbrrewn67353841eer3432cb45"
        }
			]

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

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

main();

Response

[ { changes: [], gasUsed: '0xcf08', error: undefined } ]

Code Sandbox

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

Use Cases

Here are some potential use cases for the simulateAssetChangesBundle method:

  • Testing: Developers can use simulateAssetChangesBundle to test their smart contracts before deploying them to a blockchain network. By simulating the changes that would occur, developers can identify potential errors and debug their code.

  • Optimizing asset transactions: Businesses that regularly perform asset transactions, such as banks or trading firms, can use simulateAssetChangesBundle to optimize their transaction strategies. By simulating different transactions scenarios, these firms can identify the most efficient ways to move assets between accounts.

Related Methods

Here are the methods related to simulateAssestChanges:

ReadMe