Skip to content

Commit

Permalink
feat(cli): add update validator commission cli (#477)
Browse files Browse the repository at this point in the history
Added a CLI for updating validator commission

issue: none
  • Loading branch information
0xHansLee authored Feb 14, 2025
1 parent 4fc3ae1 commit d4af730
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions client/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func bindValidatorUnjailOnBehalfFlags(cmd *cobra.Command, cfg *unjailConfig) {
cmd.Flags().StringVar(&cfg.ValidatorPubKey, "validator-pubkey", "", "Validator's hex-encoded compressed 33-byte secp256k1 public key")
}

func bindValidatorUpdateCommissionFlags(cmd *cobra.Command, cfg *updateCommissionConfig) {
bindValidatorBaseFlags(cmd, &cfg.baseConfig)
cmd.Flags().Uint32Var(&cfg.CommissionRate, "commission-rate", 0, "Commission rate to update (e.g. 1000 for 10%)")
}

// Flag Validation

func validateFlags(cmd *cobra.Command, flags []string) error {
Expand Down Expand Up @@ -280,6 +285,10 @@ func validateValidatorUnjailOnBehalfFlags(cmd *cobra.Command) error {
return validateFlags(cmd, []string{"validator-pubkey"})
}

func validateUpdateValidatorCommissionFlags(cmd *cobra.Command) error {
return validateFlags(cmd, []string{"commission-rate"})
}

func validateMinStakeAmount(ctx context.Context, cfg *stakeConfig) error {
stakeAmount, ok := new(big.Int).SetString(cfg.StakeAmount, 10)
if !ok {
Expand Down
63 changes: 63 additions & 0 deletions client/cmd/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ type unjailConfig struct {
ValidatorPubKey string
}

type updateCommissionConfig struct {
baseConfig
CommissionRate uint32
}

type operatorConfig struct {
baseConfig
Operator string
Expand Down Expand Up @@ -165,6 +170,7 @@ func newValidatorCmds() *cobra.Command {
newValidatorSetRewardsAddressCmd(),
newValidatorUnjailCmd(),
newValidatorUnjailOnBehalfCmd(),
newUpdateValidatorCommission(),
)

return cmd
Expand Down Expand Up @@ -484,6 +490,29 @@ func newValidatorUnjailOnBehalfCmd() *cobra.Command {
return cmd
}

func newUpdateValidatorCommission() *cobra.Command {
var cfg updateCommissionConfig

cmd := &cobra.Command{
Use: "update-validator-commission",
Short: "Update the commission rate of validator",
Args: cobra.NoArgs,
PreRunE: func(_ *cobra.Command, _ []string) error {
return initializeBaseConfig(&cfg.baseConfig)
},
RunE: runValidatorCommand(
validateUpdateValidatorCommissionFlags,
func(ctx context.Context) error {
return updateValidatorCommission(ctx, cfg)
},
),
}

bindValidatorUpdateCommissionFlags(cmd, &cfg)

return cmd
}

func runValidatorCommand(
validate func(cmd *cobra.Command) error,
execute func(ctx context.Context) error,
Expand Down Expand Up @@ -972,6 +1001,40 @@ func unjailOnBehalf(ctx context.Context, cfg unjailConfig) error {
return nil
}

func updateValidatorCommission(ctx context.Context, cfg updateCommissionConfig) error {
privKeyBytes, err := hex.DecodeString(cfg.PrivateKey)
if err != nil {
return errors.Wrap(err, "failed to decode private key")
}

compressedValidatorPubKeyBytes, err := privKeyToCmpPubKey(privKeyBytes)
if err != nil {
return errors.Wrap(err, "failed to get compressed pub key from private key")
}

result, err := prepareAndReadContract(ctx, &cfg.baseConfig, "fee")
if err != nil {
return err
}

var updateCommissionFee *big.Int
err = cfg.ABI.UnpackIntoInterface(&updateCommissionFee, "fee", result)
if err != nil {
return errors.Wrap(err, "failed to unpack updateValidatorCommissionFee")
}

fmt.Printf("Update validator commission fee: %s\n", updateCommissionFee.String())

_, err = prepareAndExecuteTransaction(ctx, &cfg.baseConfig, "updateValidatorCommission", updateCommissionFee, compressedValidatorPubKeyBytes, cfg.CommissionRate)
if err != nil {
return err
}

fmt.Println("Validator commission successfully updated!")

return nil
}

func initializeBaseConfig(cfg *baseConfig) error {
if cfg.PrivateKey == "" {
loadEnv()
Expand Down

0 comments on commit d4af730

Please sign in to comment.