Skip to content

Commit

Permalink
chore: move cmd into tx.go (#1019)
Browse files Browse the repository at this point in the history
Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com>
Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 22, 2022
1 parent b2dee96 commit 75558eb
Showing 1 changed file with 61 additions and 19 deletions.
80 changes: 61 additions & 19 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package client

import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/x/ecocredit"
Expand Down Expand Up @@ -46,6 +46,8 @@ func TxCmd(name string) *cobra.Command {
TxUpdateClassIssuersCmd(),
TxUpdateClassAdminCmd(),
TxCreateProject(),
TxUpdateProjectAdminCmd(),
TxUpdateProjectMetadataCmd(),
corecli.TxCreditTypeProposalCmd(),
basketcli.TxCreateBasket(),
basketcli.TxPutInBasket(),
Expand All @@ -58,15 +60,15 @@ func TxCmd(name string) *cobra.Command {
return cmd
}

func txflags(cmd *cobra.Command) *cobra.Command {
func txFlags(cmd *cobra.Command) *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
cmd.MarkFlagRequired(flags.FlagFrom)
return cmd
}

// TxCreateClassCmd returns a transaction command that creates a credit class.
func TxCreateClassCmd() *cobra.Command {
return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "create-class [issuer[,issuer]*] [credit type abbreviation] [metadata] [fee]",
Short: "Creates a new credit class with transaction author (--from) as admin",
Long: fmt.Sprintf(
Expand Down Expand Up @@ -244,7 +246,7 @@ Required Flags:
// TxCreateBatchCmd returns a transaction command that creates a credit batch.
func TxCreateBatchCmd() *cobra.Command {

return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "create-batch [msg-create-batch-json-file]",
Short: "Issues a new credit batch",
Long: fmt.Sprintf(`Issues a new credit batch.
Expand Down Expand Up @@ -302,7 +304,7 @@ Parameters:
// TxSendCmd returns a transaction command that sends credits from one account
// to another.
func TxSendCmd() *cobra.Command {
return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "send [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 Expand Up @@ -333,7 +335,7 @@ Parameters:

// TxRetireCmd returns a transaction command that retires credits.
func TxRetireCmd() *cobra.Command {
return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "retire [credits] [retirement_jurisdiction]",
Short: "Retires a specified amount of credits from the account of the transaction author (--from)",
Long: `Retires a specified amount of credits from the account of the transaction author (--from)
Expand Down Expand Up @@ -369,7 +371,7 @@ Parameters:

// TxCancelCmd returns a transaction command that cancels credits.
func TxCancelCmd() *cobra.Command {
return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "cancel [credits]",
Short: "Cancels a specified amount of credits from the account of the transaction author (--from)",
Long: `Cancels a specified amount of credits from the account of the transaction author (--from)
Expand Down Expand Up @@ -397,7 +399,7 @@ Parameters:
}

func TxUpdateClassMetadataCmd() *cobra.Command {
return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "update-class-metadata [class-id] [metadata]",
Short: "Updates the metadata for a specific credit class",
Long: `Updates the metadata for a specific credit class. the '--from' flag must equal the credit class admin.
Expand Down Expand Up @@ -435,7 +437,7 @@ Parameters:
}

func TxUpdateClassAdminCmd() *cobra.Command {
return txflags(&cobra.Command{
return txFlags(&cobra.Command{
Use: "update-class-admin [class-id] [admin]",
Short: "Updates the admin for a specific credit class",
Long: `Updates the admin for a specific credit class. the '--from' flag must equal the current credit class admin.
Expand Down Expand Up @@ -475,7 +477,7 @@ Parameters:
}

func TxUpdateClassIssuersCmd() *cobra.Command {
cmd := txflags(&cobra.Command{
cmd := txFlags(&cobra.Command{
Use: "update-class-issuers [class-id]",
Short: "Update the list of issuers for a specific credit class",
Long: `Update the list of issuers for a specific credit class. the '--from' flag must equal the current credit class admin.
Expand Down Expand Up @@ -536,7 +538,7 @@ Example:

// TxCreateProject returns a transaction command that creates a new project.
func TxCreateProject() *cobra.Command {
cmd := txflags(&cobra.Command{
cmd := txFlags(&cobra.Command{
Use: "create-project [class-id] [project-jurisdiction] [metadata] --project-id [project-id]",
Short: "Create a new project within a credit class",
Long: `Create a new project within a credit class.
Expand Down Expand Up @@ -596,11 +598,51 @@ func TxCreateProject() *cobra.Command {
return cmd
}

func decodeMetadata(metadataStr string) ([]byte, error) {
b, err := base64.StdEncoding.DecodeString(metadataStr)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("metadata is malformed, proper base64 string is required")
}
func TxUpdateProjectAdminCmd() *cobra.Command {
return txFlags(&cobra.Command{
Use: "update-project-admin [project_id] [new_admin_address] [flags]",
Short: "Update the project admin address",
Long: "Update the project admin to the provided new_admin_address. Please double check the address as " +
"this will forfeit control of the project. Passing an invalid address could cause loss of control of the project.",
Example: "regen tx ecocredit update-project-admin VERRA1 regen1ynugxwpp4lfpy0epvfqwqkpuzkz62htnex3op",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
projectId, newAdmin := args[0], args[1]
msg := core.MsgUpdateProjectAdmin{
Admin: clientCtx.GetFromAddress().String(),
NewAdmin: newAdmin,
ProjectId: projectId,
}

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

func TxUpdateProjectMetadataCmd() *cobra.Command {
return txFlags(&cobra.Command{
Use: "update-project-metadata [project-id] [new_metadata]",
Short: "Update the project metadata",
Long: "Update the project metadata, overwriting the project's current metadata.",
Example: `regen tx ecocredit update-project-metadata VERRA1 "some metadata"`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := sdkclient.GetClientTxContext(cmd)
if err != nil {
return err
}
projectId, newMetadata := args[0], args[1]
msg := core.MsgUpdateProjectMetadata{
Admin: clientCtx.GetFromAddress().String(),
NewMetadata: newMetadata,
ProjectId: projectId,
}

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

0 comments on commit 75558eb

Please sign in to comment.