-
Notifications
You must be signed in to change notification settings - Fork 117
/
msgs.go
433 lines (346 loc) · 11.8 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
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
package types
import (
"encoding/json"
"fmt"
"strconv"
"strings"
gethCommon "github.com/ethereum/go-ethereum/common"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
burnGasCost = 60000000000 * 393000 // assuming 60gigawei gas prices
lockGasCost = 60000000000 * 393000
)
// NewMsgLock is a constructor function for MsgLock
func NewMsgLock(
ethereumChainID int64, cosmosSender sdk.AccAddress,
ethereumReceiver EthereumAddress, amount sdk.Int, symbol string, cethAmount sdk.Int) MsgLock {
return MsgLock{
EthereumChainId: ethereumChainID,
CosmosSender: cosmosSender.String(),
EthereumReceiver: ethereumReceiver.String(),
Amount: amount,
Symbol: symbol,
CethAmount: cethAmount,
}
}
// Route should return the name of the module
func (msg MsgLock) Route() string { return RouterKey }
// Type should return the action
func (msg MsgLock) Type() string { return "lock" }
// ValidateBasic runs stateless checks on the message
func (msg MsgLock) ValidateBasic() error {
if strconv.FormatInt(msg.EthereumChainId, 10) == "" {
return sdkerrors.Wrapf(ErrInvalidEthereumChainID, "%d", msg.EthereumChainId)
}
if msg.CosmosSender == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CosmosSender)
}
if msg.EthereumReceiver == "" {
return ErrInvalidEthAddress
}
if !gethCommon.IsHexAddress(msg.EthereumReceiver) {
return ErrInvalidEthAddress
}
if msg.Amount.LTE(sdk.NewInt(0)) {
return ErrInvalidAmount
}
// if you don't pay enough gas, this tx won't go through
if msg.CethAmount.LT(sdk.NewInt(lockGasCost)) {
return ErrCethAmount
}
if len(msg.Symbol) == 0 {
return ErrInvalidSymbol
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgLock) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgLock) GetSigners() []sdk.AccAddress {
cosmosSender, err := sdk.AccAddressFromBech32(msg.CosmosSender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{cosmosSender}
}
// NewMsgBurn is a constructor function for MsgBurn
func NewMsgBurn(
ethereumChainID int64, cosmosSender sdk.AccAddress,
ethereumReceiver EthereumAddress, amount sdk.Int, symbol string, cethAmount sdk.Int) MsgBurn {
return MsgBurn{
EthereumChainId: ethereumChainID,
CosmosSender: cosmosSender.String(),
EthereumReceiver: ethereumReceiver.String(),
Amount: amount,
Symbol: symbol,
CethAmount: cethAmount,
}
}
// Route should return the name of the module
func (msg MsgBurn) Route() string { return RouterKey }
// Type should return the action
func (msg MsgBurn) Type() string { return "burn" }
// ValidateBasic runs stateless checks on the message
func (msg MsgBurn) ValidateBasic() error {
if msg.EthereumChainId == 0 {
return sdkerrors.Wrapf(ErrInvalidEthereumChainID, "%d", msg.EthereumChainId)
}
if msg.CosmosSender == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CosmosSender)
}
if msg.EthereumReceiver == "" {
return ErrInvalidEthAddress
}
if !gethCommon.IsHexAddress(msg.EthereumReceiver) {
return ErrInvalidEthAddress
}
if msg.Amount.LTE(sdk.NewInt(0)) {
return ErrInvalidAmount
}
prefixLength := len(PeggedCoinPrefix)
if len(msg.Symbol) <= prefixLength+1 {
return ErrInvalidBurnSymbol
}
symbolPrefix := msg.Symbol[:prefixLength]
if symbolPrefix != PeggedCoinPrefix {
return ErrInvalidBurnSymbol
}
// check that enough ceth is sent to cover the gas cost.
if msg.CethAmount.LT(sdk.NewInt(burnGasCost)) {
return ErrCethAmount
}
symbolSuffix := msg.Symbol[prefixLength:]
if len(symbolSuffix) == 0 {
return ErrInvalidBurnSymbol
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgBurn) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgBurn) GetSigners() []sdk.AccAddress {
cosmosSender, err := sdk.AccAddressFromBech32(msg.CosmosSender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{cosmosSender}
}
// NewMsgCreateEthBridgeClaim is a constructor function for MsgCreateBridgeClaim
func NewMsgCreateEthBridgeClaim(ethBridgeClaim *EthBridgeClaim) MsgCreateEthBridgeClaim {
return MsgCreateEthBridgeClaim{
EthBridgeClaim: ethBridgeClaim,
}
}
// Route should return the name of the module
func (msg MsgCreateEthBridgeClaim) Route() string { return RouterKey }
// Type should return the action
func (msg MsgCreateEthBridgeClaim) Type() string { return "create_bridge_claim" }
// ValidateBasic runs stateless checks on the message
func (msg MsgCreateEthBridgeClaim) ValidateBasic() error {
if msg.EthBridgeClaim.CosmosReceiver == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.EthBridgeClaim.CosmosReceiver)
}
if msg.EthBridgeClaim.ValidatorAddress == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.EthBridgeClaim.ValidatorAddress)
}
if msg.EthBridgeClaim.Nonce < 0 {
return ErrInvalidEthNonce
}
if !gethCommon.IsHexAddress(msg.EthBridgeClaim.EthereumSender) {
return ErrInvalidEthAddress
}
if !gethCommon.IsHexAddress(msg.EthBridgeClaim.BridgeContractAddress) {
return ErrInvalidEthAddress
}
if !gethCommon.IsHexAddress(msg.EthBridgeClaim.TokenContractAddress) {
return ErrInvalidEthAddress
}
if strings.ToLower(msg.EthBridgeClaim.Symbol) == "eth" &&
NewEthereumAddress(msg.EthBridgeClaim.TokenContractAddress) != NewEthereumAddress("0x0000000000000000000000000000000000000000") {
return ErrInvalidEthSymbol
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgCreateEthBridgeClaim) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
}
// GetSigners defines whose signature is required
func (msg MsgCreateEthBridgeClaim) GetSigners() []sdk.AccAddress {
validatorAddress, err := sdk.ValAddressFromBech32(msg.EthBridgeClaim.ValidatorAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sdk.AccAddress(validatorAddress)}
}
// NewMsgUpdateCethReceiverAccount is a constructor function for MsgUpdateCethReceiverAccount
func NewMsgUpdateCethReceiverAccount(cosmosSender sdk.AccAddress,
cethReceiverAccount sdk.AccAddress) MsgUpdateCethReceiverAccount {
return MsgUpdateCethReceiverAccount{
CosmosSender: cosmosSender.String(),
CethReceiverAccount: cethReceiverAccount.String(),
}
}
// Route should return the name of the module
func (msg MsgUpdateCethReceiverAccount) Route() string { return RouterKey }
// Type should return the action
func (msg MsgUpdateCethReceiverAccount) Type() string { return "update_ceth_receiver_account" }
// ValidateBasic runs stateless checks on the message
func (msg MsgUpdateCethReceiverAccount) ValidateBasic() error {
if msg.CosmosSender == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CosmosSender)
}
if msg.CethReceiverAccount == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CethReceiverAccount)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgUpdateCethReceiverAccount) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
}
// GetSigners defines whose signature is required
func (msg MsgUpdateCethReceiverAccount) GetSigners() []sdk.AccAddress {
cosmosSender, err := sdk.AccAddressFromBech32(msg.CosmosSender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{cosmosSender}
}
// NewMsgRescueCeth is a constructor function for NewMsgRescueCeth
func NewMsgRescueCeth(cosmosSender sdk.AccAddress, cosmosReceiver sdk.AccAddress, cethAmount sdk.Int) MsgRescueCeth {
return MsgRescueCeth{
CosmosSender: cosmosSender.String(),
CosmosReceiver: cosmosReceiver.String(),
CethAmount: cethAmount,
}
}
// Route should return the name of the module
func (msg MsgRescueCeth) Route() string { return RouterKey }
// Type should return the action
func (msg MsgRescueCeth) Type() string { return "rescue_ceth" }
// ValidateBasic runs stateless checks on the message
func (msg MsgRescueCeth) ValidateBasic() error {
if msg.CosmosSender == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CosmosSender)
}
if msg.CosmosReceiver == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CosmosReceiver)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgRescueCeth) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
}
// GetSigners defines whose signature is required
func (msg MsgRescueCeth) GetSigners() []sdk.AccAddress {
cosmosSender, err := sdk.AccAddressFromBech32(msg.CosmosSender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{cosmosSender}
}
// NewMsgUpdateWhiteListValidator is a constructor function for MsgUpdateWhiteListValidator
func NewMsgUpdateWhiteListValidator(cosmosSender sdk.AccAddress,
validator sdk.ValAddress, operationType string) MsgUpdateWhiteListValidator {
return MsgUpdateWhiteListValidator{
CosmosSender: cosmosSender.String(),
Validator: validator.String(),
OperationType: operationType,
}
}
// Route should return the name of the module
func (msg MsgUpdateWhiteListValidator) Route() string { return RouterKey }
// Type should return the action
func (msg MsgUpdateWhiteListValidator) Type() string { return "update_whitelist_validator" }
// ValidateBasic runs stateless checks on the message
func (msg MsgUpdateWhiteListValidator) ValidateBasic() error {
if msg.CosmosSender == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.CosmosSender)
}
if msg.Validator == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Validator)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgUpdateWhiteListValidator) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
}
// GetSigners defines whose signature is required
func (msg MsgUpdateWhiteListValidator) GetSigners() []sdk.AccAddress {
cosmosSender, err := sdk.AccAddressFromBech32(msg.CosmosSender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{cosmosSender}
}
// MapOracleClaimsToEthBridgeClaims maps a set of generic oracle claim data into EthBridgeClaim objects
func MapOracleClaimsToEthBridgeClaims(
ethereumChainID int64,
bridgeContract EthereumAddress,
nonce int64,
symbol string,
tokenContract EthereumAddress,
ethereumSender EthereumAddress,
oracleValidatorClaims map[string]string,
f func(int64, EthereumAddress, int64, EthereumAddress, sdk.ValAddress, string) (*EthBridgeClaim, error),
) ([]*EthBridgeClaim, error) {
mappedClaims := make([]*EthBridgeClaim, len(oracleValidatorClaims))
i := 0
for validatorBech32, validatorClaim := range oracleValidatorClaims {
validatorAddress, parseErr := sdk.ValAddressFromBech32(validatorBech32)
if parseErr != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("failed to parse claim: %s", parseErr))
}
mappedClaim, err := f(
ethereumChainID, bridgeContract, nonce, ethereumSender, validatorAddress, validatorClaim)
if err != nil {
return nil, err
}
mappedClaims[i] = mappedClaim
i++
}
return mappedClaims, nil
}
var _ sdk.Msg = &MsgSetBlacklist{}
// Route should return the name of the module
func (msg MsgSetBlacklist) Route() string { return RouterKey }
// Type should return the action
func (msg MsgSetBlacklist) Type() string { return "setblacklist" }
func (msg MsgSetBlacklist) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg *MsgSetBlacklist) ValidateBasic() error {
return nil
}
func (msg *MsgSetBlacklist) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}