0.0.3 • Published 8 months ago

@cetusprotocol/cetus-zap-sdk v0.0.3

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
8 months ago

Zap Operations

The SDK provides a Zap module for specialized liquidity operations with different modes to suit various trading strategies.

Common Parameters

  • pool_id: The ID of the liquidity pool
  • tick_lower & tick_upper: Price range boundaries for the position
  • current_sqrt_price: Current square root price of the pool
  • slippage: Maximum acceptable price slippage (e.g., 0.01 for 1%)
  • coin_type_a & coin_type_b: Coin type identifiers for the trading pair
  • coin_decimal_a & coin_decimal_b: Decimal places for each coin type

1. Deposit Operations

Mode-Specific Parameters

  1. FixedOneSide

    • fixed_amount: Fixed amount to deposit
    • fixed_coin_a: Boolean indicating whether to fix coin A (true) or coin B (false)
  2. FlexibleBoth

    • coin_amount_a: Amount of coin A to deposit
    • coin_amount_b: Amount of coin B to deposit
  3. OnlyCoinA/OnlyCoinB

    • coin_amount: Amount of single coin to deposit

Usage Example

// Initialize SDK and get pool information
const sdk = buildSdk(SdkEnv.mainnet)
const pool = await sdk.CetusClmmSDK.Pool.getPool(poolId)

// Pre-calculate deposit amounts (example: FixedOneSide mode)
const result = await sdk.Zap.preCalculateDepositAmount(
    {
        pool_id: poolId,
        tick_lower,
        tick_upper,
        current_sqrt_price: pool.current_sqrt_price.toString(),
        slippage: 0.01,
    },
    {
        mode: 'FixedOneSide',
        fixed_amount: toDecimalsAmount(1, 6).toString(),
        fixed_coin_a: false,
    }
)

// Build and send transaction
const tx = await sdk.Zap.buildDepositPayload({
    deposit_obj: result,
    pool_id: poolId,
    coin_type_a: pool.coinTypeA,
    coin_type_b: pool.coinTypeB,
    tick_lower,
    tick_upper,
    slippage: 0.01,
    pos_obj: {                    // Optional: Add to existing position
        pos_id: positionId,
        collect_fee: false,
        collect_rewarder_types: [],
    }
})

// Simulate or send the transaction
const simResult = await sdk.FullClient.sendSimulationTransaction(tx, address)
// const txResult = await sdk.FullClient.sendTransaction(sendKeyPair, tx)

2. Withdraw Operations

Withdrawals require an existing position in the pool.

Mode-Specific Parameters

  1. FixedOneSide

    • fixed_amount: Fixed amount to withdraw
    • fixed_coin_a: Boolean indicating whether to withdraw coin A (true) or coin B (false)
  2. OnlyCoinA/OnlyCoinB

    • burn_liquidity: Amount of liquidity to burn
    • available_liquidity: Total available liquidity in the position

Usage Example

// Get pool and position information
const pool = await sdk.CetusClmmSDK.Pool.getPool(poolId)
const position = await sdk.CetusClmmSDK.Position.getPositionById(positionId)

if (!pool || !position) {
    throw new Error('Pool or Position not found')
}

// Pre-calculate withdrawal (example: OnlyCoinA mode)
const result = await sdk.Zap.preCalculateWithdrawAmount({
    mode: 'OnlyCoinA',
    pool_id: poolId,
    tick_lower: position.tick_lower_index,
    tick_upper: position.tick_upper_index,
    current_sqrt_price: pool.current_sqrt_price.toString(),
    burn_liquidity: '200000',
    available_liquidity: position.liquidity.toString(),
    coin_type_a: pool.coinTypeA,
    coin_type_b: pool.coinTypeB,
    coin_decimal_a: 6,
    coin_decimal_b: 9,
})

// Build and send transaction
const tx = await sdk.Zap.buildWithdrawPayload({
    withdraw_obj: result,
    pool_id: poolId,
    pos_id: positionId,
    close_pos: false,             // Whether to close the position
    collect_fee: true,            // Whether to collect accumulated fees
    collect_rewarder_types: [],   // Types of rewards to collect
    coin_type_a: pool.coinTypeA,
    coin_type_b: pool.coinTypeB,
    tick_lower: position.tick_lower_index,
    tick_upper: position.tick_upper_index,
    slippage: 0.01,
})

// Simulate or send the transaction
const simResult = await sdk.FullClient.sendSimulationTransaction(tx, address)
// const txResult = await sdk.FullClient.sendTransaction(sendKeyPair, tx)