Sui.

Bài viết

Chia sẻ kiến thức của bạn.

Evgeniy CRYPTOCOIN.
Jun 30, 2025
Hỏi đáp Chuyên Gia

How to Create a Liquidity Pool in Sui Move?

I'm building a DeFi protocol on Sui and need to implement a basic liquidity pool (like Uniswap-style AMM) in Move. I'm struggling with:

  1. Storing LP tokens – How to handle dynamic supply and balances?
  2. Deposits/Withdrawals – Ensuring atomic swaps and proper math.
  3. Fee mechanism – Where to deduct fees without breaking invariants?
  4. Frontrunning protection – Is there a built-in way to handle slippage?

What I've tried:

  • Basic two-token pool using Table for balances.
  • Manual LP mint/burn logic.
  • Fixed 0.3% fee on swaps.

Issues encountered:

  • "Arithmetic overflow" when calculating liquidity.
  • Reentrancy risks – Can Sui Move prevent this?
  • LP token accuracy – Decimals handling feels hacky.

Questions:

  1. What’s the correct architecture for a Sui liquidity pool?
  2. How to implement safe math for swaps/deposits?
  3. Are there Sui-specific optimizations (vs. EVM AMMs)?
  4. How to make the pool composable with other DeFi protocols?
  • Sui
5
1
Chia sẻ
Bình luận
.

Câu trả lời

1
Benjamin XDV.
Jun 30 2025, 18:15

1. Cấu trúc cốt lõi Mô hình tập trung đối tượng của Sui yêu cầu:

Đối tượng Pool: Lưu trữ dự trữ, dữ liệu mã thông báo LP và cấu hình.

LP Token: Quyền sở hữu theo dõi tài sản giống như Coin.

move
module dex::amm {  
    use sui::balance;  
    use sui::coin;  
    use sui::math;  
    use sui::tx_context;  

    struct Pool<phantom X, phantom Y> has key {  
        id: UID,  
        reserve_x: Balance<X>,  
        reserve_y: Balance<Y>,  
        lp_supply: Balance<LP<X, Y>>,  
        fee_bps: u64, // e.g., 30 = 0.3%  
    }  

    struct LP<phantom X, phantom Y> has store {} // LP token type  

2. Logic nạp tiền (Thêm thanh khoản) Các bước chính:

Lấy mã thông báo đầu vào.

Đúc các mã thông báo LP theo tỷ lệ.

Cập nhật dự trữ nguyên tử.

move
public fun add_liquidity<X, Y>(  
    pool: &mut Pool<X, Y>,  
    x: Coin<X>,  
    y: Coin<Y>,  
    ctx: &mut TxContext  
): Coin<LP<X, Y>> {  
    let x_val = coin::value(&x);  
    let y_val = coin::value(&y);  
    assert!(x_val > 0 && y_val > 0, EINVALID_AMOUNT);  

    // Calculate LP tokens to mint (geometric mean)  
    let lp_amount = if (balance::supply(&pool.lp_supply) == 0) {  
        math::sqrt(x_val * y_val) // Initial mint  
    } else {  
        min(  
            (x_val * balance::supply(&pool.lp_supply)) / balance::value(&pool.reserve_x),  
            (y_val * balance::supply(&pool.lp_supply)) / balance::value(&pool.reserve_y)  
        )  
    };  

    // Update reserves  
    balance::join(&mut pool.reserve_x, coin::into_balance(x));  
    balance::join(&mut pool.reserve_y, coin::into_balance(y));  

    // Mint LP tokens  
    balance::increase_supply(&mut pool.lp_supply, lp_amount)  
}  

3. Logic Hoán đổi (Có phí)

move
public fun swap_x_to_y<X, Y>(  
    pool: &mut Pool<X, Y>,  
    dx: Coin<X>,  
    min_dy: u64,  
    ctx: &mut TxContext  
): Coin<Y> {  
    let dx_val = coin::value(&dx);  
    let fee = dx_val * pool.fee_bps / 10_000;  
    let dx_after_fee = dx_val - fee;  

    // Constant product formula  
    let dy_val = (dx_after_fee * balance::value(&pool.reserve_y)) /  
                 (balance::value(&pool.reserve_x) + dx_after_fee);  
    assert!(dy_val >= min_dy, EINSUFFICIENT_OUTPUT);  

    // Update reserves  
    balance::join(&mut pool.reserve_x, coin::into_balance(dx));  
    balance::withdraw(&mut pool.reserve_y, dy_val)  
}  

4. Tối ưu hóa chính Ưu điểm của Sui:

Không tái tham gia: Mô hình sở hữu của Move ngăn cản điều đó.

Giao dịch hàng loạt: Sử dụng chức năng nhập cho các hoạt động nhiều bước.

Trường động: Lưu trữ dữ liệu bổ sung (ví dụ: thông điệp TWAP) trên đối tượng pool.

5. Cạm bẫy & Khắc phục phổ biến Overflow: Sử dụng hàm math: :checked_* cho số học.

Làm tròn LP: Lưu trữ số lượng phân đoạn thông qua u128 bên trong.

Đi đầu:

Yêu cầu min_dy trong hoán đổi (được đặt bởi frontend).

Sử dụng txContext: :epoch () để kiểm tra thời hạn.

2
Câu trả lời hay nhất
Bình luận
.

Bạn có biết câu trả lời không?

Hãy đăng nhập và chia sẻ nó.