-
Notifications
You must be signed in to change notification settings - Fork 47
/
mtp.go
199 lines (167 loc) · 4.92 KB
/
mtp.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
package keeper
import (
"fmt"
gomath "math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/elys-network/elys/x/perpetual/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) SetMTP(ctx sdk.Context, mtp *types.MTP) error {
store := ctx.KVStore(k.storeKey)
count := k.GetMTPCount(ctx)
openCount := k.GetOpenMTPCount(ctx)
if mtp.Id == 0 {
// increment global id count
count++
mtp.Id = count
k.SetMTPCount(ctx, count)
// increment open mtp count
openCount++
k.SetOpenMTPCount(ctx, openCount)
}
if err := mtp.Validate(); err != nil {
return err
}
key := types.GetMTPKey(mtp.Address, mtp.Id)
store.Set(key, k.cdc.MustMarshal(mtp))
return nil
}
func (k Keeper) DestroyMTP(ctx sdk.Context, mtpAddress string, id uint64) error {
key := types.GetMTPKey(mtpAddress, id)
store := ctx.KVStore(k.storeKey)
if !store.Has(key) {
return types.ErrMTPDoesNotExist
}
store.Delete(key)
// decrement open mtp count
openCount := k.GetOpenMTPCount(ctx)
openCount--
// Set open MTP count
k.SetOpenMTPCount(ctx, openCount)
return nil
}
func (k Keeper) GetMTP(ctx sdk.Context, mtpAddress string, id uint64) (types.MTP, error) {
var mtp types.MTP
key := types.GetMTPKey(mtpAddress, id)
store := ctx.KVStore(k.storeKey)
if !store.Has(key) {
return mtp, types.ErrMTPDoesNotExist
}
bz := store.Get(key)
k.cdc.MustUnmarshal(bz, &mtp)
return mtp, nil
}
func (k Keeper) GetMTPIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.MTPPrefix)
}
func (k Keeper) GetAllMTPs(ctx sdk.Context) []types.MTP {
var mtpList []types.MTP
iterator := k.GetMTPIterator(ctx)
defer func(iterator sdk.Iterator) {
err := iterator.Close()
if err != nil {
panic(err)
}
}(iterator)
for ; iterator.Valid(); iterator.Next() {
var mtp types.MTP
bytesValue := iterator.Value()
k.cdc.MustUnmarshal(bytesValue, &mtp)
mtpList = append(mtpList, mtp)
}
return mtpList
}
func (k Keeper) GetMTPs(ctx sdk.Context, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtpList []*types.MTP
store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.MTPPrefix)
if pagination == nil {
pagination = &query.PageRequest{
Limit: gomath.MaxUint64 - 1,
}
}
pageRes, err := query.Paginate(mtpStore, pagination, func(key []byte, value []byte) error {
var mtp types.MTP
k.cdc.MustUnmarshal(value, &mtp)
mtpList = append(mtpList, &mtp)
return nil
})
return mtpList, pageRes, err
}
func (k Keeper) GetMTPsForPool(ctx sdk.Context, ammPoolId uint64, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtps []*types.MTP
store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.MTPPrefix)
if pagination == nil {
pagination = &query.PageRequest{
Limit: gomath.MaxUint64 - 1,
}
}
pageRes, err := query.FilteredPaginate(mtpStore, pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var mtp types.MTP
k.cdc.MustUnmarshal(value, &mtp)
if accumulate && mtp.AmmPoolId == ammPoolId {
mtps = append(mtps, &mtp)
return true, nil
}
return false, nil
})
return mtps, pageRes, err
}
func (k Keeper) GetMTPsForAddress(ctx sdk.Context, mtpAddress sdk.Address, pagination *query.PageRequest) ([]*types.MTP, *query.PageResponse, error) {
var mtps []*types.MTP
store := ctx.KVStore(k.storeKey)
mtpStore := prefix.NewStore(store, types.GetMTPPrefixForAddress(mtpAddress.String()))
if pagination == nil {
pagination = &query.PageRequest{
Limit: types.MaxPageLimit,
}
}
if pagination.Limit > types.MaxPageLimit {
return nil, nil, status.Error(codes.InvalidArgument, fmt.Sprintf("page size greater than max %d", types.MaxPageLimit))
}
pageRes, err := query.Paginate(mtpStore, pagination, func(key []byte, value []byte) error {
var mtp types.MTP
k.cdc.MustUnmarshal(value, &mtp)
mtps = append(mtps, &mtp)
return nil
})
if err != nil {
return nil, nil, err
}
return mtps, pageRes, nil
}
// Set MTP count
func (k Keeper) SetMTPCount(ctx sdk.Context, count uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.MTPCountPrefix, types.GetUint64Bytes(count))
}
func (k Keeper) GetMTPCount(ctx sdk.Context) uint64 {
var count uint64
countBz := ctx.KVStore(k.storeKey).Get(types.MTPCountPrefix)
if countBz == nil {
count = 0
} else {
count = types.GetUint64FromBytes(countBz)
}
return count
}
// Set Open MTP count
func (k Keeper) SetOpenMTPCount(ctx sdk.Context, count uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.OpenMTPCountPrefix, types.GetUint64Bytes(count))
}
func (k Keeper) GetOpenMTPCount(ctx sdk.Context) uint64 {
var count uint64
countBz := ctx.KVStore(k.storeKey).Get(types.OpenMTPCountPrefix)
if countBz == nil {
count = 0
} else {
count = types.GetUint64FromBytes(countBz)
}
return count
}