-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_lock.go
64 lines (53 loc) · 1.99 KB
/
product_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
package keeper
import (
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
"github.com/evoblockchain/evochain/x/dex/types"
ordertypes "github.com/evoblockchain/evochain/x/order/types"
)
// IsTokenPairLocked return true if token pair locked
func (k Keeper) IsTokenPairLocked(ctx sdk.Context, product string) bool {
return k.getProductLock(ctx, product) != nil
}
func (k Keeper) getProductLock(ctx sdk.Context, product string) *ordertypes.ProductLock {
store := ctx.KVStore(k.storeKey)
productInfo := store.Get(types.GetLockProductKey(product))
if productInfo == nil {
return nil
}
productLock := &ordertypes.ProductLock{}
k.cdc.MustUnmarshalBinaryLengthPrefixed(productInfo, productLock)
return productLock
}
// LockTokenPair locks token pair
func (k Keeper) LockTokenPair(ctx sdk.Context, product string, lock *ordertypes.ProductLock) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetLockProductKey(product), k.cdc.MustMarshalBinaryLengthPrefixed(*lock))
}
// UnlockTokenPair unlocks token pair
func (k Keeper) UnlockTokenPair(ctx sdk.Context, product string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetLockProductKey(product))
}
// LoadProductLocks loads product locked
func (k Keeper) LoadProductLocks(ctx sdk.Context) *ordertypes.ProductLockMap {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TokenPairLockKeyPrefix)
defer iter.Close()
lockMap := ordertypes.NewProductLockMap()
for iter.Valid() {
lock := &ordertypes.ProductLock{}
lockBytes := iter.Value()
k.cdc.MustUnmarshalBinaryLengthPrefixed(lockBytes, &lock)
lockMap.Data[types.GetKey(iter)] = lock
iter.Next()
}
return lockMap
}
// GetLockedProductsCopy returns deep copy of product locked
func (k Keeper) GetLockedProductsCopy(ctx sdk.Context) *ordertypes.ProductLockMap {
return k.LoadProductLocks(ctx)
}
// IsAnyProductLocked checks if any product is locked
func (k Keeper) IsAnyProductLocked(ctx sdk.Context) bool {
return len(k.LoadProductLocks(ctx).Data) > 0
}