forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
386 lines (340 loc) · 15 KB
/
lock.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
package keeper
import (
"encoding/json"
"fmt"
"time"
"github.com/MonOsmosis/osmosis/v3/store"
"github.com/MonOsmosis/osmosis/v3/x/lockup/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
db "github.com/tendermint/tm-db"
)
func (k Keeper) getLocksFromIterator(ctx sdk.Context, iterator db.Iterator) []types.PeriodLock {
locks := []types.PeriodLock{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
lockIDs := []uint64{}
err := json.Unmarshal(iterator.Value(), &lockIDs)
if err != nil {
panic(err)
}
for _, lockID := range lockIDs {
lock, err := k.GetLockByID(ctx, lockID)
if err != nil {
panic(err)
}
locks = append(locks, *lock)
}
}
return locks
}
func (k Keeper) beginUnlockFromIterator(ctx sdk.Context, iterator db.Iterator) ([]types.PeriodLock, sdk.Coins, error) {
coins := sdk.Coins{}
locks := k.getLocksFromIterator(ctx, iterator)
for _, lock := range locks {
err := k.BeginUnlock(ctx, lock)
if err != nil {
return locks, coins, err
}
// sum up all coins begin unlocking
coins = coins.Add(lock.Coins...)
}
return locks, coins, nil
}
func (k Keeper) unlockFromIterator(ctx sdk.Context, iterator db.Iterator) ([]types.PeriodLock, sdk.Coins) {
coins := sdk.Coins{}
locks := k.getLocksFromIterator(ctx, iterator)
for _, lock := range locks {
err := k.Unlock(ctx, lock)
if err != nil {
panic(err)
}
// sum up all coins unlocked
coins = coins.Add(lock.Coins...)
}
return locks, coins
}
func (k Keeper) getCoinsFromLocks(locks []types.PeriodLock) sdk.Coins {
coins := sdk.Coins{}
for _, lock := range locks {
coins = coins.Add(lock.Coins...)
}
return coins
}
func (k Keeper) getCoinsFromIterator(ctx sdk.Context, iterator db.Iterator) sdk.Coins {
return k.getCoinsFromLocks(k.getLocksFromIterator(ctx, iterator))
}
func (k Keeper) accumulationStore(ctx sdk.Context, denom string) store.Tree {
return store.NewTree(prefix.NewStore(ctx.KVStore(k.storeKey), accumulationStorePrefix(denom)), 10)
}
// GetModuleBalance Returns full balance of the module
func (k Keeper) GetModuleBalance(ctx sdk.Context) sdk.Coins {
// TODO: should add invariant test for module balance and lock items
acc := k.ak.GetModuleAccount(ctx, types.ModuleName)
return k.bk.GetAllBalances(ctx, acc.GetAddress())
}
// GetModuleLockedCoins Returns locked balance of the module
func (k Keeper) GetModuleLockedCoins(ctx sdk.Context) sdk.Coins {
// all not unlocking + not finished unlocking
notUnlockingCoins := k.getCoinsFromIterator(ctx, k.LockIterator(ctx, false))
unlockingCoins := k.getCoinsFromIterator(ctx, k.LockIteratorAfterTime(ctx, true, ctx.BlockTime()))
return notUnlockingCoins.Add(unlockingCoins...)
}
// GetAccountUnlockableCoins Returns whole unlockable coins which are not withdrawn yet
func (k Keeper) GetAccountUnlockableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return k.getCoinsFromIterator(ctx, k.AccountLockIteratorBeforeTime(ctx, true, addr, ctx.BlockTime()))
}
// GetAccountUnlockingCoins Returns whole unlocking coins
func (k Keeper) GetAccountUnlockingCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return k.getCoinsFromIterator(ctx, k.AccountLockIteratorAfterTime(ctx, true, addr, ctx.BlockTime()))
}
// GetAccountLockedCoins Returns a locked coins that can't be withdrawn
func (k Keeper) GetAccountLockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
// all account unlocking + not finished unlocking
notUnlockingCoins := k.getCoinsFromIterator(ctx, k.AccountLockIterator(ctx, false, addr))
unlockingCoins := k.getCoinsFromIterator(ctx, k.AccountLockIteratorAfterTime(ctx, true, addr, ctx.BlockTime()))
return notUnlockingCoins.Add(unlockingCoins...)
}
// GetAccountLockedPastTime Returns the total locks of an account whose unlock time is beyond timestamp
func (k Keeper) GetAccountLockedPastTime(ctx sdk.Context, addr sdk.AccAddress, timestamp time.Time) []types.PeriodLock {
// unlockings finish after specific time + not started locks that will finish after the time even though it start now
unlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorAfterTime(ctx, true, addr, timestamp))
duration := time.Duration(0)
if timestamp.After(ctx.BlockTime()) {
duration = timestamp.Sub(ctx.BlockTime())
}
notUnlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDuration(ctx, false, addr, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetAccountLockedPastTimeNotUnlockingOnly Returns the total locks of an account whose unlock time is beyond timestamp
func (k Keeper) GetAccountLockedPastTimeNotUnlockingOnly(ctx sdk.Context, addr sdk.AccAddress, timestamp time.Time) []types.PeriodLock {
duration := time.Duration(0)
if timestamp.After(ctx.BlockTime()) {
duration = timestamp.Sub(ctx.BlockTime())
}
return k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDuration(ctx, false, addr, duration))
}
// GetAccountUnlockedBeforeTime Returns the total unlocks of an account whose unlock time is before timestamp
func (k Keeper) GetAccountUnlockedBeforeTime(ctx sdk.Context, addr sdk.AccAddress, timestamp time.Time) []types.PeriodLock {
// unlockings finish before specific time + not started locks that can finish before the time if start now
unlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorBeforeTime(ctx, true, addr, timestamp))
if timestamp.Before(ctx.BlockTime()) {
return unlockings
}
duration := timestamp.Sub(ctx.BlockTime())
notUnlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorShorterThanDuration(ctx, false, addr, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetAccountLockedPastTimeDenom is equal to GetAccountLockedPastTime but denom specific
func (k Keeper) GetAccountLockedPastTimeDenom(ctx sdk.Context, addr sdk.AccAddress, denom string, timestamp time.Time) []types.PeriodLock {
// unlockings finish after specific time + not started locks that will finish after the time even though it start now
unlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorAfterTimeDenom(ctx, true, addr, denom, timestamp))
duration := time.Duration(0)
if timestamp.After(ctx.BlockTime()) {
duration = timestamp.Sub(ctx.BlockTime())
}
notUnlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDurationDenom(ctx, false, addr, denom, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetAccountLockedLongerDuration Returns account locked with duration longer than specified
func (k Keeper) GetAccountLockedLongerDuration(ctx sdk.Context, addr sdk.AccAddress, duration time.Duration) []types.PeriodLock {
// it does not matter started unlocking or not for duration query
unlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDuration(ctx, true, addr, duration))
notUnlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDuration(ctx, false, addr, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetAccountLockedLongerDurationNotUnlockingOnly Returns account locked with duration longer than specified
func (k Keeper) GetAccountLockedLongerDurationNotUnlockingOnly(ctx sdk.Context, addr sdk.AccAddress, duration time.Duration) []types.PeriodLock {
return k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDuration(ctx, false, addr, duration))
}
// GetAccountLockedLongerDurationDenom Returns account locked with duration longer than specified with specific denom
func (k Keeper) GetAccountLockedLongerDurationDenom(ctx sdk.Context, addr sdk.AccAddress, denom string, duration time.Duration) []types.PeriodLock {
// it does not matter started unlocking or not for duration query
unlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDurationDenom(ctx, true, addr, denom, duration))
notUnlockings := k.getLocksFromIterator(ctx, k.AccountLockIteratorLongerDurationDenom(ctx, false, addr, denom, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetLocksPastTimeDenom Returns the locks whose unlock time is beyond timestamp
func (k Keeper) GetLocksPastTimeDenom(ctx sdk.Context, denom string, timestamp time.Time) []types.PeriodLock {
// returns both unlocking started and not started assuming it started unlocking current time
unlockings := k.getLocksFromIterator(ctx, k.LockIteratorAfterTimeDenom(ctx, true, denom, timestamp))
duration := time.Duration(0)
if timestamp.After(ctx.BlockTime()) {
duration = timestamp.Sub(ctx.BlockTime())
}
notUnlockings := k.getLocksFromIterator(ctx, k.LockIteratorLongerThanDurationDenom(ctx, false, denom, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetLocksLongerThanDurationDenom Returns the locks whose unlock duration is longer than duration
func (k Keeper) GetLocksLongerThanDurationDenom(ctx sdk.Context, denom string, duration time.Duration) []types.PeriodLock {
// returns both unlocking started and not started
unlockings := k.getLocksFromIterator(ctx, k.LockIteratorLongerThanDurationDenom(ctx, true, denom, duration))
notUnlockings := k.getLocksFromIterator(ctx, k.LockIteratorLongerThanDurationDenom(ctx, false, denom, duration))
return combineLocks(notUnlockings, unlockings)
}
// GetLockByID Returns lock from lockID
func (k Keeper) GetLockByID(ctx sdk.Context, lockID uint64) (*types.PeriodLock, error) {
lock := types.PeriodLock{}
store := ctx.KVStore(k.storeKey)
lockKey := lockStoreKey(lockID)
if !store.Has(lockKey) {
return nil, fmt.Errorf("lock with ID %d does not exist", lockID)
}
bz := store.Get(lockKey)
err := proto.Unmarshal(bz, &lock)
return &lock, err
}
// GetPeriodLocks Returns the period locks on pool
func (k Keeper) GetPeriodLocks(ctx sdk.Context) ([]types.PeriodLock, error) {
unlockings := k.getLocksFromIterator(ctx, k.LockIterator(ctx, true))
notUnlockings := k.getLocksFromIterator(ctx, k.LockIterator(ctx, false))
return combineLocks(notUnlockings, unlockings), nil
}
// GetAccountPeriodLocks Returns the period locks associated to an account
func (k Keeper) GetAccountPeriodLocks(ctx sdk.Context, addr sdk.AccAddress) []types.PeriodLock {
unlockings := k.getLocksFromIterator(ctx, k.AccountLockIterator(ctx, true, addr))
notUnlockings := k.getLocksFromIterator(ctx, k.AccountLockIterator(ctx, false, addr))
return combineLocks(notUnlockings, unlockings)
}
// GetPeriodLocksByDuration returns the total amount of query.Denom tokens locked for longer than
// query.Duration
func (k Keeper) GetPeriodLocksAccumulation(ctx sdk.Context, query types.QueryCondition) sdk.Int {
beginKey := accumulationKey(query.Duration, 0)
return k.accumulationStore(ctx, query.Denom).SubsetAccumulation(beginKey, nil)
}
// BeginUnlockAllNotUnlockings begins unlock for all not unlocking coins
func (k Keeper) BeginUnlockAllNotUnlockings(ctx sdk.Context, account sdk.AccAddress) ([]types.PeriodLock, sdk.Coins, error) {
locks, coins, err := k.beginUnlockFromIterator(ctx, k.AccountLockIterator(ctx, false, account))
return locks, coins, err
}
// UnlockAllUnlockableCoins Unlock all unlockable coins
func (k Keeper) UnlockAllUnlockableCoins(ctx sdk.Context, account sdk.AccAddress) ([]types.PeriodLock, sdk.Coins, error) {
locks, coins := k.unlockFromIterator(ctx, k.AccountLockIteratorBeforeTime(ctx, true, account, ctx.BlockTime()))
return locks, coins, nil
}
// BeginUnlockPeriodLockByID begin unlock by period lock ID
func (k Keeper) BeginUnlockPeriodLockByID(ctx sdk.Context, LockID uint64) (*types.PeriodLock, error) {
lock, err := k.GetLockByID(ctx, LockID)
if err != nil {
return lock, err
}
err = k.BeginUnlock(ctx, *lock)
return lock, err
}
// UnlockPeriodLockByID unlock by period lock ID
func (k Keeper) UnlockPeriodLockByID(ctx sdk.Context, LockID uint64) (*types.PeriodLock, error) {
lock, err := k.GetLockByID(ctx, LockID)
if err != nil {
return lock, err
}
err = k.Unlock(ctx, *lock)
return lock, err
}
// LockTokens lock tokens from an account for specified duration
func (k Keeper) LockTokens(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, duration time.Duration) (types.PeriodLock, error) {
ID := k.getLastLockID(ctx) + 1
// unlock time is set at the beginning of unlocking time
lock := types.NewPeriodLock(ID, owner, duration, time.Time{}, coins)
return lock, k.Lock(ctx, lock)
}
// Lock is a utility to lock coins into module account
func (k Keeper) Lock(ctx sdk.Context, lock types.PeriodLock) error {
owner, err := sdk.AccAddressFromBech32(lock.Owner)
if err != nil {
return err
}
if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, lock.Coins); err != nil {
return err
}
lockID := lock.ID
store := ctx.KVStore(k.storeKey)
bz, err := proto.Marshal(&lock)
if err != nil {
return err
}
store.Set(lockStoreKey(lockID), bz)
k.setLastLockID(ctx, lockID)
refKeys, err := lockRefKeys(lock)
if err != nil {
return err
}
for _, refKey := range refKeys {
if err := k.addLockRefByKey(ctx, combineKeys(types.KeyPrefixNotUnlocking, refKey), lockID); err != nil {
return err
}
}
for _, coin := range lock.Coins {
k.accumulationStore(ctx, coin.Denom).Set(accumulationKey(lock.Duration, lock.ID), coin.Amount)
}
k.hooks.OnTokenLocked(ctx, owner, lock.ID, lock.Coins, lock.Duration, lock.EndTime)
return nil
}
// BeginUnlock is a utility to start unlocking coins from NotUnlocking queue
func (k Keeper) BeginUnlock(ctx sdk.Context, lock types.PeriodLock) error {
lockID := lock.ID
refKeys, err := lockRefKeys(lock)
if err != nil {
return err
}
for _, refKey := range refKeys {
err := k.deleteLockRefByKey(ctx, combineKeys(types.KeyPrefixNotUnlocking, refKey), lockID)
if err != nil {
return err
}
}
lock.EndTime = ctx.BlockTime().Add(lock.Duration)
store := ctx.KVStore(k.storeKey)
bz, err := proto.Marshal(&lock)
if err != nil {
return err
}
store.Set(lockStoreKey(lockID), bz)
refKeys, err = lockRefKeys(lock)
if err != nil {
return err
}
for _, refKey := range refKeys {
if err := k.addLockRefByKey(ctx, combineKeys(types.KeyPrefixUnlocking, refKey), lockID); err != nil {
return err
}
}
for _, coin := range lock.Coins {
k.accumulationStore(ctx, coin.Denom).Remove(accumulationKey(lock.Duration, lock.ID))
}
return nil
}
// Unlock is a utility to unlock coins from module account
func (k Keeper) Unlock(ctx sdk.Context, lock types.PeriodLock) error {
// validation for current time and unlock time
curTime := ctx.BlockTime()
if !lock.IsUnlocking() {
return fmt.Errorf("lock hasn't started unlocking yet")
}
if curTime.Before(lock.EndTime) {
return fmt.Errorf("lock is not unlockable yet: %s >= %s", curTime.String(), lock.EndTime.String())
}
owner, err := sdk.AccAddressFromBech32(lock.Owner)
if err != nil {
return err
}
// send coins back to owner
if err := k.bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, owner, lock.Coins); err != nil {
return err
}
lockID := lock.ID
store := ctx.KVStore(k.storeKey)
store.Delete(lockStoreKey(lockID)) // remove lock from store
refKeys, err := lockRefKeys(lock)
if err != nil {
return err
}
for _, refKey := range refKeys {
err := k.deleteLockRefByKey(ctx, combineKeys(types.KeyPrefixUnlocking, refKey), lockID)
if err != nil {
return err
}
}
k.hooks.OnTokenUnlocked(ctx, owner, lock.ID, lock.Coins, lock.Duration, lock.EndTime)
return nil
}