Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/ecocredit): add simpler version of send command #1213

84 changes: 83 additions & 1 deletion x/ecocredit/client/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,88 @@ func (s *IntegrationTestSuite) TestTxCreateBatchCmd() {
func (s *IntegrationTestSuite) TestTxSendCmd() {
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
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",
},
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
{
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",
},
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
{
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()

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
46 changes: 45 additions & 1 deletion x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TxCmd(name string) *cobra.Command {
TxGenBatchJSONCmd(),
TxCreateBatchCmd(),
TxSendCmd(),
TxSendBulkCmd(),
TxRetireCmd(),
TxCancelCmd(),
TxUpdateClassMetadataCmd(),
Expand Down Expand Up @@ -297,8 +298,51 @@ Example JSON:
// TxSendCmd returns a transaction command that sends credits from one account
// to another.
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
func TxSendCmd() *cobra.Command {
var retireTo string = ""
cmd := &cobra.Command{
Use: "send [recipient] [credits]",
Use: "send [amount] [batch_denom] [recipient]",
Short: "Sends credits from the transaction author (--from) to the recipient",
Long: `Sends credits from the transaction author (--from) to the recipient.

Parameters:
amount: amount to send
batch_denom: batch denomination
recipient: recipient address`,
Args: cobra.ExactArgs(3),
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
tradableAmount := args[0]
retiredAmount := "0"
if len(retireTo) > 0 {
tradableAmount = "0"
retiredAmount = args[0]
}
credit := core.MsgSend_SendCredits{
TradableAmount: tradableAmount,
BatchDenom: args[1],
RetiredAmount: retiredAmount,
RetirementJurisdiction: retireTo,
}
msg := core.MsgSend{
Sender: clientCtx.GetFromAddress().String(),
Recipient: args[2],
Credits: []*core.MsgSend_SendCredits{&credit},
}
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)")
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
return txFlags(cmd)
}

// TxSendBulkCmd returns a transaction command that sends credits from one account
// to another.
sfishel18 marked this conversation as resolved.
Show resolved Hide resolved
func TxSendBulkCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send-bulk [recipient] [credits]",
Short: "Sends credits from the transaction author (--from) to the recipient",
Long: `Sends credits from the transaction author (--from) to the recipient.

Expand Down