From 7029867d30972ea9daa7a83fd161b3786ddac9fe Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 16 Jan 2024 16:58:21 +0900 Subject: [PATCH 1/2] GSW-794 feat: more rpc data to simulate DrySwap externally --- pool/_RPC_call.gno | 109 +++++++++++++++++- ...ic_test.gno => _TEST_math_logic_test.gnoa} | 0 ..._TEST_rpc_test.gnoa => _TEST_rpc_test.gno} | 4 + pool/pool.gno | 6 + 4 files changed, 118 insertions(+), 1 deletion(-) rename pool/{_TEST_math_logic_test.gno => _TEST_math_logic_test.gnoa} (100%) rename pool/{_TEST_rpc_test.gnoa => _TEST_rpc_test.gno} (94%) diff --git a/pool/_RPC_call.gno b/pool/_RPC_call.gno index b4dd3292b..9307af733 100644 --- a/pool/_RPC_call.gno +++ b/pool/_RPC_call.gno @@ -16,8 +16,115 @@ func RpcGetPools() string { rr, err := json.Marshal(poolsDetail) if err != nil { - panic(ufmt.Sprintf("[POOL] getter_api.gno__ApiGetPools() || %v", err)) + panic(ufmt.Sprintf("[POOL] getter_api.gno__RpcGetPools() || %v", err)) } return string(rr) } + +func RpcMakePool(poolKey string) string { + rpcPool := RpcPool{} + pool := GetPoolFromPoolKey(poolKey) + + rpcPool.Token0Path = pool.token0Path + rpcPool.Token1Path = pool.token1Path + + rpcPool.BalancesToken0 = pool.balances.token0 + rpcPool.BalancesToken1 = pool.balances.token1 + + rpcPool.Fee = pool.fee + + rpcPool.TickSpacing = pool.tickSpacing + + rpcPool.MaxLiquidityPerTick = pool.maxLiquidityPerTick + + rpcPool.Slot0SqrtPriceX96 = pool.slot0.sqrtPriceX96 + rpcPool.Slot0Tick = pool.slot0.tick + rpcPool.Slot0FeeProtocol = pool.slot0.feeProtocol + rpcPool.Slot0Unlocked = pool.slot0.unlocked + + rpcPool.FeeGrowthGlobal0X128 = pool.feeGrowthGlobal0X128 + rpcPool.FeeGrowthGlobal1X128 = pool.feeGrowthGlobal1X128 + + rpcPool.ProtocolFeesToken0 = pool.protocolFees.token0 + rpcPool.ProtocolFeesToken1 = pool.protocolFees.token1 + + rpcPool.Liquidity = pool.liquidity + + rpcPool.Ticks = RpcTicks{} + for tick, tickInfo := range pool.ticks { + rpcPool.Ticks[tick] = RpcTickInfo{ + LiquidityGross: tickInfo.liquidityGross, + LiquidityNet: tickInfo.liquidityNet, + FeeGrowthOutside0X128: tickInfo.feeGrowthOutside0X128, + FeeGrowthOutside1X128: tickInfo.feeGrowthOutside1X128, + TickCumulativeOutside: tickInfo.tickCumulativeOutside, + SecondsPerLiquidityOutsideX: tickInfo.secondsPerLiquidityOutsideX128, + SecondsOutside: tickInfo.secondsOutside, + Initialized: tickInfo.initialized, + } + } + + rpcPool.TickBitmaps = pool.tickBitmaps + + rr, err := json.Marshal(rpcPool) + if err != nil { + panic(ufmt.Sprintf("[POOL] getter_api.gno__RpcMakePool() || %v", err)) + } + + return string(rr) +} + +type RpcPool struct { + Token0Path string `json:"token0_path"` + Token1Path string `json:"token1_path"` + + BalancesToken0 bigint `json:"token0_balance"` + BalancesToken1 bigint `json:"token1_balance"` + + // fee is the fee tier of the pool + Fee uint16 `json:"fee"` + + // tickSpacing is the spacing between ticks + TickSpacing int32 `json:"tick_spacing"` + + // maxLiquidityPerTick is the maximum amount of liquidity that can be added per tick + MaxLiquidityPerTick bigint `json:"max_liquidity_per_tick"` + + // slot0 is the current tick and price of the pool + Slot0SqrtPriceX96 bigint `json:"sqrt_price_x96"` + Slot0Tick int32 `json:"tick"` + Slot0FeeProtocol uint8 `json:"fee_protocol"` + Slot0Unlocked bool `json:"unlocked"` + + FeeGrowthGlobal0X128 bigint `json:"global0_fee_growth_x128"` + FeeGrowthGlobal1X128 bigint `json:"global1_fee_growth_x128"` + + ProtocolFeesToken0 bigint `json:"token0_protocol_fee"` + ProtocolFeesToken1 bigint `json:"token1_protocol_fee"` + + // liquidity is the total amount of liquidity in the pool + Liquidity bigint `json:"liquidity"` + + // ticks is a mapping from tick index to tick + Ticks RpcTicks `json:"ticks"` + + // tickBitmaps is a mapping from tick index to tick bitmap + TickBitmaps TickBitmaps `json:"tick_bitmaps"` +} + +type RpcTicks map[int32]RpcTickInfo // tick => RpcTickInfo +type RpcTickInfo struct { + LiquidityGross bigint `json:"liquidity_gross"` + LiquidityNet bigint `json:"liquidity_net"` + + FeeGrowthOutside0X128 bigint `json:"outside0_fee_growth_x128"` + FeeGrowthOutside1X128 bigint `json:"outside1_fee_growth_x128"` + + TickCumulativeOutside bigint `json:"tick_cumulative_outside"` + + SecondsPerLiquidityOutsideX bigint `json:"seconds_per_liquidity_outside_x"` + SecondsOutside bigint `json:"seconds_outside"` + + Initialized bool `json:"initialized"` +} diff --git a/pool/_TEST_math_logic_test.gno b/pool/_TEST_math_logic_test.gnoa similarity index 100% rename from pool/_TEST_math_logic_test.gno rename to pool/_TEST_math_logic_test.gnoa diff --git a/pool/_TEST_rpc_test.gnoa b/pool/_TEST_rpc_test.gno similarity index 94% rename from pool/_TEST_rpc_test.gnoa rename to pool/_TEST_rpc_test.gno index f4eaa7588..5f55c0fbd 100644 --- a/pool/_TEST_rpc_test.gnoa +++ b/pool/_TEST_rpc_test.gno @@ -87,3 +87,7 @@ func TestApiGetPool(t *testing.T) { func TestRpcGetPools(t *testing.T) { RpcGetPools() } + +func TestRpcMakePool(t *testing.T) { + RpcMakePool("gno.land/r/demo/bar:gno.land/r/demo/baz:500") +} diff --git a/pool/pool.gno b/pool/pool.gno index a66c3a39a..48ec94454 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -447,6 +447,7 @@ func DrySwap( ) (bigint, bigint, bool) { if amountSpecified == 0 { + println("# 1") return 0, 0, false } @@ -455,10 +456,12 @@ func DrySwap( if zeroForOne { if !(sqrtPriceLimitX96 < slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 > MIN_SQRT_RATIO) { + println("# 2") return 0, 0, false } } else { if !(sqrtPriceLimitX96 > slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 < MAX_SQRT_RATIO) { + println("# 3") return 0, 0, false } } @@ -615,18 +618,21 @@ func DrySwap( if zeroForOne { if !(pool.balances.token1 >= absBigint(amount1)) { // NOT ENOUGH BALANCE for output token1 + println("# 4") return 0, 0, false } } else { if !(pool.balances.token0 >= absBigint(amount0)) { // NOT ENOUGH BALANCE for output token0 + println("# 5") return 0, 0, false } } // just not enough balance if amount0 == 0 && amount1 == 0 { + println("# 6") return 0, 0, false } From e873d3afabb795a5272a1775d69ab9614e0dc1b9 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 16 Jan 2024 16:59:04 +0900 Subject: [PATCH 2/2] chore: remove comments --- pool/pool.gno | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pool/pool.gno b/pool/pool.gno index 48ec94454..a66c3a39a 100644 --- a/pool/pool.gno +++ b/pool/pool.gno @@ -447,7 +447,6 @@ func DrySwap( ) (bigint, bigint, bool) { if amountSpecified == 0 { - println("# 1") return 0, 0, false } @@ -456,12 +455,10 @@ func DrySwap( if zeroForOne { if !(sqrtPriceLimitX96 < slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 > MIN_SQRT_RATIO) { - println("# 2") return 0, 0, false } } else { if !(sqrtPriceLimitX96 > slot0Start.sqrtPriceX96 && sqrtPriceLimitX96 < MAX_SQRT_RATIO) { - println("# 3") return 0, 0, false } } @@ -618,21 +615,18 @@ func DrySwap( if zeroForOne { if !(pool.balances.token1 >= absBigint(amount1)) { // NOT ENOUGH BALANCE for output token1 - println("# 4") return 0, 0, false } } else { if !(pool.balances.token0 >= absBigint(amount0)) { // NOT ENOUGH BALANCE for output token0 - println("# 5") return 0, 0, false } } // just not enough balance if amount0 == 0 && amount1 == 0 { - println("# 6") return 0, 0, false }