-
Notifications
You must be signed in to change notification settings - Fork 55
/
request.go
381 lines (349 loc) · 11.7 KB
/
request.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
package types
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewDepositRequest returns a new DepositRequest.
func NewDepositRequest(msg *MsgDeposit, pool Pool, id uint64, msgHeight int64) DepositRequest {
return DepositRequest{
Id: id,
PoolId: msg.PoolId,
MsgHeight: msgHeight,
Depositor: msg.Depositor,
DepositCoins: msg.DepositCoins,
AcceptedCoins: nil,
MintedPoolCoin: sdk.NewCoin(pool.PoolCoinDenom, sdk.ZeroInt()),
Status: RequestStatusNotExecuted,
}
}
func (req DepositRequest) GetDepositor() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(req.Depositor)
if err != nil {
panic(err)
}
return addr
}
// Validate validates DepositRequest for genesis.
func (req DepositRequest) Validate() error {
if req.Id == 0 {
return fmt.Errorf("id must not be 0")
}
if req.PoolId == 0 {
return fmt.Errorf("pool id must not be 0")
}
if req.MsgHeight == 0 {
return fmt.Errorf("message height must not be 0")
}
if _, err := sdk.AccAddressFromBech32(req.Depositor); err != nil {
return fmt.Errorf("invalid depositor address %s: %w", req.Depositor, err)
}
if err := req.DepositCoins.Validate(); err != nil {
return fmt.Errorf("invalid deposit coins: %w", err)
}
if len(req.DepositCoins) != 2 {
return fmt.Errorf("wrong number of deposit coins: %d", len(req.DepositCoins))
}
if err := req.AcceptedCoins.Validate(); err != nil {
return fmt.Errorf("invalid accepted coins: %w", err)
}
if len(req.AcceptedCoins) != 0 && len(req.AcceptedCoins) != 2 {
return fmt.Errorf("wrong number of accepted coins: %d", len(req.AcceptedCoins))
}
for _, coin := range req.AcceptedCoins {
if req.DepositCoins.AmountOf(coin.Denom).IsZero() {
return fmt.Errorf("mismatching denom pair between deposit coins and accepted coins")
}
}
if err := req.MintedPoolCoin.Validate(); err != nil {
return fmt.Errorf("invalid minted pool coin %s: %w", req.MintedPoolCoin, err)
}
if !req.Status.IsValid() {
return fmt.Errorf("invalid status: %s", req.Status)
}
return nil
}
// SetStatus sets the request's status.
// SetStatus is to easily find locations where the status is changed.
func (req *DepositRequest) SetStatus(status RequestStatus) {
req.Status = status
}
// NewWithdrawRequest returns a new WithdrawRequest.
func NewWithdrawRequest(msg *MsgWithdraw, id uint64, msgHeight int64) WithdrawRequest {
return WithdrawRequest{
Id: id,
PoolId: msg.PoolId,
MsgHeight: msgHeight,
Withdrawer: msg.Withdrawer,
PoolCoin: msg.PoolCoin,
WithdrawnCoins: nil,
Status: RequestStatusNotExecuted,
}
}
func (req WithdrawRequest) GetWithdrawer() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(req.Withdrawer)
if err != nil {
panic(err)
}
return addr
}
// Validate validates WithdrawRequest for genesis.
func (req WithdrawRequest) Validate() error {
if req.Id == 0 {
return fmt.Errorf("id must not be 0")
}
if req.PoolId == 0 {
return fmt.Errorf("pool id must not be 0")
}
if req.MsgHeight == 0 {
return fmt.Errorf("message height must not be 0")
}
if _, err := sdk.AccAddressFromBech32(req.Withdrawer); err != nil {
return fmt.Errorf("invalid withdrawer address %s: %w", req.Withdrawer, err)
}
if err := req.PoolCoin.Validate(); err != nil {
return fmt.Errorf("invalid pool coin %s: %w", req.PoolCoin, err)
}
if req.PoolCoin.IsZero() {
return fmt.Errorf("pool coin must not be 0")
}
if err := req.WithdrawnCoins.Validate(); err != nil {
return fmt.Errorf("invalid withdrawn coins: %w", err)
}
if len(req.WithdrawnCoins) > 2 {
return fmt.Errorf("wrong number of withdrawn coins: %d", len(req.WithdrawnCoins))
}
if !req.Status.IsValid() {
return fmt.Errorf("invalid status: %s", req.Status)
}
return nil
}
// SetStatus sets the request's status.
// SetStatus is to easily find locations where the status is changed.
func (req *WithdrawRequest) SetStatus(status RequestStatus) {
req.Status = status
}
// NewOrderForLimitOrder returns a new Order from MsgLimitOrder.
func NewOrderForLimitOrder(msg *MsgLimitOrder, id uint64, pair Pair, offerCoin sdk.Coin, price sdk.Dec, expireAt time.Time, msgHeight int64) Order {
return Order{
Id: id,
PairId: pair.Id,
MsgHeight: msgHeight,
Orderer: msg.Orderer,
Direction: msg.Direction,
OfferCoin: offerCoin,
RemainingOfferCoin: offerCoin,
ReceivedCoin: sdk.NewCoin(msg.DemandCoinDenom, sdk.ZeroInt()),
Price: price,
Amount: msg.Amount,
OpenAmount: msg.Amount,
BatchId: pair.CurrentBatchId,
ExpireAt: expireAt,
Status: OrderStatusNotExecuted,
}
}
// NewOrderForMarketOrder returns a new Order from MsgMarketOrder.
func NewOrderForMarketOrder(msg *MsgMarketOrder, id uint64, pair Pair, offerCoin sdk.Coin, price sdk.Dec, expireAt time.Time, msgHeight int64) Order {
return Order{
Id: id,
PairId: pair.Id,
MsgHeight: msgHeight,
Orderer: msg.Orderer,
Direction: msg.Direction,
OfferCoin: offerCoin,
RemainingOfferCoin: offerCoin,
ReceivedCoin: sdk.NewCoin(msg.DemandCoinDenom, sdk.ZeroInt()),
Price: price,
Amount: msg.Amount,
OpenAmount: msg.Amount,
BatchId: pair.CurrentBatchId,
ExpireAt: expireAt,
Status: OrderStatusNotExecuted,
}
}
func (order Order) GetOrderer() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(order.Orderer)
if err != nil {
panic(err)
}
return addr
}
// Validate validates Order for genesis.
func (order Order) Validate() error {
if order.Id == 0 {
return fmt.Errorf("id must not be 0")
}
if order.PairId == 0 {
return fmt.Errorf("pair id must not be 0")
}
if order.MsgHeight == 0 {
return fmt.Errorf("message height must not be 0")
}
if _, err := sdk.AccAddressFromBech32(order.Orderer); err != nil {
return fmt.Errorf("invalid orderer address %s: %w", order.Orderer, err)
}
if order.Direction != OrderDirectionBuy && order.Direction != OrderDirectionSell {
return fmt.Errorf("invalid direction: %s", order.Direction)
}
if err := order.OfferCoin.Validate(); err != nil {
return fmt.Errorf("invalid offer coin %s: %w", order.OfferCoin, err)
}
if order.OfferCoin.IsZero() {
return fmt.Errorf("offer coin must not be 0")
}
if err := order.RemainingOfferCoin.Validate(); err != nil {
return fmt.Errorf("invalid remaining offer coin %s: %w", order.RemainingOfferCoin, err)
}
if order.OfferCoin.Denom != order.RemainingOfferCoin.Denom {
return fmt.Errorf("offer coin denom %s != remaining offer coin denom %s", order.OfferCoin.Denom, order.RemainingOfferCoin.Denom)
}
if err := order.ReceivedCoin.Validate(); err != nil {
return fmt.Errorf("invalid received coin %s: %w", order.ReceivedCoin, err)
}
if !order.Price.IsPositive() {
return fmt.Errorf("price must be positive: %s", order.Price)
}
if !order.Amount.IsPositive() {
return fmt.Errorf("amount must be positive: %s", order.Amount)
}
if order.OpenAmount.IsNegative() {
return fmt.Errorf("open amount must not be negative: %s", order.OpenAmount)
}
if order.BatchId == 0 {
return fmt.Errorf("batch id must not be 0")
}
if order.ExpireAt.IsZero() {
return fmt.Errorf("no expiration info")
}
if !order.Status.IsValid() {
return fmt.Errorf("invalid status: %s", order.Status)
}
return nil
}
// ExpiredAt returns whether the order should be deleted at given time.
func (order Order) ExpiredAt(t time.Time) bool {
return !order.ExpireAt.After(t)
}
// SetStatus sets the order's status.
// SetStatus is to easily find locations where the status is changed.
func (order *Order) SetStatus(status OrderStatus) {
order.Status = status
}
// IsValid returns true if the RequestStatus is one of:
// RequestStatusNotExecuted, RequestStatusSucceeded, RequestStatusFailed.
func (status RequestStatus) IsValid() bool {
switch status {
case RequestStatusNotExecuted, RequestStatusSucceeded, RequestStatusFailed:
return true
default:
return false
}
}
// ShouldBeDeleted returns true if the RequestStatus is one of:
// RequestStatusSucceeded, RequestStatusFailed.
func (status RequestStatus) ShouldBeDeleted() bool {
switch status {
case RequestStatusSucceeded, RequestStatusFailed:
return true
default:
return false
}
}
// IsValid returns true if the OrderStatus is one of:
// OrderStatusNotExecuted, OrderStatusNotMatched, OrderStatusPartiallyMatched,
// OrderStatusCompleted, OrderStatusCanceled, OrderStatusExpired.
func (status OrderStatus) IsValid() bool {
switch status {
case OrderStatusNotExecuted, OrderStatusNotMatched, OrderStatusPartiallyMatched,
OrderStatusCompleted, OrderStatusCanceled, OrderStatusExpired:
return true
default:
return false
}
}
// IsMatchable returns true if the OrderStatus is one of:
// OrderStatusNotExecuted, OrderStatusNotMatched, OrderStatusPartiallyMatched.
func (status OrderStatus) IsMatchable() bool {
switch status {
case OrderStatusNotExecuted, OrderStatusNotMatched, OrderStatusPartiallyMatched:
return true
default:
return false
}
}
// CanBeExpired has the same condition as IsMatchable.
func (status OrderStatus) CanBeExpired() bool {
return status.IsMatchable()
}
// IsCanceledOrExpired returns true if the OrderStatus is one of:
// OrderStatusCanceled, OrderStatusExpired.
func (status OrderStatus) IsCanceledOrExpired() bool {
switch status {
case OrderStatusCanceled, OrderStatusExpired:
return true
default:
return false
}
}
// ShouldBeDeleted returns true if the OrderStatus is one of:
// OrderStatusCompleted, OrderStatusCanceled, OrderStatusExpired.
func (status OrderStatus) ShouldBeDeleted() bool {
return status == OrderStatusCompleted || status.IsCanceledOrExpired()
}
// MustMarshalDepositRequest returns the DepositRequest bytes. Panics if fails.
func MustMarshalDepositRequest(cdc codec.BinaryCodec, msg DepositRequest) []byte {
return cdc.MustMarshal(&msg)
}
// UnmarshalDepositRequest returns the DepositRequest from bytes.
func UnmarshalDepositRequest(cdc codec.BinaryCodec, value []byte) (msg DepositRequest, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
// MustUnmarshalDepositRequest returns the DepositRequest from bytes.
// It throws panic if it fails.
func MustUnmarshalDepositRequest(cdc codec.BinaryCodec, value []byte) DepositRequest {
msg, err := UnmarshalDepositRequest(cdc, value)
if err != nil {
panic(err)
}
return msg
}
// MustMarshaWithdrawRequest returns the WithdrawRequest bytes.
// It throws panic if it fails.
func MustMarshaWithdrawRequest(cdc codec.BinaryCodec, msg WithdrawRequest) []byte {
return cdc.MustMarshal(&msg)
}
// UnmarshalWithdrawRequest returns the WithdrawRequest from bytes.
func UnmarshalWithdrawRequest(cdc codec.BinaryCodec, value []byte) (msg WithdrawRequest, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
// MustUnmarshalWithdrawRequest returns the WithdrawRequest from bytes.
// It throws panic if it fails.
func MustUnmarshalWithdrawRequest(cdc codec.BinaryCodec, value []byte) WithdrawRequest {
msg, err := UnmarshalWithdrawRequest(cdc, value)
if err != nil {
panic(err)
}
return msg
}
// MustMarshaOrder returns the Order bytes.
// It throws panic if it fails.
func MustMarshaOrder(cdc codec.BinaryCodec, order Order) []byte {
return cdc.MustMarshal(&order)
}
// UnmarshalOrder returns the Order from bytes.
func UnmarshalOrder(cdc codec.BinaryCodec, value []byte) (order Order, err error) {
err = cdc.Unmarshal(value, &order)
return order, err
}
// MustUnmarshalOrder returns the Order from bytes.
// It throws panic if it fails.
func MustUnmarshalOrder(cdc codec.BinaryCodec, value []byte) Order {
msg, err := UnmarshalOrder(cdc, value)
if err != nil {
panic(err)
}
return msg
}