@@ -2,114 +2,180 @@ package pool
2
2
3
3
import (
4
4
b64 "encoding/base64"
5
-
6
5
"encoding/json"
7
6
"strings"
8
7
9
8
"gno.land/p/demo/ufmt"
10
9
)
11
10
12
- type ApiQueryBase struct {
11
+ type RpcPool struct {
12
+ PoolPath string `json:"poolPath"`
13
+
14
+ Token0Path string `json:"token0Path"`
15
+ Token1Path string `json:"token1Path"`
16
+
17
+ BalancesToken0 bigint `json:"balanceToken0"`
18
+ BalancesToken1 bigint `json:"balanceToken1"`
19
+
20
+ // fee is the fee tier of the pool
21
+ Fee uint16 `json:"fee"`
22
+
23
+ // tickSpacing is the spacing between ticks
24
+ TickSpacing int32 `json:"tickSpacing"`
25
+
26
+ // maxLiquidityPerTick is the maximum amount of liquidity that can be added per tick
27
+ MaxLiquidityPerTick bigint `json:"maxLiquidityPerTick"`
28
+
29
+ // slot0 is the current tick and price of the pool
30
+ Slot0SqrtPriceX96 bigint `json:"sqrtPriceX96"`
31
+ Slot0Tick int32 `json:"tick"`
32
+ Slot0FeeProtocol uint8 `json:"feeProtocol"`
33
+ Slot0Unlocked bool `json:"unlocked"`
34
+
35
+ FeeGrowthGlobal0X128 bigint `json:"feeGrowthGlobal0X128"`
36
+ FeeGrowthGlobal1X128 bigint `json:"feeGrowthGlobal1X128"`
37
+
38
+ ProtocolFeesToken0 bigint `json:"protocolFeeToken0"`
39
+ ProtocolFeesToken1 bigint `json:"protocolFeeToken1"`
40
+
41
+ // liquidity is the total amount of liquidity in the pool
42
+ Liquidity bigint `json:"liquidity"`
43
+
44
+ // ticks is a mapping from tick index to tick
45
+ Ticks RpcTicks `json:"ticks"`
46
+
47
+ // tickBitmaps is a mapping from tick index to tick bitmap
48
+ TickBitmaps TickBitmaps `json:"tickBitmaps"`
49
+
50
+ Positions []RpcPosition `json:"positions"`
51
+ }
52
+
53
+ type RpcTicks map[int32]RpcTickInfo // tick => RpcTickInfo
54
+
55
+ type RpcTickInfo struct {
56
+ LiquidityGross bigint `json:"liquidityGross"`
57
+ LiquidityNet bigint `json:"liquidityNet"`
58
+
59
+ FeeGrowthOutside0X128 bigint `json:"feeGrowthOutside0X128"`
60
+ FeeGrowthOutside1X128 bigint `json:"feeGrowthOutside1X128"`
61
+
62
+ TickCumulativeOutside bigint `json:"tickCumulativeOutside"`
63
+
64
+ SecondsPerLiquidityOutsideX bigint `json:"secondsPerLiquidityOutsideX"`
65
+ SecondsOutside bigint `json:"secondsOutside"`
66
+
67
+ Initialized bool `json:"initialized"`
68
+ }
69
+
70
+ type RpcPosition struct {
71
+ Owner string `json:"owner"`
72
+
73
+ TickLower bigint `json:"tickLower"`
74
+ TickUpper bigint `json:"tickUpper"`
75
+
76
+ Liquidity bigint `json:"liquidity"`
77
+
78
+ Token0Owed bigint `json:"token0Owed"`
79
+ Token1Owed bigint `json:"token1Owed"`
80
+ }
81
+
82
+ type ResponseQueryBase struct {
13
83
Height int64 `json:"height"`
14
84
Timestamp int64 `json:"timestamp"`
15
85
}
16
86
17
- type ResponseGetPools struct {
18
- Stat ApiQueryBase `json:"stat"`
19
- Response struct {
20
- Data []string `json:"data"`
21
- } `json:"response"`
87
+ type ResponseApiGetPools struct {
88
+ Stat ResponseQueryBase `json:"stat"`
89
+ Response []RpcPool `json:"response"`
22
90
}
23
91
24
92
func ApiGetPools() string {
25
- poolPathList := []string{}
26
-
27
- for k, _ := range pools {
28
- poolPathList = append(poolPathList, k)
29
- }
30
-
31
- qb := ApiQueryBase{
32
- Height: GetHeight(),
33
- Timestamp: GetTimestamp(),
93
+ rpcPools := []RpcPool{}
94
+ for poolPath, _ := range pools {
95
+ rpcPool := rpcMakePool(poolPath)
96
+ rpcPools = append(rpcPools, rpcPool)
34
97
}
35
98
36
- r := ResponseGetPools{
37
- Stat: qb,
38
- Response: struct {
39
- Data []string `json:"data"`
40
- }{
41
- Data: poolPathList,
99
+ r := ResponseApiGetPools{
100
+ Stat: ResponseQueryBase{
101
+ Height: GetHeight(),
102
+ Timestamp: GetTimestamp(),
42
103
},
104
+ Response: rpcPools,
43
105
}
44
106
45
107
rr, err := json.Marshal(r)
46
108
if err != nil {
47
- panic(ufmt.Sprintf("[POOL] getter_api .gno__ApiGetPools() || %v", err))
109
+ panic(ufmt.Sprintf("[POOL] _RPC_api .gno__ApiGetPools() || %v", err))
48
110
}
49
111
50
112
return string(rr)
51
113
}
52
114
53
- type ApiPositionInfo struct {
54
- Owner string `json:"owner"`
55
- TickLower bigint `json:"tick_lower"`
56
- TickUpper bigint `json:"tick_upper"`
115
+ func rpcMakePool(poolKey string) RpcPool {
116
+ rpcPool := RpcPool{}
117
+ pool := GetPoolFromPoolKey(poolKey)
57
118
58
- Liquidity bigint `json:"liquidity"`
59
- T0Owed bigint `json:"token0_owed"`
60
- T1Owed bigint `json:"token1_owed"`
61
- }
119
+ rpcPool.PoolPath = poolKey
62
120
63
- type SinglePool struct {
64
- PoolPath string `json:"pool_path"`
65
- Token0Path string `json:"token0_path"`
66
- Token1Path string `json:"token1_path"`
67
- Fee uint16 `json:"fee"`
68
- Token0Balance bigint `json:"token0_balance"`
69
- Token1Balance bigint `json:"token1_balance"`
70
- TickSpacing int32 `json:"tick_spacing"`
71
- MaxLiquidityPerTick bigint `json:"max_liquidity_per_tick"`
72
- SqrtPriceX96 bigint `json:"sqrt_price_x96"` // slot0
73
- Tick int32 `json:"tick"` // slot0
74
- FeeProtocol uint8 `json:"fee_protocol"` // slot0
75
- T0ProtocolFee bigint `json:"token0_protocol_fee"`
76
- T1ProtocolFee bigint `json:"token1_protocol_fee"`
77
- Liquidity bigint `json:"liquidity"`
78
- Ticks []int32 `json:"ticks"`
79
- TickBitmaps map[int16]bigint `json:"tick_bitmaps"`
80
- Positions []ApiPositionInfo `json:"positions"`
81
- }
121
+ rpcPool.Token0Path = pool.token0Path
122
+ rpcPool.Token1Path = pool.token1Path
82
123
83
- type ResponseGetPool struct {
84
- Stat ApiQueryBase `json:"stat"`
85
- Response struct {
86
- Data SinglePool `json:"data"`
87
- } `json:"response"`
88
- }
124
+ rpcPool.BalancesToken0 = pool.balances.token0
125
+ rpcPool.BalancesToken1 = pool.balances.token1
89
126
90
- func ApiGetPool(key string) string {
91
- singlePool := handleSinglePool(key)
127
+ rpcPool.Fee = pool.fee
92
128
93
- qb := ApiQueryBase{
94
- Height: GetHeight(),
95
- Timestamp: GetTimestamp(),
96
- }
129
+ rpcPool.TickSpacing = pool.tickSpacing
97
130
98
- r := ResponseGetPool{
99
- Stat: qb,
100
- Response: struct {
101
- Data SinglePool `json:"data"`
102
- }{
103
- Data: singlePool,
104
- },
131
+ rpcPool.MaxLiquidityPerTick = pool.maxLiquidityPerTick
132
+
133
+ rpcPool.Slot0SqrtPriceX96 = pool.slot0.sqrtPriceX96
134
+ rpcPool.Slot0Tick = pool.slot0.tick
135
+ rpcPool.Slot0FeeProtocol = pool.slot0.feeProtocol
136
+ rpcPool.Slot0Unlocked = pool.slot0.unlocked
137
+
138
+ rpcPool.FeeGrowthGlobal0X128 = pool.feeGrowthGlobal0X128
139
+ rpcPool.FeeGrowthGlobal1X128 = pool.feeGrowthGlobal1X128
140
+
141
+ rpcPool.ProtocolFeesToken0 = pool.protocolFees.token0
142
+ rpcPool.ProtocolFeesToken1 = pool.protocolFees.token1
143
+
144
+ rpcPool.Liquidity = pool.liquidity
145
+
146
+ rpcPool.Ticks = RpcTicks{}
147
+ for tick, tickInfo := range pool.ticks {
148
+ rpcPool.Ticks[tick] = RpcTickInfo{
149
+ LiquidityGross: tickInfo.liquidityGross,
150
+ LiquidityNet: tickInfo.liquidityNet,
151
+ FeeGrowthOutside0X128: tickInfo.feeGrowthOutside0X128,
152
+ FeeGrowthOutside1X128: tickInfo.feeGrowthOutside1X128,
153
+ TickCumulativeOutside: tickInfo.tickCumulativeOutside,
154
+ SecondsPerLiquidityOutsideX: tickInfo.secondsPerLiquidityOutsideX128,
155
+ SecondsOutside: tickInfo.secondsOutside,
156
+ Initialized: tickInfo.initialized,
157
+ }
105
158
}
106
159
107
- rr, err := json.Marshal(r)
108
- if err != nil {
109
- panic(ufmt.Sprintf("[POOL] getter_api.gno__ApiGetPool() || %v", err))
160
+ rpcPool.TickBitmaps = pool.tickBitmaps
161
+
162
+ Positions := pool.positions
163
+ rpcPositions := []RpcPosition{}
164
+ for posKey, posInfo := range Positions {
165
+ owner, tickLower, tickUpper := posKeyDivide(posKey)
166
+
167
+ rpcPositions = append(rpcPositions, RpcPosition{
168
+ Owner: owner,
169
+ TickLower: tickLower,
170
+ TickUpper: tickUpper,
171
+ Liquidity: posInfo.liquidity,
172
+ Token0Owed: posInfo.tokensOwed0,
173
+ Token1Owed: posInfo.tokensOwed1,
174
+ })
110
175
}
176
+ rpcPool.Positions = rpcPositions
111
177
112
- return string(rr)
178
+ return rpcPool
113
179
}
114
180
115
181
func posKeyDivide(posKey string) (string, bigint, bigint) {
@@ -119,59 +185,10 @@ func posKeyDivide(posKey string) (string, bigint, bigint) {
119
185
120
186
res := strings.Split(posKey, "__")
121
187
if len(res) != 3 {
122
- panic(ufmt.Sprintf("[POOL] getter_api .gno__posKeyDivide() || invalid posKey(%s)", posKey))
188
+ panic(ufmt.Sprintf("[POOL] _RPC_api .gno__posKeyDivide() || invalid posKey(%s)", posKey))
123
189
}
124
190
125
191
owner, tickLower, tickUpper := res[0], res[1], res[2]
126
192
127
193
return owner, bigint(tickLower), bigint(tickUpper)
128
194
}
129
-
130
- func handleSinglePool(poolPath string) SinglePool {
131
- pool, exist := pools[poolPath]
132
- if !exist {
133
- return SinglePool{}
134
- }
135
-
136
- ticks := pool.ticks
137
- tickList := []int32{}
138
- for k, _ := range ticks {
139
- tickList = append(tickList, k)
140
- }
141
-
142
- positions := pool.positions
143
- positionList := []ApiPositionInfo{}
144
- for k, v := range positions {
145
- owner, tl, th := posKeyDivide(k)
146
-
147
- posInfo := ApiPositionInfo{
148
- Owner: owner,
149
- TickLower: tl,
150
- TickUpper: th,
151
- Liquidity: v.liquidity,
152
- T0Owed: v.tokensOwed0,
153
- T1Owed: v.tokensOwed1,
154
- }
155
- positionList = append(positionList, posInfo)
156
- }
157
-
158
- return SinglePool{
159
- PoolPath: poolPath,
160
- Token0Path: pool.token0Path,
161
- Token1Path: pool.token1Path,
162
- Fee: pool.fee,
163
- Token0Balance: pool.balances.token0,
164
- Token1Balance: pool.balances.token1,
165
- TickSpacing: pool.tickSpacing,
166
- MaxLiquidityPerTick: pool.maxLiquidityPerTick,
167
- SqrtPriceX96: pool.slot0.sqrtPriceX96,
168
- Tick: pool.slot0.tick,
169
- FeeProtocol: pool.slot0.feeProtocol,
170
- T0ProtocolFee: pool.protocolFees.token0,
171
- T1ProtocolFee: pool.protocolFees.token1,
172
- Liquidity: pool.liquidity,
173
- Ticks: tickList,
174
- TickBitmaps: pool.tickBitmaps,
175
- Positions: positionList,
176
- }
177
- }
0 commit comments