-
Notifications
You must be signed in to change notification settings - Fork 47
/
proposal_handler.go
237 lines (216 loc) · 8.64 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
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
package nativedex
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/AltheaFoundation/althea-L1/contracts"
"github.com/AltheaFoundation/althea-L1/x/nativedex/keeper"
"github.com/AltheaFoundation/althea-L1/x/nativedex/types"
)
// Callpaths to be used in governance proposals
const BOOT_PATH uint16 = 0
const COLD_PATH uint16 = 3
const SAFEMODE_PATH uint16 = 9999
const (
UPGRADE_PROXY_CMD uint8 = 21
COLLECT_TREASURY_CMD uint8 = 40
SET_TREASURY_CMD uint8 = 41
AUTHORITY_TRANSFER_CMD uint8 = 20
HOT_PATH_OPEN_CMD uint8 = 22
SET_SAFE_MODE_CMD uint8 = 23
)
// Return governance handler to process dex governance proposals
func NewNativeDexProposalHandler(k *keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.UpgradeProxyProposal:
return handleUpgradeProxyProposal(ctx, k, c)
case *types.CollectTreasuryProposal:
return handleCollectTreasuryProposal(ctx, k, c)
case *types.SetTreasuryProposal:
return handleSetTreasuryProposal(ctx, k, c)
case *types.AuthorityTransferProposal:
return handleAuthorityTransferProposal(ctx, k, c)
case *types.HotPathOpenProposal:
return handleHotPathOpenProposal(ctx, k, c)
case *types.SetSafeModeProposal:
return handleSetSafeModeProposal(ctx, k, c)
case *types.TransferGovernanceProposal:
return handleTransferGovernanceProposal(ctx, k, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s proposal content type: %T", types.ModuleName, c)
}
}
}
// nolint: dupl
func handleUpgradeProxyProposal(ctx sdk.Context, k *keeper.Keeper, p *types.UpgradeProxyProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
callpath := BOOT_PATH
cmd_code := UPGRADE_PROXY_CMD
// protocolCmd ABI: (uint8, address, uint16)
encodedProtocolCmd, err := contracts.EncodeTypes([]string{"uint8", "address", "uint16"}, []interface{}{cmd_code, common.HexToAddress(md.CallpathAddress), uint16(md.CallpathIndex)})
if err != nil {
ctx.Logger().Error("Encode protocolCmd args UpgradeProxyProposal", "err", err)
return err
}
// CrocPolicy ABI: treasuryResolution (address, uint16, bytes, bool)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "treasuryResolution", k.GetNativeDexAddress(ctx), callpath, encodedProtocolCmd, true)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy.treasuryResolution() for UpgradeProxyProposal", "err", err)
return err
}
return nil
}
// nolint: dupl
func handleCollectTreasuryProposal(ctx sdk.Context, k *keeper.Keeper, p *types.CollectTreasuryProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
callpath := COLD_PATH
if p.InSafeMode {
callpath = SAFEMODE_PATH
}
cmd_code := COLLECT_TREASURY_CMD
// protocolCmd ABI: (uint8, address)
encodedProtocolCmd, err := contracts.EncodeTypes([]string{"uint8", "address"}, []interface{}{cmd_code, common.HexToAddress(md.TokenAddress)})
if err != nil {
ctx.Logger().Error("Encode protocolCmd args CollectTreasuryProposal", "err", err)
return err
}
// CrocPolicy ABI: treasuryResolution (address, uint16, bytes, bool)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "treasuryResolution", k.GetNativeDexAddress(ctx), callpath, encodedProtocolCmd, true)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy. treasuryResolution() for CollectTreasuryProposal", "err", err)
return err
}
return nil
}
// nolint: dupl
func handleSetTreasuryProposal(ctx sdk.Context, k *keeper.Keeper, p *types.SetTreasuryProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
callpath := COLD_PATH
if p.InSafeMode {
callpath = SAFEMODE_PATH
}
cmd_code := SET_TREASURY_CMD
// protocolCmd ABI: (uint8, address)
encodedProtocolCmd, err := contracts.EncodeTypes([]string{"uint8", "address"}, []interface{}{cmd_code, common.HexToAddress(md.TreasuryAddress)})
if err != nil {
ctx.Logger().Error("Encode protocolCmd args SetTreasuryProposal", "err", err)
return err
}
// CrocPolicy ABI: treasuryResolution (address, uint16, bytes, bool)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "treasuryResolution", k.GetNativeDexAddress(ctx), callpath, encodedProtocolCmd, true)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy.treasuryResolution() for SetTreasuryProposal", "err", err)
return err
}
return nil
}
// nolint: dupl
func handleAuthorityTransferProposal(ctx sdk.Context, k *keeper.Keeper, p *types.AuthorityTransferProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
callpath := COLD_PATH
if p.InSafeMode {
callpath = SAFEMODE_PATH
}
cmd_code := AUTHORITY_TRANSFER_CMD
// protocolCmd ABI: (uint8, address)
encodedProtocolCmd, err := contracts.EncodeTypes([]string{"uint8", "address"}, []interface{}{cmd_code, common.HexToAddress(md.AuthAddress)})
if err != nil {
ctx.Logger().Error("Encode protocolCmd args AuthorityTransferProposal", "err", err)
return err
}
// CrocPolicy ABI: treasuryResolution (address, uint16, bytes, bool)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "treasuryResolution", k.GetNativeDexAddress(ctx), callpath, encodedProtocolCmd, true)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy.treasuryResolution() for AuthorityTransferProposal", "err", err)
return err
}
return nil
}
// nolint: dupl
func handleHotPathOpenProposal(ctx sdk.Context, k *keeper.Keeper, p *types.HotPathOpenProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
callpath := COLD_PATH
if p.InSafeMode {
callpath = SAFEMODE_PATH
}
cmd_code := HOT_PATH_OPEN_CMD
// protocolCmd ABI: (uint8, address)
encodedProtocolCmd, err := contracts.EncodeTypes([]string{"uint8", "bool"}, []interface{}{cmd_code, md.Open})
if err != nil {
ctx.Logger().Error("Encode protocolCmd args HotPathOpenProposal", "err", err)
return err
}
// CrocPolicy ABI: treasuryResolution (address, uint16, bytes, bool)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "treasuryResolution", k.GetNativeDexAddress(ctx), callpath, encodedProtocolCmd, true)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy.treasuryResolution() for HotPathOpenProposal", "err", err)
return err
}
return nil
}
// nolint: dupl
func handleSetSafeModeProposal(ctx sdk.Context, k *keeper.Keeper, p *types.SetSafeModeProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
callpath := COLD_PATH
if p.InSafeMode {
callpath = SAFEMODE_PATH
}
cmd_code := SET_SAFE_MODE_CMD
// protocolCmd ABI: (uint8, address)
encodedProtocolCmd, err := contracts.EncodeTypes([]string{"uint8", "bool"}, []interface{}{cmd_code, md.LockDex})
if err != nil {
ctx.Logger().Error("Encode protocolCmd args SetSafeModeProposal", "err", err)
return err
}
// CrocPolicy ABI: treasuryResolution (address, uint16, bytes, bool)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "treasuryResolution", k.GetNativeDexAddress(ctx), callpath, encodedProtocolCmd, true)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy.treasuryResolution() for SetSafeModeProposal", "err", err)
return err
}
return nil
}
// nolint: dupl
func handleTransferGovernanceProposal(ctx sdk.Context, k *keeper.Keeper, p *types.TransferGovernanceProposal) error {
err := p.ValidateBasic()
if err != nil {
return err
}
md := p.GetMetadata()
// This proposal does not directly work on the DEX, so no callpath nor cmd_code are used
// CrocPolicy ABI: transferGovernance (address ops, address treasury, address emergency)
ops := common.HexToAddress(md.Ops)
emergency := common.HexToAddress(md.Emergency)
_, err = k.EVMKeeper.CallEVM(ctx, contracts.CrocPolicyContract.ABI, types.ModuleEVMAddress, k.GetVerifiedCrocPolicyAddress(ctx), true, "transferGovernance", ops, types.ModuleEVMAddress, emergency)
if err != nil {
ctx.Logger().Error("Unable to call CrocPolicy.transferGovernance() for TransferGovernanceProposal", "err", err)
return err
}
return nil
}