-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
150 lines (128 loc) · 4.94 KB
/
msg.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
// Copyright 2022 Serv Foundation
// This file is part of the Serv Network packages.
//
// Serv is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Serv packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Serv packages. If not, see https://github.com/twobitedd/serv/blob/main/LICENSE
package types
import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
"github.com/ethereum/go-ethereum/common"
)
var (
_ sdk.Msg = &MsgConvertCoin{}
_ sdk.Msg = &MsgConvertERC20{}
_ sdk.Msg = &MsgUpdateParams{}
)
const (
TypeMsgConvertCoin = "convert_coin"
TypeMsgConvertERC20 = "convert_ERC20"
)
// NewMsgConvertCoin creates a new instance of MsgConvertCoin
func NewMsgConvertCoin(coin sdk.Coin, receiver common.Address, sender sdk.AccAddress) *MsgConvertCoin { //nolint: interfacer
return &MsgConvertCoin{
Coin: coin,
Receiver: receiver.Hex(),
Sender: sender.String(),
}
}
// Route should return the name of the module
func (msg MsgConvertCoin) Route() string { return RouterKey }
// Type should return the action
func (msg MsgConvertCoin) Type() string { return TypeMsgConvertCoin }
// ValidateBasic runs stateless checks on the message
func (msg MsgConvertCoin) ValidateBasic() error {
if err := ValidateErc20Denom(msg.Coin.Denom); err != nil {
if err := ibctransfertypes.ValidateIBCDenom(msg.Coin.Denom); err != nil {
return err
}
}
if !msg.Coin.Amount.IsPositive() {
return errorsmod.Wrapf(errortypes.ErrInvalidCoins, "cannot mint a non-positive amount")
}
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return errorsmod.Wrap(err, "invalid sender address")
}
if !common.IsHexAddress(msg.Receiver) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid receiver hex address %s", msg.Receiver)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgConvertCoin) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgConvertCoin) GetSigners() []sdk.AccAddress {
addr := sdk.MustAccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{addr}
}
// NewMsgConvertERC20 creates a new instance of MsgConvertERC20
func NewMsgConvertERC20(amount math.Int, receiver sdk.AccAddress, contract, sender common.Address) *MsgConvertERC20 { //nolint: interfacer
return &MsgConvertERC20{
ContractAddress: contract.String(),
Amount: amount,
Receiver: receiver.String(),
Sender: sender.Hex(),
}
}
// Route should return the name of the module
func (msg MsgConvertERC20) Route() string { return RouterKey }
// Type should return the action
func (msg MsgConvertERC20) Type() string { return TypeMsgConvertERC20 }
// ValidateBasic runs stateless checks on the message
func (msg MsgConvertERC20) ValidateBasic() error {
if !common.IsHexAddress(msg.ContractAddress) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid contract hex address '%s'", msg.ContractAddress)
}
if !msg.Amount.IsPositive() {
return errorsmod.Wrapf(errortypes.ErrInvalidCoins, "cannot mint a non-positive amount")
}
_, err := sdk.AccAddressFromBech32(msg.Receiver)
if err != nil {
return errorsmod.Wrap(err, "invalid receiver address")
}
if !common.IsHexAddress(msg.Sender) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid sender hex address %s", msg.Sender)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgConvertERC20) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// GetSigners defines whose signature is required
func (msg MsgConvertERC20) GetSigners() []sdk.AccAddress {
addr := common.HexToAddress(msg.Sender)
return []sdk.AccAddress{addr.Bytes()}
}
// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr := sdk.MustAccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}
// ValidateBasic does a sanity check of the provided data
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "Invalid authority address")
}
return m.Params.Validate()
}
// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&m))
}