-
Notifications
You must be signed in to change notification settings - Fork 859
/
proposals.go
289 lines (243 loc) · 10 KB
/
proposals.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
package keeper
import (
"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"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tharsis/evmos/v3/contracts"
"github.com/tharsis/evmos/v3/x/erc20/types"
)
// RegisterCoin deploys an erc20 contract and creates the token pair for the existing cosmos coin
func (k Keeper) RegisterCoin(ctx sdk.Context, coinMetadata banktypes.Metadata) (*types.TokenPair, error) {
// check if the conversion is globally enabled
params := k.GetParams(ctx)
if !params.EnableErc20 {
return nil, sdkerrors.Wrap(types.ErrERC20Disabled, "registration is currently disabled by governance")
}
// prohibit denominations that contain the evm denom
if strings.Contains(coinMetadata.Base, "evm") {
return nil, sdkerrors.Wrapf(types.ErrEVMDenom, "cannot register the EVM denomination %s", coinMetadata.Base)
}
// check if the denomination already registered
if k.IsDenomRegistered(ctx, coinMetadata.Name) {
return nil, sdkerrors.Wrapf(types.ErrTokenPairAlreadyExists, "coin denomination already registered: %s", coinMetadata.Name)
}
// check if the coin exists by ensuring the supply is set
if !k.bankKeeper.HasSupply(ctx, coinMetadata.Base) {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidCoins,
"base denomination '%s' cannot have a supply of 0", coinMetadata.Base,
)
}
if err := k.verifyMetadata(ctx, coinMetadata); err != nil {
return nil, sdkerrors.Wrapf(types.ErrInternalTokenPair, "coin metadata is invalid %s", coinMetadata.Name)
}
addr, err := k.DeployERC20Contract(ctx, coinMetadata)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to create wrapped coin denom metadata for ERC20")
}
pair := types.NewTokenPair(addr, coinMetadata.Base, true, types.OWNER_MODULE)
k.SetTokenPair(ctx, pair)
k.SetDenomMap(ctx, pair.Denom, pair.GetID())
k.SetERC20Map(ctx, common.HexToAddress(pair.Erc20Address), pair.GetID())
return &pair, nil
}
// verify if the metadata matches the existing one, if not it sets it to the store
func (k Keeper) verifyMetadata(ctx sdk.Context, coinMetadata banktypes.Metadata) error {
meta, found := k.bankKeeper.GetDenomMetaData(ctx, coinMetadata.Base)
if !found {
k.bankKeeper.SetDenomMetaData(ctx, coinMetadata)
return nil
}
// If it already existed, Check that is equal to what is stored
return types.EqualMetadata(meta, coinMetadata)
}
// DeployERC20Contract creates and deploys an ERC20 contract on the EVM with the
// erc20 module account as owner.
func (k Keeper) DeployERC20Contract(
ctx sdk.Context,
coinMetadata banktypes.Metadata,
) (common.Address, error) {
decimals := uint8(coinMetadata.DenomUnits[0].Exponent)
ctorArgs, err := contracts.ERC20MinterBurnerDecimalsContract.ABI.Pack(
"",
coinMetadata.Name,
coinMetadata.Symbol,
decimals,
)
if err != nil {
return common.Address{}, sdkerrors.Wrapf(types.ErrABIPack, "coin metadata is invalid %s: %s", coinMetadata.Name, err.Error())
}
data := make([]byte, len(contracts.ERC20MinterBurnerDecimalsContract.Bin)+len(ctorArgs))
copy(data[:len(contracts.ERC20MinterBurnerDecimalsContract.Bin)], contracts.ERC20MinterBurnerDecimalsContract.Bin)
copy(data[len(contracts.ERC20MinterBurnerDecimalsContract.Bin):], ctorArgs)
nonce, err := k.accountKeeper.GetSequence(ctx, types.ModuleAddress.Bytes())
if err != nil {
return common.Address{}, err
}
contractAddr := crypto.CreateAddress(types.ModuleAddress, nonce)
_, err = k.CallEVMWithData(ctx, types.ModuleAddress, nil, data, true)
if err != nil {
return common.Address{}, sdkerrors.Wrapf(err, "failed to deploy contract for %s", coinMetadata.Name)
}
return contractAddr, nil
}
// RegisterERC20 creates a cosmos coin and registers the token pair between the coin and the ERC20
func (k Keeper) RegisterERC20(ctx sdk.Context, contract common.Address) (*types.TokenPair, error) {
params := k.GetParams(ctx)
if !params.EnableErc20 {
return nil, sdkerrors.Wrap(types.ErrERC20Disabled, "registration is currently disabled by governance")
}
if k.IsERC20Registered(ctx, contract) {
return nil, sdkerrors.Wrapf(types.ErrTokenPairAlreadyExists, "token ERC20 contract already registered: %s", contract.String())
}
metadata, err := k.CreateCoinMetadata(ctx, contract)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to create wrapped coin denom metadata for ERC20")
}
pair := types.NewTokenPair(contract, metadata.Name, true, types.OWNER_EXTERNAL)
k.SetTokenPair(ctx, pair)
k.SetDenomMap(ctx, pair.Denom, pair.GetID())
k.SetERC20Map(ctx, common.HexToAddress(pair.Erc20Address), pair.GetID())
return &pair, nil
}
// CreateCoinMetadata generates the metadata to represent the ERC20 token on evmos.
func (k Keeper) CreateCoinMetadata(ctx sdk.Context, contract common.Address) (*banktypes.Metadata, error) {
strContract := contract.String()
erc20Data, err := k.QueryERC20(ctx, contract)
if err != nil {
return nil, err
}
_, found := k.bankKeeper.GetDenomMetaData(ctx, types.CreateDenom(strContract))
if found {
// metadata already exists; exit
return nil, sdkerrors.Wrap(types.ErrInternalTokenPair, "denom metadata already registered")
}
if k.IsDenomRegistered(ctx, types.CreateDenom(strContract)) {
return nil, sdkerrors.Wrapf(types.ErrInternalTokenPair, "coin denomination already registered: %s", erc20Data.Name)
}
// base denomination
base := types.CreateDenom(strContract)
// create a bank denom metadata based on the ERC20 token ABI details
// metadata name is should always be the contract since it's the key
// to the bank store
metadata := banktypes.Metadata{
Description: types.CreateDenomDescription(strContract),
Base: base,
// NOTE: Denom units MUST be increasing
DenomUnits: []*banktypes.DenomUnit{
{
Denom: base,
Exponent: 0,
},
},
Name: types.CreateDenom(strContract),
Symbol: erc20Data.Symbol,
Display: base,
}
// only append metadata if decimals > 0, otherwise validation fails
if erc20Data.Decimals > 0 {
nameSanitized := types.SanitizeERC20Name(erc20Data.Name)
metadata.DenomUnits = append(
metadata.DenomUnits,
&banktypes.DenomUnit{
Denom: nameSanitized,
Exponent: uint32(erc20Data.Decimals),
},
)
metadata.Display = nameSanitized
}
if err := metadata.Validate(); err != nil {
return nil, sdkerrors.Wrapf(err, "ERC20 token data is invalid for contract %s", strContract)
}
k.bankKeeper.SetDenomMetaData(ctx, metadata)
return &metadata, nil
}
// ToggleRelay toggles relaying for a given token pair
func (k Keeper) ToggleRelay(ctx sdk.Context, token string) (types.TokenPair, error) {
id := k.GetTokenPairID(ctx, token)
if len(id) == 0 {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrTokenPairNotFound, "token '%s' not registered by id", token)
}
pair, found := k.GetTokenPair(ctx, id)
if !found {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrTokenPairNotFound, "token '%s' not registered", token)
}
pair.Enabled = !pair.Enabled
k.SetTokenPair(ctx, pair)
return pair, nil
}
// UpdateTokenPairERC20 updates the ERC20 token address for the registered token pair
func (k Keeper) UpdateTokenPairERC20(ctx sdk.Context, erc20Addr, newERC20Addr common.Address) (types.TokenPair, error) {
id := k.GetERC20Map(ctx, erc20Addr)
if len(id) == 0 {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrInternalTokenPair, "token %s not registered", erc20Addr)
}
pair, found := k.GetTokenPair(ctx, id)
if !found {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrTokenPairNotFound, "token '%s' not registered", erc20Addr)
}
// Get current stored metadata
metadata, found := k.bankKeeper.GetDenomMetaData(ctx, pair.Denom)
if !found {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrInternalTokenPair, "could not get metadata for %s", pair.Denom)
}
// safety check
if len(metadata.DenomUnits) == 0 {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrInternalTokenPair, "metadata denom units for %s cannot be empty", pair.Erc20Address)
}
// Get new erc20 values
erc20Data, err := k.QueryERC20(ctx, newERC20Addr)
if err != nil {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrInternalTokenPair, "could not get token %s erc20Data", newERC20Addr.String())
}
// compare metadata and ERC20 details
if metadata.Display != erc20Data.Name ||
metadata.Symbol != erc20Data.Symbol ||
metadata.Description != types.CreateDenomDescription(erc20Addr.String()) {
return types.TokenPair{}, sdkerrors.Wrapf(types.ErrInternalTokenPair, "metadata details (display, symbol, description) don't match the ERC20 details from %s ", pair.Erc20Address)
}
// check that the denom units contain one item with the same
// name and decimal values as the ERC20
found = false
for _, denomUnit := range metadata.DenomUnits {
// iterate denom units until we found the one with the ERC20 Name
if denomUnit.Denom != erc20Data.Name {
continue
}
// once found, check it has the same exponent
if denomUnit.Exponent != uint32(erc20Data.Decimals) {
return types.TokenPair{}, sdkerrors.Wrapf(
types.ErrInternalTokenPair, "metadata denom unit exponent doesn't match the ERC20 details from %s, expected %d, got %d",
pair.Erc20Address, erc20Data.Decimals, denomUnit.Exponent,
)
}
// break as metadata might contain denom units for higher exponents
found = true
break
}
if !found {
return types.TokenPair{}, sdkerrors.Wrapf(
types.ErrInternalTokenPair,
"metadata doesn't contain denom unit found for ERC20 %s (%s)",
erc20Data.Name, pair.Erc20Address,
)
}
// Update the metadata description with the new address
metadata.Description = types.CreateDenomDescription(newERC20Addr.String())
k.bankKeeper.SetDenomMetaData(ctx, metadata)
// Delete old token pair (id is changed because the ERC20 address was modifed)
k.DeleteTokenPair(ctx, pair)
// Update the address
pair.Erc20Address = newERC20Addr.Hex()
newID := pair.GetID()
// Set the new pair
k.SetTokenPair(ctx, pair)
// Overwrite the value because id was changed
k.SetDenomMap(ctx, pair.Denom, newID)
// Add the new address
k.SetERC20Map(ctx, newERC20Addr, newID)
return pair, nil
}