What are web3 transaction simulations?
Written by Alchemy
Reviewed by Brady Werkheiser
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?
What is 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).
What is the transaction lifecycle?
The transaction lifecycle happens in four sequential stages:
Create: You decide to send a transaction at a specific gas price
Simulate: You preview how your transaction will behave once it is sent
Send: You send your transaction to get executed on chain
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.
Transaction Simulation API Methods
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.
1. Asset Changes
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.
2. Execution Simulation
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.
3. Bundle Simulation
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.
What blockchains support the Transaction Simulation API endpoint?
All Transaction Simulation APIs are multi-chain and available with one line of code via the Alchemy SDK.
Supported Mainnets
Transaction Simulation APIs are available on:
Ethereum
Polygon
Arbitrum
Optimism (coming soon)
Supported Testnets
Transaction Simulation APIs are available on:
Ethereum Goerli
Arbitrum Goerli
Mumbai Testnet (Polygon)
How does the Transaction Simulation API endpoint work?
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:
A user prepares to send a transaction
A user creates a transaction with some attributes
A user signs the transaction with their private key
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.
Simulating Transactions with Custom Tracers
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.
How can the Transaction Simulation API endpoint be used?
Transaction Simulation is useful for both builders and traders including wallets, security products, DEXs, NFT Marketplaces, lending protocols, trade execution, and MEV simulations.
Transaction Simulation Use Cases for Web3 Developers
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.
1. Wallets
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.
2. Security products
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.
3. DEXs
Developers can utilize Transaction Simulation to proactively report accurate slippage, accurate tokens received, and accurate failing calls.
4. Auctions / NFT Marketplaces
Transaction Simulation can help identify whether a bid, mint, purchase or listing will go through and the potential outcomes of the transaction.
5. Lending protocols
Lending protocol developers can utilize Transaction Simulation to detect and account for whether a Borrow / Repay / Claim transaction will successfully land on chain.
6. Smart contract developers
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.
Transaction Simulation Use Cases for Traders
Traders can utilize Transaction Simulation in a multitude of ways:
To preview many iterations of the same trade and execute only the most profitable one
To understand potential outcomes of a trade or arbitrage opportunity prior to execution
Make existing MEV searching strategies more successful and profitable
Get Started Using Alchemy Transaction Simulation
Head to our Transaction Simulation Quickstart Guide to get started, and sign up for an Alchemy account to start building today.
Related overviews
Learn About The Tokenization Initiatives and Why Banking Institutions are Tokenizing Their Assets
Learn About Enterprise Grade Blockchain Infrastructure to Support Your Businesses Specific Needs
Learn About The Advantages, Disadvantages, and Use Cases of Private and Public Blockchains