simulateAssetChanges - SDK

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.

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<SimulateAssetChangesResponse>objectReturns the transaction 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 trnx = "0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b";

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

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

main();

Response

{
  changes: [
    {
      assetType: 'NATIVE',
      changeType: 'TRANSFER',
      from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
      to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
      rawAmount: '4290000000000000',
      contractAddress: undefined,
      tokenId: undefined,
      decimals: 18,
      symbol: 'ETH',
      name: 'Ethereum',
      logo: 'https://static.alchemyapi.io/images/network-assets/eth.png',
      amount: '0.00429'
    }
  ],
  gasUsed: '0x53a0',
  error: undefined
}

Code Sandbox

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

Use Cases

Here are some potential use cases for the simulateAssetChanges method:

  • Testing: Developers can use simulateAssetChanges 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 simulateAssetChanges to optimize their transaction strategies. By simulating different transaction scenarios, these firms can identify the most efficient ways to move assets between accounts.

Related Methods

Here are the methods related to simulateAssestChanges: