Open position
Function
clmmpool/sources/pool.move
/// Open a new position within the given tick range in the specified pool.
///
/// # Arguments
///
/// _ `config` - A reference to the `GlobalConfig` object.
/// _ `pool` - A mutable reference to the `Pool` object.
/// _ `tick_lower` - The lower tick index for the pool.
/// _ `tick_upper` - The upper tick index for the pool.
///
/// # Generic Type Parameters
///
/// _ `CoinTypeA` - The type of the first coin in the pool.
/// _ `CoinTypeB` - The type of the second coin in the pool.
///
/// # Returns
///
/// \* `Positon` - The new `Position` object that was opened, also means position nft.
public fun open_position<CoinTypeA, CoinTypeB>(
config: &GlobalConfig,
pool: &mut Pool<CoinTypeA, CoinTypeB>,
tick_lower: u32,
tick_upper: u32,
ctx: &mut TxContext
): Position {}
Example
1. Open position with both coins
If you want to support open_position_with_liquidity_with_all
, this method you need to implement by your contract, here the example about it. Firstly, you need to open position and get the position nft Position
.
Secondly, you need to add liquidity with fixed coin. If fix_amount_a
is true, it means fixed coin a, others means fixed coin b. Finally, you need to repay the receipt when add liquidity.
public entry fun open_position_with_liquidity_with_all<CoinTypeA, CoinTypeB>(
config: &GlobalConfig,
pool: &mut Pool<CoinTypeA, CoinTypeB>,
tick_lower_idx: u32,
tick_upper_idx: u32,
coins_a: vector<Coin<CoinTypeA>>,
coins_b: vector<Coin<CoinTypeB>>,
amount_a: u64,
amount_b: u64,
fix_amount_a: bool,
clock: &Clock,
ctx: &mut TxContext
) {
let position_nft = pool::open_position(
config,
pool,
tick_lower_idx,
tick_upper_idx,
ctx
);
let amount = if (fix_amount_a) amount_a else amount_b;
let receipt = pool::add_liquidity_fix_coin(
config,
pool,
&mut position_nft,
amount,
fix_amount_a,
clock
);
repay_add_liquidity(config, pool, receipt, coins_a, coins_b, amount_a, amount_b, ctx);
// transfer::public_transfer(position_nft, tx_context::sender(ctx));
}
2. Open position with coin_a only
This method is similar to the previous method. The only difference is that coin_b is zero.
public entry fun open_position_with_liquidity_only_a<CoinTypeA, CoinTypeB>(
config: &GlobalConfig,
pool: &mut Pool<CoinTypeA, CoinTypeB>,
tick_lower_idx: u32,
tick_upper_idx: u32,
coins_a: vector<Coin<CoinTypeA>>,
amount: u64,
clock: &Clock,
ctx: &mut TxContext
) {
let position_nft = pool::open_position(
config,
pool,
tick_lower_idx,
tick_upper_idx,
ctx
);
let receipt = pool::add_liquidity_fix_coin(
config,
pool,
&mut position_nft,
amount,
true,
clock
);
repay_add_liquidity(config, pool, receipt, coins_a, vector::empty(), amount, 0, ctx);
transfer::public_transfer(position_nft, tx_context::sender(ctx));
}
3. Open position with coin_b only
This method is similar to the previous method. The only difference is that coin_a is zero.
public entry fun open_position_with_liquidity_only_b<CoinTypeA, CoinTypeB>(
config: &GlobalConfig,
pool: &mut Pool<CoinTypeA, CoinTypeB>,
tick_lower_idx: u32,
tick_upper_idx: u32,
coins_b: vector<Coin<CoinTypeB>>,
amount: u64,
clock: &Clock,
ctx: &mut TxContext
) {
let position_nft = pool::open_position(
config,
pool,
tick_lower_idx,
tick_upper_idx,
ctx
);
let receipt = pool::add_liquidity_fix_coin(
config,
pool,
&mut position_nft,
amount,
false,
clock
);
repay_add_liquidity(config, pool, receipt, vector::empty(), coins_b, 0, amount, ctx);
// transfer::public_transfer(position_nft, tx_context::sender(ctx));
}
Last updated