Skip to content

Commit

Permalink
- add support for --retire-to flag
Browse files Browse the repository at this point in the history
- add integration tests for new command
  • Loading branch information
sfishel18 committed Jul 1, 2022
1 parent b6c8eda commit 2510b4c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
86 changes: 84 additions & 2 deletions x/ecocredit/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,93 @@ func (s *IntegrationTestSuite) TestTxCreateBatchCmd() {
func (s *IntegrationTestSuite) TestTxSendCmd() {
require := s.Require()

sender := s.addr1.String()
amount := "10"
retirementJurisdiction := "US-WA"

recipient := s.addr2.String()

testCases := []struct {
name string
args []string
expErr bool
expErrMsg string
}{
{
name: "missing args",
args: []string{"foo"},
expErr: true,
expErrMsg: "Error: accepts 3 arg(s), received 1",
},
{
name: "missing args",
args: []string{"foo", "bar"},
expErr: true,
expErrMsg: "Error: accepts 3 arg(s), received 2",
},
{
name: "too many args",
args: []string{"foo", "bar", "baz", "foobarbaz"},
expErr: true,
expErrMsg: "Error: accepts 2 arg(s), received 3",
},
{
name: "missing from flag",
args: []string{
amount,
s.batchDenom,
recipient,
},
expErr: true,
expErrMsg: "Error: required flag(s) \"from\" not set",
},
{
name: "valid tradeable",
args: []string{
amount,
s.batchDenom,
recipient,
fmt.Sprintf("--%s=%s", flags.FlagFrom, sender),
},
},
{
name: "valid retire",
args: []string{
amount,
s.batchDenom,
recipient,
fmt.Sprintf("--%s=%s", flags.FlagFrom, sender),
fmt.Sprintf("--retire-to=%s", retirementJurisdiction),
},
},
}
for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := coreclient.TxSendCmd()
args := append(tc.args, s.commonTxFlags()...)
out, err := cli.ExecTestCLICmd(s.val.ClientCtx, cmd, args)
if tc.expErr {
require.Error(err)
require.Contains(out.String(), tc.expErrMsg)
} else {
require.NoError(err)

var res sdk.TxResponse
require.NoError(s.val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
require.Zero(res.Code, res.RawLog)
}
})
}
}

func (s *IntegrationTestSuite) TestTxSendBulkCmd() {
require := s.Require()

sender := s.addr1.String()
recipient := s.addr2.String()

// using json package because array is not a proto message
bz, err := json.Marshal([]core.MsgSendBulk_SendCredits{
bz, err := json.Marshal([]core.MsgSend_SendCredits{
{
BatchDenom: s.batchDenom,
TradableAmount: "10",
Expand Down Expand Up @@ -441,7 +523,7 @@ func (s *IntegrationTestSuite) TestTxSendCmd() {

for _, tc := range testCases {
s.Run(tc.name, func() {
cmd := coreclient.TxSendCmd()
cmd := coreclient.TxSendBulkCmd()
args := append(tc.args, s.commonTxFlags()...)
out, err := cli.ExecTestCLICmd(s.val.ClientCtx, cmd, args)
if tc.expErr {
Expand Down
14 changes: 12 additions & 2 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Example JSON:
// TxSendCmd returns a transaction command that sends credits from one account
// to another.
func TxSendCmd() *cobra.Command {
var retireTo string = ""
cmd := &cobra.Command{
Use: "send [amount] [batch_denom] [recipient]",
Short: "Sends credits from the transaction author (--from) to the recipient",
Expand All @@ -313,9 +314,17 @@ Parameters:
if err != nil {
return err
}
tradableAmount := args[0]
retiredAmount := "0"
if len(retireTo) > 0 {
tradableAmount = "0"
retiredAmount = args[0]
}
credit := core.MsgSend_SendCredits{
TradableAmount: args[0],
BatchDenom: args[1],
TradableAmount: tradableAmount,
BatchDenom: args[1],
RetiredAmount: retiredAmount,
RetirementJurisdiction: retireTo,
}
msg := core.MsgSend{
Sender: clientCtx.GetFromAddress().String(),
Expand All @@ -325,6 +334,7 @@ Parameters:
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
cmd.Flags().StringVar(&retireTo, "retire-to", "", "Jurisdiction to retire the credits to. If empty, credits are not retired. (default empty)")
return txFlags(cmd)
}

Expand Down

0 comments on commit 2510b4c

Please sign in to comment.