-
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
76469b1
Integrate ecocredits module
robert-zaremba 79e555b
use interfaces for Msg/Query services
robert-zaremba 56fd908
simplify decimal parsing
robert-zaremba 8639f35
use protobuf server interfaces
robert-zaremba ff2a903
Refactor the MsgServer usage with modules
robert-zaremba f8b2270
clean module.go
robert-zaremba b8a3d73
update app
robert-zaremba 6c56276
Add Query CLI
robert-zaremba dc04836
move client utility functions
robert-zaremba 9fbfcb0
Adding MsgClient CLI: wip
robert-zaremba 9c255e7
remove Server interface
robert-zaremba 228aec0
add AppModuleBasic to enable interface types registration
robert-zaremba d5aa4f7
fix tests
aaronc 51851c5
grammar check
aaronc 18d09ea
Merge branch 'master' of github.com:regen-network/regen-ledger into r…
aaronc 0fcbfaa
fix imports
aaronc 5bb66b2
fix lint
aaronc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "Query 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: "Retrieve credit class info", | ||
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: "Retrieve the credit issuance batch info", | ||
Long: "Retrieve the credit issuance batch info based on the bach_denom (ID)", | ||
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: "Retrieve the tradable and retired balances of the credit batch", | ||
Long: "Retrieve the tradable and retired balances of the credit batch for a given account address", | ||
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: "Retrieve the tradable and retired supply of the credit batch", | ||
Long: "Retrieve the tradable and retired supply of the credit batch", | ||
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: "Retrieve the maximum length of the fractional part of credits in the given batch", | ||
Long: "Retrieve 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", | ||
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) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ecocredit | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/types/msgservice" | ||
) | ||
|
||
func RegisterTypes(registry types.InterfaceRegistry) { | ||
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So what do you think about using flags
--binary-metadata
and--string-metadata
? Often it will be json I imagine