Skip to content

Commit

Permalink
Merge pull request #150 from gnoswap-labs/GSW-838-feat-add-query-meta…
Browse files Browse the repository at this point in the history
…data-in-every-api-function

GSW-838 feat add query metadata in every api function
  • Loading branch information
notJoon authored Feb 5, 2024
2 parents ad4d41a + b2d6a04 commit 76b1d13
Show file tree
Hide file tree
Showing 16 changed files with 641 additions and 997 deletions.
263 changes: 140 additions & 123 deletions pool/_RPC_api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,180 @@ package pool

import (
b64 "encoding/base64"

"encoding/json"
"strings"

"gno.land/p/demo/ufmt"
)

type ApiQueryBase struct {
type RpcPool struct {
PoolPath string `json:"poolPath"`

Token0Path string `json:"token0Path"`
Token1Path string `json:"token1Path"`

BalancesToken0 bigint `json:"balanceToken0"`
BalancesToken1 bigint `json:"balanceToken1"`

// fee is the fee tier of the pool
Fee uint16 `json:"fee"`

// tickSpacing is the spacing between ticks
TickSpacing int32 `json:"tickSpacing"`

// maxLiquidityPerTick is the maximum amount of liquidity that can be added per tick
MaxLiquidityPerTick bigint `json:"maxLiquidityPerTick"`

// slot0 is the current tick and price of the pool
Slot0SqrtPriceX96 bigint `json:"sqrtPriceX96"`
Slot0Tick int32 `json:"tick"`
Slot0FeeProtocol uint8 `json:"feeProtocol"`
Slot0Unlocked bool `json:"unlocked"`

FeeGrowthGlobal0X128 bigint `json:"feeGrowthGlobal0X128"`
FeeGrowthGlobal1X128 bigint `json:"feeGrowthGlobal1X128"`

ProtocolFeesToken0 bigint `json:"protocolFeeToken0"`
ProtocolFeesToken1 bigint `json:"protocolFeeToken1"`

// 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:"tickBitmaps"`

Positions []RpcPosition `json:"positions"`
}

type RpcTicks map[int32]RpcTickInfo // tick => RpcTickInfo

type RpcTickInfo struct {
LiquidityGross bigint `json:"liquidityGross"`
LiquidityNet bigint `json:"liquidityNet"`

FeeGrowthOutside0X128 bigint `json:"feeGrowthOutside0X128"`
FeeGrowthOutside1X128 bigint `json:"feeGrowthOutside1X128"`

TickCumulativeOutside bigint `json:"tickCumulativeOutside"`

SecondsPerLiquidityOutsideX bigint `json:"secondsPerLiquidityOutsideX"`
SecondsOutside bigint `json:"secondsOutside"`

Initialized bool `json:"initialized"`
}

type RpcPosition struct {
Owner string `json:"owner"`

TickLower bigint `json:"tickLower"`
TickUpper bigint `json:"tickUpper"`

Liquidity bigint `json:"liquidity"`

Token0Owed bigint `json:"token0Owed"`
Token1Owed bigint `json:"token1Owed"`
}

type ResponseQueryBase struct {
Height int64 `json:"height"`
Timestamp int64 `json:"timestamp"`
}

type ResponseGetPools struct {
Stat ApiQueryBase `json:"stat"`
Response struct {
Data []string `json:"data"`
} `json:"response"`
type ResponseApiGetPools struct {
Stat ResponseQueryBase `json:"stat"`
Response []RpcPool `json:"response"`
}

func ApiGetPools() string {
poolPathList := []string{}

for k, _ := range pools {
poolPathList = append(poolPathList, k)
}

qb := ApiQueryBase{
Height: GetHeight(),
Timestamp: GetTimestamp(),
rpcPools := []RpcPool{}
for poolPath, _ := range pools {
rpcPool := rpcMakePool(poolPath)
rpcPools = append(rpcPools, rpcPool)
}

r := ResponseGetPools{
Stat: qb,
Response: struct {
Data []string `json:"data"`
}{
Data: poolPathList,
r := ResponseApiGetPools{
Stat: ResponseQueryBase{
Height: GetHeight(),
Timestamp: GetTimestamp(),
},
Response: rpcPools,
}

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

return string(rr)
}

type ApiPositionInfo struct {
Owner string `json:"owner"`
TickLower bigint `json:"tick_lower"`
TickUpper bigint `json:"tick_upper"`
func rpcMakePool(poolKey string) RpcPool {
rpcPool := RpcPool{}
pool := GetPoolFromPoolKey(poolKey)

Liquidity bigint `json:"liquidity"`
T0Owed bigint `json:"token0_owed"`
T1Owed bigint `json:"token1_owed"`
}
rpcPool.PoolPath = poolKey

type SinglePool struct {
PoolPath string `json:"pool_path"`
Token0Path string `json:"token0_path"`
Token1Path string `json:"token1_path"`
Fee uint16 `json:"fee"`
Token0Balance bigint `json:"token0_balance"`
Token1Balance bigint `json:"token1_balance"`
TickSpacing int32 `json:"tick_spacing"`
MaxLiquidityPerTick bigint `json:"max_liquidity_per_tick"`
SqrtPriceX96 bigint `json:"sqrt_price_x96"` // slot0
Tick int32 `json:"tick"` // slot0
FeeProtocol uint8 `json:"fee_protocol"` // slot0
T0ProtocolFee bigint `json:"token0_protocol_fee"`
T1ProtocolFee bigint `json:"token1_protocol_fee"`
Liquidity bigint `json:"liquidity"`
Ticks []int32 `json:"ticks"`
TickBitmaps map[int16]bigint `json:"tick_bitmaps"`
Positions []ApiPositionInfo `json:"positions"`
}
rpcPool.Token0Path = pool.token0Path
rpcPool.Token1Path = pool.token1Path

type ResponseGetPool struct {
Stat ApiQueryBase `json:"stat"`
Response struct {
Data SinglePool `json:"data"`
} `json:"response"`
}
rpcPool.BalancesToken0 = pool.balances.token0
rpcPool.BalancesToken1 = pool.balances.token1

func ApiGetPool(key string) string {
singlePool := handleSinglePool(key)
rpcPool.Fee = pool.fee

qb := ApiQueryBase{
Height: GetHeight(),
Timestamp: GetTimestamp(),
}
rpcPool.TickSpacing = pool.tickSpacing

r := ResponseGetPool{
Stat: qb,
Response: struct {
Data SinglePool `json:"data"`
}{
Data: singlePool,
},
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,
}
}

rr, err := json.Marshal(r)
if err != nil {
panic(ufmt.Sprintf("[POOL] getter_api.gno__ApiGetPool() || %v", err))
rpcPool.TickBitmaps = pool.tickBitmaps

Positions := pool.positions
rpcPositions := []RpcPosition{}
for posKey, posInfo := range Positions {
owner, tickLower, tickUpper := posKeyDivide(posKey)

rpcPositions = append(rpcPositions, RpcPosition{
Owner: owner,
TickLower: tickLower,
TickUpper: tickUpper,
Liquidity: posInfo.liquidity,
Token0Owed: posInfo.tokensOwed0,
Token1Owed: posInfo.tokensOwed1,
})
}
rpcPool.Positions = rpcPositions

return string(rr)
return rpcPool
}

func posKeyDivide(posKey string) (string, bigint, bigint) {
Expand All @@ -119,59 +185,10 @@ func posKeyDivide(posKey string) (string, bigint, bigint) {

res := strings.Split(posKey, "__")
if len(res) != 3 {
panic(ufmt.Sprintf("[POOL] getter_api.gno__posKeyDivide() || invalid posKey(%s)", posKey))
panic(ufmt.Sprintf("[POOL] _RPC_api.gno__posKeyDivide() || invalid posKey(%s)", posKey))
}

owner, tickLower, tickUpper := res[0], res[1], res[2]

return owner, bigint(tickLower), bigint(tickUpper)
}

func handleSinglePool(poolPath string) SinglePool {
pool, exist := pools[poolPath]
if !exist {
return SinglePool{}
}

ticks := pool.ticks
tickList := []int32{}
for k, _ := range ticks {
tickList = append(tickList, k)
}

positions := pool.positions
positionList := []ApiPositionInfo{}
for k, v := range positions {
owner, tl, th := posKeyDivide(k)

posInfo := ApiPositionInfo{
Owner: owner,
TickLower: tl,
TickUpper: th,
Liquidity: v.liquidity,
T0Owed: v.tokensOwed0,
T1Owed: v.tokensOwed1,
}
positionList = append(positionList, posInfo)
}

return SinglePool{
PoolPath: poolPath,
Token0Path: pool.token0Path,
Token1Path: pool.token1Path,
Fee: pool.fee,
Token0Balance: pool.balances.token0,
Token1Balance: pool.balances.token1,
TickSpacing: pool.tickSpacing,
MaxLiquidityPerTick: pool.maxLiquidityPerTick,
SqrtPriceX96: pool.slot0.sqrtPriceX96,
Tick: pool.slot0.tick,
FeeProtocol: pool.slot0.feeProtocol,
T0ProtocolFee: pool.protocolFees.token0,
T1ProtocolFee: pool.protocolFees.token1,
Liquidity: pool.liquidity,
Ticks: tickList,
TickBitmaps: pool.tickBitmaps,
Positions: positionList,
}
}
Loading

0 comments on commit 76b1d13

Please sign in to comment.