Pool transaction structure
In pool page,when Liquidity Providers(LPs) mananger their liquidity, 5 types of transaction may be involved:
- Anyone can creat a pool
- Liquidity Providers(LPs) submit add liquidity request
- Liquidity Providers(LPs) submit remove liquidity request
- Aggregators match liquidity request with pool to help LP to manager their liquidity sucessfully
- Liquidity Providers(LPs) cancel liquidity request
1. Creat a pool#
Creat a pool in Gliaswap is permissionless. In the first version, Gliaswap only support to creat sudt/CKB pool.
Creat a pool actually is to creat two cell:
- info cell used to store pool info, including reserve balance, Liquidity token balance and other ID info to indentify the pool
- pool cell used to store the real asset, including CKB and sudt
In this transaction, Only info type script will be run to verify the tx, which leads to the third validation rule of info type script
Info type script - rule 4#
Rule 4 - If this is a creating pool transaction, verify that the Pool cell is created at the same time as the Info cell, and that the data filled in info cell and pool cell are correct.
2. LPs submit add liquidity request#
If a LP want to add liqidity to a pool, he/she needs to submit a add liquidity request firstly. For example,Alice want to add M CKB and N sUDT_A to pool:
Notice that this transaction only validates regular SUDT rules constrained in sudt type script.
3. LPs submit remove liquidity request#
If a LP want to remove liquidity from a pool, he/she needs to submit a remove liquidity request firstly. For example,Bob want to burn L Liquidity token and withdraw the
Notice that this transaction only validates regular SUDT rules constrained in sudt type script.
4. Aggregators match liquidity request with pool#
Motivated by the Tip fee claimed in the liquidity request cell, aggregators will continually retrieve the liquidity request cells(including add liquidity request and remove liquidity request) and the pool cells and then compete to match them off-chain and submit matching transactions.
In this transaction, four types of script will be run to verify the tx:
Info type script - rule 5/rule6/rule7#
Rule 5 - If this is a matching liquidity transaction, the cell sequence in this tx should follow the rules blow:
Rules 6 - If this is initially adding liquidity, verify if minting the correct amount Liquidity token for user, and verify if the data storaged in info cell and pool cell is correct.
Rule 7 - If this is not initially adding liquidity, iterate through all request cell
- If it is add liquidity request cell, verify if minting the correct amount Liquidity token for user, and add up the CKB amount, sUDT amount and Liquidity token amount for verifying info cell and pool cell later.
- If it is remove liquidity request cell, verify if withdraw the correct CKB amount and sudt amount for user, and add up the CKB amount, sUDT amount and Liquidity token amount for verifying info cell and pool cell later.
- Verify if the data storaged in info cell and pool cell is correct
Info lock script - rule 1#
Rules 1 - Verify if lock.args is consistent with info type
Liquidity request lock script - rule 1#
Rule 1 - verifity input index[1] is info cell
sUDT type script#
5. LPs can cancel their own liquidity request#
Normally your will add liquidity or remove liquidity successfully soon after you submit the liquidity request, but if the price fluctuates above the slippage, your request will be pending until the pool price fluctuates back to the price you submitted.
So we provide the cancel option for LPs. LPs can send a transaction to cancel their own liquidity request.
For example, if Bob want to cancel his remove liqidity request:
This transaction leads to another rule of LIQUIDITY_REQ_LOCK
Liquidity request lock script - rule 2#
Rule 2 - If one of input cell in the transaction use user's lock specified in liquidity request cell args and the corresponding witness is not 0, unlock the request cell directly.