Are you looking to put your Substrate knowledge to the test? Do you think you are ready to get hydrated? Then this small practical exercise might be what you are looking for.
Implement a simplified version of a trading pool which holds 2 stable assets and has no trading fees.
The trading pool should support the following functionality which should be implemented as extrinsics:
- create pool
- add liquidity
- remove liquidity
You can find the math-related functionality in the HydraDX-math crate under branch simpleswap: https://github.com/galacticcouncil/HydraDX-math/tree/simpleswap
Design a storage which keeps the pool information. The following information needs to be stored:
- amplification: u128
- assets: AssetId (assets in the pool)
- balances: u128 (balances of each asset in the pool)
- pool asset: AssetId (asset minted or burnt when liquidity is added to or removed from the pool)
Also consider which type of storage to use and what is the best way to access the pool.
Here are some further details on the required extrinsics.
In the first place, create_pool should check if a pool between the two pairs already exists. If this is not the case, it should create a new pool in the asset registry and store all the necessary information in the storage. You can check the XYK pallet for reference on how to work with the asset registry.
This extrinsic should add liquidity to the pool, provided that the pool already exists and that the account has a sufficient balance of both assets that are added to the pool.
You can use
calculate_pool_value from the math crate to calculate the amount which should be minted for the user.
Sell and buy are two very similar extrinsics which change the amount of the assets in the pool - the supply of one asset increases while the supply of the other asset decreases.
Before the sell or buy is effectuated, the methods should perform the necessary validations in order to determine that the transaction can be completed successfully. If this is the case, it should calculate the amounts in and out, perform the transfers and, finally - update the pool storage.
As a bonus task, you can prepare your newly created pool to be plugged into the Exchange pallet. For that purpose, an AMM trait from the primitives crate should be implemented for this pallet.
You can look for inspiration on how this can be done in the XYK pallet.
If you have any questions, please feel free to contact us on Discord.