0%
Overview page background
HomeOverviewsInfra
What are web3 transaction simulations?

What are web3 transaction simulations?

Alchemy headshot

Written by Alchemy

Brady Werkheiser headshot

Reviewed by Brady Werkheiser

Published on January 25, 20235 min read

Transaction Simulation allows anyone in web3 to simulate or preview a transaction before signing it on-chain. This gives people better visibility and predictability into their transactions, enhances their understanding of how their assets will be impacted after a transaction and ensures they’re able to save gas by not sending transactions that will later revert on chain. 

Alchemy's new Transaction Simulation API endpoints include support for simulating asset changes, single transaction simulations, and bundle transaction simulations.

In this article, we’ll cover: 

  • What is Transaction Simulation? 

  • What is the transaction lifecycle?

  • The methods and features of Transaction Simulation? 

  • What blockchains support Transaction Simulation APIs? 

  • How Transaction Simulation works?

  • How can Transaction Simulation be used? 

  • How to get started with Transaction Simulation? 

Transaction Simulation is the ability to preview how a transaction will behave, before authorizing (i.e. signing) transactions on-chain. Human readable interpretations of transactions ensure that a user knows that a transaction will behave how they want and expect it to behave. 

Transaction Simulation improves transaction security and allows any web3 user to protect themselves from fraudulent transactions, all with a single method change. 

By knowing the impact of a transaction before it hits the blockchain, people will save money on gas fees, too (the visibility that comes from Transaction Simulation makes it easy to avoid spending gas on transactions that will revert on-chain). 

The transaction lifecycle happens in four sequential stages:

  1. Create: You decide to send a transaction at a specific gas price

  2. Simulate: You preview how your transaction will behave once it is sent

  3. Send: You send your transaction to get executed on chain

  4. Monitor: You track notifications to ensure your transaction is successful

Alchemy Transact is a series of products for faster, cheaper, and safer transactions, offering improvements throughout the entire web3 transaction lifecycle.  

Alchemy’s Transaction Simulation is the first set of available products for the “Simulate” phase of the web3 transaction lifecycle, allowing users to know exactly what their transactions will do on-chain, keep their assets safe, and protect themselves from fraudulent scams.

Alchemy's new Transaction Simulation product includes three APIs - simulateAssetChanges, simulateExecution, simulateBundle - that are designed for three different use cases: simulating asset transfers, single transactions and multiple (sequential) transactions. 

With alchemy_simulateAssetChanges, any transaction can be simulated, and the simulation output will be optimized, simplified data, inclusive of balance changes and approvals. The Asset Changes simulation API endpoint makes it easy for developers to show users the asset deltas of a transaction in their dapp, before it's executed.

The Asset Changes API method is particularly useful for wallet developers and builders creating web3 security tools, who are seeking highly performant and simplified data to pass on to their users. 

With alchemy_simulateExecution, developers receive an end-to-end simulation of a transaction’s EVM execution, inclusive of ABI decoded results and comprehensive logs, events and trace data.

The Simulate Execution API endpoint outputs significantly more data than Asset Changes API, which is useful for teams that need more transaction preview details. The decoded alchemy_simulateExecution data implicitly includes the asset changes data in the logs.

With alchemy_simulateBundle, developers pass in an array of transactions to accurately preview a transaction bundle in sequence. State changes are persisted after each transaction to simulate submitting the transactions serially. 

The Bundle Simulation API endpoint is necessary in situations where the outcome of a transaction is dependent on the outcome of a previous transaction, and therefore, for accurate transaction previews, a developer must simulate both transactions in order. 

All Transaction Simulation APIs are multi-chain and available with one line of code via the Alchemy SDK.

Transaction Simulation APIs are available on:

  1. Ethereum

  2. Polygon

  3. Arbitrum

  4. Optimism (coming soon)

Transaction Simulation APIs are available on:

  1. Ethereum Goerli

  2. Arbitrum Goerli

  3. Mumbai Testnet (Polygon)

Alchemy’s Transaction Simulation products are built using custom tracers, built on top of the debug_traceCall RPC method. The method accepts a custom JS tracer that can return internal transactions, logs, and more.

Here’s how it works:

  1. A user prepares to send a transaction

  2. A user creates a transaction with some attributes

  3. A user signs the transaction with their private key

  4. The transaction is sent to the receiving address. 

If a transaction is not signed with a private key, then the transaction will never be executed, but the results of the transaction can be previewed, making this a useful implementation for transaction simulation. 

When a transaction is sent, the transaction attributes include hexadecimal inputs. debug_traceCall can then be used to run the transaction in the exact same manner as it would be executed on the network. The output of this simulation is in hex. 

To decode these results into human readable outputs, we utilize a custom tracer system. This system can detect that there is an event from Contract X on Address Y and that event transfers Z amount of ABC tokens. 

The custom tracer enables: 

  • Retrieving events from a node

  • Identifying smart contracts involved in a transaction 

  • Identifying the assets that would be transferred*

To identify the assets that would be transferred during simulated transactions Alchemy uses both the NFT API and Token API to enrich the simulated data.

Transaction Simulation is useful for both builders and traders including wallets, security products, DEXs, NFT Marketplaces, lending protocols, trade execution, and MEV simulations.

Developers building wallets, security products, DEXs, auctions / NFT marketplaces, lending protocols or smart contract developers all can start using Transaction Simulation to enhance their product experience. 

Transaction Simulation will help users preview balance changes prior to signing or sending a transaction. They can use the simulation results to display to users the flow of assets transferred and preview net-balance changes. They can also identify malicious or buggy smart contracts before a user interacts with them.

Any tools, e.g. browser extensions or threat detection networks, that monitor the blockchain in order to warn users if a transaction may be malicious can benefit from Transaction Simulation. 

Developers can utilize Transaction Simulation to proactively report accurate slippage, accurate tokens received, and accurate failing calls.

Transaction Simulation can help identify whether a bid, mint, purchase or listing will go through and the potential outcomes of the transaction. 

Lending protocol developers can utilize Transaction Simulation to detect and account for whether a Borrow / Repay / Claim transaction will successfully land on chain.

Transaction Simulation can help developers remove the need for testnets, test eth, and faucets. By providing a mechanism to simulate / preview transactions, developers can avoid creating an entirely new testing environment. 

Traders can utilize Transaction Simulation in a multitude of ways: 

  1. To preview many iterations of the same trade and execute only the most profitable one 

  2. To understand potential outcomes of a trade or arbitrage opportunity prior to execution

  3. Make existing MEV searching strategies more successful and profitable 

Head to our Transaction Simulation Quickstart Guide to get started, and sign up for an Alchemy account to start building today. 

Overview cards background graphic
Desktop section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key