-
Notifications
You must be signed in to change notification settings - Fork 117
/
msgs.go
218 lines (184 loc) · 6.24 KB
/
msgs.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
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
_ sdk.Msg = &MsgRemoveLiquidity{}
_ sdk.Msg = &MsgCreatePool{}
_ sdk.Msg = &MsgAddLiquidity{}
_ sdk.Msg = &MsgSwap{}
_ sdk.Msg = &MsgDecommissionPool{}
)
func NewMsgDecommissionPool(signer sdk.AccAddress, symbol string) MsgDecommissionPool {
return MsgDecommissionPool{Signer: signer.String(), Symbol: symbol}
}
func (m MsgDecommissionPool) Route() string {
return RouterKey
}
func (m MsgDecommissionPool) Type() string {
return "decommission_pool"
}
func (m MsgDecommissionPool) ValidateBasic() error {
if len(m.Signer) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Signer)
}
if !VerifyRange(len(strings.TrimSpace(m.Symbol)), 0, MaxSymbolLength) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, m.Symbol)
}
return nil
}
func (m MsgDecommissionPool) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgDecommissionPool) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}
func NewMsgSwap(signer sdk.AccAddress, sentAsset Asset, receivedAsset Asset, sentAmount sdk.Uint, minReceivingAmount sdk.Uint) MsgSwap {
return MsgSwap{Signer: signer.String(), SentAsset: &sentAsset, ReceivedAsset: &receivedAsset, SentAmount: sentAmount, MinReceivingAmount: minReceivingAmount}
}
func (m MsgSwap) Route() string {
return RouterKey
}
func (m MsgSwap) Type() string {
return "swap"
}
func (m MsgSwap) ValidateBasic() error {
if len(m.Signer) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Signer)
}
if !m.SentAsset.Validate() {
return sdkerrors.Wrap(ErrInValidAsset, m.SentAsset.Symbol)
}
if !m.ReceivedAsset.Validate() {
return sdkerrors.Wrap(ErrInValidAsset, m.ReceivedAsset.Symbol)
}
if m.SentAsset.Equals(*m.ReceivedAsset) {
return sdkerrors.Wrap(ErrInValidAsset, "Sent And Received asset cannot be the same")
}
if m.SentAmount.IsZero() {
return sdkerrors.Wrap(ErrInValidAmount, m.SentAmount.String())
}
return nil
}
func (m MsgSwap) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgSwap) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}
func NewMsgRemoveLiquidity(signer sdk.AccAddress, externalAsset Asset, wBasisPoints sdk.Int, asymmetry sdk.Int) MsgRemoveLiquidity {
return MsgRemoveLiquidity{Signer: signer.String(), ExternalAsset: &externalAsset, WBasisPoints: wBasisPoints, Asymmetry: asymmetry}
}
func (m MsgRemoveLiquidity) Route() string {
return RouterKey
}
func (m MsgRemoveLiquidity) Type() string {
return "remove_liquidity"
}
func (m MsgRemoveLiquidity) ValidateBasic() error {
if len(m.Signer) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Signer)
}
if !m.ExternalAsset.Validate() {
return sdkerrors.Wrap(ErrInValidAsset, m.ExternalAsset.Symbol)
}
if !(m.WBasisPoints.IsPositive()) || m.WBasisPoints.GT(sdk.NewInt(MaxWbasis)) {
return sdkerrors.Wrap(ErrInvalidWBasis, m.WBasisPoints.String())
}
if m.Asymmetry.GT(sdk.NewInt(10000)) || m.Asymmetry.LT(sdk.NewInt(-10000)) {
return sdkerrors.Wrap(ErrInvalidAsymmetry, m.Asymmetry.String())
}
return nil
}
func (m MsgRemoveLiquidity) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgRemoveLiquidity) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}
func NewMsgAddLiquidity(signer sdk.AccAddress, externalAsset Asset, nativeAssetAmount sdk.Uint, externalAssetAmount sdk.Uint) MsgAddLiquidity {
return MsgAddLiquidity{Signer: signer.String(), ExternalAsset: &externalAsset, NativeAssetAmount: nativeAssetAmount, ExternalAssetAmount: externalAssetAmount}
}
func (m MsgAddLiquidity) Route() string {
return RouterKey
}
func (m MsgAddLiquidity) Type() string {
return "add_liquidity"
}
func (m MsgAddLiquidity) ValidateBasic() error {
if len(m.Signer) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Signer)
}
if !m.ExternalAsset.Validate() {
return sdkerrors.Wrap(ErrInValidAsset, m.ExternalAsset.Symbol)
}
if m.ExternalAsset.Equals(GetSettlementAsset()) {
return sdkerrors.Wrap(ErrInValidAsset, "External asset cannot be rowan")
}
if (!m.NativeAssetAmount.GT(sdk.ZeroUint())) && (!m.ExternalAssetAmount.GT(sdk.ZeroUint())) {
return sdkerrors.Wrap(ErrInValidAmount, fmt.Sprintf("Both asset ammounts cannot be 0 %s / %s", m.NativeAssetAmount.String(), m.ExternalAssetAmount.String()))
}
return nil
}
func (m MsgAddLiquidity) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgAddLiquidity) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}
func NewMsgCreatePool(signer sdk.AccAddress, externalAsset Asset, nativeAssetAmount sdk.Uint, externalAssetAmount sdk.Uint) MsgCreatePool {
return MsgCreatePool{Signer: signer.String(), ExternalAsset: &externalAsset, NativeAssetAmount: nativeAssetAmount, ExternalAssetAmount: externalAssetAmount}
}
func (m MsgCreatePool) Route() string {
return RouterKey
}
func (m MsgCreatePool) Type() string {
return "create_pool"
}
func (m MsgCreatePool) ValidateBasic() error {
if len(m.Signer) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Signer)
}
if !m.ExternalAsset.Validate() {
return sdkerrors.Wrap(ErrInValidAsset, m.ExternalAsset.Symbol)
}
if m.ExternalAsset.Equals(GetSettlementAsset()) {
return sdkerrors.Wrap(ErrInValidAsset, "External Asset cannot be rowan")
}
if !(m.NativeAssetAmount.GT(sdk.ZeroUint())) {
return sdkerrors.Wrap(ErrInValidAmount, m.NativeAssetAmount.String())
}
if !(m.ExternalAssetAmount.GT(sdk.ZeroUint())) {
return sdkerrors.Wrap(ErrInValidAmount, m.NativeAssetAmount.String())
}
return nil
}
func (m MsgCreatePool) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgCreatePool) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(m.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}