-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal_handler.go
97 lines (83 loc) · 2.85 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
package erc20
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "cosmossdk.io/x/gov/types"
"github.com/ethereum/go-ethereum/common"
"github.com/furyanrasta/blackfury/v3/x/erc20/keeper"
"github.com/furyanrasta/blackfury/v3/x/erc20/types"
)
// NewErc20ProposalHandler creates a governance handler to manage new proposal types.
// It enables RegisterTokenPairProposal to propose a registration of token mapping
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.ToggleTokenRelayProposal:
return handleToggleRelayProposal(ctx, k, c)
case *types.UpdateTokenPairERC20Proposal:
return handleUpdateTokenPairERC20Proposal(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 handleToggleRelayProposal(ctx sdk.Context, k *keeper.Keeper, p *types.ToggleTokenRelayProposal) error {
pair, err := k.ToggleRelay(ctx, p.Token)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeToggleTokenRelay,
sdk.NewAttribute(types.AttributeKeyCosmosCoin, pair.Denom),
sdk.NewAttribute(types.AttributeKeyERC20Token, pair.Erc20Address),
),
)
return nil
}
func handleUpdateTokenPairERC20Proposal(ctx sdk.Context, k *keeper.Keeper, p *types.UpdateTokenPairERC20Proposal) error {
pair, err := k.UpdateTokenPairERC20(ctx, p.GetERC20Address(), p.GetNewERC20Address())
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeUpdateTokenPairERC20,
sdk.NewAttribute(types.AttributeKeyCosmosCoin, pair.Denom),
sdk.NewAttribute(types.AttributeKeyERC20Token, pair.Erc20Address),
),
)
return nil
}