Skip to content

Commit

Permalink
LSM distr cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Nov 27, 2023
1 parent e94237a commit 75bec41
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
46 changes: 46 additions & 0 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryValidatorSlashes(),
GetCmdQueryDelegatorRewards(),
GetCmdQueryCommunityPool(),
GetCmdQueryTokenizeShareRecordReward(),
)

return distQueryCmd
Expand Down Expand Up @@ -364,3 +365,48 @@ $ %s query distribution community-pool
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryTokenizeShareRecordReward implements the query tokenize share record rewards
func GetCmdQueryTokenizeShareRecordReward() *cobra.Command {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()

cmd := &cobra.Command{
Use: "tokenize-share-record-rewards [owner]",
Args: cobra.ExactArgs(1),
Short: "Query distribution tokenize share record rewards",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the query tokenize share record rewards.
Example:
$ %s query distribution tokenize-share-record-rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`,
version.AppName, bech32PrefixAccAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

ownerAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

res, err := queryClient.TokenizeShareRecordReward(
cmd.Context(),
&types.QueryTokenizeShareRecordRewardRequest{OwnerAddress: ownerAddr.String()},
)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
48 changes: 48 additions & 0 deletions x/distribution/client/cli/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,51 @@ func (s *CLITestSuite) TestNewFundCommunityPoolCmd() {
})
}
}

func (s *CLITestSuite) TestNewWithdrawAllTokenizeShareRecordRewardCmd() {
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)

testCases := []struct {
name string
args []string
expectErr bool
expectedCode uint32
respType proto.Message
}{
{
"valid transaction of withdraw tokenize share record reward",
[]string{
fmt.Sprintf("--%s=%s", flags.FlagFrom, val[0].Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(10))).String()),
},
false, 0, &sdk.TxResponse{},
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewWithdrawAllTokenizeShareRecordRewardCmd()

out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(s.clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())

txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
}
})
}
}

// This test requires multiple validators, if I add this test to `IntegrationTestSuite` by increasing
// `NumValidators` the existing tests are leading to non-determnism so created new suite for this test.
func (s *CLITestSuite) TestNewWithdrawAllRewardsGenerateOnly() {
// TODO add LSM test
}
72 changes: 72 additions & 0 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,6 +41,8 @@ func NewTxCmd() *cobra.Command {
NewWithdrawAllRewardsCmd(),
NewSetWithdrawAddrCmd(),
NewFundCommunityPoolCmd(),
NewWithdrawTokenizeShareRecordRewardCmd(),
NewWithdrawAllTokenizeShareRecordRewardCmd(),
)

return distTxCmd
Expand Down Expand Up @@ -254,3 +257,72 @@ $ %s tx distribution fund-community-pool 100uatom --from mykey

return cmd
}

// WithdrawAllTokenizeShareRecordReward defines a method to withdraw reward for all owning TokenizeShareRecord
func NewWithdrawAllTokenizeShareRecordRewardCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-all-tokenize-share-rewards",
Args: cobra.ExactArgs(0),
Short: "Withdraw reward for all owning TokenizeShareRecord",
Long: strings.TrimSpace(
fmt.Sprintf(`Withdraw reward for all owned TokenizeShareRecord
Example:
$ %s tx distribution withdraw-tokenize-share-rewards --from mykey
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgWithdrawAllTokenizeShareRecordReward(clientCtx.GetFromAddress())

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// WithdrawTokenizeShareRecordReward defines a method to withdraw reward for an owning TokenizeShareRecord
func NewWithdrawTokenizeShareRecordRewardCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-tokenize-share-rewards",
Args: cobra.ExactArgs(1),
Short: "Withdraw reward for an owning TokenizeShareRecord",
Long: strings.TrimSpace(
fmt.Sprintf(`Withdraw reward for an owned TokenizeShareRecord
Example:
$ %s tx distribution withdraw-tokenize-share-rewards 1 --from mykey
`,
version.AppName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

recordID, err := strconv.Atoi(args[0])
if err != nil {
return err
}

msg := types.NewMsgWithdrawTokenizeShareRecordReward(clientCtx.GetFromAddress(), uint64(recordID))

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

0 comments on commit 75bec41

Please sign in to comment.