/
proposal_handler.go
124 lines (109 loc) · 3.62 KB
/
proposal_handler.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
// Copyright 2022 Evmos Foundation
// This file is part of the Evmos Network packages.
//
// Evmos 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 Evmos 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 Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE
package erc20
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/ethereum/go-ethereum/common"
"github.com/xpladev/ethermint/x/erc20/keeper"
"github.com/xpladev/ethermint/x/erc20/types"
)
// NewErc20ProposalHandler creates a governance handler to manage new proposal types.
func NewErc20ProposalHandler(k *keeper.Keeper) govv1beta1.Handler {
return func(ctx sdk.Context, content govv1beta1.Content) error {
// Check if the conversion is globally enabled
if !k.IsERC20Enabled(ctx) {
return errorsmod.Wrap(
types.ErrERC20Disabled, "registration is currently disabled by governance",
)
}
switch c := content.(type) {
case *types.RegisterCoinProposal:
return handleRegisterCoinProposal(ctx, k, c)
case *types.RegisterERC20Proposal:
return handleRegisterERC20Proposal(ctx, k, c)
case *types.ToggleTokenConversionProposal:
return handleToggleConversionProposal(ctx, k, c)
default:
return errorsmod.Wrapf(errortypes.ErrUnknownRequest, "unrecognized %s proposal content type: %T", types.ModuleName, c)
}
}
}
// handleRegisterCoinProposal handles the registration proposal for multiple
// native Cosmos coins
func handleRegisterCoinProposal(
ctx sdk.Context,
k *keeper.Keeper,
p *types.RegisterCoinProposal,
) error {
for _, metadata := range p.Metadata {
pair, err := k.RegisterCoin(ctx, metadata)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRegisterCoin,
sdk.NewAttribute(types.AttributeKeyCosmosCoin, pair.Denom),
sdk.NewAttribute(types.AttributeKeyERC20Token, pair.Erc20Address),
),
)
}
return nil
}
// handleRegisterERC20Proposal handles the registration proposal for multiple
// ERC20 tokens
func handleRegisterERC20Proposal(
ctx sdk.Context,
k *keeper.Keeper,
p *types.RegisterERC20Proposal,
) error {
for _, address := range p.Erc20Addresses {
pair, err := k.RegisterERC20(ctx, common.HexToAddress(address))
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRegisterERC20,
sdk.NewAttribute(types.AttributeKeyCosmosCoin, pair.Denom),
sdk.NewAttribute(types.AttributeKeyERC20Token, pair.Erc20Address),
),
)
}
return nil
}
// handleToggleConversionProposal handles the toggle proposal for a token pair
func handleToggleConversionProposal(
ctx sdk.Context,
k *keeper.Keeper,
p *types.ToggleTokenConversionProposal,
) error {
pair, err := k.ToggleConversion(ctx, p.Token)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeToggleTokenConversion,
sdk.NewAttribute(types.AttributeKeyCosmosCoin, pair.Denom),
sdk.NewAttribute(types.AttributeKeyERC20Token, pair.Erc20Address),
),
)
return nil
}