Skip to content

Commit

Permalink
feat: add add-to-basket cli command (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleem1314 authored Feb 12, 2022
1 parent 4f3888d commit 7ac5f1a
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 3 deletions.
61 changes: 58 additions & 3 deletions x/ecocredit/client/basket/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,75 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"

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

const (
FlagRetirementLocation = "retirement-location"
FlagRetireOnTake = "retire-on-take"
)

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
}

func TxTakeFromBasket() *cobra.Command {
cmd := &cobra.Command{
Use: "take-from-basket [basket_denom] [amount]",
Short: "Takes credits from a basket",
Long: strings.TrimSpace(`takes credits from a basket starting from the oldest credits first.
Parameters:
basket_denom: denom identifying basket from which we redeem credits.
amount: amount is a positive integer number of basket tokens to convert into credits.
Expand Down Expand Up @@ -52,7 +107,7 @@ Flags:
return err
}

msg := keeper.MsgTake{
msg := basket.MsgTake{
Owner: clientCtx.FromAddress.String(),
BasketDenom: args[0],
Amount: args[1],
Expand Down
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)
}
})
}
}
1 change: 1 addition & 0 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TxCmd(name string) *cobra.Command {
TxUpdateClassMetadataCmd(),
TxUpdateClassIssuersCmd(),
TxUpdateClassAdminCmd(),
basketcli.TxPutInBasket(),
basketcli.TxTakeFromBasket(),
)
return cmd
Expand Down

0 comments on commit 7ac5f1a

Please sign in to comment.