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 add-to-basket cli command #750

Merged
merged 11 commits into from
Feb 12, 2022
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.14
github.com/tendermint/tm-db v0.6.6
golang.org/x/crypto v0.0.0-20220209155544-dad33157f4bf // indirect
google.golang.org/genproto v0.0.0-20220208230804-65c12eb4c068 // indirect
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 // indirect
google.golang.org/genproto v0.0.0-20220210181026-6fee9acbd336 // indirect
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,8 @@ golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWP
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220209155544-dad33157f4bf h1:gdgmgieTI2lLaGI2N+xEiaCMUgo2XFmAS0rlF8HZoso=
golang.org/x/crypto v0.0.0-20220209155544-dad33157f4bf/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 h1:XdAboW3BNMv9ocSCOk/u1MFioZGzCNkiJZ19v9Oe3Ig=
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -1564,8 +1564,8 @@ google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1/go.mod h1:9lPAdzaEmUacj36I+k7YKbEc5CXzPIeORRgDAUOu28A=
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220208230804-65c12eb4c068 h1:pwzFiZfBTH/GjBWz1BcDwMBaHBo8mZvpLa7eBKJpFAk=
google.golang.org/genproto v0.0.0-20220208230804-65c12eb4c068/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
google.golang.org/genproto v0.0.0-20220210181026-6fee9acbd336 h1:RK2ysGpQApbI6U7xn+ROT2rrm08lE/t8AcGqG8XI1CY=
google.golang.org/genproto v0.0.0-20220210181026-6fee9acbd336/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
2 changes: 1 addition & 1 deletion proto/regen/ecocredit/basket/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ message BasketCredit {
// Decimal values are acceptable within the precision of the corresponding
// credit type for this batch.
string amount = 2;
}
}
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion x/ecocredit/basket/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions x/ecocredit/client/basket/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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"

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

func TxPutInBasket() *cobra.Command {
cmd := &cobra.Command{
Use: "put-in-basket [basket_denom] [credits_json_file]",
Short: "add credits to the basket",
Long: strings.TrimSpace(`add credits to the basket.

Parameters:
basket_denom: basket identifier

Flags:
from: account address of the owner

Example:
$regen tx ecocredit put-in-basket [basket_denom] [credits_json_file]

Where credits_json_file contains:

[
{
"batch_denom": "C01-20210101-20220101-001",
"amount": "10"
},
{
"batch_denom": "C01-20210101-20220101-001",
"amount": "10.5"
}
]
`),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

credits, err := parseBasketCredits(args[1])
if err != nil {
return err
}

msg := basket.MsgPut{
Owner: clientCtx.FromAddress.String(),
BasketDenom: args[0],
Credits: credits,
}

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

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}
27 changes: 27 additions & 0 deletions x/ecocredit/client/basket/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package basketclient

import (
"encoding/json"
"fmt"
"io/ioutil"

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

func parseBasketCredits(creditsFile string) ([]*basket.BasketCredit, error) {
credits := []*basket.BasketCredit{}

if creditsFile == "" {
return nil, fmt.Errorf("credits file path is empty")
}

bz, err := ioutil.ReadFile(creditsFile)
if err != nil {
return nil, err
}
if err = json.Unmarshal(bz, &credits); err != nil {
return nil, err
}

return credits, nil
}
77 changes: 77 additions & 0 deletions x/ecocredit/client/basket/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package basketclient

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/testutil"

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

func TestParseCredits(t *testing.T) {
invalidContent := testutil.WriteToNewTempFile(t, `{}`).Name()
validCredits := testutil.WriteToNewTempFile(t, `[
{
"batch_denom": "C01-20210101-20220101-001",
"amount": "10"
},
{
"batch_denom": "C01-20210101-20220101-001",
"amount": "10.555"
}
]`).Name()

testCases := []struct {
name string
filePath string
expErr bool
result []*basket.BasketCredit
errMsg string
}{
{
"empty file path",
"",
true,
nil,
"file path is empty",
},
{
"invalid file content",
invalidContent,
true,
nil,
"cannot unmarshal object",
},
{
"valid test",
validCredits,
false,
[]*basket.BasketCredit{
{
BatchDenom: "C01-20210101-20220101-001",
Amount: "10",
},
{
BatchDenom: "C01-20210101-20220101-001",
Amount: "10.555",
},
},
"",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := parseBasketCredits(tc.filePath)
if tc.expErr {
require.Error(t, err, err.Error())
require.Contains(t, err.Error(), tc.errMsg)
} else {
require.NoError(t, err)
require.Equal(t, res, tc.result)
}
})
}
}
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.TxAddToBasket(),
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved
)
return cmd
}
Expand Down