3️⃣Estimate Router Data
To properly estimate the logics of the router results, closely follow the steps below.
Step 1: Build Router Data
First, you need to prepare the Router Data, which will include the chainId
, account
, and logics
data.
Step 2: Estimate Router Data Result
Next, use api.estimateRouterData
to estimate how much funds will be spent (funds) and how many balances will be obtained (balances) from this transaction. It will also identify any approvals that the user needs to execute (approvals) before the transaction.
In the current implementation of Permit2, users have two options for authorizing Agent
to spend their assets: permit
and approve
. These methods provide distinct approaches to token authorization, allowing users to choose the one that best suits their needs. If not specified, permit
is the default behavior.
The permit
method involves obtaining authorization data (permitData
) for the ERC20 tokens being spent in the router data. Users are required to sign this data. The permitData
and the associated signature are then sent to the Protocolink API as part of the transaction.
The approve
method provides users with the necessary transactions to approve the expenditure of ERC20 tokens in the router data. Users must complete these approval transactions before sending the router transaction.
The structure of the estimateResult
obtained is roughly as follows:
Funds: For this transaction, 1000 USDC
will be spent, which can be displayed on the interface.
Balances: For this transaction, 0.03579397 aEthWBTC
will be obtained, which can be displayed on the interface.
Fees: For this transaction, 0.001240475451939898 ETH
will be charged, which can be displayed on the interface.
Step 3: Approvals (optional)
For this transaction, the user may need to perform one or more of the following approvals before proceeding:
Approve the Permit2 contract to spend their assets.
Approve the
Agent
to spend their assets.Authorize the
Agent
to execute protocol specific operations that require authorization.
If using ethers
package, you can refer to the following code snippet to help the user send approval transactions:
Step 4: PermitData (optional)
For this transaction, the user needs to sign to permit Protocolink contract to spend his USDC fist. If using ethers
package, you can refer to the following code snippet to help the user sign the `permitData`:
After signing, it is necessary to include permitData
and permitSig
into routerData
. Without this step, the transaction will fail.
Last updated