/
position.go
165 lines (139 loc) · 5.76 KB
/
position.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
package concentrated_liquidity
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/merlinslair/merlin/osmoutils"
"github.com/merlinslair/merlin/osmoutils/accum"
"github.com/merlinslair/merlin/x/concentrated-liquidity/model"
types "github.com/merlinslair/merlin/x/concentrated-liquidity/types"
)
var emptyOptions = &accum.Options{}
// getOrInitPosition retrieves the position for the given tick range. If it doesn't exist, it returns an initialized position with zero liquidity.
func (k Keeper) getOrInitPosition(
ctx sdk.Context,
poolId uint64,
owner sdk.AccAddress,
lowerTick, upperTick int64,
frozenUntil time.Time,
) (*model.Position, error) {
if !k.poolExists(ctx, poolId) {
return nil, types.PoolNotFoundError{PoolId: poolId}
}
if k.hasFullPosition(ctx, poolId, owner, lowerTick, upperTick, frozenUntil) {
position, err := k.GetPosition(ctx, poolId, owner, lowerTick, upperTick, frozenUntil)
if err != nil {
return nil, err
}
return position, nil
}
return &model.Position{Liquidity: sdk.ZeroDec()}, nil
}
// initOrUpdatePosition checks to see if the specified owner has an existing position at the given tick range.
// If a position is not present, it initializes the position with the provided liquidity delta.
// If a position is present, it combines the existing liquidity in that position with the provided liquidity delta.
func (k Keeper) initOrUpdatePosition(
ctx sdk.Context,
poolId uint64,
owner sdk.AccAddress,
lowerTick, upperTick int64,
liquidityDelta sdk.Dec,
frozenUntil time.Time,
) (err error) {
position, err := k.getOrInitPosition(ctx, poolId, owner, lowerTick, upperTick, frozenUntil)
if err != nil {
return err
}
liquidityBefore := position.Liquidity
// note that liquidityIn can be either positive or negative.
// If negative, this would work as a subtraction from liquidityBefore
liquidityAfter := liquidityBefore.Add(liquidityDelta)
if liquidityAfter.IsNegative() {
return types.NegativeLiquidityError{Liquidity: liquidityAfter}
}
position.Liquidity = liquidityAfter
position.FrozenUntil = frozenUntil
// TODO: consider deleting position if liquidity becomes zero
// Create records for relevant uptime accumulators here.
uptimeAccumulators, err := k.getUptimeAccumulators(ctx, poolId)
if err != nil {
return err
}
for uptimeIndex, uptime := range types.SupportedUptimes {
// We assume every position update requires the position to be frozen for the
// min uptime again. Thus, the difference between the position's `FrozenUntil`
// and the blocktime when the update happens should be greater than or equal
// to the required uptime.
if position.FrozenUntil.Sub(ctx.BlockTime()) >= uptime {
curUptimeAccum := uptimeAccumulators[uptimeIndex]
// If a record does not exist for this uptime accumulator, create a new position.
// Otherwise, add to existing record.
positionName := string(types.KeyFullPosition(poolId, owner, lowerTick, upperTick, frozenUntil))
recordExists, err := curUptimeAccum.HasPosition(positionName)
if err != nil {
return err
}
if !recordExists {
err = curUptimeAccum.NewPosition(positionName, position.Liquidity, emptyOptions)
} else if !liquidityDelta.IsNegative() {
err = curUptimeAccum.AddToPosition(positionName, liquidityDelta)
} else {
err = curUptimeAccum.RemoveFromPosition(positionName, liquidityDelta.Neg())
}
if err != nil {
return err
}
}
}
k.setPosition(ctx, poolId, owner, lowerTick, upperTick, position, frozenUntil)
return nil
}
func (k Keeper) hasFullPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, lowerTick, upperTick int64, frozenUntil time.Time) bool {
store := ctx.KVStore(k.storeKey)
key := types.KeyFullPosition(poolId, owner, lowerTick, upperTick, frozenUntil)
return store.Has(key)
}
// GetPosition checks if a position exists at the provided upper and lower ticks and frozenUntil time for the given owner. Returns position if found.
func (k Keeper) GetPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, lowerTick, upperTick int64, frozenUntil time.Time) (*model.Position, error) {
store := ctx.KVStore(k.storeKey)
positionStruct := &model.Position{}
key := types.KeyFullPosition(poolId, owner, lowerTick, upperTick, frozenUntil)
found, err := osmoutils.Get(store, key, positionStruct)
if err != nil {
return nil, err
}
if !found {
return nil, types.PositionNotFoundError{PoolId: poolId, LowerTick: lowerTick, UpperTick: upperTick, FrozenUntil: frozenUntil}
}
return positionStruct, nil
}
// GetUserPositions gets all the existing user positions across many pools.
func (k Keeper) GetUserPositions(ctx sdk.Context, addr sdk.AccAddress) ([]types.FullPositionByOwnerResult, error) {
return osmoutils.GatherValuesFromStorePrefixWithKeyParser(ctx.KVStore(k.storeKey), types.KeyUserPositions(addr), ParseFullPositionFromBytes)
}
// ParsePositionFromBz parses bytes into a position struct. Returns a parsed position and nil on success.
// Returns error if bytes length is zero or if fails to parse the given bytes into the position struct.
func (k Keeper) setPosition(ctx sdk.Context,
poolId uint64,
owner sdk.AccAddress,
lowerTick, upperTick int64,
position *model.Position,
frozenUntil time.Time,
) {
store := ctx.KVStore(k.storeKey)
key := types.KeyFullPosition(poolId, owner, lowerTick, upperTick, frozenUntil)
osmoutils.MustSet(store, key, position)
}
func (k Keeper) deletePosition(ctx sdk.Context,
poolId uint64,
owner sdk.AccAddress,
lowerTick, upperTick int64,
frozenUntil time.Time,
) error {
store := ctx.KVStore(k.storeKey)
key := types.KeyFullPosition(poolId, owner, lowerTick, upperTick, frozenUntil)
if !store.Has(key) {
return types.PositionNotFoundError{PoolId: poolId, LowerTick: lowerTick, UpperTick: upperTick, FrozenUntil: frozenUntil}
}
store.Delete(key)
return nil
}