From 3b334c85dff38ef40b70bfb608289cbebe9f0517 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 7 Feb 2024 18:31:43 +0900 Subject: [PATCH 1/2] GSW-856 feat: mutex lock --- pool/pool.gno | 90 +++++++++++++++++++++--------------------- pool/pool_register.gno | 22 ++++++++++- 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/pool/pool.gno b/pool/pool.gno index 1d626c7a3..fafe33b72 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -4,26 +4,26 @@ import ( "std" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" + g "gno.land/r/demo/gov" ) // only position contract can call this function func Mint( - pToken0Path string, - pToken1Path string, - pFee uint16, + token0Path string, + token1Path string, + fee uint16, recipient std.Address, tickLower int32, tickUpper int32, liquidityAmount bigint, ) (bigint, bigint) { - require(PrevRealmPath() == "gno.land/r/demo/position", ufmt.Sprintf("[POOL] pool.gno__Mint() || PrevRealmPath(%s) == \"gno.land/r/demo/position\"", PrevRealmPath())) + require(PrevRealmPath() == consts.POSITION_PATH, ufmt.Sprintf("[POOL] pool.gno__Mint() || PrevRealmPath(%s) == \"gno.land/r/demo/position\"", PrevRealmPath())) require(liquidityAmount > 0, ufmt.Sprintf("[POOL] pool.gno__Mint() || liquidityAmount(%d) > 0", liquidityAmount)) - pool := GetPool(pToken0Path, pToken1Path, pFee) + pool := GetPool(token0Path, token1Path, fee) _, amount0Int, amount1Int := pool.modifyPosition( ModifyPositionParams{ recipient, // owner @@ -87,18 +87,18 @@ func Mint( // only position contract can call this function func Burn( - pToken0Path string, - pToken1Path string, - pFee uint16, + token0Path string, + token1Path string, + fee uint16, tickLower int32, tickUpper int32, amount bigint, ) (bigint, bigint) { - require(PrevRealmPath() == "gno.land/r/demo/position", ufmt.Sprintf("[POOL] pool.gno__Burn() || caller(%s) must be position contract", PrevRealmPath())) + require(PrevRealmPath() == consts.POSITION_PATH, ufmt.Sprintf("[POOL] pool.gno__Burn() || caller(%s) must be position contract", PrevRealmPath())) requireUnsigned(amount, ufmt.Sprintf("[POOL] pool.gno__Burn() || amount(%d) >= 0", amount)) - pool := GetPool(pToken0Path, pToken1Path, pFee) + pool := GetPool(token0Path, token1Path, fee) position, amount0Int, amount1Int := pool.modifyPosition( ModifyPositionParams{ @@ -121,73 +121,75 @@ func Burn( key := positionGetKey(PrevRealmAddr(), tickLower, tickUpper) pool.positions[key] = position + // actual token transfer happens in Collect() return amount0, amount1 } // only position contract can call this function func Collect( - pToken0Path string, - pToken1Path string, - pFee uint16, + token0Path string, + token1Path string, + fee uint16, recipient std.Address, tickLower int32, tickUpper int32, amount0Requested bigint, amount1Requested bigint, ) (bigint, bigint) { - require(PrevRealmPath() == "gno.land/r/demo/position", ufmt.Sprintf("[POOL] pool.gno__Collect() || caller(%s) must be position contract(gno.land/r/demo/position)", PrevRealmPath())) + require(PrevRealmPath() == consts.POSITION_PATH, ufmt.Sprintf("[POOL] pool.gno__Collect() || caller(%s) must be position contract(gno.land/r/demo/position)", PrevRealmPath())) requireUnsigned(amount0Requested, ufmt.Sprintf("pool.gno__Collect() || amount0Requested(%d) >= 0", amount0Requested)) requireUnsigned(amount1Requested, ufmt.Sprintf("pool.gno__Collect() || amount1Requested(%d) >= 0", amount1Requested)) - pool := GetPool(pToken0Path, pToken1Path, pFee) + pool := GetPool(token0Path, token1Path, fee) key := positionGetKey(PrevRealmAddr(), tickLower, tickUpper) position, exist := pool.positions[key] require(exist, ufmt.Sprintf("[POOL] pool.gno__Collect() || position(%s) does not exist", key)) + // Smallest of three: amount0Requested, position.tokensOwed0, pool.balances.token0 amount0 := min(amount0Requested, position.tokensOwed0) + amount0 = min(amount0, pool.balances.token0) requireUnsigned(amount0, ufmt.Sprintf("[POOL] pool.gno__Collect() || amount0(%d) >= 0", amount0)) - amount1 := min(amount1Requested, position.tokensOwed1) - requireUnsigned(amount1, ufmt.Sprintf("[POOL] pool.gno__Collect() || amount1(%d) >= 0", amount1)) - - require(pool.balances.token0 >= amount0, ufmt.Sprintf("[POOL] pool.gno__Collect() || pool.balances.token0(%d) >= amount0(%d)", pool.balances.token0, amount0)) + // Update state first then transfer + position.tokensOwed0 -= amount0 + pool.balances.token0 -= amount0 transferByRegisterCall(pool.token0Path, recipient, uint64(amount0)) + requireUnsigned(pool.balances.token0, ufmt.Sprintf("[POOL] pool.gno__Burn() || pool.balances.token0(%d) >= 0", pool.balances.token0)) - require(pool.balances.token1 >= amount1, ufmt.Sprintf("[POOL] pool.gno__Collect() || pool.balances.token1(%d) >= amount1(%d)", pool.balances.token1, amount1)) - transferByRegisterCall(pool.token1Path, recipient, uint64(amount1)) + // Smallest of three: amount0Requested, position.tokensOwed0, pool.balances.token0 + amount1 := min(amount1Requested, position.tokensOwed1) + amount1 = min(amount1, pool.balances.token1) + requireUnsigned(amount1, ufmt.Sprintf("[POOL] pool.gno__Collect() || amount1(%d) >= 0", amount1)) - // adjust position - position.tokensOwed0 -= amount0 + // Update state first then transfer position.tokensOwed1 -= amount1 - pool.positions[key] = position - - // adjust pool - pool.balances.token0 -= amount0 pool.balances.token1 -= amount1 - - requireUnsigned(pool.balances.token0, ufmt.Sprintf("[POOL] pool.gno__Burn() || pool.balances.token0(%d) >= 0", pool.balances.token0)) + transferByRegisterCall(pool.token1Path, recipient, uint64(amount1)) requireUnsigned(pool.balances.token1, ufmt.Sprintf("[POOL] pool.gno__Burn() || pool.balances.token1(%d) >= 0", pool.balances.token1)) + pool.positions[key] = position + return amount0, amount1 } func Swap( - pToken0Path string, - pToken1Path string, - pFee uint16, + token0Path string, + token1Path string, + fee uint16, recipient std.Address, zeroForOne bool, amountSpecified bigint, sqrtPriceLimitX96 bigint, payer std.Address, // router ) (bigint, bigint) { - require(PrevRealmPath() == "gno.land/r/demo/router", ufmt.Sprintf("[POOL] pool.gno__Swap() || caller(%s) must be router contract(gno.land/r/demo/router)", PrevRealmPath())) + require(PrevRealmPath() == consts.ROUTER_PATH, ufmt.Sprintf("[POOL] pool.gno__Swap() || caller(%s) must be router contract(gno.land/r/demo/router)", PrevRealmPath())) + // early panic require(amountSpecified != 0, "[POOL] pool.gno__Swap() || amountSpecified can't be zero") - pool := GetPool(pToken0Path, pToken1Path, pFee) + pool := GetPool(token0Path, token1Path, fee) slot0Start := pool.slot0 require(slot0Start.unlocked, "[POOL] pool.gno__Swap() || slot0 must be unlocked") @@ -377,9 +379,9 @@ func Swap( if zeroForOne { // payer > pool balance0Before := bigint(balanceOfByRegisterCall(pool.token0Path, GetOrigPkgAddr())) - ok := transferFromByRegisterCall(pool.token0Path, payer, consts.ADDR_POOL, uint64(amount0)) + ok := transferFromByRegisterCall(pool.token0Path, payer, consts.POOL_ADDR, uint64(amount0)) if !ok { - panic("[POOL] pool.gno__Swap() || transferFromByRegisterCall(pool.token0Path, payer, ADDR_POOL, uint64(amount0)) failed") + panic("[POOL] pool.gno__Swap() || transferFromByRegisterCall(pool.token0Path, payer, POOL_ADDR, uint64(amount0)) failed") } require( @@ -393,7 +395,7 @@ func Swap( require(pool.balances.token0 >= 0, ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token0(%d) >= 0__#1", pool.balances.token0)) if amount1 < 0 { // pool > recipient - require(pool.balances.token1 > (-amount1), ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token1(%d) > (-1 * amount1)(%d)", pool.balances.token1, (-amount1))) + require(pool.balances.token1 > -amount1, ufmt.Sprintf("[POOL] pool.gno__Swap() || pool.balances.token1(%d) > (-1 * amount1)(%d)", pool.balances.token1, (-amount1))) ok := transferByRegisterCall(pool.token1Path, recipient, uint64(-amount1)) if !ok { panic("[POOL] pool.gno__Swap() || transferByRegisterCall(pool.token1Path, recipient, uint64(-amount1)) failed") @@ -406,9 +408,9 @@ func Swap( } else { // payer > pool balance1Before := bigint(balanceOfByRegisterCall(pool.token1Path, GetOrigPkgAddr())) - ok := transferFromByRegisterCall(pool.token1Path, payer, consts.ADDR_POOL, uint64(amount1)) + ok := transferFromByRegisterCall(pool.token1Path, payer, consts.POOL_ADDR, uint64(amount1)) if !ok { - panic("[POOL] pool.gno__Swap() || transferFromByRegisterCall(pool.token1Path, payer, ADDR_POOL, uint64(amount1)) failed") + panic("[POOL] pool.gno__Swap() || transferFromByRegisterCall(pool.token1Path, payer, POOL_ADDR, uint64(amount1)) failed") } require( @@ -460,9 +462,9 @@ func SetFeeProtocol( // ADMIN func CollectProtocol( - pToken0Path string, - pToken1Path string, - pFee uint16, + token0Path string, + token1Path string, + fee uint16, recipient std.Address, amount0Requested bigint, amount1Requested bigint, @@ -471,7 +473,7 @@ func CollectProtocol( requireUnsigned(amount1Requested, ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || amount1Requested(%d) >= 0", amount1Requested)) require(isAdmin(PrevRealmAddr()), ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || caller(%s) must be admin", PrevRealmAddr())) - pool := GetPool(pToken0Path, pToken1Path, pFee) + pool := GetPool(token0Path, token1Path, fee) amount0 := min(amount0Requested, pool.protocolFees.token0) requireUnsigned(amount0, ufmt.Sprintf("[POOL] pool.gno__CollectProtocol() || amount0(%d) >= 0", amount0)) diff --git a/pool/pool_register.gno b/pool/pool_register.gno index 786928681..07daaa5f2 100644 --- a/pool/pool_register.gno +++ b/pool/pool_register.gno @@ -10,6 +10,8 @@ import ( var registered = []GRC20Pair{} +var locked bool // mutex flag + type GRC20Interface interface { Transfer() func(to users.AddressOrName, amount uint64) TransferFrom() func(from, to users.AddressOrName, amount uint64) @@ -95,8 +97,16 @@ func transferByRegisterCall(pkgPath string, to std.Address, amount uint64) bool return false } - registered[i].igrc20.Transfer()(users.AddressOrName(to), amount) + if !locked { + locked = true + registered[i].igrc20.Transfer()(users.AddressOrName(to), amount) + defer func() { + locked = false + }() + } else { + panic("[POOl] pool_register.gno transferByRegisterCall: locked") + } return true } @@ -108,8 +118,16 @@ func transferFromByRegisterCall(pkgPath string, from, to std.Address, amount uin return false } - registered[i].igrc20.TransferFrom()(users.AddressOrName(from), users.AddressOrName(to), amount) + if !locked { + locked = true + registered[i].igrc20.TransferFrom()(users.AddressOrName(from), users.AddressOrName(to), amount) + defer func() { + locked = false + }() + } else { + panic("[POOl] pool_register.gno transferFromByRegisterCall: locked") + } return true } From 0a30238b8a884376837875f922d47c0353a732ca Mon Sep 17 00:00:00 2001 From: n3wbie Date: Wed, 7 Feb 2024 18:33:42 +0900 Subject: [PATCH 2/2] chore: unify variable name --- pool/_RPC_api.gno | 6 ++-- pool/_RPC_dry.gno | 6 ++-- pool/bit_math.gno | 1 - pool/emergency_halt.gno | 1 - pool/pool_manager.gno | 55 ++++++++++++++++--------------- pool/position.gno | 1 - pool/sqrt_price_math.gno | 1 - pool/tick.gno | 1 - pool/tick_bitmap.gno | 1 - pool/tick_math.gno | 1 - position/_RPC_api.gno | 4 +-- position/liquidity_management.gno | 2 +- position/position.gno | 8 ++--- router/_RPC_api.gno | 2 +- staker/staker.gno | 2 +- 15 files changed, 43 insertions(+), 49 deletions(-) diff --git a/pool/_RPC_api.gno b/pool/_RPC_api.gno index 1c63aec86..a00fbf332 100644 --- a/pool/_RPC_api.gno +++ b/pool/_RPC_api.gno @@ -112,11 +112,11 @@ func ApiGetPools() string { return string(rr) } -func rpcMakePool(poolKey string) RpcPool { +func rpcMakePool(poolPath string) RpcPool { rpcPool := RpcPool{} - pool := GetPoolFromPoolKey(poolKey) + pool := GetPoolFromPoolPath(poolPath) - rpcPool.PoolPath = poolKey + rpcPool.PoolPath = poolPath rpcPool.Token0Path = pool.token0Path rpcPool.Token1Path = pool.token1Path diff --git a/pool/_RPC_dry.gno b/pool/_RPC_dry.gno index 015750e42..0a07b2cfc 100644 --- a/pool/_RPC_dry.gno +++ b/pool/_RPC_dry.gno @@ -9,8 +9,8 @@ import ( ) func DrySwap( - pToken0Path string, - pToken1Path string, + token0Path string, + token1Path string, pFee uint16, recipient std.Address, zeroForOne bool, @@ -22,7 +22,7 @@ func DrySwap( return 0, 0, false } - pool := GetPool(pToken0Path, pToken1Path, pFee) + pool := GetPool(token0Path, token1Path, pFee) slot0Start := pool.slot0 if zeroForOne { diff --git a/pool/bit_math.gno b/pool/bit_math.gno index 5906e8ce5..1e2b16525 100644 --- a/pool/bit_math.gno +++ b/pool/bit_math.gno @@ -2,7 +2,6 @@ package pool import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/pool/emergency_halt.gno b/pool/emergency_halt.gno index 318cc0dfb..85eee64e9 100644 --- a/pool/emergency_halt.gno +++ b/pool/emergency_halt.gno @@ -4,7 +4,6 @@ import ( "std" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/pool/pool_manager.gno b/pool/pool_manager.gno index a8a7f793f..604aade8b 100644 --- a/pool/pool_manager.gno +++ b/pool/pool_manager.gno @@ -6,6 +6,8 @@ import ( "gno.land/p/demo/ufmt" + "gno.land/r/demo/consts" + gns "gno.land/r/demo/gns" ) @@ -13,8 +15,8 @@ var ( admins []std.Address initialized bool = false - feeAmountTickSpacing map[uint16]int32 = make(map[uint16]int32) // map[fee_amount]tick_spacing - pools map[string]*Pool = make(map[string]*Pool) // map[pool_key]*Pool + feeAmountTickSpacing map[uint16]int32 = make(map[uint16]int32) // map[feeAmount]tick_spacing + pools map[string]*Pool = make(map[string]*Pool) // map[poolPath]*Pool ) func InitManual() { @@ -28,16 +30,16 @@ func InitManual() { } func CreatePool( - tokenAPath string, - tokenBPath string, + token0Path string, + token1Path string, fee uint16, sqrtPriceX96 bigint, ) *Pool { require(initialized, "[POOl] pool_manager.gno__CreatePool() || contract must be initialized") - require(tokenAPath != tokenBPath, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || token pair cannot be the same__tokenAPath(%s) != tokenBPath(%s)", tokenAPath, tokenBPath)) + require(token0Path != token1Path, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || token pair cannot be the same__token0Path(%s) != token1Path(%s)", token0Path, token1Path)) - if tokenBPath < tokenAPath { - tokenAPath, tokenBPath = tokenBPath, tokenAPath + if token1Path < token0Path { + token0Path, token1Path = token1Path, token0Path tick := -(TickMathGetTickAtSqrtRatio(sqrtPriceX96)) sqrtPriceX96 = TickMathGetSqrtRatioAtTick(tick) } @@ -46,42 +48,41 @@ func CreatePool( tickSpacing := feeAmountTickSpacing[fee] require(tickSpacing > 0, ufmt.Sprintf("[POOL] pool_manager.gno__CreatePool() || tickSpacing(%d) > 0", tickSpacing)) - // calculate poolKey - poolKey := GetPoolKey(tokenAPath, tokenBPath, fee) + // calculate poolPath + poolPath := GetPoolPath(token0Path, token1Path, fee) // check whether the pool already exist - pool, exist := pools[poolKey] - require(!exist, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || pool(%s) already exist", poolKey)) + pool, exist := pools[poolPath] + require(!exist, ufmt.Sprintf("[POOl] pool_manager.gno__CreatePool() || pool(%s) already exist", poolPath)) if !exist { - // 500 GNS as creation fee - // recipent is same address that receives protocol fee - // r3v4_xxx: change address when publish - gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(std.Address("g1vaekzh6lta047h6lta047h6lta047h6lutmjdk")), 500) + // recipient is same address that receives protocol fee + // r3v4_xxx: change admin address when publish + gns.TransferFrom(a2u(std.GetOrigCaller()), a2u(consts.GNOSWAP_ADMIN), consts.POOL_CREATION_FEE) - pool = newPool(tokenAPath, tokenBPath, fee, tickSpacing, sqrtPriceX96) - pools[poolKey] = pool + pool = newPool(token0Path, token1Path, fee, tickSpacing, sqrtPriceX96) + pools[poolPath] = pool } return pool } -func GetPool(token0, token1 string, fee uint16) *Pool { - poolKey := GetPoolKey(token0, token1, fee) - pool, exist := pools[poolKey] - require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPool() || pool(%s) not found", poolKey)) +func GetPool(token0Path, token1Path string, fee uint16) *Pool { + poolPath := GetPoolPath(token0Path, token1Path, fee) + pool, exist := pools[poolPath] + require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPool() || pool(%s) not found", poolPath)) return pool } -func GetPoolFromPoolKey(poolKey string) *Pool { - pool, exist := pools[poolKey] - require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPoolFromPoolKey() || pool(%s) not found", poolKey)) +func GetPoolFromPoolPath(poolPath string) *Pool { + pool, exist := pools[poolPath] + require(exist, ufmt.Sprintf("[POOL] pool_manager.gno__GetPoolFromPoolPath() || pool(%s) not found", poolPath)) return pool } -func GetPoolKey(token0Path, token1Path string, fee uint16) string { +func GetPoolPath(token0Path, token1Path string, fee uint16) string { if token0Path < token1Path { return token0Path + ":" + token1Path + ":" + strconv.Itoa(int(fee)) } else { @@ -94,7 +95,7 @@ func AddAdmin(addr std.Address) { if isAdmin(caller) { admins = append(admins, addr) } else { - panic("[POOL] pool_manager.gno__AddAdmin() || caller is not admin") // r3v4_xx: detailed error message ?? + panic("[POOL] pool_manager.gno__AddAdmin() || caller is not admin") } } @@ -112,7 +113,7 @@ func RemoveAdmin(addr std.Address) { } } } else { - panic("[POOL] pool_manager.gno__RemoveAdmin() || caller is not admin") // r3v4_xx: detailed error message ?? + panic("[POOL] pool_manager.gno__RemoveAdmin() || caller is not admin") } } diff --git a/pool/position.gno b/pool/position.gno index 46a77dded..ee4222c05 100644 --- a/pool/position.gno +++ b/pool/position.gno @@ -5,7 +5,6 @@ import ( "std" "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/pool/sqrt_price_math.gno b/pool/sqrt_price_math.gno index 0429787c6..1c721d93d 100644 --- a/pool/sqrt_price_math.gno +++ b/pool/sqrt_price_math.gno @@ -2,7 +2,6 @@ package pool import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/pool/tick.gno b/pool/tick.gno index 75e9336f7..7d3486757 100644 --- a/pool/tick.gno +++ b/pool/tick.gno @@ -2,7 +2,6 @@ package pool import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/pool/tick_bitmap.gno b/pool/tick_bitmap.gno index b4060f3ed..69ed5a32d 100644 --- a/pool/tick_bitmap.gno +++ b/pool/tick_bitmap.gno @@ -2,7 +2,6 @@ package pool import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/pool/tick_math.gno b/pool/tick_math.gno index 7ea65faec..fb6e07ece 100644 --- a/pool/tick_math.gno +++ b/pool/tick_math.gno @@ -2,7 +2,6 @@ package pool import ( "gno.land/p/demo/ufmt" - "gno.land/r/demo/consts" ) diff --git a/position/_RPC_api.gno b/position/_RPC_api.gno index e4991fa4e..018cf8588 100644 --- a/position/_RPC_api.gno +++ b/position/_RPC_api.gno @@ -65,7 +65,7 @@ func rpcMakePosition(lpTokenId uint64) RpcPosition { panic(ufmt.Sprintf("[POSITION] getter_api.gno__rpcMakePosition() || position not found for lpTokenId(%d)", lpTokenId)) } - pool := p.GetPoolFromPoolKey(position.poolKey) + pool := p.GetPoolFromPoolPath(position.poolKey) currentX96 := pool.PoolGetSlot0SqrtPriceX96() lowerX96 := p.TickMathGetSqrtRatioAtTick(position.tickLower) upperX96 := p.TickMathGetSqrtRatioAtTick(position.tickUpper) @@ -105,7 +105,7 @@ func unclaimedFee(tokenId uint64) (bigint, bigint) { tickUpper := positions[tokenId].tickUpper poolKey := positions[tokenId].poolKey - pool := p.GetPoolFromPoolKey(poolKey) + pool := p.GetPoolFromPoolPath(poolKey) currentTick := pool.PoolGetSlot0Tick() diff --git a/position/liquidity_management.gno b/position/liquidity_management.gno index f552912c5..f689c6b34 100644 --- a/position/liquidity_management.gno +++ b/position/liquidity_management.gno @@ -5,7 +5,7 @@ import ( ) func addLiquidity(params AddLiquidityParams) (bigint, bigint, bigint) { - pool := p.GetPoolFromPoolKey(params.poolKey) + pool := p.GetPoolFromPoolPath(params.poolKey) sqrtPriceX96 := pool.PoolGetSlot0SqrtPriceX96() sqrtRatioAX96 := p.TickMathGetSqrtRatioAtTick(params.tickLower) diff --git a/position/position.gno b/position/position.gno index 31db71652..75e204c24 100644 --- a/position/position.gno +++ b/position/position.gno @@ -58,7 +58,7 @@ func mint(params MintParams) (uint64, bigint, bigint, bigint) { pool := p.GetPool(params.token0, params.token1, params.fee) liquidity, amount0, amount1 := addLiquidity( AddLiquidityParams{ - poolKey: p.GetPoolKey(params.token0, params.token1, params.fee), + poolKey: p.GetPoolPath(params.token0, params.token1, params.fee), recipient: GetOrigPkgAddr(), // hardcoded tickLower: params.tickLower, tickUpper: params.tickUpper, @@ -80,7 +80,7 @@ func mint(params MintParams) (uint64, bigint, bigint, bigint) { position := Position{ nonce: 0, operator: PrevRealmAddr(), - poolKey: p.GetPoolKey(params.token0, params.token1, params.fee), + poolKey: p.GetPoolPath(params.token0, params.token1, params.fee), tickLower: params.tickLower, tickUpper: params.tickUpper, liquidity: liquidity, @@ -108,7 +108,7 @@ func CollectFee(tokenId uint64) (uint64, bigint, bigint, string) { // tokenId, t p.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, 0) // burn '0' liquidity to collect fee positionKey := positionKeyCompute(GetOrigPkgAddr(), position.tickLower, position.tickUpper) - pool := p.GetPoolFromPoolKey(position.poolKey) + pool := p.GetPoolFromPoolPath(position.poolKey) feeGrowthInside0LastX128, feeGrowthInside1LastX128 := pool.PoolGetPositionFeeGrowthInside0LastX128(positionKey), pool.PoolGetPositionFeeGrowthInside1LastX128(positionKey) tokensOwed0 += (feeGrowthInside0LastX128 - position.feeGrowthInside0LastX128) * position.liquidity / Q128 @@ -145,7 +145,7 @@ func Burn(tokenId uint64) (uint64, bigint, bigint, bigint, string) { // tokenId, position := positions[tokenId] positionLiquidity := position.liquidity - pool := p.GetPoolFromPoolKey(position.poolKey) // poolKey == poolPath + pool := p.GetPoolFromPoolPath(position.poolKey) // poolKey == poolPath pToken0, pToken1, pFee := poolKeyDivide(position.poolKey) burnAmount0, burnAmount1 := p.Burn(pToken0, pToken1, pFee, position.tickLower, position.tickUpper, positionLiquidity) diff --git a/router/_RPC_api.gno b/router/_RPC_api.gno index 00a307455..ccc32da61 100644 --- a/router/_RPC_api.gno +++ b/router/_RPC_api.gno @@ -103,7 +103,7 @@ func getRatiosFromBase(maxHops int) []TokenPathPrice { func calculateTokenPrice(currentToken, swapPath string, numPools, proceed int, currentPriceX96 bigint) bigint { currentPoolPathKey := makePoolPath(swapPath, proceed) - currentPool := p.GetPoolFromPoolKey(currentPoolPathKey) + currentPool := p.GetPoolFromPoolPath(currentPoolPathKey) poolToken0 := currentPool.PoolGetToken0Path() poolToken1 := currentPool.PoolGetToken1Path() diff --git a/staker/staker.gno b/staker/staker.gno index a9988041b..e72c59e98 100644 --- a/staker/staker.gno +++ b/staker/staker.gno @@ -282,7 +282,7 @@ func deleteFromIncentives(m map[string]Incentive, key string) map[string]Incenti func getTokenPairBalanceFromPosition(tokenId uint64) (bigint, bigint) { poolKey := pos.PositionGetPositionPoolKey(tokenId) - pool := pl.GetPoolFromPoolKey(poolKey) + pool := pl.GetPoolFromPoolPath(poolKey) currentX96 := pool.PoolGetSlot0SqrtPriceX96() lowerX96 := pl.TickMathGetSqrtRatioAtTick(pos.PositionGetPositionTickLower(tokenId)) upperX96 := pl.TickMathGetSqrtRatioAtTick(pos.PositionGetPositionTickUpper(tokenId))