forked from ArableProtocol/acrechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal.go
166 lines (136 loc) · 5.42 KB
/
proposal.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
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
ethermint "github.com/evmos/ethermint/types"
)
// constants
const (
ProposalTypeRegisterCoin string = "RegisterCoin"
ProposalTypeRegisterERC20 string = "RegisterERC20"
ProposalTypeToggleTokenConversion string = "ToggleTokenConversion" // #nosec
)
// Implements Proposal Interface
var (
_ govtypes.Content = &RegisterCoinProposal{}
_ govtypes.Content = &RegisterERC20Proposal{}
_ govtypes.Content = &ToggleTokenConversionProposal{}
)
func init() {
govtypes.RegisterProposalType(ProposalTypeRegisterCoin)
govtypes.RegisterProposalType(ProposalTypeRegisterERC20)
govtypes.RegisterProposalType(ProposalTypeToggleTokenConversion)
govtypes.RegisterProposalTypeCodec(&RegisterCoinProposal{}, "erc20/RegisterCoinProposal")
govtypes.RegisterProposalTypeCodec(&RegisterERC20Proposal{}, "erc20/RegisterERC20Proposal")
govtypes.RegisterProposalTypeCodec(&ToggleTokenConversionProposal{}, "erc20/ToggleTokenConversionProposal")
}
// CreateDenomDescription generates a string with the coin description
func CreateDenomDescription(address string) string {
return fmt.Sprintf("Cosmos coin token representation of %s", address)
}
// CreateDenom generates a string the module name plus the address to avoid conflicts with names staring with a number
func CreateDenom(address string) string {
return fmt.Sprintf("%s/%s", ModuleName, address)
}
// NewRegisterCoinProposal returns new instance of RegisterCoinProposal
func NewRegisterCoinProposal(title, description string, coinMetadata banktypes.Metadata) govtypes.Content {
return &RegisterCoinProposal{
Title: title,
Description: description,
Metadata: coinMetadata,
}
}
// ProposalRoute returns router key for this proposal
func (*RegisterCoinProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type for this proposal
func (*RegisterCoinProposal) ProposalType() string {
return ProposalTypeRegisterCoin
}
// ValidateBasic performs a stateless check of the proposal fields
func (rtbp *RegisterCoinProposal) ValidateBasic() error {
if err := rtbp.Metadata.Validate(); err != nil {
return err
}
if err := ibctransfertypes.ValidateIBCDenom(rtbp.Metadata.Base); err != nil {
return err
}
if err := validateIBCVoucherMetadata(rtbp.Metadata); err != nil {
return err
}
return govtypes.ValidateAbstract(rtbp)
}
// validateIBCVoucherMetadata checks that the coin metadata fields are consistent
// with an IBC voucher denomination.
func validateIBCVoucherMetadata(metadata banktypes.Metadata) error {
// Check ibc/ denom
denomSplit := strings.SplitN(metadata.Base, "/", 2)
if denomSplit[0] == metadata.Base && strings.TrimSpace(metadata.Base) != "" {
// Not IBC
return nil
}
if len(denomSplit) != 2 || denomSplit[0] != ibctransfertypes.DenomPrefix {
// NOTE: should be unaccessible (covered on ValidateIBCDenom)
return fmt.Errorf("invalid metadata. %s denomination should be prefixed with the format 'ibc/", metadata.Base)
}
return nil
}
// ValidateErc20Denom checks if a denom is a valid erc20/
// denomination
func ValidateErc20Denom(denom string) error {
denomSplit := strings.SplitN(denom, "/", 2)
if len(denomSplit) != 2 || denomSplit[0] != ModuleName {
return fmt.Errorf("invalid denom. %s denomination should be prefixed with the format 'erc20/", denom)
}
return ethermint.ValidateAddress(denomSplit[1])
}
// NewRegisterERC20Proposal returns new instance of RegisterERC20Proposal
func NewRegisterERC20Proposal(title, description, erc20Addr string) govtypes.Content {
return &RegisterERC20Proposal{
Title: title,
Description: description,
Erc20Address: erc20Addr,
}
}
// ProposalRoute returns router key for this proposal
func (*RegisterERC20Proposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type for this proposal
func (*RegisterERC20Proposal) ProposalType() string {
return ProposalTypeRegisterERC20
}
// ValidateBasic performs a stateless check of the proposal fields
func (rtbp *RegisterERC20Proposal) ValidateBasic() error {
if err := ethermint.ValidateAddress(rtbp.Erc20Address); err != nil {
return sdkerrors.Wrap(err, "ERC20 address")
}
return govtypes.ValidateAbstract(rtbp)
}
// NewToggleTokenConversionProposal returns new instance of ToggleTokenConversionProposal
func NewToggleTokenConversionProposal(title, description string, token string) govtypes.Content {
return &ToggleTokenConversionProposal{
Title: title,
Description: description,
Token: token,
}
}
// ProposalRoute returns router key for this proposal
func (*ToggleTokenConversionProposal) ProposalRoute() string { return RouterKey }
// ProposalType returns proposal type for this proposal
func (*ToggleTokenConversionProposal) ProposalType() string {
return ProposalTypeToggleTokenConversion
}
// ValidateBasic performs a stateless check of the proposal fields
func (ttcp *ToggleTokenConversionProposal) ValidateBasic() error {
// check if the token is a hex address, if not, check if it is a valid SDK
// denom
if err := ethermint.ValidateAddress(ttcp.Token); err != nil {
if err := sdk.ValidateDenom(ttcp.Token); err != nil {
return err
}
}
return govtypes.ValidateAbstract(ttcp)
}