Skip to content

Commit

Permalink
add fee whitelist cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Apr 16, 2024
1 parent 1e0835b commit 1af9f7e
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion x/opchild/client/cli/genesis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cli

import (
"bufio"
"encoding/json"
"fmt"

"cosmossdk.io/core/address"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -19,7 +22,7 @@ import (
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
)

// AddGenesisValidatorCmd builds the application's gentx command.
// AddGenesisValidatorCmd builds the application's add-genesis-validator command.
func AddGenesisValidatorCmd(mbm module.BasicManager, txEncCfg client.TxEncodingConfig, genBalIterator genutiltypes.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "add-genesis-validator [key_name]",
Expand Down Expand Up @@ -120,3 +123,87 @@ $ %s add-genesis-validator my-key-name --home=/path/to/home/dir --keyring-backen

return cmd
}

// AddFeeWhitelistCmd builds the application's fee-whitelist command.
func AddFeeWhitelistCmd(defaultNodeHome string, addressCodec address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "add-fee-whitelist [address_or_key_name]",
Short: "Add an address to the fee whitelist",
Args: cobra.ExactArgs(1),
Long: `Add an address to fee whitelist of genesis.json. The provided account must specify
the account address or key name . If a key name is given,
the address will be looked up in the local Keybase.
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config

config.SetRoot(clientCtx.HomeDir)

var kr keyring.Keyring
addr, err := addressCodec.StringToBytes(args[0])
if err != nil {
inBuf := bufio.NewReader(cmd.InOrStdin())
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)

if keyringBackend != "" && clientCtx.Keyring == nil {
var err error
kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf, clientCtx.Codec)
if err != nil {
return err
}
} else {
kr = clientCtx.Keyring
}

k, err := kr.Key(args[0])
if err != nil {
return fmt.Errorf("failed to get address from Keyring: %w", err)
}

addr, err = k.GetAddress()
if err != nil {
return err
}
}
addrStr, err := addressCodec.BytesToString(addr)
if err != nil {
return err
}

genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

cdc := clientCtx.Codec
opchildState := opchildtypes.GetGenesisStateFromAppState(cdc, appState)
opchildState.Params.FeeWhitelist = append(opchildState.Params.FeeWhitelist, addrStr)

opchildGenStateBz, err := cdc.MarshalJSON(opchildState)
if err != nil {
return fmt.Errorf("failed to marshal opchild genesis state: %w", err)
}
appState[opchildtypes.ModuleName] = opchildGenStateBz

appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}

genDoc.AppState = appStateJSON
if err = genutil.ExportGenesisFile(genDoc, config.GenesisFile()); err != nil {
return errors.Wrap(err, "Failed to export genesis file")
}

return nil
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
flags.AddTxFlagsToCmd(cmd)

return cmd
}

0 comments on commit 1af9f7e

Please sign in to comment.