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

Integrate ecocredits module #153

Merged
merged 17 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ import (
"net/http"
"os"

"github.com/cosmos/cosmos-sdk/x/auth/tx"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"

baseapp "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/capability"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
Expand Down Expand Up @@ -63,9 +67,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/tendermint/tendermint/libs/log"

// types
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
Expand All @@ -81,16 +85,8 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

// "os"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"

baseapp "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"

data "github.com/regen-network/regen-ledger/x/data/module"
ecocredit "github.com/regen-network/regen-ledger/x/ecocredit/module"
)

const (
Expand Down Expand Up @@ -349,6 +345,7 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
params.NewAppModule(app.ParamsKeeper),
transferModule,
data.NewAppModule(keys[data.StoreKey]),
ecocredit.NewAppModule(),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down
10 changes: 4 additions & 6 deletions x/data/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package module
import (
"encoding/json"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/tendermint/tendermint/abci/types"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/regen-network/regen-ledger/x/data"
"github.com/regen-network/regen-ledger/x/data/client"
Expand Down
126 changes: 126 additions & 0 deletions x/ecocredit/client/query.go
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Short: "Querying commands for the ecocredit module",
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: "Select a class info",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Short: "Select a class info",
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: "Select the credit issuance batch info",
Long: "Select the credit issuance batch info based on the bach_denom (ID)",
Copy link
Member

Choose a reason for hiding this comment

The 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",
Copy link
Member

Choose a reason for hiding this comment

The 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)",
Copy link
Member

Choose a reason for hiding this comment

The 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",
Copy link
Member

Choose a reason for hiding this comment

The 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)
},
}
}
65 changes: 65 additions & 0 deletions x/ecocredit/client/tx.go
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`,
Copy link
Member

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

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
}
36 changes: 36 additions & 0 deletions x/ecocredit/client/util.go
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
}
12 changes: 2 additions & 10 deletions x/ecocredit/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import (
// ParseNonNegativeDecimal parses a non-negative decimal or returns an error.
func ParseNonNegativeDecimal(x string) (*apd.Decimal, error) {
res, _, err := apd.NewFromString(x)
if err != nil {
return nil, errors.Wrap(errors.ErrInvalidRequest, fmt.Sprintf("expected a non-negative decimal, got %s", x))
}

if res.Sign() < 0 {
if err != nil || res.Sign() < 0 {
return nil, errors.Wrap(errors.ErrInvalidRequest, fmt.Sprintf("expected a non-negative decimal, got %s", x))
}

Expand All @@ -26,11 +22,7 @@ func ParseNonNegativeDecimal(x string) (*apd.Decimal, error) {
// ParsePositiveDecimal parses a positive decimal or returns an error.
func ParsePositiveDecimal(x string) (*apd.Decimal, error) {
res, _, err := apd.NewFromString(x)
if err != nil {
return nil, errors.Wrap(errors.ErrInvalidRequest, fmt.Sprintf("expected a positive decimal, got %s", x))
}

if res.Sign() <= 0 {
if err != nil || res.Sign() <= 0 {
return nil, errors.Wrap(errors.ErrInvalidRequest, fmt.Sprintf("expected a positive decimal, got %s", x))
}

Expand Down
Loading