-
Notifications
You must be signed in to change notification settings - Fork 558
/
tick.go
443 lines (368 loc) · 19.6 KB
/
tick.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package concentrated_liquidity
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
db "github.com/tendermint/tm-db"
"github.com/osmosis-labs/osmosis/osmoutils"
"github.com/osmosis-labs/osmosis/osmoutils/accum"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/client/queryproto"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/math"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/model"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/swapstrategy"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
"github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types/genesis"
)
// initOrUpdateTick retrieves the tickInfo from the specified tickIndex and updates both the liquidityNet and LiquidityGross.
// The given currentTick value is used to determine the strategy for updating the spread factor accumulator.
// We update the tick's spread reward growth opposite direction of last traversal accumulator to the spread reward growth global when tick index is <= current tick.
// Otherwise, it is set to zero.
// Note that liquidityDelta can be either positive or negative depending on whether we are adding or removing liquidity.
// if we are initializing or updating an upper tick, we subtract the liquidityIn from the LiquidityNet
// if we are initializing or updating a lower tick, we add the liquidityIn from the LiquidityNet
// WARNING: this method may mutate the pool, make sure to refetch the pool after calling this method.
func (k Keeper) initOrUpdateTick(ctx sdk.Context, poolId uint64, currentTick int64, tickIndex int64, liquidityDelta sdk.Dec, upper bool) (err error) {
tickInfo, err := k.GetTickInfo(ctx, poolId, tickIndex)
if err != nil {
return err
}
// If both liquidity fields are zero, we consume the base gas spread factor for initializing a tick.
if tickInfo.LiquidityGross.IsZero() && tickInfo.LiquidityNet.IsZero() {
ctx.GasMeter().ConsumeGas(uint64(types.BaseGasFeeForInitializingTick), "initialize tick gas spread factor")
}
// calculate liquidityGross, which does not care about whether liquidityIn is positive or negative
liquidityBefore := tickInfo.LiquidityGross
// if given tickIndex is LTE to the current tick and the liquidityBefore is zero,
// set the tick's spread reward growth opposite direction of last traversal to the spread factor accumulator's value
if liquidityBefore.IsZero() {
if tickIndex <= currentTick {
accum, err := k.GetSpreadRewardAccumulator(ctx, poolId)
if err != nil {
return err
}
tickInfo.SpreadRewardGrowthOppositeDirectionOfLastTraversal = accum.GetValue()
}
}
// note that liquidityIn can be either positive or negative.
// If negative, this would work as a subtraction from liquidityBefore
liquidityAfter := liquidityBefore.Add(liquidityDelta)
tickInfo.LiquidityGross = liquidityAfter
// calculate liquidityNet, which we take into account and track depending on whether liquidityIn is positive or negative
if upper {
tickInfo.LiquidityNet = tickInfo.LiquidityNet.Sub(liquidityDelta)
} else {
tickInfo.LiquidityNet = tickInfo.LiquidityNet.Add(liquidityDelta)
}
k.SetTickInfo(ctx, poolId, tickIndex, &tickInfo)
return nil
}
// crossTick crosses the given tick. The tick is specified by its index and tick info.
// It updates the given tick's uptime and spread reward accumulators and writes it back to state.
// Prior to updating the tick info and writing it to state, it updates the pool uptime accumulators until the current block time.
// WARNING: this method may mutate the pool, make sure to refetch the pool after calling this method.
// CONTRACT: the caller validates that the pool with the given id exists.
// CONTRACT: caller is responsible for the uptimeAccums to be up-to-date.
// CONTRACT: uptimeAccums are associated with the given pool id.
func (k Keeper) crossTick(ctx sdk.Context, poolId uint64, tickIndex int64, tickInfo *model.TickInfo, swapStateSpreadRewardGrowth sdk.DecCoin, spreadRewardAccumValue sdk.DecCoins, uptimeAccums []accum.AccumulatorObject) (liquidityDelta sdk.Dec, err error) {
if tickInfo == nil {
return sdk.Dec{}, types.ErrNextTickInfoNil
}
// subtract tick's spread reward growth opposite direction of last traversal from current spread reward growth global, including the spread reward growth of the current swap.
tickInfo.SpreadRewardGrowthOppositeDirectionOfLastTraversal = spreadRewardAccumValue.Add(swapStateSpreadRewardGrowth).Sub(tickInfo.SpreadRewardGrowthOppositeDirectionOfLastTraversal)
// For each supported uptime, subtract tick's uptime growth outside from the respective uptime accumulator
// This is functionally equivalent to "flipping" the trackers once the tick is crossed
updatedUptimeTrackers := tickInfo.UptimeTrackers.List
for uptimeId := range uptimeAccums {
updatedUptimeTrackers[uptimeId].UptimeGrowthOutside = uptimeAccums[uptimeId].GetValue().Sub(updatedUptimeTrackers[uptimeId].UptimeGrowthOutside)
}
k.SetTickInfo(ctx, poolId, tickIndex, tickInfo)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtCrossTick,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(poolId, 10)),
sdk.NewAttribute(types.AttributeKeyTickIndex, strconv.FormatInt(tickIndex, 10)),
sdk.NewAttribute(types.AttributeKeySpreadRewardGrowthOppositeDirectionOfLastTraversal, tickInfo.SpreadRewardGrowthOppositeDirectionOfLastTraversal.String()),
sdk.NewAttribute(types.AttributeKeyUptimeGrowthOppositeDirectionOfLastTraversal, tickInfo.UptimeTrackers.String()),
),
})
return tickInfo.LiquidityNet, nil
}
// GetTickInfo gets the tickInfo given a poolId and tickIndex. If the tick has not been initialized, it will initialize it.
// If the tick has been initialized, it will return the tickInfo. If the pool does not exist, it will return an error.
// CONTRACT: The caller must check that the pool with given id exists.
// WARNING: this method may mutate the pool, make sure to refetch the pool after calling this method.
func (k Keeper) GetTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64) (tickInfo model.TickInfo, err error) {
store := ctx.KVStore(k.storeKey)
tickStruct := model.TickInfo{}
key := types.KeyTick(poolId, tickIndex)
found, err := osmoutils.Get(store, key, &tickStruct)
// return 0 values if key has not been initialized
if !found {
// If tick has not yet been initialized, we create a new one and initialize
// the spread reward growth opposite direction of last traversal value.
initialSpreadRewardGrowthOppositeDirectionOfLastTraversal, err := k.getInitialSpreadRewardGrowthOppositeDirectionOfLastTraversalForTick(ctx, poolId, tickIndex)
if err != nil {
return tickStruct, err
}
// Sync global uptime accumulators to ensure the uptime tracker init values are up to date.
if err := k.updatePoolUptimeAccumulatorsToNow(ctx, poolId); err != nil {
return tickStruct, err
}
// Initialize uptime trackers for the new tick to the appropriate starting values.
valuesToAdd, err := k.getInitialUptimeGrowthOppositeDirectionOfLastTraversalForTick(ctx, poolId, tickIndex)
if err != nil {
return tickStruct, err
}
initialUptimeTrackers := []model.UptimeTracker{}
for _, uptimeTrackerValue := range valuesToAdd {
initialUptimeTrackers = append(initialUptimeTrackers, model.UptimeTracker{UptimeGrowthOutside: uptimeTrackerValue})
}
return model.TickInfo{LiquidityGross: sdk.ZeroDec(), LiquidityNet: sdk.ZeroDec(), SpreadRewardGrowthOppositeDirectionOfLastTraversal: initialSpreadRewardGrowthOppositeDirectionOfLastTraversal, UptimeTrackers: model.UptimeTrackers{List: initialUptimeTrackers}}, nil
}
if err != nil {
return tickStruct, err
}
return tickStruct, nil
}
func (k Keeper) SetTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64, tickInfo *model.TickInfo) {
store := ctx.KVStore(k.storeKey)
key := types.KeyTick(poolId, tickIndex)
osmoutils.MustSet(store, key, tickInfo)
}
func (k Keeper) GetAllInitializedTicksForPool(ctx sdk.Context, poolId uint64) ([]genesis.FullTick, error) {
return osmoutils.GatherValuesFromStorePrefixWithKeyParser(ctx.KVStore(k.storeKey), types.KeyTickPrefixByPoolId(poolId), ParseFullTickFromBytes)
}
// validateTickInRangeIsValid validates that given ticks are valid. That is:
// - both lower and upper ticks are divisible by the tick spacing
// - both lower and upper ticks are within MinTick and MaxTick range
// - lower tick must be less than upper tick.
//
// Returns error if validation fails. Otherwise, nil.
func validateTickRangeIsValid(tickSpacing uint64, lowerTick int64, upperTick int64) error {
// Check if the lower and upper tick values are divisible by the tick spacing.
if lowerTick%int64(tickSpacing) != 0 || upperTick%int64(tickSpacing) != 0 {
return types.TickSpacingError{LowerTick: lowerTick, UpperTick: upperTick, TickSpacing: tickSpacing}
}
// Check if the lower tick value is within the valid range of MinTick to MaxTick.
if lowerTick < types.MinTick || lowerTick >= types.MaxTick {
return types.InvalidTickError{Tick: lowerTick, IsLower: true, MinTick: types.MinTick, MaxTick: types.MaxTick}
}
// Check if the upper tick value is within the valid range of MinTick to MaxTick.
if upperTick > types.MaxTick || upperTick <= types.MinTick {
return types.InvalidTickError{Tick: upperTick, IsLower: false, MinTick: types.MinTick, MaxTick: types.MaxTick}
}
// Check if the lower tick value is greater than or equal to the upper tick value.
if lowerTick >= upperTick {
return types.InvalidLowerUpperTickError{LowerTick: lowerTick, UpperTick: upperTick}
}
return nil
}
// roundTickToCanonicalPriceTick takes a tick and determines if multiple ticks can represent the same price as the provided tick. If so, it
// rounds that tick up to the largest tick that can represent the same price that the original tick corresponded to. If one of
// the two ticks happen to be rounded, we re-validate the tick range to ensure that the tick range is still valid.
//
// i.e. the provided tick is -161795100. With our precision, this tick correlates to a sqrtPrice of 0.000000001414213563
// the first tick (given our precision) that is able to represent this price is -161000000, so we use this tick instead.
//
// This really only applies to very small tick values, as the increment of a single tick continues to get smaller as the tick value gets smaller.
func roundTickToCanonicalPriceTick(lowerTick, upperTick int64, priceTickLower, priceTickUpper sdk.Dec, tickSpacing uint64) (int64, int64, error) {
newLowerTick, err := math.PriceToTickRoundDown(priceTickLower, tickSpacing)
if err != nil {
return 0, 0, err
}
newUpperTick, err := math.PriceToTickRoundDown(priceTickUpper, tickSpacing)
if err != nil {
return 0, 0, err
}
// If the lower or upper tick has changed, we need to re-validate the tick range.
if lowerTick != newLowerTick || upperTick != newUpperTick {
err := validateTickRangeIsValid(tickSpacing, newLowerTick, newUpperTick)
if err != nil {
return 0, 0, err
}
}
return newLowerTick, newUpperTick, nil
}
// GetTickLiquidityForFullRange returns an array of liquidity depth for all ticks existing from min tick ~ max tick.
func (k Keeper) GetTickLiquidityForFullRange(ctx sdk.Context, poolId uint64) ([]queryproto.LiquidityDepthWithRange, error) {
pool, err := k.getPoolById(ctx, poolId)
if err != nil {
return []queryproto.LiquidityDepthWithRange{}, err
}
// use false for zeroForOne since we're going from lower tick -> upper tick
zeroForOne := false
swapStrategy := swapstrategy.New(zeroForOne, sdk.ZeroDec(), k.storeKey, sdk.ZeroDec(), pool.GetTickSpacing())
// set current tick to min tick, and find the first initialized tick starting from min tick -1.
// we do -1 to make min tick inclusive.
currentTick := types.MinTick - 1
nextTickIter := swapStrategy.InitializeNextTickIterator(ctx, poolId, currentTick)
defer nextTickIter.Close()
if !nextTickIter.Valid() {
return []queryproto.LiquidityDepthWithRange{}, types.RanOutOfTicksForPoolError{PoolId: poolId}
}
nextTick, err := types.TickIndexFromBytes(nextTickIter.Key())
if err != nil {
return []queryproto.LiquidityDepthWithRange{}, err
}
tick, err := k.getTickByTickIndex(ctx, poolId, nextTick)
if err != nil {
return []queryproto.LiquidityDepthWithRange{}, err
}
liquidityDepthsForRange := []queryproto.LiquidityDepthWithRange{}
// use the smallest tick initialized as the starting point for calculating liquidity.
currentLiquidity := tick.LiquidityNet
currentTick = nextTick
totalLiquidityWithinRange := currentLiquidity
previousTickIndex := currentTick
// start from the next index so that the current tick can become lower tick.
nextTickIter.Next()
for ; nextTickIter.Valid(); nextTickIter.Next() {
tickIndex, err := types.TickIndexFromBytes(nextTickIter.Key())
if err != nil {
return []queryproto.LiquidityDepthWithRange{}, err
}
tickStruct, err := ParseTickFromBz(nextTickIter.Value())
if err != nil {
return []queryproto.LiquidityDepthWithRange{}, err
}
liquidityDepthForRange := queryproto.LiquidityDepthWithRange{
LowerTick: previousTickIndex,
UpperTick: tickIndex,
LiquidityAmount: totalLiquidityWithinRange,
}
liquidityDepthsForRange = append(liquidityDepthsForRange, liquidityDepthForRange)
currentLiquidity = tickStruct.LiquidityNet
previousTickIndex = tickIndex
totalLiquidityWithinRange = totalLiquidityWithinRange.Add(currentLiquidity)
}
return liquidityDepthsForRange, nil
}
// GetLiquidityNetInDirection is a method that returns an array of TickLiquidityNet objects representing the net liquidity in a specified direction
// for a given pool. It provides an option to specify the bounds with start tick and bound tick.
// Swap direction is determined by the token in given (zero for one vs one for zero).
// See the swapstrategy package documentation for more details.
// Both start tick and bound tick must be in the appropriate range relative to the current tick and the min/max tick
// depending on the swap strategy and as defined by ValidateSqrtPrice method of the strategy.
// Parameters:
// * ctx (sdk.Context): The context for the SDK.
// * poolId (uint64): The ID of the pool for which the liquidity needs to be checked.
// * tokenIn (string): The token denom that determines the swap direction and strategy.
// * userGivenStartTick (sdk.Int): The starting tick for grabbing liquidities. If not provided, the current tick of the pool is used.
// * boundTick (sdk.Int): An optional bound tick to limit the range of the queryproto. If not provided, the minimum or maximum tick will be used, depending on the strategy.
//
// Returns:
// ([]queryproto.TickLiquidityNet): An array of TickLiquidityNet objects representing the net liquidity in the specified direction.
//
// Note that the start tick is never included if given. The same goes for the current tick.
// Returns liquidity net amounts starting from the next tick relative to start/current tick
//
// (error): An error if any issue occurs during the operation.
// Errors:
// * types.PoolNotFoundError: If the given pool does not exist.
// * types.TokenInDenomNotInPoolError: If the given tokenIn is not an asset in the pool.
func (k Keeper) GetTickLiquidityNetInDirection(ctx sdk.Context, poolId uint64, tokenIn string, userGivenStartTick sdk.Int, boundTick sdk.Int) ([]queryproto.TickLiquidityNet, error) {
// get min and max tick for the pool
p, err := k.getPoolById(ctx, poolId)
if err != nil {
return []queryproto.TickLiquidityNet{}, err
}
ctx.Logger().Debug(fmt.Sprintf("userGivenStartTick %s, boundTick %s, currentTick %d\n", userGivenStartTick, boundTick, p.GetCurrentTick()))
startTick := p.GetCurrentTick()
// If start tick is set, use it as the current tick for grabbing liquidities from.
if !userGivenStartTick.IsNil() {
startTick = userGivenStartTick.Int64()
ctx.Logger().Debug(fmt.Sprintf("startTick %d set to user given\n", startTick))
}
// sanity check that given tokenIn is an asset in pool.
if tokenIn != p.GetToken0() && tokenIn != p.GetToken1() {
return []queryproto.TickLiquidityNet{}, types.TokenInDenomNotInPoolError{TokenInDenom: tokenIn}
}
// figure out zero for one depending on the token in.
zeroForOne := p.GetToken0() == tokenIn
ctx.Logger().Debug(fmt.Sprintf("is_zero_for_one %t\n", zeroForOne))
// use max or min tick if provided bound is nil
ctx.Logger().Debug(fmt.Sprintf("min_tick %d\n", types.MinTick))
ctx.Logger().Debug(fmt.Sprintf("max_tick %d\n", types.MaxTick))
if boundTick.IsNil() {
if zeroForOne {
boundTick = sdk.NewInt(types.MinTick)
} else {
boundTick = sdk.NewInt(types.MaxTick)
}
}
liquidityDepths := []queryproto.TickLiquidityNet{}
swapStrategy := swapstrategy.New(zeroForOne, sdk.ZeroDec(), k.storeKey, sdk.ZeroDec(), p.GetTickSpacing())
currentTick := p.GetCurrentTick()
_, currentTickSqrtPrice, err := math.TickToSqrtPrice(currentTick)
if err != nil {
return []queryproto.TickLiquidityNet{}, err
}
ctx.Logger().Debug(fmt.Sprintf("currentTick %d; current tick's sqrt price%s\n", currentTick, currentTickSqrtPrice))
// function to validate that start tick and bound tick are
// between current tick and the min/max tick depending on the swap direction.
validateTickIsInValidRange := func(validateTick sdk.Int) error {
_, validateSqrtPrice, err := math.TickToSqrtPrice(validateTick.Int64())
if err != nil {
return err
}
ctx.Logger().Debug(fmt.Sprintf("validateTick %s; validate sqrtPrice %s\n", validateTick.String(), validateSqrtPrice.String()))
if err := swapStrategy.ValidateSqrtPrice(validateSqrtPrice, currentTickSqrtPrice); err != nil {
return err
}
return nil
}
ctx.Logger().Debug("validating bound tick")
if err := validateTickIsInValidRange(boundTick); err != nil {
return []queryproto.TickLiquidityNet{}, fmt.Errorf("failed validating bound tick (%s) with current sqrt price of (%s): %w", boundTick, currentTickSqrtPrice, err)
}
ctx.Logger().Debug("validating start tick")
if err := validateTickIsInValidRange(sdk.NewInt(startTick)); err != nil {
return []queryproto.TickLiquidityNet{}, fmt.Errorf("failed validating start tick (%d) with current sqrt price of (%s): %w", startTick, currentTickSqrtPrice, err)
}
// iterator assignments
store := ctx.KVStore(k.storeKey)
prefixBz := types.KeyTickPrefixByPoolId(poolId)
prefixStore := prefix.NewStore(store, prefixBz)
startTickKey := types.TickIndexToBytes(startTick)
boundTickKey := types.TickIndexToBytes(boundTick.Int64())
// define iterator depending on swap strategy
var iterator db.Iterator
if zeroForOne {
iterator = prefixStore.ReverseIterator(boundTickKey, startTickKey)
} else {
iterator = prefixStore.Iterator(startTickKey, storetypes.InclusiveEndBytes(boundTickKey))
}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
tickIndex, err := types.TickIndexFromBytes(iterator.Key())
if err != nil {
return []queryproto.TickLiquidityNet{}, err
}
tickStruct, err := ParseTickFromBz(iterator.Value())
if err != nil {
return []queryproto.TickLiquidityNet{}, err
}
liquidityDepth := queryproto.TickLiquidityNet{
LiquidityNet: tickStruct.LiquidityNet,
TickIndex: tickIndex,
}
liquidityDepths = append(liquidityDepths, liquidityDepth)
}
return liquidityDepths, nil
}
func (k Keeper) getTickByTickIndex(ctx sdk.Context, poolId uint64, tickIndex int64) (model.TickInfo, error) {
store := ctx.KVStore(k.storeKey)
keyTick := types.KeyTick(poolId, tickIndex)
tickStruct := model.TickInfo{}
found, err := osmoutils.Get(store, keyTick, &tickStruct)
if err != nil {
return model.TickInfo{}, err
}
if !found {
return model.TickInfo{}, types.TickNotFoundError{Tick: tickIndex}
}
return tickStruct, nil
}