getter.gno
10.55 Kb · 385 lines
1package pool
2
3import (
4 u256 "gno.land/p/gnoswap/uint256"
5 ufmt "gno.land/p/nt/ufmt/v0"
6 pl "gno.land/r/gnoswap/pool"
7)
8
9// GetPoolPath generates a unique pool path string based on the token paths and fee tier.
10func GetPoolPath(token0Path, token1Path string, fee uint32) string {
11 return pl.GetPoolPath(token0Path, token1Path, fee)
12}
13
14// GetPool retrieves a pool instance based on the provided token paths and fee tier.
15func (i *poolV1) GetPool(token0Path, token1Path string, fee uint32) (*pl.Pool, error) {
16 poolPath := pl.GetPoolPath(token0Path, token1Path, fee)
17 return i.getPool(poolPath)
18}
19
20// GetFeeAmountTickSpacing retrieves the tick spacing associated with a given fee amount.
21func (i *poolV1) GetFeeAmountTickSpacing(fee uint32) (spacing int32) {
22 feeAmountTickSpacing := i.store.GetFeeAmountTickSpacing()
23
24 spacing, exist := feeAmountTickSpacing[fee]
25 if !exist {
26 panic(newErrorWithDetail(
27 errUnsupportedFeeTier,
28 ufmt.Sprintf("expected fee(%d) to be one of %d, %d, %d, %d", fee, FeeTier100, FeeTier500, FeeTier3000, FeeTier10000),
29 ))
30 }
31
32 return spacing
33}
34
35func (i *poolV1) GetToken0Path(poolPath string) string {
36 return i.mustGetPool(poolPath).Token0Path()
37}
38
39func (i *poolV1) GetToken1Path(poolPath string) string {
40 return i.mustGetPool(poolPath).Token1Path()
41}
42
43func (i *poolV1) GetFee(poolPath string) uint32 {
44 return i.mustGetPool(poolPath).Fee()
45}
46
47func (i *poolV1) GetBalanceToken0(poolPath string) int64 {
48 return i.mustGetPool(poolPath).BalanceToken0()
49}
50
51func (i *poolV1) GetBalanceToken1(poolPath string) int64 {
52 return i.mustGetPool(poolPath).BalanceToken1()
53}
54
55func (i *poolV1) GetTickSpacing(poolPath string) int32 {
56 return i.mustGetPool(poolPath).TickSpacing()
57}
58
59func (i *poolV1) GetMaxLiquidityPerTick(poolPath string) string {
60 return calculateMaxLiquidityPerTick(i.mustGetPool(poolPath).TickSpacing()).ToString()
61}
62
63func (i *poolV1) GetSlot0FeeProtocol(poolPath string) uint8 {
64 return i.mustGetPool(poolPath).Slot0FeeProtocol()
65}
66
67func (i *poolV1) GetSlot0Unlocked(poolPath string) bool {
68 return i.mustGetPool(poolPath).Slot0Unlocked()
69}
70
71func (i *poolV1) GetFeeGrowthGlobal0X128(poolPath string) *u256.Uint {
72 return i.mustGetPool(poolPath).FeeGrowthGlobal0X128()
73}
74
75func (i *poolV1) GetFeeGrowthGlobal1X128(poolPath string) *u256.Uint {
76 return i.mustGetPool(poolPath).FeeGrowthGlobal1X128()
77}
78
79func (i *poolV1) GetProtocolFeesToken0(poolPath string) int64 {
80 return i.mustGetPool(poolPath).ProtocolFeesToken0()
81}
82
83func (i *poolV1) GetProtocolFeesToken1(poolPath string) int64 {
84 return i.mustGetPool(poolPath).ProtocolFeesToken1()
85}
86
87func (i *poolV1) GetLiquidity(poolPath string) *u256.Uint {
88 return i.mustGetPool(poolPath).Liquidity()
89}
90
91func (i *poolV1) MustGetPosition(poolPath, key string) *pl.PositionInfo {
92 pool := i.mustGetPool(poolPath)
93
94 positions := pool.Positions()
95
96 result := positions.Get(key)
97 if result == nil {
98 panic(newErrorWithDetail(
99 errDataNotFound,
100 ufmt.Sprintf("expected position(%s) to exist", key),
101 ))
102 }
103
104 position, ok := result.(pl.PositionInfo)
105 if !ok {
106 panic("failed to cast position to PositionInfo")
107 }
108
109 return &position
110}
111
112func (i *poolV1) GetPositionFeeGrowthInside0LastX128(poolPath, key string) string {
113 return i.MustGetPosition(poolPath, key).FeeGrowthInside0LastX128()
114}
115
116func (i *poolV1) GetPositionFeeGrowthInside1LastX128(poolPath, key string) string {
117 return i.MustGetPosition(poolPath, key).FeeGrowthInside1LastX128()
118}
119
120func (i *poolV1) GetPositionTokensOwed0(poolPath, key string) int64 {
121 return i.MustGetPosition(poolPath, key).TokensOwed0()
122}
123
124func (i *poolV1) GetPositionTokensOwed1(poolPath, key string) int64 {
125 return i.MustGetPosition(poolPath, key).TokensOwed1()
126}
127
128func (i *poolV1) GetTickLiquidityGross(poolPath string, tick int32) string {
129 pool := i.mustGetPool(poolPath)
130 return GetTickLiquidityGross(pool, tick)
131}
132
133func (i *poolV1) GetTickLiquidityNet(poolPath string, tick int32) string {
134 pool := i.mustGetPool(poolPath)
135 return GetTickLiquidityNet(pool, tick)
136}
137
138func (i *poolV1) GetTickFeeGrowthOutside0X128(poolPath string, tick int32) string {
139 pool := i.mustGetPool(poolPath)
140 return GetTickFeeGrowthOutside0X128(pool, tick)
141}
142
143func (i *poolV1) GetTickFeeGrowthOutside1X128(poolPath string, tick int32) string {
144 pool := i.mustGetPool(poolPath)
145 return GetTickFeeGrowthOutside1X128(pool, tick)
146}
147
148func (i *poolV1) GetTickCumulativeOutside(poolPath string, tick int32) int64 {
149 pool := i.mustGetPool(poolPath)
150 return GetTickCumulativeOutside(pool, tick)
151}
152
153func (i *poolV1) GetTickSecondsPerLiquidityOutsideX128(poolPath string, tick int32) string {
154 pool := i.mustGetPool(poolPath)
155 return GetTickSecondsPerLiquidityOutsideX128(pool, tick)
156}
157
158func (i *poolV1) GetTickSecondsOutside(poolPath string, tick int32) uint32 {
159 pool := i.mustGetPool(poolPath)
160 return GetTickSecondsOutside(pool, tick)
161}
162
163func (i *poolV1) GetTickInitialized(poolPath string, tick int32) bool {
164 pool := i.mustGetPool(poolPath)
165 return GetTickInitialized(pool, tick)
166}
167
168func (i *poolV1) GetSlot0Tick(poolPath string) int32 {
169 return i.mustGetPool(poolPath).Slot0Tick()
170}
171
172func (i *poolV1) GetSlot0SqrtPriceX96(poolPath string) *u256.Uint {
173 return i.mustGetPool(poolPath).Slot0SqrtPriceX96()
174}
175
176func (i *poolV1) GetFeeGrowthGlobalX128(poolPath string) (*u256.Uint, *u256.Uint) {
177 pool := i.mustGetPool(poolPath)
178 return pool.FeeGrowthGlobal0X128(), pool.FeeGrowthGlobal1X128()
179}
180
181func (i *poolV1) GetTickFeeGrowthOutsideX128(poolPath string, tick int32) (string, string) {
182 pool := i.mustGetPool(poolPath)
183 return GetTickFeeGrowthOutside0X128(pool, tick),
184 GetTickFeeGrowthOutside1X128(pool, tick)
185}
186
187func (i *poolV1) GetPositionFeeGrowthInsideLastX128(poolPath, key string) (string, string) {
188 position := i.MustGetPosition(poolPath, key)
189 return position.FeeGrowthInside0LastX128(),
190 position.FeeGrowthInside1LastX128()
191}
192
193func (i *poolV1) GetPositionLiquidity(poolPath, key string) string {
194 return i.MustGetPosition(poolPath, key).Liquidity()
195}
196
197func (i *poolV1) ExistsPoolPath(poolPath string) bool {
198 pools := i.store.GetPools()
199 return pools.Has(poolPath)
200}
201
202func (i *poolV1) GetLastObservation(poolPath string) (
203 tickCumulative int64,
204 secondsPerLiquidityCumulativeX128 string,
205 blockTimestamp int64,
206) {
207 pool := i.mustGetPool(poolPath)
208 observationState := pool.ObservationState()
209
210 if observationState == nil {
211 return 0, "0", 0
212 }
213
214 lastObservation, err := lastObservation(observationState)
215 if err != nil {
216 return 0, "0", 0
217 }
218
219 tickCumulative = lastObservation.TickCumulative()
220 secondsPerLiquidityCumulativeX128 = lastObservation.SecondsPerLiquidityCumulativeX128()
221 blockTimestamp = lastObservation.BlockTimestamp()
222
223 return
224}
225
226func (i *poolV1) GetPoolCreationFee() int64 {
227 return i.store.GetPoolCreationFee()
228}
229
230func (i *poolV1) GetWithdrawalFee() uint64 {
231 return i.store.GetWithdrawalFeeBPS()
232}
233
234// GetTWAP returns the time-weighted average price for a pool over a specified period
235//
236// Parameters:
237// - poolPath: The path of the pool (e.g., "gno.land/r/demo/bar:gno.land/r/demo/foo:500")
238// - secondsAgo: Number of seconds ago to calculate TWAP from
239//
240// Returns TWAP tick and error
241func (i *poolV1) GetTWAP(poolPath string, secondsAgo uint32) (int32, *u256.Uint, error) {
242 pool, err := i.getPool(poolPath)
243 if err != nil {
244 return 0, nil, err
245 }
246
247 tick, liquidity, err := getTWAP(pool, secondsAgo)
248 if err != nil {
249 return 0, nil, err
250 }
251
252 return tick, liquidity, nil
253}
254
255// GetPoolCount returns the total number of pools.
256func (i *poolV1) GetPoolCount() int {
257 pools := i.store.GetPools()
258 return pools.Size()
259}
260
261// GetPoolPaths returns a paginated list of pool paths.
262func (i *poolV1) GetPoolPaths(offset, count int) []string {
263 pools := i.store.GetPools()
264 poolPaths := make([]string, 0)
265
266 pools.IterateByOffset(offset, count, func(key string, _ any) bool {
267 poolPaths = append(poolPaths, key)
268 return false
269 })
270
271 return poolPaths
272}
273
274// GetFeeAmountTickSpacings returns all fee tier to tick spacing mappings.
275func (i *poolV1) GetFeeAmountTickSpacings() map[uint32]int32 {
276 return i.store.GetFeeAmountTickSpacing()
277}
278
279// GetPoolPositionCount returns the number of positions in a pool.
280func (i *poolV1) GetPoolPositionCount(poolPath string) int {
281 pool, err := i.getPool(poolPath)
282 if err != nil {
283 return 0
284 }
285
286 return pool.Positions().Size()
287}
288
289// GetPoolPositionKeys returns a paginated list of position keys in a pool.
290func (i *poolV1) GetPoolPositionKeys(poolPath string, offset, count int) []string {
291 pool, err := i.getPool(poolPath)
292 if err != nil {
293 return []string{}
294 }
295
296 positionKeys := make([]string, 0)
297
298 pool.Positions().IterateByOffset(offset, count, func(key string, _ any) bool {
299 positionKeys = append(positionKeys, key)
300 return false
301 })
302
303 return positionKeys
304}
305
306// Tick enumeration
307
308// GetInitializedTicksInRange returns initialized ticks within the given range.
309func (i *poolV1) GetInitializedTicksInRange(poolPath string, tickLower, tickUpper int32) []int32 {
310 pool, err := i.getPool(poolPath)
311 if err != nil {
312 return []int32{}
313 }
314
315 ticks := make([]int32, 0)
316
317 pool.IterateTicks(tickLower, tickUpper, func(tick int32, _ pl.TickInfo) bool {
318 ticks = append(ticks, tick)
319 return false
320 })
321
322 return ticks
323}
324
325// Structure getters
326
327// GetTickInfo returns the tick info for a given tick.
328func (i *poolV1) GetTickInfo(poolPath string, tick int32) (pl.TickInfo, error) {
329 pool, err := i.getPool(poolPath)
330 if err != nil {
331 return pl.NewTickInfo(), err
332 }
333
334 return pool.GetTick(tick)
335}
336
337// GetTickBitmaps returns the tick bitmap for a given word position.
338func (i *poolV1) GetTickBitmaps(poolPath string, wordPos int16) (string, error) {
339 pool, err := i.getPool(poolPath)
340 if err != nil {
341 return "", err
342 }
343
344 tickBitmap, ok := pool.TickBitmaps()[wordPos]
345 if !ok {
346 return "", ufmt.Errorf("tick bitmap %d not found", wordPos)
347 }
348
349 return tickBitmap, nil
350}
351
352// GetPosition returns the position info for a given key.
353func (i *poolV1) GetPosition(poolPath, key string) (pl.PositionInfo, error) {
354 pool, err := i.getPool(poolPath)
355 if err != nil {
356 return pl.NewPositionInfo(), err
357 }
358
359 iPositionInfo := pool.Positions().Get(key)
360 if iPositionInfo == nil {
361 return pl.NewPositionInfo(), ufmt.Errorf("position %s not found", key)
362 }
363
364 positionInfo, ok := iPositionInfo.(pl.PositionInfo)
365 if !ok {
366 return pl.NewPositionInfo(), ufmt.Errorf("failed to cast positionInfo: %T", iPositionInfo)
367 }
368
369 return positionInfo, nil
370}
371
372// GetObservationState returns the observation state for a pool.
373func (i *poolV1) GetObservationState(poolPath string) (*pl.ObservationState, error) {
374 pool, err := i.getPool(poolPath)
375 if err != nil {
376 return nil, err
377 }
378
379 observationState := pool.ObservationState()
380 if observationState == nil {
381 return nil, ufmt.Errorf("observation state not found for pool %s", poolPath)
382 }
383
384 return observationState, nil
385}