Skip to content

Commit

Permalink
Merge pull request #147 from gnoswap-labs/GSW-810-feat-make-rpc-pools…
Browse files Browse the repository at this point in the history
…-to-return-list-of-pools-data

GSW-810 feat: `MakeRpcPools` to return list of pools data
  • Loading branch information
notJoon authored Jan 22, 2024
2 parents 03dbf7b + 560241d commit 79cba23
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
69 changes: 69 additions & 0 deletions pool/_RPC_call.gno
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,77 @@ func RpcGetPools() string {
return string(rr)
}

func RpcMakePools() string {
rpcPools := []RpcPool{}

for poolPath, pool := range pools {
singlePool := RpcMakePoolStruct(poolPath)
rpcPools = append(rpcPools, singlePool)
}

rr, err := json.Marshal(rpcPools)
if err != nil {
panic(ufmt.Sprintf("[POOL] getter_api.gno__RpcMakePools() || %v", err))
}

return string(rr)
}

func RpcMakePoolStruct(poolKey string) RpcPool {
rpcPool := RpcPool{}
pool := GetPoolFromPoolKey(poolKey)

rpcPool.PoolPath = 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
return rpcPool
}

func RpcMakePool(poolKey string) string {
rpcPool := RpcPool{}
pool := GetPoolFromPoolKey(poolKey)

rpcPool.PoolPath = poolKey

rpcPool.Token0Path = pool.token0Path
rpcPool.Token1Path = pool.token1Path

Expand Down Expand Up @@ -76,6 +143,8 @@ func RpcMakePool(poolKey string) string {
}

type RpcPool struct {
PoolPath string `json:"pool_path"`

Token0Path string `json:"token0_path"`
Token1Path string `json:"token1_path"`

Expand Down
15 changes: 9 additions & 6 deletions pool/tick_math.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func TickMathGetSqrtRatioAtTick(tick int32) bigint {
var absTick bigint
if tick < 0 {
absTick = -1 * bigint(tick)
absTick = -bigint(tick)
} else {
absTick = bigint(tick)
}
Expand Down Expand Up @@ -84,11 +84,12 @@ func TickMathGetSqrtRatioAtTick(tick int32) bigint {

shiftedRatio := ratio >> 32
remainder := ratio % (1 << 32)
if remainder == 0 {
if shiftedRatio+remainder == 0 {
return shiftedRatio + 0
} else {
return shiftedRatio + 1
}

}

func TickMathGetTickAtSqrtRatio(sqrtPriceX96 bigint) int32 {
Expand Down Expand Up @@ -145,11 +146,13 @@ func TickMathGetTickAtSqrtRatio(sqrtPriceX96 bigint) int32 {
tickLow := int32(int64((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128))
tickHi := int32(int64((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128))

tick := tickLow
var tick int32
if tickLow == tickHi {
if TickMathGetSqrtRatioAtTick(tickHi) <= sqrtPriceX96 {
tick = tickHi
}
tick = tickLow
} else if TickMathGetSqrtRatioAtTick(tickHi) <= sqrtPriceX96 {
tick = tickHi
} else {
tick = tickLow
}

return tick
Expand Down

0 comments on commit 79cba23

Please sign in to comment.