forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx.go
177 lines (145 loc) · 5.51 KB
/
tx.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
package cli
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
evertypes "github.com/EscanBE/evermint/v12/types"
"github.com/EscanBE/evermint/v12/x/revenue/v1/types"
)
// NewTxCmd returns a root CLI command handler for certain modules/revenue
// transaction commands.
func NewTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "revenue subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
NewRegisterRevenue(),
NewCancelRevenue(),
NewUpdateRevenue(),
)
return txCmd
}
// NewRegisterRevenue returns a CLI command handler for registering a
// contract for fee distribution
func NewRegisterRevenue() *cobra.Command {
cmd := &cobra.Command{
Use: "register CONTRACT_HEX NONCE... [WITHDRAWER_BECH32]",
Short: "Register a contract for fee distribution. **NOTE** Please ensure, that the deployer of the contract (or the factory that deployes the contract) is an account that is owned by your project, to avoid that an individual deployer who leaves your project becomes malicious.",
Long: "Register a contract for fee distribution.\nOnly the contract deployer can register a contract.\nProvide the account nonce(s) used to derive the contract address. E.g.: you have an account nonce of 4 when you send a deployment transaction for a contract A; you use this contract as a factory, to create another contract B. If you register A, the nonces value is \"4\". If you register B, the nonces value is \"4,1\" (B is the first contract created by A). \nThe withdrawer address defaults to the deployer address if not provided.",
Args: cobra.RangeArgs(2, 3),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
var withdrawer string
deployer := cliCtx.GetFromAddress()
contract := args[0]
if err := evertypes.ValidateNonZeroAddress(contract); err != nil {
return fmt.Errorf("invalid contract hex address %w", err)
}
var nonces []uint64
if err = json.Unmarshal([]byte("["+args[1]+"]"), &nonces); err != nil {
return fmt.Errorf("invalid nonces %w", err)
}
if len(args) == 3 {
withdrawer = args[2]
if _, err := sdk.AccAddressFromBech32(withdrawer); err != nil {
return fmt.Errorf("invalid withdrawer bech32 address %w", err)
}
}
// If withdraw address is the same as contract deployer, remove the
// field for avoiding storage bloat
if deployer.String() == withdrawer {
withdrawer = ""
}
msg := &types.MsgRegisterRevenue{
ContractAddress: contract,
DeployerAddress: deployer.String(),
WithdrawerAddress: withdrawer,
Nonces: nonces,
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewCancelRevenue returns a CLI command handler for canceling a
// contract for fee distribution
func NewCancelRevenue() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel CONTRACT_HEX",
Short: "Cancel a contract from fee distribution",
Long: "Cancel a contract from fee distribution. The deployer will no longer receive fees from users interacting with the contract. \nOnly the contract deployer can cancel a contract.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
deployer := cliCtx.GetFromAddress()
contract := args[0]
if err := evertypes.ValidateNonZeroAddress(contract); err != nil {
return fmt.Errorf("invalid contract hex address %w", err)
}
msg := &types.MsgCancelRevenue{
ContractAddress: contract,
DeployerAddress: deployer.String(),
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewUpdateRevenue returns a CLI command handler for updating the withdraw
// address of a contract for fee distribution
func NewUpdateRevenue() *cobra.Command {
cmd := &cobra.Command{
Use: "update CONTRACT_HEX WITHDRAWER_BECH32",
Short: "Update withdrawer address for a contract registered for fee distribution.",
Long: "Update withdrawer address for a contract registered for fee distribution. \nOnly the contract deployer can update the withdrawer address.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
deployer := cliCtx.GetFromAddress()
contract := args[0]
if err := evertypes.ValidateNonZeroAddress(contract); err != nil {
return fmt.Errorf("invalid contract hex address %w", err)
}
withdrawer := args[1]
if _, err := sdk.AccAddressFromBech32(withdrawer); err != nil {
return fmt.Errorf("invalid withdrawer bech32 address %w", err)
}
msg := &types.MsgUpdateRevenue{
ContractAddress: contract,
DeployerAddress: deployer.String(),
WithdrawerAddress: withdrawer,
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}