An open API service indexing awesome lists of open source software.

https://github.com/0xalberto/sui-dex-uniswapv2

SuiNetwork Move version AMM Dex based on the logic of UniswapV2.
https://github.com/0xalberto/sui-dex-uniswapv2

amm dex exchange smartcontracts sui suiblockchain suinetwork swap uniswap-v2

Last synced: 3 months ago
JSON representation

SuiNetwork Move version AMM Dex based on the logic of UniswapV2.

Awesome Lists containing this project

README

          

A simple Sui Move version AMM Dex based on the logic of UniswapV2.

## Introduction
+ This module implements the factory and pool logic of UniswapV2 where anyone can freely create pair of two coin types, add or remove liquidity, and swap.
+ After a `Pool` of two coin types is created, a `PoolItem` will be added to `Factory`'s `table` field, which guarantees there is at most one pool for a pair.
+ The two coin types of a pair are first sorted according to their `type_name`, and then assigned to `PoolItem`'s `a` and `b` fields respectively.
+ Users can add liquidity to the pool according to the current ratio of coin balances. The remaining coin will be returned to the users as well as the LP coin.
+ Each pool are set with a `0.3%` swap fee by default which is actually distributed to all LP holders.
+ Core functions like `create_pool`, `add_liquidity`, `remove_liquidity`, `swap_a_for_b`, `swap_b_for_a` are all provided with three kind of interfaces (call with `Balance`, call with `Coin` and return `Coin`, call with `Coin` and transfer the output `Coin` to the sender in that entry function) considering both composability and convenience.

## Structs
1. LP witness
+ LP witness `LP` is used as unique identifier of `Coin>` type.

2. Pool
+ A `Pool
` is a global shared object that is created by the one who calls the `create_pool` function.
+ It records its `Balance
`, `Balance`, `Supply>`, and default fee.

3. Factory
+ A `Factory` is a global shared object that is created only once during the package publishment.
+ It has a `table` field recording each `PoolItem`.

4. PoolItem
+ A `PoolItem` is used to record the pool info in the `Factory`.
+ It guarantees each pair is unique and the coin types it records are sorted.

## Core functions
1. create_pool

+ Create a new `Pool
` with initial liquidity.
+ Input with `Factory`, `Balance
` and `Balance`, return `Balance>`.

2. create_pool_with_coins
+ Input with `Factory`, `Coin
` and `Coin`, return `Coin>`.

3. create_pool_with_coins_and_transfer_lp_to_sender
+ Input with `Factory`, `Coin
` and `Coin`, and transfer `Coin>` to sender in the function.

4. add_liquidity
+ Add liquidity to `Pool
` to get LP coin.
+ Input with `Pool
`, `Balance`, `Balance` and minimal LP output amount, return remaining `Balance`, `Balance`, and `Balance>`.

5. add_liquidity_with_coins
+ Input with `Pool
`, `Coin`, `Coin` and minimal LP output amount, return remaining `Coin`, `Coin`, and `Coin>`.

6. add_liquidity_with_coins_and_transfer_to_sender
+ Input with `Pool
`, `Coin`, `Coin` and minimal LP output amount, and transfer remaining `Coin`, `Coin`, and `Coin>` to sender in the function.

7. remove_liquidity
+ Remove liquidity from `Pool
` and burn LP coin.
+ Input with `Pool
`, `Balance>` and minimal A output amount, minimal B output amount, return `Balance` and `Balance`.

8. remove_liquidity_with_coins
+ Input with `Pool
`, `Coin>` and minimal A output amount, minimal B output amount, return `Coin` and `Coin`.

9. remove_liquidity_with_coins_and_transfer_to_sender
+ Input with `Pool
`, `Coin>` and minimal A output amount, minimal B output amount, and transfer `Coin` and `Coin` to sender in the function.

10. swap_a_for_b
+ Swap exact `Balance
` for `Balance`.
+ Input with `Pool
`, `Balance` and minimal B output amount, return `Balance`.

11. swap_a_for_b_with_coin
+ Input with `Pool
`, `Coin` and minimal B output amount, return `Coin`.

12. swap_a_for_b_with_coin_and_transfer_to_sender
+ Input with `Pool
`, `Coin` and minimal B output amount, and transfer `Coin` to sender in the function.

13. swap_b_for_a
+ Swap exact `Balance` for `Balance
`.
+ Input with `Pool
`, `Balance` and minimal A output amount, return `Balance`.

14. swap_b_for_a_with_coin
+ Input with `Pool
`, `Coin` and minimal A output amount, return `Coin`.

15. swap_b_for_a_with_coin_and_transfer_to_sender
+ Input with `Pool
`, `Coin` and minimal A output amount, and transfer `Coin` to sender in the function.

## Unit test
![]()