Single-Hop Transaction: Off-chain Pricing
Last updated
Last updated
A general process for swap on Native is that when user inputs an amount, Native queries the backend to get an indicative quote. When user confirms the swap, Native will query the backend again, and execute with the firm quote with the Router.
User selects desired tokens to swap and fill in the desired amount to swap in the GUI
The Get Indicative Quote API in the GUI will ask the Native Backend for the indicative quote
The Native Backend will in turn call its Get Indicative Quote API to ask the Pricer for an indicative quote
The Pricer will check for the pricing model of the selected trading pair. In this case, it is using off-chain pricing
The Pricer will do an off-chain calculation for the price and return an indicative quote to the Native Backend
The Native Backend will return the the indicative quote to the GUI which would then display the quote to the user
At the same time that the Get Indicative Quote API in the GUI is called, the Get Sign Quote API is also called
It follows the same flow as getting the indicative quote with the exception that it will get the quote signed by the Signer
This signed quote is used to run the estimatedGas function to estimate the gas fee required for the transaction and displayed in the GUI
User clicks on "Swap" in the GUI
The Get Firm Quote API is called. An order is created in the Native database and the GUI will ask the Native Backend for a firm quote
The Native Backend will in turn ask the Pricer for a firm quote
The firm quote is returned from the Pricer and sent to the Signer to be signed
The signed quote is returned from the Signer then to the Native backend then to the GUI
The User Wallet will be prompted to sign the transaction
With the signed transaction, the GUI will call the Router contract to trigger the swap() function in the Pool
Using the safeTransferFrom() function of the Pool, assets are transferred from the Treasury to the User Wallet
The swapCallback() function in the Router is called to transfer assets from the User Wallet to the Pool
The safeTransferFrom() function is used again to transfer assets from the Pool to the Treasury
After the swapping is done, there is a check to make sure the slippage for the entire transaction is within the slippage threshold set by the user. If yes, the swap is completed. If no, the transaction will be reverted