Skip to content

Commit

Permalink
feat!(state): enable fee granting
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Apr 8, 2024
1 parent 02030bf commit 80d1aae
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 7 deletions.
83 changes: 83 additions & 0 deletions nodebuilder/state/cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/celestiaorg/celestia-node/state"
)

var (
amount uint64
)

func init() {
Cmd.AddCommand(
accountAddressCmd,
Expand All @@ -26,6 +30,16 @@ func init() {
queryDelegationCmd,
queryUnbondingCmd,
queryRedelegationCmd,
grantFeeCmd,
revokeGrantFeeCmd,
)

grantFeeCmd.PersistentFlags().Uint64Var(
&amount,
"amount",
0,
"specifies the spend limit(in utia) for the grantee.\n"+
"The default value is 0 which means the grantee does not have a spend limit.",
)
}

Expand Down Expand Up @@ -402,6 +416,75 @@ var queryRedelegationCmd = &cobra.Command{
},
}

var grantFeeCmd = &cobra.Command{
Use: "grant-fee [grantee] [fee] [gasLimit]",
Short: "Adds permission for Grantee to spend up to Allowance of fees from the account of Granter.\n" +
"Grantee could spend any amount of tokens in case if spend limit is not set.",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

granteeAddr, err := parseAddressFromString(args[0])
if err != nil {
return fmt.Errorf("error parsing an address:%v", err)
}

fee, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a fee:%v", err)
}
gasLimit, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a gas limit:%v", err)
}

txResponse, err := client.State.GrantFee(
cmd.Context(),
granteeAddr.Address.(state.AccAddress),
math.NewInt(int64(amount)), math.NewInt(fee), gasLimit,
)
return cmdnode.PrintOutput(txResponse, err, nil)
},
}

var revokeGrantFeeCmd = &cobra.Command{
Use: "revoke-grant-fee [grantee] [fee] [gasLimit]",
Short: "Adds permission for Grantee to spend up to Allowance of fees from the account of Granter.",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
return err
}
defer client.Close()

granteeAddr, err := parseAddressFromString(args[0])
if err != nil {
return fmt.Errorf("error parsing an address:%v", err)
}

fee, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a fee:%v", err)
}
gasLimit, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return fmt.Errorf("error parsing a gas limit:%v", err)
}

txResponse, err := client.State.RevokeGrantFee(
cmd.Context(),
granteeAddr.Address.(state.AccAddress),
math.NewInt(fee), gasLimit,
)
return cmdnode.PrintOutput(txResponse, err, nil)
},
}

func parseAddressFromString(addrStr string) (state.Address, error) {
var address state.Address
err := address.UnmarshalJSON([]byte(addrStr))
Expand Down
30 changes: 30 additions & 0 deletions nodebuilder/state/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions nodebuilder/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ type Module interface {
srcValAddr,
dstValAddr state.ValAddress,
) (*types.QueryRedelegationsResponse, error)

GrantFee(
ctx context.Context,
grantee state.AccAddress,
amont state.Int,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error)

RevokeGrantFee(
ctx context.Context,
grantee state.AccAddress,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error)
}

// API is a wrapper around Module for the RPC.
Expand Down Expand Up @@ -160,6 +175,20 @@ type API struct {
srcValAddr,
dstValAddr state.ValAddress,
) (*types.QueryRedelegationsResponse, error) `perm:"read"`
GrantFee func(
ctx context.Context,
grantee state.AccAddress,
amount state.Int,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`

RevokeGrantFee func(
ctx context.Context,
grantee state.AccAddress,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`
}
}

Expand Down Expand Up @@ -256,3 +285,22 @@ func (api *API) QueryRedelegations(
func (api *API) Balance(ctx context.Context) (*state.Balance, error) {
return api.Internal.Balance(ctx)
}

func (api *API) GrantFee(
ctx context.Context,
grantee state.AccAddress,
amount state.Int,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) {
return api.Internal.GrantFee(ctx, grantee,amount, fee, gasLim)

Check failure on line 296 in nodebuilder/state/state.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

File is not `gofmt`-ed with `-s` (gofmt)
}

func (api *API) RevokeGrantFee(
ctx context.Context,
grantee state.AccAddress,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) {
return api.Internal.RevokeGrantFee(ctx, grantee, fee, gasLim)
}
19 changes: 19 additions & 0 deletions nodebuilder/state/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,22 @@ func (s stubbedStateModule) QueryRedelegations(
) (*types.QueryRedelegationsResponse, error) {
return nil, ErrNoStateAccess
}

func (s stubbedStateModule) GrantFee(
_ context.Context,
_ state.AccAddress,
_,
_ state.Int,
_ uint64,
) (*state.TxResponse, error) {
return nil, ErrNoStateAccess
}

func (s stubbedStateModule) RevokeGrantFee(
_ context.Context,
_ state.AccAddress,
_ state.Int,
_ uint64,
) (*state.TxResponse, error) {
return nil, ErrNoStateAccess
}
Loading

0 comments on commit 80d1aae

Please sign in to comment.