Portal
Portal Team
Share the love
Want to stay updated? Subscribe below to keep in touch.
Intro to Transaction Simulation on Portal
Protect your users from accidental or malicious transactions on the blockchain.
September 26, 2023
Web3 is more than just simple token transfers. Today’s applications regularly execute complex transactions, such as contracts, agreements, and asset exchanges. These complex agreements are constituted of many parts that happen automatically once they are set in motion. Because a transaction can not be halted once it begins, there is a lot of value in knowing up front if it will succeed or fail, and how much it will cost to execute. That is where transaction simulation comes in.
The value of transaction simulation
Executing a transaction on a blockchain is similar to hitting ‘send’ on an email. Once it is done, it can’t be recalled. Now imagine that you were charged a fee for sending that email. That’s what web3 transactions are like. Once initiated, it can’t be undone, no matter the consequences.
For a user, sometimes the consequences are truly detrimental, such as loss of funds or other unintentional outcomes. A failed or reverted transaction on the blockchain can be frustrating because the user still has to pay a gas fee on these transactions. Worse, a user can also lose funds to malicious smart contract addresses. In 2022, more than $5B was lost in crypto scams alone.
If you are an app developer or organization offering web3 access to your users, you need to secure your users’ transactions as there is not only money on the line but also trust and reputation.
Portal’s Transaction Simulation is a tool that allows developers or organizations to protect their users from malicious or costly smart contract transactions by simulating them before any real users approve those transactions. Think of it as “try before you buy.”
How transaction simulation works in a nutshell
For the technically inclined, you must be wondering how a transaction can be simulated without being submitted.
When a developer calls the simulateTransaction function, a transaction is prepared with all of its associated parameters and submitted to an EVM node that is dry run without being submitted on-chain. The conditions of that transaction are simulated as close as possible to get the expected outcomes.
Most importantly, the transaction must be simulated in a short period of time to ensure the results are actionable. A matter of seconds can mean different outcomes altogether.
How developers can use transaction simulation with Portal’s SDK
Once a developer has created a Portal account, the developer can call the portal.api.simulateTransaction function (see docs here) to see how a transaction would play out without actually finalizing it.
Developers will then receive technical details of the potential transaction outcomes from the JSON-RPC request, particularly changes in tokens, approvals, contracts, and events; gas used; and errors.
The simulation offers a look at the results of the full transaction sequence. Would it go smoothly or would it fail before it is complete? Are there errors lurking in unknown places?
As a developer, once you get those insights in advance, you can choose use cases accordingly. These are a few common use cases:
- Prevent users from making accidental or failed transactions. You can prevent your users from spending money on accidental or failed transactions by including a step for users to simulate their transactions. That way, they are not wasting funds on transactions that were never meant to be.
- Alert users to malicious transactions and prevent their wallets from being drained! Developers can show the user the results of the transaction simulation, or implement an approve/decline policy based on those results. Either way, if your user was expecting to be charged 0.1 SOL but is shown that the transaction will take 100 SOL instead, then you/ your user will know something is up!
- Create a better user experience by estimating if the user has enough gas. Remember when your bank would tell you you had "Insufficient Funds" to make a transaction? How annoying was that? As the developer, you can simulate the transaction to understand how much gas is needed for a transaction (using the gasUsed response). If the user does not have enough to pay for gas, you can create customised user flows like enabling auto top-ups to enable a better user experience!
The big wins from transaction simulation
This feature offers significant value from a simple and straightforward function:
- Fewer Mistakes: Reduce those "Oh no, what did I just do?" moments by catching mistakes before they become real problems.
- Smarter Choices: Transaction simulation is like having a crystal ball. Developers and organizations can help their users make decisions with a lot more confidence.
- Happy Users: Less guesswork and fewer errors mean a smoother experience for everyone involved.
You can test out transaction simulation for yourself today! Book a demo today.