Differences

This shows you the differences between two versions of the page.

Link to this comparison view

fob:laboratoare:08 [2022/09/27 14:18]
127.0.0.1 external edit
fob:laboratoare:08 [2022/12/15 18:28] (current)
costin.carabas
Line 1: Line 1:
-===== Laboratorul 08. =====+===== Liquidity Pools =====
  
 +In this lab you will learn how to setup a Liquidity Pool. The original [[https://​github.com/​ElrondNetwork/​sc-dex-rs/​tree/​main/​dex/​pair|Elrond'​s Pair SC]] will not be used, but we will use a [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​tree/​master/​labs/​lab07| simplified version]].
  
 +Please clone the git repo from [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​tree/​master/​labs/​lab07|here]].
 +
 +Tasks for this practical session:
 +  - Create tokens: First Token, Second Token, LiquidityPool Token (see previous labs)
 +  - Build SC (see previous labs)
 +  - Deploy SC (see previous labs)
 +  - Set Roles of deployed SC: setTransferExecGasLimit,​ setLpTokenIdentifier,​ setSpecialRoleToken
 +  - Add Liquidity
 +  - Partially Remove Liquidity
 +  - Perform Swaps: Trade token1 for token2
 +
 +Please check [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​master/​labs/​lab07/​lab07/​interaction/​testnet.snippets.sh|interaction script]].
 +You can use this file by sourcing it:
 +<​code>​
 +costin@Byblos:​~/​facultate/​cursuri/​fob/​Foundation-Of-Blockchains/​labs/​lab07/​lab07/​interaction$ . testnet.snippets.sh ​
 +costin@Byblos:​~/​facultate/​cursuri/​fob/​Foundation-Of-Blockchains/​labs/​lab07/​lab07/​interaction$ issueToken FobToken FOB
 +INFO:​accounts:​Account.sync_nonce()
 +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: https://​testnet-explorer.elrond.com/​transactions/​0a735360d68047ace30a26e64c7b0e67b654d227400b3929332efa05a52db344
 +{
 +    "​emittedTransaction":​ {
 +        "​nonce":​ 59,
 +        "​value":​ "​50000000000000000",​
 +        "​receiver":​ "​erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u",​
 +        "​sender":​ "​erd1ld6er5zpdze3cynzkapur9qhzh826jje6n87g7tvdfrtszs8jn2qv44nqd",​
 +        "​gasPrice":​ 1000000000,
 +        "​gasLimit":​ 60000000,
 +        "​data":​ "​aXNzdWVANDY2RjYyNTQ2RjZCNjU2RUA0NjRGNDJARkZGRkZGRkZGRkZGRkZGRkZGRkZAMTI=",​
 +        "​chainID":​ "​T",​
 +        "​version":​ 1,
 +        "​signature":​ "​c30f1b3bf9edf0e4dea32ca89ba950165bb2d027797bfaf4d2629b6bec341464c7bd4cd93174fc440857b067871fffedb610272afb9013190dc1a5a03d65d709"​
 +    },
 +    "​emittedTransactionData":​ "​issue@466F62546F6B656E@464F42@FFFFFFFFFFFFFFFFFFFF@12",​
 +    "​emittedTransactionHash":​ "​0a735360d68047ace30a26e64c7b0e67b654d227400b3929332efa05a52db344",​
 +    "​contractAddress":​ "​erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u"​
 +}
 +</​code>​
 +
 + 
 +=== Set Roles for deployed Pair SC ===
 +
 +Use [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​config.rs#​L14|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 [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​lib.rs#​L520|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 [[https://​docs.elrond.com/​tokens/​esdt-tokens/#​setting-and-unsetting-special-roles|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 [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​lib.rs#​L73|this code]]. You will have to call [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​config.rs#​L43|resume]] endpoint to change to state to **Active** so that swaps may be performed.
 +
 +=== Add and Remove Liquidity ===
 +
 +<note warning>​To add liquidity to the Pair SC you have to send two tokens. Please use [[https://​docs.elrond.com/​sdk-and-tools/​erdpy/​smart-contract-interactions/#​multi-esdt-transfer|Multi ESDT Transfer]] documentation.</​note>​
 +
 +Now that the Pair SC is configured, you can call [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​lib.rs#​L88|addLiquidity]] and [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​lib.rs#​L190|removeLiquidity]].
 +<note important>​Check the transactions. What tokens were sent/​received?</​note>​
 +
 +The transactions should follow the order:
 +<​code>​
 +MultiESDTNFTTransfer@Contract_address_in_hex@02@Token1Identifier_in_hex@Token1Nonce_in_hex@Token1Value_in_hex@Token2Identifier_in_hex@Token2Nonce_in_hex@Token2Value_in_hex@myEndpoint_in_hex@parameters
 +</​code>​
 +
 +=== Swaps ===
 +
 +Now with the SC configured and having liquidity, you can perform swaps using [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​lib.rs#​L351|swap_tokens_fixed_input]] and [[https://​github.com/​systems-cs-pub-ro/​Foundation-Of-Blockchains/​blob/​5634f03ce56ba191a0e6685f6b5e675f02c9431f/​labs/​lab07/​lab07/​src/​lib.rs#​L438|swap_tokens_fixed_output]]. Remember the difference from last lab?
 +<note important>​What happens with the ratio Token1/​Token2?</​note>​
 +
 +=== Bonus ===
 +
 +This Pair SC has a vulnerability. Can you find it?
fob/laboratoare/08.1664277495.txt.gz · Last modified: 2022/10/09 12:49 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0