Single-Hop Transaction: On-chain Pricing
Last updated
Last updated
On-chain pricing shares a similar process with off-chain. Except that the pricer in use here is replaced by an on-chain smart contract to generate the quotes.
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 on-chain pricing
The Pricer will call the Pool contract. Using the getAmountOut() function, the Pool will call the Registry
The Registry will refer to the Pricer ID to find and point to the correct On-chain Pricer
The price will be calculated in the On-chain Pricer which will return the indicative quote to the Registry, then to the Pool, then 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, then Pool, then Registry, then On-chain Pricer for a firm quote
The firm quote is returned from the On-chain Pricer to the Registry, then to the Pool, then to Pricer, then to the Native Backend and then 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