A simple Sui Move version AMM Dex based on the logic of UniswapV2.
- 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, aPoolItem
will be added toFactory
'stable
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 toPoolItem
'sa
andb
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 withBalance
, call withCoin
and returnCoin
, call withCoin
and transfer the outputCoin
to the sender in that entry function) considering both composability and convenience.
- LP witness
- LP witness
LP<A, B>
is used as unique identifier ofCoin<LP<A, B>>
type.
- Pool
- A
Pool<A, B>
is a global shared object that is created by the one who calls thecreate_pool
function. - It records its
Balance<A>
,Balance<B>
,Supply<LP<A, B>>
, and default fee.
- Factory
- A
Factory
is a global shared object that is created only once during the package publishment. - It has a
table
field recording eachPoolItem
.
- PoolItem
- A
PoolItem
is used to record the pool info in theFactory
. - It guarantees each pair is unique and the coin types it records are sorted.
- create_pool<A, B>
- Create a new
Pool<A, B>
with initial liquidity. - Input with
Factory
,Balance<A>
andBalance<B>
, returnBalance<LP<A, B>>
.
- create_pool_with_coins<A, B>
- Input with
Factory
,Coin<A>
andCoin<B>
, returnCoin<LP<A, B>>
.
- create_pool_with_coins_and_transfer_lp_to_sender<A, B>
- Input with
Factory
,Coin<A>
andCoin<B>
, and transferCoin<LP<A, B>>
to sender in the function.
- add_liquidity<A, B>
- Add liquidity to
Pool<A, B>
to get LP coin. - Input with
Pool<A, B>
,Balance<A>
,Balance<B>
and minimal LP output amount, return remainingBalance<A>
,Balance<B>
, andBalance<LP<A, B>>
.
- add_liquidity_with_coins<A, B>
- Input with
Pool<A, B>
,Coin<A>
,Coin<B>
and minimal LP output amount, return remainingCoin<A>
,Coin<B>
, andCoin<LP<A, B>>
.
- add_liquidity_with_coins_and_transfer_to_sender<A, B>
- Input with
Pool<A, B>
,Coin<A>
,Coin<B>
and minimal LP output amount, and transfer remainingCoin<A>
,Coin<B>
, andCoin<LP<A, B>>
to sender in the function.
- remove_liquidity<A, B>
- Remove liquidity from
Pool<A, B>
and burn LP coin. - Input with
Pool<A, B>
,Balance<LP<A, B>>
and minimal A output amount, minimal B output amount, returnBalance<A>
andBalance<B>
.
- remove_liquidity_with_coins<A, B>
- Input with
Pool<A, B>
,Coin<LP<A, B>>
and minimal A output amount, minimal B output amount, returnCoin<A>
andCoin<B>
.
- remove_liquidity_with_coins_and_transfer_to_sender<A, B>
- Input with
Pool<A, B>
,Coin<LP<A, B>>
and minimal A output amount, minimal B output amount, and transferCoin<A>
andCoin<B>
to sender in the function.
- swap_a_for_b<A, B>
- Swap exact
Balance<A>
forBalance<B>
. - Input with
Pool<A, B>
,Balance<A>
and minimal B output amount, returnBalance<B>
.
- swap_a_for_b_with_coin<A, B>
- Input with
Pool<A, B>
,Coin<A>
and minimal B output amount, returnCoin<B>
.
- swap_a_for_b_with_coin_and_transfer_to_sender<A, B>
- Input with
Pool<A, B>
,Coin<A>
and minimal B output amount, and transferCoin<B>
to sender in the function.
- swap_b_for_a<A, B>
- Swap exact
Balance<B>
forBalance<A>
. - Input with
Pool<A, B>
,Balance<B>
and minimal A output amount, returnBalance<A>
.
- swap_b_for_a_with_coin<A, B>
- Input with
Pool<A, B>
,Coin<B>
and minimal A output amount, returnCoin<A>
.
- swap_b_for_a_with_coin_and_transfer_to_sender<A, B>
- Input with
Pool<A, B>
,Coin<B>
and minimal A output amount, and transferCoin<A>
to sender in the function.