-
Notifications
You must be signed in to change notification settings - Fork 845
/
proposal_handler.go
77 lines (66 loc) · 2.23 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
package erc20
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/evmos/v8/x/erc20/keeper"
"github.com/evmos/evmos/v8/x/erc20/types"
)
// NewErc20ProposalHandler creates a governance handler to manage new proposal types.
func NewErc20ProposalHandler(k *keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
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 sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s proposal content type: %T", types.ModuleName, c)
}
}
}
func handleRegisterCoinProposal(ctx sdk.Context, k *keeper.Keeper, p *types.RegisterCoinProposal) error {
pair, err := k.RegisterCoin(ctx, p.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
}
func handleRegisterERC20Proposal(ctx sdk.Context, k *keeper.Keeper, p *types.RegisterERC20Proposal) error {
pair, err := k.RegisterERC20(ctx, common.HexToAddress(p.Erc20Address))
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
}
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
}