/
batch_processing.go
107 lines (88 loc) · 4.13 KB
/
batch_processing.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
func (k Keeper) SetLastSwapRequestIndex(ctx sdk.Context, index uint64) {
store := ctx.TransientStore(k.transientStoreKey)
store.Set([]byte(types.TLastSwapRequestIndex), sdk.Uint64ToBigEndian(index))
}
func (k Keeper) GetLastSwapRequestIndex(ctx sdk.Context) uint64 {
store := ctx.TransientStore(k.transientStoreKey)
bz := store.Get([]byte(types.TLastSwapRequestIndex))
if bz == nil {
return 0
}
return sdk.BigEndianToUint64(bz)
}
// SetSwapExactAmountInRequests stores swap exact amount in request
func (k Keeper) SetSwapExactAmountInRequests(ctx sdk.Context, msg *types.MsgSwapExactAmountIn, index uint64) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountInKey))
b := k.cdc.MustMarshal(msg)
store.Set(types.TKeyPrefixSwapExactAmountIn(msg, index), b)
}
// DeleteSwapExactAmountInRequest removes a swap exact amount in request
func (k Keeper) DeleteSwapExactAmountInRequest(ctx sdk.Context, msg *types.MsgSwapExactAmountIn, index uint64) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountInKey))
store.Delete(types.TKeyPrefixSwapExactAmountIn(msg, index))
}
// GetAllSwapExactAmountInRequests returns all SwapExactAmountIn requests
func (k Keeper) GetAllSwapExactAmountInRequests(ctx sdk.Context) (list []types.MsgSwapExactAmountIn) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountInKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.MsgSwapExactAmountIn
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) GetFirstSwapExactAmountInRequest(ctx sdk.Context, sprefix []byte) (*types.MsgSwapExactAmountIn, uint64) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountInKey))
iterator := sdk.KVStorePrefixIterator(store, sprefix)
defer iterator.Close()
if !iterator.Valid() {
return nil, 0
}
var val types.MsgSwapExactAmountIn
k.cdc.MustUnmarshal(iterator.Value(), &val)
key := iterator.Key()
return &val, sdk.BigEndianToUint64(key[len(key)-8:])
}
// SetSwapExactAmountInRequests stores swap exact amount out request
func (k Keeper) SetSwapExactAmountOutRequests(ctx sdk.Context, msg *types.MsgSwapExactAmountOut, index uint64) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountOutKey))
b := k.cdc.MustMarshal(msg)
store.Set(types.TKeyPrefixSwapExactAmountOut(msg, index), b)
}
// DeleteSwapExactAmountOutRequest deletes a swap exact amount out request
func (k Keeper) DeleteSwapExactAmountOutRequest(ctx sdk.Context, msg *types.MsgSwapExactAmountOut, index uint64) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountOutKey))
store.Delete(types.TKeyPrefixSwapExactAmountOut(msg, index))
}
// GetAllSwapExactAmountOutRequests returns all SwapExactAmountOut requests
func (k Keeper) GetAllSwapExactAmountOutRequests(ctx sdk.Context) (list []types.MsgSwapExactAmountOut) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountOutKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.MsgSwapExactAmountOut
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) GetFirstSwapExactAmountOutRequest(ctx sdk.Context, sprefix []byte) (*types.MsgSwapExactAmountOut, uint64) {
store := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.KeyPrefix(types.TSwapExactAmountOutKey))
iterator := sdk.KVStorePrefixIterator(store, sprefix)
defer iterator.Close()
if !iterator.Valid() {
return nil, 0
}
var val types.MsgSwapExactAmountOut
k.cdc.MustUnmarshal(iterator.Value(), &val)
key := iterator.Key()
return &val, sdk.BigEndianToUint64(key[len(key)-8:])
}