https://dacade.org/communities/sui/challenges/19885730-fb83-477a-b95b-4ab265b61438
The joy_swap module provides a decentralized finance (DeFi) application where users can create liquidity pools, add liquidity, remove liquidity, and swap tokens. This module supports operations involving two different types of tokens, Coin_A and Coin_B, in a liquidity pool.
- This struct represents a liquidity pool's LP (Liquidity Provider) token.
- It is a phantom struct that pairs two types of coins (
Coin_AandCoin_B) together to create an LP token for the pool.
- This struct represents a liquidity pool containing two types of tokens (
Coin_AandCoin_B). - It holds the balances of each token and tracks the LP token supply for the pool.
-
Description: Users can create a new liquidity pool containing two different tokens:
Coin_AandCoin_B. When creating the pool, the user must provide an amount ofCoin_AandCoin_B. -
How it Works:
- The user calls the
create_pool<Coin_A, Coin_B>function with the two tokensCoin_AandCoin_B. - The function verifies that both tokens have non-zero values.
- The values of the tokens are converted into
Balancetypes, and the number of liquidity pool (LP) tokens to mint is calculated based on the square root of the product of the two token values. - The pool is created and the LP tokens are issued to the user.
- The user calls the
-
Related Event:
PoolCreated: Emits an event containing the pool's ID, token types, initial token values, and minted LP tokens.
-
Description: Users can add liquidity to an existing liquidity pool. The user provides additional amounts of
Coin_AandCoin_B. If the amounts do not match the existing pool ratios, excess tokens are refunded to the user. -
How it Works:
- The user calls the
add_liquidity<Coin_A, Coin_B>function, passing the liquidity pool and the tokens to add. - The function verifies that both tokens have non-zero values and calculates the current ratios of the pool's tokens.
- If the provided tokens do not match the pool's ratio, the excess tokens are refunded.
- The LP token supply is adjusted based on the new liquidity added.
- The newly minted LP tokens are issued to the user.
- The user calls the
-
Related Event:
LiquidityAdded: Emits an event containing the pool's ID, token types, added token values, and minted LP tokens.
-
Description: Users can remove liquidity from an existing liquidity pool by providing LP tokens. The system calculates the amounts of
Coin_AandCoin_Bto be refunded based on the LP token value and the pool's ratio. -
How it Works:
- The user calls the
remove_liquidity<Coin_A, Coin_B>function with the LP token to burn. - The function verifies that the LP token has a non-zero value.
- The liquidity to be removed is calculated based on the LP token value and the pool's token ratios.
- The LP tokens are burned, and the corresponding amounts of
Coin_AandCoin_Bare returned to the user.
- The user calls the
-
Related Event:
LiquidityRemoved: Emits an event containing the pool's ID, token types, removed token values, and burned LP tokens.
-
Description: This function allows users to swap
Coin_AforCoin_B. The swap follows the constant product formula:k = balance_a * balance_b. Fees are deducted from the output token. -
How it Works:
- The user calls the
swap_a_to_b<COin_A, Coin_B>function withCoin_Ato swap. - The function calculates the new balance after the swap and ensures that the new token value is within the expected range.
- A small fee (0.3%) is deducted from the output token (
Coin_B). - The swapped tokens are transferred to the user.
- The user calls the
-
Related Event:
Swaped: Emits an event containing the pool's ID, the input token, input value, output token, and output value.
