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: add take-from-basket command #749

Merged
merged 12 commits into from
Feb 12, 2022
76 changes: 76 additions & 0 deletions x/ecocredit/client/basket/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package basketclient

import (
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"

keeper "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

const (
FlagRetirementLocation = "retirement-location"
FlagRetireOnTake = "retire-on-take"
)

func TxTakeFromBasket() *cobra.Command {
cmd := &cobra.Command{
Use: "take-from-basket [basket_denom] [amount]",
Short: "Takes credits from a basket",
Long: strings.TrimSpace(`takes credits from a basket starting from the oldest credits first.

Parameters:
basket_denom: denom identifying basket from which we redeem credits.
amount: amount is a positive integer number of basket tokens to convert into credits.
Flags:
from: account address of the owner of the basket.
retirement-location: retirement location is the optional retirement location for the credits
which will be used only if --retire-on-take flag is true.
retire-on-take: retire on take is a boolean that dictates whether the ecocredits
received in exchange for the basket tokens will be received as
retired or tradable credits.

`),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

retirementLocation, err := cmd.Flags().GetString(FlagRetirementLocation)
if err != nil {
return err
}

retireOnTake, err := cmd.Flags().GetBool(FlagRetireOnTake)
if err != nil {
return err
}

msg := keeper.MsgTake{
Owner: clientCtx.FromAddress.String(),
BasketDenom: args[0],
Amount: args[1],
RetirementLocation: retirementLocation,
RetireOnTake: retireOnTake,
}

if err := msg.ValidateBasic(); err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(FlagRetirementLocation, "", "location for the credits which will be used only if --retire-on-take flag is true")
cmd.Flags().Bool(FlagRetireOnTake, false, "dictates whether the ecocredits received in exchange for the basket tokens will be received as retired or tradable credits")

return cmd
}
2 changes: 2 additions & 0 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/regen-network/regen-ledger/x/ecocredit"
basketcli "github.com/regen-network/regen-ledger/x/ecocredit/client/basket"
)

// TxCmd returns a root CLI command handler for all x/ecocredit transaction commands.
Expand All @@ -39,6 +40,7 @@ func TxCmd(name string) *cobra.Command {
TxUpdateClassMetadataCmd(),
TxUpdateClassIssuersCmd(),
TxUpdateClassAdminCmd(),
basketcli.TxTakeFromBasket(),
)
return cmd
}
Expand Down