Search Apps Documentation Source Content File Folder Download Copy Actions Download

getter.gno

10.64 Kb · 387 lines
  1package v1
  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, exist := positions.Get(key)
 97	if !exist {
 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) GetObservation(poolPath string, secondsAgo int64) (
203	tickCumulative int64,
204	liquidityCumulative,
205	secondsPerLiquidityCumulativeX128 string,
206	blockTimestamp int64,
207) {
208	pool := i.mustGetPool(poolPath)
209	observationState := pool.ObservationState()
210
211	if observationState == nil {
212		return 0, "0", "0", 0
213	}
214
215	lastObservation, err := lastObservation(observationState)
216	if err != nil {
217		return 0, "0", "0", 0
218	}
219
220	tickCumulative = lastObservation.TickCumulative()
221	liquidityCumulative = lastObservation.LiquidityCumulative()
222	secondsPerLiquidityCumulativeX128 = lastObservation.SecondsPerLiquidityCumulativeX128()
223	blockTimestamp = lastObservation.BlockTimestamp()
224
225	return
226}
227
228func (i *poolV1) GetPoolCreationFee() int64 {
229	return i.store.GetPoolCreationFee()
230}
231
232func (i *poolV1) GetWithdrawalFee() uint64 {
233	return i.store.GetWithdrawalFeeBPS()
234}
235
236// GetTWAP returns the time-weighted average price for a pool over a specified period
237//
238// Parameters:
239//   - poolPath: The path of the pool (e.g., "gno.land/r/demo/bar:gno.land/r/demo/foo:500")
240//   - secondsAgo: Number of seconds ago to calculate TWAP from
241//
242// Returns TWAP tick and error
243func (i *poolV1) GetTWAP(poolPath string, secondsAgo uint32) (int32, *u256.Uint, error) {
244	pool, err := i.getPool(poolPath)
245	if err != nil {
246		return 0, nil, err
247	}
248
249	tick, liquidity, err := getTWAP(pool, secondsAgo)
250	if err != nil {
251		return 0, nil, err
252	}
253
254	return tick, liquidity, nil
255}
256
257// GetPoolCount returns the total number of pools.
258func (i *poolV1) GetPoolCount() int {
259	pools := i.store.GetPools()
260	return pools.Size()
261}
262
263// GetPoolPaths returns a paginated list of pool paths.
264func (i *poolV1) GetPoolPaths(offset, count int) []string {
265	pools := i.store.GetPools()
266	poolPaths := make([]string, 0)
267
268	pools.IterateByOffset(offset, count, func(key string, _ any) bool {
269		poolPaths = append(poolPaths, key)
270		return false
271	})
272
273	return poolPaths
274}
275
276// GetFeeAmountTickSpacings returns all fee tier to tick spacing mappings.
277func (i *poolV1) GetFeeAmountTickSpacings() map[uint32]int32 {
278	return i.store.GetFeeAmountTickSpacing()
279}
280
281// GetPoolPositionCount returns the number of positions in a pool.
282func (i *poolV1) GetPoolPositionCount(poolPath string) int {
283	pool, err := i.getPool(poolPath)
284	if err != nil {
285		return 0
286	}
287
288	return pool.Positions().Size()
289}
290
291// GetPoolPositionKeys returns a paginated list of position keys in a pool.
292func (i *poolV1) GetPoolPositionKeys(poolPath string, offset, count int) []string {
293	pool, err := i.getPool(poolPath)
294	if err != nil {
295		return []string{}
296	}
297
298	positionKeys := make([]string, 0)
299
300	pool.Positions().IterateByOffset(offset, count, func(key string, _ any) bool {
301		positionKeys = append(positionKeys, key)
302		return false
303	})
304
305	return positionKeys
306}
307
308// Tick enumeration
309
310// GetInitializedTicksInRange returns initialized ticks within the given range.
311func (i *poolV1) GetInitializedTicksInRange(poolPath string, tickLower, tickUpper int32) []int32 {
312	pool, err := i.getPool(poolPath)
313	if err != nil {
314		return []int32{}
315	}
316
317	ticks := make([]int32, 0)
318
319	pool.IterateTicks(tickLower, tickUpper, func(tick int32, _ pl.TickInfo) bool {
320		ticks = append(ticks, tick)
321		return false
322	})
323
324	return ticks
325}
326
327// Structure getters
328
329// GetTickInfo returns the tick info for a given tick.
330func (i *poolV1) GetTickInfo(poolPath string, tick int32) (pl.TickInfo, error) {
331	pool, err := i.getPool(poolPath)
332	if err != nil {
333		return pl.NewTickInfo(), err
334	}
335
336	return pool.GetTick(tick)
337}
338
339// GetTickBitmaps returns the tick bitmap for a given word position.
340func (i *poolV1) GetTickBitmaps(poolPath string, wordPos int16) (string, error) {
341	pool, err := i.getPool(poolPath)
342	if err != nil {
343		return "", err
344	}
345
346	tickBitmap, ok := pool.TickBitmaps()[wordPos]
347	if !ok {
348		return "", ufmt.Errorf("tick bitmap %d not found", wordPos)
349	}
350
351	return tickBitmap, nil
352}
353
354// GetPosition returns the position info for a given key.
355func (i *poolV1) GetPosition(poolPath, key string) (pl.PositionInfo, error) {
356	pool, err := i.getPool(poolPath)
357	if err != nil {
358		return pl.NewPositionInfo(), err
359	}
360
361	iPositionInfo, ok := pool.Positions().Get(key)
362	if !ok {
363		return pl.NewPositionInfo(), ufmt.Errorf("position %s not found", key)
364	}
365
366	positionInfo, ok := iPositionInfo.(pl.PositionInfo)
367	if !ok {
368		return pl.NewPositionInfo(), ufmt.Errorf("failed to cast positionInfo: %T", iPositionInfo)
369	}
370
371	return positionInfo, nil
372}
373
374// GetObservationState returns the observation state for a pool.
375func (i *poolV1) GetObservationState(poolPath string) (*pl.ObservationState, error) {
376	pool, err := i.getPool(poolPath)
377	if err != nil {
378		return nil, err
379	}
380
381	observationState := pool.ObservationState()
382	if observationState == nil {
383		return nil, ufmt.Errorf("observation state not found for pool %s", poolPath)
384	}
385
386	return observationState, nil
387}