-
Notifications
You must be signed in to change notification settings - Fork 29
/
tx.go
713 lines (593 loc) · 23.2 KB
/
tx.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ixotypes "github.com/ixofoundation/ixo-blockchain/lib/ixo"
didexported "github.com/ixofoundation/ixo-blockchain/lib/legacydid"
didtypes "github.com/ixofoundation/ixo-blockchain/lib/legacydid"
)
const (
TypeMsgCreateBond = "create_bond"
TypeMsgEditBond = "edit_bond"
TypeMsgSetNextAlpha = "set_next_alpha"
TypeMsgUpdateBondState = "update_bond_state"
TypeMsgBuy = "buy"
TypeMsgSell = "sell"
TypeMsgSwap = "swap"
TypeMsgMakeOutcomePayment = "make_outcome_payment"
TypeMsgWithdrawShare = "withdraw_share"
TypeMsgWithdrawReserve = "withdraw_reserve"
)
var (
_ ixotypes.IxoMsg = &MsgCreateBond{}
_ ixotypes.IxoMsg = &MsgEditBond{}
_ ixotypes.IxoMsg = &MsgSetNextAlpha{}
_ ixotypes.IxoMsg = &MsgUpdateBondState{}
_ ixotypes.IxoMsg = &MsgBuy{}
_ ixotypes.IxoMsg = &MsgSell{}
_ ixotypes.IxoMsg = &MsgSwap{}
_ ixotypes.IxoMsg = &MsgMakeOutcomePayment{}
_ ixotypes.IxoMsg = &MsgWithdrawShare{}
_ ixotypes.IxoMsg = &MsgWithdrawReserve{}
)
func NewMsgCreateBond(token, name, description string, creatorDid, controllerDid didexported.Did,
functionType string, functionParameters FunctionParams, reserveTokens []string,
txFeePercentage, exitFeePercentage sdk.Dec, feeAddress, reserveWithdrawalAddress sdk.AccAddress,
maxSupply sdk.Coin, orderQuantityLimits sdk.Coins, sanityRate, sanityMarginPercentage sdk.Dec,
allowSell, allowReserveWithdrawals, alphaBond bool, batchBlocks sdk.Uint, outcomePayment sdk.Int,
bondDid didexported.Did, creatorAddress string) *MsgCreateBond {
return &MsgCreateBond{
BondDid: bondDid,
Token: token,
Name: name,
Description: description,
CreatorDid: creatorDid,
ControllerDid: controllerDid,
FunctionType: functionType,
FunctionParameters: functionParameters,
ReserveTokens: reserveTokens,
TxFeePercentage: txFeePercentage,
ExitFeePercentage: exitFeePercentage,
FeeAddress: feeAddress.String(),
ReserveWithdrawalAddress: reserveWithdrawalAddress.String(),
MaxSupply: maxSupply,
OrderQuantityLimits: orderQuantityLimits,
SanityRate: sanityRate,
SanityMarginPercentage: sanityMarginPercentage,
AllowSells: allowSell,
AllowReserveWithdrawals: allowReserveWithdrawals,
AlphaBond: alphaBond,
BatchBlocks: batchBlocks,
OutcomePayment: outcomePayment,
CreatorAddress: creatorAddress,
}
}
func (msg MsgCreateBond) GetIidController() string { return msg.CreatorDid }
func (msg MsgCreateBond) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
} else if strings.TrimSpace(msg.Token) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "token")
} else if strings.TrimSpace(msg.Name) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "name")
} else if strings.TrimSpace(msg.Description) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "description")
} else if strings.TrimSpace(msg.CreatorDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "creator DID")
} else if strings.TrimSpace(msg.ControllerDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "controller DID")
} else if len(msg.ReserveTokens) == 0 {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "reserve tokens")
} else if strings.TrimSpace(msg.FeeAddress) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "fee address")
} else if strings.TrimSpace(msg.ReserveWithdrawalAddress) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "reserve withdrawal address")
} else if strings.TrimSpace(msg.FunctionType) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "function type")
}
// Note: FunctionParameters can be empty
// Check that bond token is a valid token name
err := CheckCoinDenom(msg.Token)
if err != nil {
return err
}
// Validate function parameters
if err := msg.FunctionParameters.Validate(msg.FunctionType); err != nil {
return err
}
// Validate reserve tokens
if err = CheckReserveTokenNames(msg.ReserveTokens, msg.Token); err != nil {
return err
} else if err = CheckNoOfReserveTokens(msg.ReserveTokens, msg.FunctionType); err != nil {
return err
}
// Validate coins
if !msg.MaxSupply.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "max supply")
} else if !msg.OrderQuantityLimits.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "order quantity limits")
}
// Check that max supply denom matches token denom
if msg.MaxSupply.Denom != msg.Token {
return ErrMaxSupplyDenomDoesNotMatchTokenDenom
}
// Check that Sanity values not negative
if msg.SanityRate.IsNegative() {
return sdkerrors.Wrap(ErrArgumentCannotBeNegative, "sanity rate")
} else if msg.SanityMarginPercentage.IsNegative() {
return sdkerrors.Wrap(ErrArgumentCannotBeNegative, "sanity margin percentage")
}
// Check FeePercentages not negative and don't add up to 100
if msg.TxFeePercentage.IsNegative() {
return sdkerrors.Wrap(ErrArgumentCannotBeNegative, "tx fee percentage")
} else if msg.ExitFeePercentage.IsNegative() {
return sdkerrors.Wrap(ErrArgumentCannotBeNegative, "exit fee percentage")
} else if msg.TxFeePercentage.Add(msg.ExitFeePercentage).GTE(sdk.NewDec(100)) {
return ErrFeesCannotBeOrExceed100Percent
}
// Check that not zero
if msg.BatchBlocks.IsZero() {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "batch blocks")
} else if msg.MaxSupply.Amount.IsZero() {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "max supply")
}
// Alpha bonds have to be augmented bonding curves
if msg.AlphaBond && msg.FunctionType != AugmentedFunction {
return sdkerrors.Wrap(ErrFunctionNotAvailableForFunctionType,
"only augmented bonding curves can be alpha bonds")
}
// Check that outcome payment not negative
if msg.OutcomePayment.IsNegative() {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "outcome payment")
}
// Note: uniqueness of reserve tokens checked when parsing
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.CreatorDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "creator DID")
}
// Check that allowSells and allowReserveWithdrawals are not both True
if msg.AllowSells && msg.AllowReserveWithdrawals {
return ErrCannotAllowSellsAndWithdrawals
}
return nil
}
func (msg MsgCreateBond) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgCreateBond) GetSignerDid() didexported.Did { return msg.CreatorDid }
func (msg MsgCreateBond) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.CreatorAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgCreateBond) Route() string { return RouterKey }
func (msg MsgCreateBond) Type() string { return TypeMsgCreateBond }
func NewMsgEditBond(name, description, orderQuantityLimits, sanityRate,
sanityMarginPercentage string, editorDid, bondDid didexported.Did, editorAddress string) *MsgEditBond {
return &MsgEditBond{
BondDid: bondDid,
Name: name,
Description: description,
OrderQuantityLimits: orderQuantityLimits,
SanityRate: sanityRate,
SanityMarginPercentage: sanityMarginPercentage,
EditorDid: editorDid,
EditorAddress: editorAddress,
}
}
func (msg MsgEditBond) GetIidController() string { return msg.EditorDid }
func (msg MsgEditBond) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
} else if strings.TrimSpace(msg.Name) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "name")
} else if strings.TrimSpace(msg.Description) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "description")
} else if strings.TrimSpace(msg.SanityRate) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "sanity rate")
} else if strings.TrimSpace(msg.SanityMarginPercentage) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "sanity margin percentage")
} else if strings.TrimSpace(msg.EditorDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "editor DID")
}
// Note: order quantity limits can be blank
// Check that at least one editable was edited. Fields that will not
// be edited should be "DoNotModifyField", and not an empty string
inputList := []string{
msg.Name, msg.Description, msg.OrderQuantityLimits,
msg.SanityRate, msg.SanityMarginPercentage,
}
atLeaseOneEdit := false
for _, e := range inputList {
if e != DoNotModifyField {
atLeaseOneEdit = true
break
}
}
if !atLeaseOneEdit {
return ErrDidNotEditAnything
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.EditorDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "editor DID")
}
return nil
}
func (msg MsgEditBond) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgEditBond) GetSignerDid() didexported.Did { return msg.EditorDid }
func (msg MsgEditBond) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.EditorAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgEditBond) Route() string { return RouterKey }
func (msg MsgEditBond) Type() string { return TypeMsgEditBond }
func NewMsgSetNextAlpha(alpha sdk.Dec, editorDid, bondDid didexported.Did, editorAddress string) *MsgSetNextAlpha {
return &MsgSetNextAlpha{
BondDid: bondDid,
Alpha: alpha,
EditorDid: editorDid,
EditorAddress: editorAddress,
}
}
func (msg MsgSetNextAlpha) GetIidController() string { return msg.EditorDid }
func (msg MsgSetNextAlpha) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "BondDid")
} else if strings.TrimSpace(msg.EditorDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "EditorDid")
}
// Check that 0.0001 <= alpha <= 0.9999. Note that we cannot set public
// alpha to 0 or 1, because these are edge cases which cause the system
// alpha to get stuck if we try to change the value of public alpha again.
minNextAlpha := sdk.MustNewDecFromStr("0.0001")
maxNextAlpha := sdk.MustNewDecFromStr("0.9999")
if msg.Alpha.LT(minNextAlpha) || msg.Alpha.GT(maxNextAlpha) {
return sdkerrors.Wrap(ErrInvalidAlpha, "0.0001 <= alpha <= 0.9999")
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond did is invalid")
} else if !didtypes.IsValidDid(msg.EditorDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "editor did is invalid")
}
return nil
}
func (msg MsgSetNextAlpha) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgSetNextAlpha) GetSignerDid() didexported.Did { return msg.EditorDid }
func (msg MsgSetNextAlpha) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.EditorAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgSetNextAlpha) Route() string { return RouterKey }
func (msg MsgSetNextAlpha) Type() string { return TypeMsgSetNextAlpha }
func NewMsgUpdateBondState(state BondState, editorDid, bondDid didexported.Did, editorAddress string) *MsgUpdateBondState {
return &MsgUpdateBondState{
BondDid: bondDid,
State: state.String(),
EditorDid: editorDid,
EditorAddress: editorAddress,
}
}
func (msg MsgUpdateBondState) GetIidController() string { return msg.EditorDid }
func (msg MsgUpdateBondState) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "BondDid")
} else if strings.TrimSpace(msg.EditorDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "EditorDid")
}
// Bond status can only be updated to SETTLE or FAILED
if msg.State != SettleState.String() && msg.State != FailedState.String() {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "cannot transition to that state")
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond did is invalid")
} else if !didtypes.IsValidDid(msg.EditorDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "editor did is invalid")
}
return nil
}
func (msg MsgUpdateBondState) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgUpdateBondState) GetSignerDid() didexported.Did { return msg.EditorDid }
func (msg MsgUpdateBondState) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.EditorAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgUpdateBondState) Route() string { return RouterKey }
func (msg MsgUpdateBondState) Type() string { return TypeMsgUpdateBondState }
func NewMsgBuy(buyerDid didexported.Did, amount sdk.Coin, maxPrices sdk.Coins,
bondDid didexported.Did, buyerAddress string) *MsgBuy {
return &MsgBuy{
BuyerDid: buyerDid,
Amount: amount,
MaxPrices: maxPrices,
BondDid: bondDid,
BuyerAddress: buyerAddress,
}
}
func (msg MsgBuy) GetIidController() string { return msg.BuyerDid }
func (msg MsgBuy) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.BuyerDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "buyer DID")
} else if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
}
// Check that amount valid and non zero
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
} else if msg.Amount.Amount.IsZero() {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "amount")
}
// Check that maxPrices valid
if !msg.MaxPrices.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "max prices")
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.BuyerDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "buyer DID")
}
return nil
}
func (msg MsgBuy) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgBuy) GetSignerDid() didexported.Did { return msg.BuyerDid }
func (msg MsgBuy) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.BuyerAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgBuy) Route() string { return RouterKey }
func (msg MsgBuy) Type() string { return TypeMsgBuy }
func NewMsgSell(sellerDid didexported.Did, amount sdk.Coin, bondDid didexported.Did, sellerAddress string) *MsgSell {
return &MsgSell{
SellerDid: sellerDid,
Amount: amount,
BondDid: bondDid,
SellerAddress: sellerAddress,
}
}
func (msg MsgSell) GetIidController() string { return msg.SellerDid }
func (msg MsgSell) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.SellerDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "seller DID")
} else if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
}
// Check that amount valid and non zero
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
} else if msg.Amount.Amount.IsZero() {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "amount")
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.SellerDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "seller DID")
}
return nil
}
func (msg MsgSell) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgSell) GetSignerDid() didexported.Did { return msg.SellerDid }
func (msg MsgSell) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.SellerAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgSell) Route() string { return RouterKey }
func (msg MsgSell) Type() string { return TypeMsgSell }
func NewMsgSwap(swapperDid didexported.Did, from sdk.Coin, toToken string,
bondDid didexported.Did, swapperAddress string) *MsgSwap {
return &MsgSwap{
SwapperDid: swapperDid,
From: from,
ToToken: toToken,
BondDid: bondDid,
SwapperAddress: swapperAddress,
}
}
func (msg MsgSwap) GetIidController() string { return msg.SwapperDid }
func (msg MsgSwap) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.SwapperDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "swapper DID")
} else if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
} else if strings.TrimSpace(msg.ToToken) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "to token")
}
// Validate from amount
if !msg.From.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.From.String())
}
// Validate to token
err := CheckCoinDenom(msg.ToToken)
if err != nil {
return err
}
// Check if from and to the same token
if msg.From.Denom == msg.ToToken {
return ErrFromAndToCannotBeTheSameToken
}
// Check that non zero
if msg.From.Amount.IsZero() {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "amount")
}
// Note: From denom and amount must be valid since sdk.Coin
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID is invalid")
} else if !didtypes.IsValidDid(msg.SwapperDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "swapper DID is invalid")
}
return nil
}
func (msg MsgSwap) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgSwap) GetSignerDid() didexported.Did { return msg.SwapperDid }
func (msg MsgSwap) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.SwapperAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgSwap) Route() string { return RouterKey }
func (msg MsgSwap) Type() string { return TypeMsgSwap }
func NewMsgMakeOutcomePayment(senderDid didexported.Did, amount sdk.Int, bondDid didexported.Did, senderAddress string) *MsgMakeOutcomePayment {
return &MsgMakeOutcomePayment{
SenderDid: senderDid,
Amount: amount,
BondDid: bondDid,
SenderAddress: senderAddress,
}
}
func (msg MsgMakeOutcomePayment) GetIidController() string { return msg.SenderDid }
func (msg MsgMakeOutcomePayment) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.SenderDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "sender DID")
} else if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
}
// Outcome payment amount has to be greater than 0
if msg.Amount.LTE(sdk.ZeroInt()) {
return sdkerrors.Wrap(ErrArgumentMustBePositive, "amount")
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.SenderDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "sender DID")
}
return nil
}
func (msg MsgMakeOutcomePayment) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgMakeOutcomePayment) GetSignerDid() didexported.Did { return msg.SenderDid }
func (msg MsgMakeOutcomePayment) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.SenderAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgMakeOutcomePayment) Route() string { return RouterKey }
func (msg MsgMakeOutcomePayment) Type() string { return TypeMsgMakeOutcomePayment }
func NewMsgWithdrawShare(recipientDid, bondDid didexported.Did, recipientAddress string) *MsgWithdrawShare {
return &MsgWithdrawShare{
RecipientDid: recipientDid,
BondDid: bondDid,
RecipientAddress: recipientAddress,
}
}
func (msg MsgWithdrawShare) GetIidController() string { return msg.RecipientDid }
func (msg MsgWithdrawShare) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.RecipientDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "recipient DID")
} else if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.RecipientDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "recipient DID")
}
return nil
}
func (msg MsgWithdrawShare) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgWithdrawShare) GetSignerDid() didexported.Did { return msg.RecipientDid }
func (msg MsgWithdrawShare) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.RecipientAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgWithdrawShare) Route() string { return RouterKey }
func (msg MsgWithdrawShare) Type() string { return TypeMsgWithdrawShare }
func NewMsgWithdrawReserve(withdrawerDid didexported.Did, amount sdk.Coins,
bondDid didexported.Did, withdrawerAddress string) *MsgWithdrawReserve {
return &MsgWithdrawReserve{
WithdrawerDid: withdrawerDid,
Amount: amount,
BondDid: bondDid,
WithdrawerAddress: withdrawerAddress,
}
}
func (msg MsgWithdrawReserve) GetIidController() string { return msg.WithdrawerDid }
func (msg MsgWithdrawReserve) ValidateBasic() error {
// Check if empty
if strings.TrimSpace(msg.WithdrawerDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "withdrawer DID")
} else if strings.TrimSpace(msg.BondDid) == "" {
return sdkerrors.Wrap(ErrArgumentCannotBeEmpty, "bond DID")
}
// Validate amount
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
// Check that DIDs valid
if !didtypes.IsValidDid(msg.BondDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "bond DID")
} else if !didtypes.IsValidDid(msg.WithdrawerDid) {
return sdkerrors.Wrap(didtypes.ErrInvalidDid, "withdrawer DID")
}
return nil
}
func (msg MsgWithdrawReserve) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgWithdrawReserve) GetSignerDid() didexported.Did { return msg.WithdrawerDid }
func (msg MsgWithdrawReserve) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress)
if err != nil {
return []sdk.AccAddress{}
}
return []sdk.AccAddress{address}
}
func (msg MsgWithdrawReserve) Route() string { return RouterKey }
func (msg MsgWithdrawReserve) Type() string { return TypeMsgWithdrawReserve }