-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
303 lines (276 loc) · 9.43 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
package types
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
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
}
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 { // TODO: is this check correct?
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))
}
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
}
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
}
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 { // TODO: is this check correct?
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
}
func NewSwapRequestForLimitOrder(msg *MsgLimitOrder, id uint64, pair Pair, offerCoin sdk.Coin, expireAt time.Time, msgHeight int64) SwapRequest {
return SwapRequest{
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: msg.Price,
Amount: msg.Amount,
OpenAmount: msg.Amount,
BatchId: pair.CurrentBatchId,
ExpireAt: expireAt,
Status: SwapRequestStatusNotExecuted,
}
}
func NewSwapRequestForMarketOrder(msg *MsgMarketOrder, id uint64, pair Pair, price sdk.Dec, expireAt time.Time, msgHeight int64) SwapRequest {
return SwapRequest{
Id: id,
PairId: pair.Id,
MsgHeight: msgHeight,
Orderer: msg.Orderer,
Direction: msg.Direction,
OfferCoin: msg.OfferCoin,
RemainingOfferCoin: msg.OfferCoin,
ReceivedCoin: sdk.NewCoin(msg.DemandCoinDenom, sdk.ZeroInt()),
Price: price,
Amount: msg.Amount,
OpenAmount: msg.Amount,
BatchId: pair.CurrentBatchId,
ExpireAt: expireAt,
Status: SwapRequestStatusNotExecuted,
}
}
func (req SwapRequest) GetOrderer() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(req.Orderer)
if err != nil {
panic(err)
}
return addr
}
func (req SwapRequest) Validate() error {
if req.Id == 0 {
return fmt.Errorf("id must not be 0")
}
if req.PairId == 0 {
return fmt.Errorf("pair id must not be 0")
}
if req.MsgHeight == 0 { // TODO: is this check correct?
return fmt.Errorf("message height must not be 0")
}
if _, err := sdk.AccAddressFromBech32(req.Orderer); err != nil {
return fmt.Errorf("invalid orderer address %s: %w", req.Orderer, err)
}
if req.Direction != SwapDirectionBuy && req.Direction != SwapDirectionSell {
return fmt.Errorf("invalid direction: %s", req.Direction)
}
if err := req.OfferCoin.Validate(); err != nil {
return fmt.Errorf("invalid offer coin %s: %w", req.OfferCoin, err)
}
if req.OfferCoin.IsZero() {
return fmt.Errorf("offer coin must not be 0")
}
if err := req.RemainingOfferCoin.Validate(); err != nil {
return fmt.Errorf("invalid remaining offer coin %s: %w", req.RemainingOfferCoin, err)
}
if err := req.ReceivedCoin.Validate(); err != nil {
return fmt.Errorf("invalid received coin %s: %w", req.ReceivedCoin, err)
}
if !req.Price.IsPositive() {
return fmt.Errorf("price must be positive: %s", req.Price)
}
if !req.Amount.IsPositive() {
return fmt.Errorf("amount must be positive: %s", req.Amount)
}
if req.OpenAmount.IsNegative() {
return fmt.Errorf("open amount must not be negative: %s", req.OpenAmount)
}
if req.BatchId == 0 {
return fmt.Errorf("batch id must not be 0")
}
if req.ExpireAt.IsZero() {
return fmt.Errorf("no expiration info")
}
if !req.Status.IsValid() {
return fmt.Errorf("invalid status: %s", req.Status)
}
return nil
}
func (status RequestStatus) IsValid() bool {
return status == RequestStatusNotExecuted || status == RequestStatusSucceeded || status == RequestStatusFailed
}
func (status RequestStatus) ShouldBeDeleted() bool {
return status == RequestStatusSucceeded || status == RequestStatusFailed
}
func (status SwapRequestStatus) IsValid() bool {
return status == SwapRequestStatusNotExecuted || status == SwapRequestStatusNotMatched ||
status == SwapRequestStatusPartiallyMatched || status == SwapRequestStatusCompleted ||
status == SwapRequestStatusCanceled || status == SwapRequestStatusExpired
}
func (status SwapRequestStatus) IsMatchable() bool {
return status == SwapRequestStatusNotExecuted ||
status == SwapRequestStatusNotMatched ||
status == SwapRequestStatusPartiallyMatched
}
func (status SwapRequestStatus) IsCanceledOrExpired() bool {
return status == SwapRequestStatusCanceled || status == SwapRequestStatusExpired
}
func (status SwapRequestStatus) ShouldBeDeleted() bool {
return status == SwapRequestStatusCompleted || 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
}
// MustMarshaSwapRequest returns the SwapRequest bytes.
// It throws panic if it fails.
func MustMarshaSwapRequest(cdc codec.BinaryCodec, msg SwapRequest) []byte {
return cdc.MustMarshal(&msg)
}
// UnmarshalSwapRequest returns the SwapRequest from bytes.
func UnmarshalSwapRequest(cdc codec.BinaryCodec, value []byte) (msg SwapRequest, err error) {
err = cdc.Unmarshal(value, &msg)
return msg, err
}
// MustUnmarshalSwapRequest returns the SwapRequest from bytes.
// It throws panic if it fails.
func MustUnmarshalSwapRequest(cdc codec.BinaryCodec, value []byte) SwapRequest {
msg, err := UnmarshalSwapRequest(cdc, value)
if err != nil {
panic(err)
}
return msg
}