Liquidity Pools

In this lab you will learn how to setup a Liquidity Pool. The original Elrond's Pair SC will not be used, but we will use a simplified version.

Please clone the git repo from here.

Tasks for this practical session:

  1. Create tokens: First Token, Second Token, LiquidityPool Token (see previous labs)
  2. Build SC (see previous labs)
  3. Deploy SC (see previous labs)
  4. Set Roles of deployed SC: setTransferExecGasLimit, setLpTokenIdentifier, setSpecialRoleToken
  5. Add Liquidity
  6. Partially Remove Liquidity
  7. Perform Swaps: Trade token1 for token2

Please check interaction script. You can use this file by sourcing it:

costin@Byblos:~/facultate/cursuri/fob/Foundation-Of-Blockchains/labs/lab07/lab07/interaction$ . 
costin@Byblos:~/facultate/cursuri/fob/Foundation-Of-Blockchains/labs/lab07/lab07/interaction$ issueToken FobToken FOB
INFO:accounts:Account.sync_nonce() done: 59
INFO:transactions:Transaction.send: nonce=59
INFO:transactions:Hash: 0a735360d68047ace30a26e64c7b0e67b654d227400b3929332efa05a52db344
INFO:utils:View this transaction in the Elrond Testnet Explorer:
    "emittedTransaction": {
        "nonce": 59,
        "value": "50000000000000000",
        "receiver": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u",
        "sender": "erd1ld6er5zpdze3cynzkapur9qhzh826jje6n87g7tvdfrtszs8jn2qv44nqd",
        "gasPrice": 1000000000,
        "gasLimit": 60000000,
        "chainID": "T",
        "version": 1,
        "signature": "c30f1b3bf9edf0e4dea32ca89ba950165bb2d027797bfaf4d2629b6bec341464c7bd4cd93174fc440857b067871fffedb610272afb9013190dc1a5a03d65d709"
    "emittedTransactionData": "issue@466F62546F6B656E@464F42@FFFFFFFFFFFFFFFFFFFF@12",
    "emittedTransactionHash": "0a735360d68047ace30a26e64c7b0e67b654d227400b3929332efa05a52db344",
    "contractAddress": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"

Set Roles for deployed Pair SC

Use set_transfer_exec_gas_limit function to set the gas limit for transfer. This will set the transfer_exec_gas_limit variable from storage and will be further used by the contract.

Use setLpTokenIdentifier endpoint to set the LP Token for the Pair SC. This token will be used when users add and remove liquidity. For X Token1 and X Token2, the users will receive Y LP Token as proof that they provided liquidity. The user will use the LP Token to remove liquidity and get the tokens back.

Use setSpecialRole to provide the Pair SC setSpecialRoleToken role so that it can create LP Tokens.

When deployed, the Pair SC is in ActiveNoSwaps state. Check this code. You will have to call resume endpoint to change to state to Active so that swaps may be performed.

Add and Remove Liquidity

To add liquidity to the Pair SC you have to send two tokens. Please use Multi ESDT Transfer documentation.

Now that the Pair SC is configured, you can call addLiquidity and removeLiquidity.

Check the transactions. What tokens were sent/received?

The transactions should follow the order:



Now with the SC configured and having liquidity, you can perform swaps using swap_tokens_fixed_input and swap_tokens_fixed_output. Remember the difference from last lab?

What happens with the ratio Token1/Token2?


This Pair SC has a vulnerability. Can you find it?

fob/laboratoare/08.txt · Last modified: 2022/12/15 18:28 by costin.carabas
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0