Overview
Learn how to use Simulation API in your dapp to allow users to preview transaction outcomes before sending them on-chain. This way, you can help them avoid financial losses due to failed or incorrect transactions and increase their confidence when using your dapp. To integrate Tenderly Simulations API into your dapp to detect if a transaction would fail before you even send it on-chain, you need to:- Populate raw transactions from ethers.js library.
- Simulate transactions before sending them.
- (optional) Wrap every blockchain interaction into a Tenderly simulation.
Populate raw transactions from ethers.js
The first step is to create a transaction object that’s going to be sent to our simulation endpoint. We’re going to generate it directly from theethers.js library. In the example below, we’re going to see how we can achieve that for the transfer() function on top of a DAI contract:
example.tsx
Simulate transactions before sending
Now that we have extracted an unsigned raw transaction, let’s simulate it before sending it on-chain (to the Ethereum Mainnet):example.tsx
Wrap every blockchain interaction into a Tenderly simulation (optional)
To ensure that every blockchain interaction is simulated first, write a simple wrapper around theethers.js signer object that would always simulate transactions.
Additionally, going forward, you can introduce typed errors with which your logic can interact:
example.tsx
Here’s a GitHub
repo where you
can find an example of this implementation.