github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/state/cmd/state.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"strconv"
     7  
     8  	"cosmossdk.io/math"
     9  	"github.com/spf13/cobra"
    10  
    11  	cmdnode "github.com/celestiaorg/celestia-node/cmd"
    12  	"github.com/celestiaorg/celestia-node/state"
    13  )
    14  
    15  func init() {
    16  	Cmd.AddCommand(
    17  		accountAddressCmd,
    18  		balanceCmd,
    19  		balanceForAddressCmd,
    20  		transferCmd,
    21  		submitTxCmd,
    22  		cancelUnbondingDelegationCmd,
    23  		beginRedelegateCmd,
    24  		undelegateCmd,
    25  		delegateCmd,
    26  		queryDelegationCmd,
    27  		queryUnbondingCmd,
    28  		queryRedelegationCmd,
    29  	)
    30  }
    31  
    32  var Cmd = &cobra.Command{
    33  	Use:               "state [command]",
    34  	Short:             "Allows interaction with the State Module via JSON-RPC",
    35  	Args:              cobra.NoArgs,
    36  	PersistentPreRunE: cmdnode.InitClient,
    37  }
    38  
    39  var accountAddressCmd = &cobra.Command{
    40  	Use:   "account-address",
    41  	Short: "Retrieves the address of the node's account/signer.",
    42  	Args:  cobra.NoArgs,
    43  	RunE: func(cmd *cobra.Command, args []string) error {
    44  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
    45  		if err != nil {
    46  			return err
    47  		}
    48  		defer client.Close()
    49  
    50  		address, err := client.State.AccountAddress(cmd.Context())
    51  		return cmdnode.PrintOutput(address, err, nil)
    52  	},
    53  }
    54  
    55  var balanceCmd = &cobra.Command{
    56  	Use: "balance",
    57  	Short: "Retrieves the Celestia coin balance for the node's account/signer and verifies it against " +
    58  		"the corresponding block's AppHash.",
    59  	Args: cobra.NoArgs,
    60  	RunE: func(cmd *cobra.Command, args []string) error {
    61  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
    62  		if err != nil {
    63  			return err
    64  		}
    65  		defer client.Close()
    66  
    67  		balance, err := client.State.Balance(cmd.Context())
    68  		return cmdnode.PrintOutput(balance, err, nil)
    69  	},
    70  }
    71  
    72  var balanceForAddressCmd = &cobra.Command{
    73  	Use: "balance-for-address [address]",
    74  	Short: "Retrieves the Celestia coin balance for the given address and verifies the returned balance against " +
    75  		"the corresponding block's AppHash.",
    76  	Args: cobra.ExactArgs(1),
    77  	RunE: func(cmd *cobra.Command, args []string) error {
    78  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
    79  		if err != nil {
    80  			return err
    81  		}
    82  		defer client.Close()
    83  
    84  		addr, err := parseAddressFromString(args[0])
    85  		if err != nil {
    86  			return fmt.Errorf("error parsing an address:%v", err)
    87  		}
    88  
    89  		balance, err := client.State.BalanceForAddress(cmd.Context(), addr)
    90  		return cmdnode.PrintOutput(balance, err, nil)
    91  	},
    92  }
    93  
    94  var transferCmd = &cobra.Command{
    95  	Use:   "transfer [address] [amount] [fee] [gasLimit]",
    96  	Short: "Sends the given amount of coins from default wallet of the node to the given account address.",
    97  	Args:  cobra.ExactArgs(4),
    98  	RunE: func(cmd *cobra.Command, args []string) error {
    99  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   100  		if err != nil {
   101  			return err
   102  		}
   103  		defer client.Close()
   104  
   105  		addr, err := parseAddressFromString(args[0])
   106  		if err != nil {
   107  			return fmt.Errorf("error parsing an address:%v", err)
   108  		}
   109  
   110  		amount, err := strconv.ParseInt(args[1], 10, 64)
   111  		if err != nil {
   112  			return fmt.Errorf("error parsing an amount:%v", err)
   113  		}
   114  		fee, err := strconv.ParseInt(args[2], 10, 64)
   115  		if err != nil {
   116  			return fmt.Errorf("error parsing a fee:%v", err)
   117  		}
   118  		gasLimit, err := strconv.ParseUint(args[3], 10, 64)
   119  		if err != nil {
   120  			return fmt.Errorf("error parsing a gas limit:%v", err)
   121  		}
   122  
   123  		txResponse, err := client.State.Transfer(
   124  			cmd.Context(),
   125  			addr.Address.(state.AccAddress),
   126  			math.NewInt(amount),
   127  			math.NewInt(fee), gasLimit,
   128  		)
   129  		return cmdnode.PrintOutput(txResponse, err, nil)
   130  	},
   131  }
   132  
   133  var submitTxCmd = &cobra.Command{
   134  	Use:   "submit-tx [tx]",
   135  	Short: "Submits the given transaction/message to the Celestia network and blocks until the tx is included in a block.",
   136  	Args:  cobra.ExactArgs(1),
   137  	RunE: func(cmd *cobra.Command, args []string) error {
   138  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   139  		if err != nil {
   140  			return err
   141  		}
   142  		defer client.Close()
   143  
   144  		decoded, err := hex.DecodeString(args[0])
   145  		if err != nil {
   146  			return fmt.Errorf("failed to decode tx: %v", err)
   147  		}
   148  		txResponse, err := client.State.SubmitTx(
   149  			cmd.Context(),
   150  			decoded,
   151  		)
   152  		return cmdnode.PrintOutput(txResponse, err, nil)
   153  	},
   154  }
   155  
   156  var cancelUnbondingDelegationCmd = &cobra.Command{
   157  	Use:   "cancel-unbonding-delegation [address] [amount] [height] [fee] [gasLimit]",
   158  	Short: "Cancels a user's pending undelegation from a validator.",
   159  	Args:  cobra.ExactArgs(5),
   160  	RunE: func(cmd *cobra.Command, args []string) error {
   161  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   162  		if err != nil {
   163  			return err
   164  		}
   165  		defer client.Close()
   166  
   167  		addr, err := parseAddressFromString(args[0])
   168  		if err != nil {
   169  			return fmt.Errorf("error parsing an address:%v", err)
   170  		}
   171  
   172  		amount, err := strconv.ParseInt(args[1], 10, 64)
   173  		if err != nil {
   174  			return fmt.Errorf("error parsing an amount:%v", err)
   175  		}
   176  
   177  		height, err := strconv.ParseInt(args[2], 10, 64)
   178  		if err != nil {
   179  			return fmt.Errorf("error parsing a fee:%v", err)
   180  		}
   181  
   182  		fee, err := strconv.ParseInt(args[3], 10, 64)
   183  		if err != nil {
   184  			return fmt.Errorf("error parsing a fee:%v", err)
   185  		}
   186  
   187  		gasLimit, err := strconv.ParseUint(args[4], 10, 64)
   188  		if err != nil {
   189  			return fmt.Errorf("error parsing a gas limit:%v", err)
   190  		}
   191  
   192  		txResponse, err := client.State.CancelUnbondingDelegation(
   193  			cmd.Context(),
   194  			addr.Address.(state.ValAddress),
   195  			math.NewInt(amount),
   196  			math.NewInt(height),
   197  			math.NewInt(fee),
   198  			gasLimit,
   199  		)
   200  		return cmdnode.PrintOutput(txResponse, err, nil)
   201  	},
   202  }
   203  
   204  var beginRedelegateCmd = &cobra.Command{
   205  	Use:   "begin-redelegate [srcAddress] [dstAddress] [amount] [fee] [gasLimit]",
   206  	Short: "Sends a user's delegated tokens to a new validator for redelegation",
   207  	Args:  cobra.ExactArgs(5),
   208  	RunE: func(cmd *cobra.Command, args []string) error {
   209  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   210  		if err != nil {
   211  			return err
   212  		}
   213  		defer client.Close()
   214  
   215  		srcAddr, err := parseAddressFromString(args[0])
   216  		if err != nil {
   217  			return fmt.Errorf("error parsing an address:%v", err)
   218  		}
   219  
   220  		dstAddr, err := parseAddressFromString(args[1])
   221  		if err != nil {
   222  			return fmt.Errorf("error parsing an address:%v", err)
   223  		}
   224  
   225  		amount, err := strconv.ParseInt(args[2], 10, 64)
   226  		if err != nil {
   227  			return fmt.Errorf("error parsing an amount:%v", err)
   228  		}
   229  
   230  		fee, err := strconv.ParseInt(args[3], 10, 64)
   231  		if err != nil {
   232  			return fmt.Errorf("error parsing a fee:%v", err)
   233  		}
   234  		gasLimit, err := strconv.ParseUint(args[4], 10, 64)
   235  		if err != nil {
   236  			return fmt.Errorf("error parsing a gas limit:%v", err)
   237  		}
   238  
   239  		txResponse, err := client.State.BeginRedelegate(
   240  			cmd.Context(),
   241  			srcAddr.Address.(state.ValAddress),
   242  			dstAddr.Address.(state.ValAddress),
   243  			math.NewInt(amount),
   244  			math.NewInt(fee),
   245  			gasLimit,
   246  		)
   247  		return cmdnode.PrintOutput(txResponse, err, nil)
   248  	},
   249  }
   250  
   251  var undelegateCmd = &cobra.Command{
   252  	Use:   "undelegate [valAddress] [amount] [fee] [gasLimit]",
   253  	Short: "Undelegates a user's delegated tokens, unbonding them from the current validator.",
   254  	Args:  cobra.ExactArgs(4),
   255  	RunE: func(cmd *cobra.Command, args []string) error {
   256  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   257  		if err != nil {
   258  			return err
   259  		}
   260  		defer client.Close()
   261  
   262  		addr, err := parseAddressFromString(args[0])
   263  		if err != nil {
   264  			return fmt.Errorf("error parsing an address:%v", err)
   265  		}
   266  
   267  		amount, err := strconv.ParseInt(args[1], 10, 64)
   268  		if err != nil {
   269  			return fmt.Errorf("error parsing an amount:%v", err)
   270  		}
   271  		fee, err := strconv.ParseInt(args[2], 10, 64)
   272  		if err != nil {
   273  			return fmt.Errorf("error parsing a fee:%v", err)
   274  		}
   275  		gasLimit, err := strconv.ParseUint(args[3], 10, 64)
   276  		if err != nil {
   277  			return fmt.Errorf("error parsing a gas limit:%v", err)
   278  		}
   279  
   280  		txResponse, err := client.State.Undelegate(
   281  			cmd.Context(),
   282  			addr.Address.(state.ValAddress),
   283  			math.NewInt(amount),
   284  			math.NewInt(fee),
   285  			gasLimit,
   286  		)
   287  		return cmdnode.PrintOutput(txResponse, err, nil)
   288  	},
   289  }
   290  
   291  var delegateCmd = &cobra.Command{
   292  	Use:   "delegate [valAddress] [amount] [fee] [gasLimit]",
   293  	Short: "Sends a user's liquid tokens to a validator for delegation.",
   294  	Args:  cobra.ExactArgs(4),
   295  	RunE: func(cmd *cobra.Command, args []string) error {
   296  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   297  		if err != nil {
   298  			return err
   299  		}
   300  		defer client.Close()
   301  
   302  		addr, err := parseAddressFromString(args[0])
   303  		if err != nil {
   304  			return fmt.Errorf("error parsing an address:%v", err)
   305  		}
   306  
   307  		amount, err := strconv.ParseInt(args[1], 10, 64)
   308  		if err != nil {
   309  			return fmt.Errorf("error parsing an amount:%v", err)
   310  		}
   311  
   312  		fee, err := strconv.ParseInt(args[2], 10, 64)
   313  		if err != nil {
   314  			return fmt.Errorf("error parsing a fee:%v", err)
   315  		}
   316  
   317  		gasLimit, err := strconv.ParseUint(args[3], 10, 64)
   318  		if err != nil {
   319  			return fmt.Errorf("error parsing a gas limit:%v", err)
   320  		}
   321  
   322  		txResponse, err := client.State.Delegate(
   323  			cmd.Context(),
   324  			addr.Address.(state.ValAddress),
   325  			math.NewInt(amount),
   326  			math.NewInt(fee),
   327  			gasLimit,
   328  		)
   329  		return cmdnode.PrintOutput(txResponse, err, nil)
   330  	},
   331  }
   332  
   333  var queryDelegationCmd = &cobra.Command{
   334  	Use:   "get-delegation [valAddress]",
   335  	Short: "Retrieves the delegation information between a delegator and a validator.",
   336  	Args:  cobra.ExactArgs(1),
   337  	RunE: func(cmd *cobra.Command, args []string) error {
   338  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   339  		if err != nil {
   340  			return err
   341  		}
   342  		defer client.Close()
   343  
   344  		addr, err := parseAddressFromString(args[0])
   345  		if err != nil {
   346  			return fmt.Errorf("error parsing an address:%v", err)
   347  		}
   348  
   349  		balance, err := client.State.QueryDelegation(cmd.Context(), addr.Address.(state.ValAddress))
   350  		return cmdnode.PrintOutput(balance, err, nil)
   351  	},
   352  }
   353  
   354  var queryUnbondingCmd = &cobra.Command{
   355  	Use:   "get-unbonding [valAddress]",
   356  	Short: "Retrieves the unbonding status between a delegator and a validator.",
   357  	Args:  cobra.ExactArgs(1),
   358  	RunE: func(cmd *cobra.Command, args []string) error {
   359  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   360  		if err != nil {
   361  			return err
   362  		}
   363  		defer client.Close()
   364  
   365  		addr, err := parseAddressFromString(args[0])
   366  		if err != nil {
   367  			return fmt.Errorf("error parsing an address:%v", err)
   368  		}
   369  
   370  		response, err := client.State.QueryUnbonding(cmd.Context(), addr.Address.(state.ValAddress))
   371  		return cmdnode.PrintOutput(response, err, nil)
   372  	},
   373  }
   374  
   375  var queryRedelegationCmd = &cobra.Command{
   376  	Use:   "get-redelegations [srcAddress] [dstAddress]",
   377  	Short: "Retrieves the status of the redelegations between a delegator and a validator.",
   378  	Args:  cobra.ExactArgs(2),
   379  	RunE: func(cmd *cobra.Command, args []string) error {
   380  		client, err := cmdnode.ParseClientFromCtx(cmd.Context())
   381  		if err != nil {
   382  			return err
   383  		}
   384  		defer client.Close()
   385  
   386  		srcAddr, err := parseAddressFromString(args[0])
   387  		if err != nil {
   388  			return fmt.Errorf("error parsing a src address:%v", err)
   389  		}
   390  
   391  		dstAddr, err := parseAddressFromString(args[1])
   392  		if err != nil {
   393  			return fmt.Errorf("error parsing a dst address:%v", err)
   394  		}
   395  
   396  		response, err := client.State.QueryRedelegations(
   397  			cmd.Context(),
   398  			srcAddr.Address.(state.ValAddress),
   399  			dstAddr.Address.(state.ValAddress),
   400  		)
   401  		return cmdnode.PrintOutput(response, err, nil)
   402  	},
   403  }
   404  
   405  func parseAddressFromString(addrStr string) (state.Address, error) {
   406  	var address state.Address
   407  	err := address.UnmarshalJSON([]byte(addrStr))
   408  	if err != nil {
   409  		return address, err
   410  	}
   411  	return address, nil
   412  }