Search Apps Documentation Source Content File Folder Download Copy Actions Download

liquidity_management.gno

1.93 Kb ยท 64 lines
 1package v1
 2
 3import (
 4	u256 "gno.land/p/gnoswap/uint256"
 5	"gno.land/p/nt/ufmt"
 6
 7	"gno.land/r/gnoswap/common"
 8	pl "gno.land/r/gnoswap/pool"
 9)
10
11type AddLiquidityParams struct {
12	poolKey        string     // poolPath of the pool which has the position
13	tickLower      int32      // lower end of the tick range for the position
14	tickUpper      int32      // upper end of the tick range for the position
15	amount0Desired *u256.Uint // desired amount of token0 to be minted
16	amount1Desired *u256.Uint // desired amount of token1 to be minted
17	amount0Min     *u256.Uint // minimum amount of token0 to be minted
18	amount1Min     *u256.Uint // minimum amount of token1 to be minted
19	caller         address    // address to call the function
20}
21
22// addLiquidity calculates liquidity amounts and mints position tokens to a pool.
23func (p *positionV1) addLiquidity(params AddLiquidityParams) (*u256.Uint, *u256.Uint, *u256.Uint) {
24	sqrtPriceX96 := pl.GetSlot0SqrtPriceX96(params.poolKey)
25	sqrtRatioAX96 := common.TickMathGetSqrtRatioAtTick(params.tickLower)
26	sqrtRatioBX96 := common.TickMathGetSqrtRatioAtTick(params.tickUpper)
27
28	liquidity := common.GetLiquidityForAmounts(
29		sqrtPriceX96,
30		sqrtRatioAX96,
31		sqrtRatioBX96,
32		params.amount0Desired,
33		params.amount1Desired,
34	)
35
36	token0, token1, fee := splitOf(params.poolKey)
37	amount0Str, amount1Str := pl.Mint(
38		cross,
39		token0,
40		token1,
41		fee,
42		params.tickLower,
43		params.tickUpper,
44		liquidity.ToString(),
45		params.caller,
46	)
47
48	amount0 := u256.MustFromDecimal(amount0Str)
49	amount1 := u256.MustFromDecimal(amount1Str)
50
51	amount0Cond := amount0.Gte(params.amount0Min)
52	amount1Cond := amount1.Gte(params.amount1Min)
53
54	if !(amount0Cond && amount1Cond) {
55		panic(newErrorWithDetail(
56			errSlippage,
57			ufmt.Sprintf(
58				"Price Slippage Check(amount0(%s) >= amount0Min(%s), amount1(%s) >= amount1Min(%s))",
59				amount0Str, params.amount0Min.ToString(), amount1Str, params.amount1Min.ToString()),
60		))
61	}
62
63	return liquidity, amount0, amount1
64}