-
Notifications
You must be signed in to change notification settings - Fork 103
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
Integrate ecocredits module #153
Changes from 10 commits
76469b1
79e555b
56fd908
8639f35
ff2a903
f8b2270
b8a3d73
6c56276
dc04836
9fbfcb0
9c255e7
228aec0
d5aa4f7
51851c5
18d09ea
0fcbfaa
5bb66b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,126 @@ | ||||||
package client | ||||||
|
||||||
import ( | ||||||
"github.com/cosmos/cosmos-sdk/client" | ||||||
"github.com/cosmos/cosmos-sdk/client/flags" | ||||||
"github.com/spf13/cobra" | ||||||
|
||||||
"github.com/regen-network/regen-ledger/x/ecocredit" | ||||||
) | ||||||
|
||||||
// QueryCmd returns the parent command for all x/data CLI query commands | ||||||
func QueryCmd() *cobra.Command { | ||||||
cmd := &cobra.Command{ | ||||||
SuggestionsMinimumDistance: 2, | ||||||
DisableFlagParsing: true, | ||||||
|
||||||
Args: cobra.ExactArgs(1), | ||||||
Use: ecocredit.ModuleName, | ||||||
Short: "Querying commands for the ecocredit module", | ||||||
RunE: client.ValidateCmd, | ||||||
} | ||||||
cmd.AddCommand( | ||||||
queryClassInfo(), | ||||||
queryBatchInfo(), | ||||||
queryBalance(), | ||||||
querySupply(), | ||||||
queryPrecision(), | ||||||
) | ||||||
flags.AddQueryFlagsToCmd(cmd) | ||||||
|
||||||
return cmd | ||||||
} | ||||||
|
||||||
func queryClassInfo() *cobra.Command { | ||||||
return &cobra.Command{ | ||||||
Use: "class_info [class_id]", | ||||||
Short: "Select a class info", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Args: cobra.ExactArgs(1), | ||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||
c, ctx, err := mkQueryClient(cmd) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
res, err := c.ClassInfo(cmd.Context(), &ecocredit.QueryClassInfoRequest{ | ||||||
ClassId: args[0], | ||||||
}) | ||||||
return print(ctx, res, err) | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func queryBatchInfo() *cobra.Command { | ||||||
return &cobra.Command{ | ||||||
Use: "batch_info [batch_denom]", | ||||||
Short: "Select the credit issuance batch info", | ||||||
Long: "Select the credit issuance batch info based on the bach_denom (ID)", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
Args: cobra.ExactArgs(1), | ||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||
c, ctx, err := mkQueryClient(cmd) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
res, err := c.BatchInfo(cmd.Context(), &ecocredit.QueryBatchInfoRequest{ | ||||||
BatchDenom: args[0], | ||||||
}) | ||||||
return print(ctx, res, err) | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func queryBalance() *cobra.Command { | ||||||
return &cobra.Command{ | ||||||
Use: "balance [batch_denom] [account]", | ||||||
Short: "Select the tradable and retired balance of the credit batch", | ||||||
Long: "Select the credit tradable and retired balance of the credit batch denom (ID) and account address", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
Args: cobra.ExactArgs(2), | ||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||
c, ctx, err := mkQueryClient(cmd) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
res, err := c.Balance(cmd.Context(), &ecocredit.QueryBalanceRequest{ | ||||||
BatchDenom: args[0], Account: args[1], | ||||||
}) | ||||||
return print(ctx, res, err) | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func querySupply() *cobra.Command { | ||||||
return &cobra.Command{ | ||||||
Use: "supply [batch_denom]", | ||||||
Short: "Select the tradable and retired supply of the credit batch", | ||||||
Long: "Select the tradable and retired supply of the credit batch batch denom (ID)", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
Args: cobra.ExactArgs(1), | ||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||
c, ctx, err := mkQueryClient(cmd) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
res, err := c.Supply(cmd.Context(), &ecocredit.QuerySupplyRequest{ | ||||||
BatchDenom: args[0], | ||||||
}) | ||||||
return print(ctx, res, err) | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func queryPrecision() *cobra.Command { | ||||||
return &cobra.Command{ | ||||||
Use: "precision [batch_denom]", | ||||||
Short: "Select the maximum length of the fractional part of credits in the given batch", | ||||||
Long: "Select the maximum length of the fractional part of credits in the given batch. The precision tells what is the minimum unit of a credit.\nExample: a decimal number 12.345 has fractional part length equal 3. A precision=5 means that the minimum unit we can trade is 0.00001", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||||
Args: cobra.ExactArgs(1), | ||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||
c, ctx, err := mkQueryClient(cmd) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
res, err := c.Precision(cmd.Context(), &ecocredit.QueryPrecisionRequest{ | ||||||
BatchDenom: args[0], | ||||||
}) | ||||||
return print(ctx, res, err) | ||||||
}, | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/regen-network/regen-ledger/x/ecocredit" | ||
) | ||
|
||
// TxCmd returns a root CLI command handler for all x/data transaction commands. | ||
func TxCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
SuggestionsMinimumDistance: 2, | ||
DisableFlagParsing: true, | ||
|
||
Use: ecocredit.ModuleName, | ||
Short: "Ecocredit module transactions", | ||
RunE: client.ValidateCmd, | ||
} | ||
cmd.AddCommand() | ||
|
||
return cmd | ||
} | ||
|
||
func txCreateClass() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create_class [designer] [issuer[,issuer]*] [base64_metadata]", | ||
Short: `Creates a new credit class. | ||
designer: address of the account which designed the credit class | ||
issuer: comma separated (no spaces) list of issuer account addresses. Example: "addr1,addr2" | ||
metadata: base64 encoded metadata - arbitrary data attached to the credit class info`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what do you think about using flags |
||
Args: cobra.ExactArgs(3), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
issuers := strings.Split(args[1], ",") | ||
for i := range issuers { | ||
issuers[i] = strings.TrimSpace(issuers[i]) | ||
} | ||
if args[2] == "" { | ||
return errors.New("base64_metadata is required") | ||
} | ||
b, err := base64.StdEncoding.DecodeString(args[2]) | ||
if err != nil { | ||
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "base64_metadata is malformed, proper base64 string is required") | ||
} | ||
|
||
c, ctx, err := mkMsgClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := c.CreateClass(cmd.Context(), &ecocredit.MsgCreateClassRequest{ | ||
Designer: args[0], Issuers: issuers, Metadata: b, | ||
}) | ||
return print(ctx, res, err) | ||
// TODO: return mkTx(ctx, cmd, err) | ||
}, | ||
} | ||
flags.AddTxFlagsToCmd(cmd) | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/regen-network/regen-ledger/x/ecocredit" | ||
) | ||
|
||
func print(ctx client.Context, res proto.Message, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
return ctx.PrintOutput(res) | ||
} | ||
|
||
func mkTx(ctx client.Context, cmd *cobra.Command, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(ctx, cmd.Flags()) | ||
} | ||
|
||
func mkQueryClient(cmd *cobra.Command) (ecocredit.QueryClient, client.Context, error) { | ||
ctx := client.GetClientContextFromCmd(cmd) | ||
ctx, err := client.ReadQueryCommandFlags(ctx, cmd.Flags()) | ||
return ecocredit.NewQueryClient(ctx), ctx, err | ||
} | ||
|
||
func mkMsgClient(cmd *cobra.Command) (ecocredit.MsgClient, client.Context, error) { | ||
ctx := client.GetClientContextFromCmd(cmd) | ||
ctx, err := client.ReadTxCommandFlags(ctx, cmd.Flags()) | ||
return ecocredit.NewMsgClient(ctx), ctx, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.