Portal Team

Share the love

Want to stay updated? Subscribe below to keep in touch.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"> <mask id="mask0_996_165" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="2" y="1" width="46" height="46"> <path d="M47.2941 1.41177H2.11768V46.5882H47.2941V1.41177Z" fill="white"/> </mask> <g mask="url(#mask0_996_165)"> <path fill-rule="evenodd" clip-rule="evenodd" d="M16.2353 4.2353C16.2353 2.67591 17.4995 1.41177 19.0589 1.41177H44.4706C46.0301 1.41177 47.2941 2.67591 47.2941 4.2353V29.6471C47.2941 31.2065 46.0301 32.4706 44.4706 32.4706H38.8236V12.7059C38.8236 11.1465 37.5595 9.88236 36 9.88236H16.2353V4.2353ZM4.94121 15.5294C3.3818 15.5294 2.11768 16.7935 2.11768 18.353V43.7647C2.11768 45.3242 3.3818 46.5882 4.94121 46.5882H30.353C31.9124 46.5882 33.1765 45.3242 33.1765 43.7647V18.353C33.1765 16.7936 31.9124 15.5294 30.353 15.5294H4.94121Z" fill="currentColor"/> </g> </svg>
Product Updates
3 min read

Intro to Transaction Simulation on Portal

Protect your users from accidental or malicious transactions on the blockchain.

September 26, 2023

Intro to Transaction Simulation on Portal

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.