From 62591c7fc80119a46e5c4105bfca02d4018c0e0b Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 10 Feb 2022 18:56:00 +0530 Subject: [PATCH 1/8] feat: add add-to-basket cli command --- go.mod | 2 +- go.sum | 4 +- proto/regen/ecocredit/basket/v1/types.proto | 6 + x/basket/spec/protobuf.md | 16 ++ x/ecocredit/basket/tx.pb.go | 6 +- x/ecocredit/basket/types.pb.go | 200 +++++++++++++++++++- x/ecocredit/client/basket/tx.go | 74 ++++++++ x/ecocredit/client/basket/util.go | 29 +++ 8 files changed, 325 insertions(+), 12 deletions(-) create mode 100644 x/ecocredit/client/basket/tx.go create mode 100644 x/ecocredit/client/basket/util.go diff --git a/go.mod b/go.mod index a962c9b320..5d2f124ee1 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ 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 + golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a // indirect google.golang.org/genproto v0.0.0-20220208230804-65c12eb4c068 // indirect ) diff --git a/go.sum b/go.sum index 0e3635473f..6123773638 100644 --- a/go.sum +++ b/go.sum @@ -1149,8 +1149,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-20220209195652-db638375bc3a h1:atOEWVSedO4ksXBe/UrlbSLVxQQ9RxM/tT2Jy10IaHo= +golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a/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= diff --git a/proto/regen/ecocredit/basket/v1/types.proto b/proto/regen/ecocredit/basket/v1/types.proto index b2d5143c41..5b9d352da9 100644 --- a/proto/regen/ecocredit/basket/v1/types.proto +++ b/proto/regen/ecocredit/basket/v1/types.proto @@ -15,3 +15,9 @@ message BasketCredit { // credit type for this batch. string amount = 2; } + +// BasketCredits represents the slice of BasketCredit objects. +message BasketCredits { + // credits is the list of basket credits + repeated BasketCredit credits = 1; +} \ No newline at end of file diff --git a/x/basket/spec/protobuf.md b/x/basket/spec/protobuf.md index d0204ae4bc..6a35bb663c 100644 --- a/x/basket/spec/protobuf.md +++ b/x/basket/spec/protobuf.md @@ -6,6 +6,7 @@ - [regen/ecocredit/basket/v1/types.proto](#regen/ecocredit/basket/v1/types.proto) - [BasketCredit](#regen.ecocredit.basket.v1.BasketCredit) + - [BasketCredits](#regen.ecocredit.basket.v1.BasketCredits) - [regen/ecocredit/basket/v1/tx.proto](#regen/ecocredit/basket/v1/tx.proto) - [MsgCreate](#regen.ecocredit.basket.v1.MsgCreate) @@ -43,6 +44,21 @@ BasketCredit represents the information for a credit batch inside a basket. + + + +### BasketCredits +BasketCredits represents the slice of BasketCredit objects. + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| credits | [BasketCredit](#regen.ecocredit.basket.v1.BasketCredit) | repeated | credits is the list of basket credits | + + + + + diff --git a/x/ecocredit/basket/tx.pb.go b/x/ecocredit/basket/tx.pb.go index e7761519a4..a45878edec 100644 --- a/x/ecocredit/basket/tx.pb.go +++ b/x/ecocredit/basket/tx.pb.go @@ -62,8 +62,8 @@ type MsgCreate struct { // min_start_date is the earliest start date for batches of credits allowed // into the basket. MinStartDate *types.Timestamp `protobuf:"bytes,8,opt,name=min_start_date,json=minStartDate,proto3" json:"min_start_date,omitempty"` - // fee is the fee that the curator will pay to create the basket. It must be >= - // the required Params.basket_creation_fee. We include the fee explicitly + // fee is the fee that the curator will pay to create the basket. It must be + // >= the required Params.basket_creation_fee. We include the fee explicitly // here so that the curator explicitly acknowledges paying this fee and // is not surprised to learn that the paid a big fee and didn't know // beforehand. @@ -329,7 +329,7 @@ func (m *MsgPutResponse) GetAmountReceived() string { type MsgTake struct { // owner is the owner of the basket tokens. Owner string `protobuf:"bytes,1,opt,name=owner,proto3" json:"owner,omitempty"` - // basket_denom is the basket bank denom to take credits from. + // basket_denom is the basket denom to take credits from. BasketDenom string `protobuf:"bytes,2,opt,name=basket_denom,json=basketDenom,proto3" json:"basket_denom,omitempty"` // amount is the integer number of basket tokens to convert into credits. Amount string `protobuf:"bytes,3,opt,name=amount,proto3" json:"amount,omitempty"` diff --git a/x/ecocredit/basket/types.pb.go b/x/ecocredit/basket/types.pb.go index c32674ae43..dfed916f92 100644 --- a/x/ecocredit/basket/types.pb.go +++ b/x/ecocredit/basket/types.pb.go @@ -79,8 +79,55 @@ func (m *BasketCredit) GetAmount() string { return "" } +// BasketCredits represents the slice of BasketCredit objects. +type BasketCredits struct { + // credits is the list of basket credits + Credits []*BasketCredit `protobuf:"bytes,1,rep,name=credits,proto3" json:"credits,omitempty"` +} + +func (m *BasketCredits) Reset() { *m = BasketCredits{} } +func (m *BasketCredits) String() string { return proto.CompactTextString(m) } +func (*BasketCredits) ProtoMessage() {} +func (*BasketCredits) Descriptor() ([]byte, []int) { + return fileDescriptor_e6c256e957c69c4d, []int{1} +} +func (m *BasketCredits) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *BasketCredits) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_BasketCredits.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *BasketCredits) XXX_Merge(src proto.Message) { + xxx_messageInfo_BasketCredits.Merge(m, src) +} +func (m *BasketCredits) XXX_Size() int { + return m.Size() +} +func (m *BasketCredits) XXX_DiscardUnknown() { + xxx_messageInfo_BasketCredits.DiscardUnknown(m) +} + +var xxx_messageInfo_BasketCredits proto.InternalMessageInfo + +func (m *BasketCredits) GetCredits() []*BasketCredit { + if m != nil { + return m.Credits + } + return nil +} + func init() { proto.RegisterType((*BasketCredit)(nil), "regen.ecocredit.basket.v1.BasketCredit") + proto.RegisterType((*BasketCredits)(nil), "regen.ecocredit.basket.v1.BasketCredits") } func init() { @@ -88,7 +135,7 @@ func init() { } var fileDescriptor_e6c256e957c69c4d = []byte{ - // 197 bytes of a gzipped FileDescriptorProto + // 229 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x2d, 0x4a, 0x4d, 0x4f, 0xcd, 0xd3, 0x4f, 0x4d, 0xce, 0x4f, 0x2e, 0x4a, 0x4d, 0xc9, 0x2c, 0xd1, 0x4f, 0x4a, 0x2c, 0xce, 0x4e, 0x2d, 0xd1, 0x2f, 0x33, 0xd4, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, @@ -96,12 +143,14 @@ var fileDescriptor_e6c256e957c69c4d = []byte{ 0xe7, 0xe2, 0x71, 0x02, 0x73, 0x9c, 0xc1, 0x32, 0x42, 0xf2, 0x5c, 0xdc, 0x49, 0x89, 0x25, 0xc9, 0x19, 0xf1, 0x29, 0xa9, 0x79, 0xf9, 0xb9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x5c, 0x60, 0x21, 0x17, 0x90, 0x88, 0x90, 0x18, 0x17, 0x5b, 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0x13, - 0x58, 0x0e, 0xca, 0x73, 0x0a, 0x3a, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, + 0x58, 0x0e, 0xca, 0x53, 0x0a, 0xe2, 0xe2, 0x45, 0x36, 0xa8, 0x58, 0xc8, 0x91, 0x8b, 0x1d, 0x62, + 0x5b, 0xb1, 0x04, 0xa3, 0x02, 0xb3, 0x06, 0xb7, 0x91, 0xba, 0x1e, 0x4e, 0x67, 0xe8, 0x21, 0x6b, + 0x0d, 0x82, 0xe9, 0x73, 0x0a, 0x3a, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, - 0x8b, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0xb0, 0x43, 0x74, 0xf3, - 0x52, 0x4b, 0xca, 0xf3, 0x8b, 0xb2, 0xa1, 0xbc, 0x9c, 0xd4, 0x94, 0xf4, 0xd4, 0x22, 0xfd, 0x0a, - 0x0c, 0x6f, 0x24, 0xb1, 0x81, 0x9d, 0x6f, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0xcf, 0x71, 0xfc, - 0x2f, 0xe7, 0x00, 0x00, 0x00, + 0x8b, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0xb0, 0xa9, 0xba, 0x79, + 0xa9, 0x25, 0xe5, 0xf9, 0x45, 0xd9, 0x50, 0x5e, 0x4e, 0x6a, 0x4a, 0x7a, 0x6a, 0x91, 0x7e, 0x05, + 0x46, 0xd0, 0x24, 0xb1, 0x81, 0x83, 0xc4, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x9b, 0x6b, 0xa4, + 0xd9, 0x3b, 0x01, 0x00, 0x00, } func (m *BasketCredit) Marshal() (dAtA []byte, err error) { @@ -141,6 +190,43 @@ func (m *BasketCredit) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *BasketCredits) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *BasketCredits) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *BasketCredits) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Credits) > 0 { + for iNdEx := len(m.Credits) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Credits[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { offset -= sovTypes(v) base := offset @@ -169,6 +255,21 @@ func (m *BasketCredit) Size() (n int) { return n } +func (m *BasketCredits) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Credits) > 0 { + for _, e := range m.Credits { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } + } + return n +} + func sovTypes(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -292,6 +393,93 @@ func (m *BasketCredit) Unmarshal(dAtA []byte) error { } return nil } +func (m *BasketCredits) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: BasketCredits: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: BasketCredits: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Credits", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Credits = append(m.Credits, &BasketCredit{}) + if err := m.Credits[len(m.Credits)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipTypes(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/ecocredit/client/basket/tx.go b/x/ecocredit/client/basket/tx.go new file mode 100644 index 0000000000..d523388c62 --- /dev/null +++ b/x/ecocredit/client/basket/tx.go @@ -0,0 +1,74 @@ +package basket + +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" + + keeper "github.com/regen-network/regen-ledger/x/ecocredit/basket" +) + +func TxAddToBasket() *cobra.Command { + cmd := &cobra.Command{ + Use: "add-to-basket [owner] [basket_denom] [credits_json_file]", + Short: "add credits to the basket", + Long: strings.TrimSpace(`add credits to the basket. + +Parameters: + owner: account address of the owner of credits being put into the basket. + Note, the'--from' flag is ignored as it is implied from [owner] + basket_denom: basket denom is the basket denom to add credits to. + +Example: + $regen tx ecocredit add-to-basket [owner] [basket_denom] [credits_json_file] + + Where credits_json_file contains: + + { + "credits": [ + { + "batch_denom": "C01-20210101-20220101-001", + "amount": "10", + }, + { + "batch_denom": "C01-20210101-20220101-001", + "amount": "10.5", + } + ] + } + `), + Args: cobra.ExactArgs(3), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.Flags().Set(flags.FlagFrom, args[0]) + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + credits, err := parseBasketCredits(clientCtx, args[2]) + if err != nil { + return err + } + + msg := keeper.MsgPut{ + Owner: clientCtx.FromAddress.String(), + BasketDenom: args[1], + Credits: credits, + } + + if err := msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/ecocredit/client/basket/util.go b/x/ecocredit/client/basket/util.go new file mode 100644 index 0000000000..f4e61e9bf6 --- /dev/null +++ b/x/ecocredit/client/basket/util.go @@ -0,0 +1,29 @@ +package basket + +import ( + "fmt" + "io/ioutil" + + "github.com/cosmos/cosmos-sdk/client" + + keeper "github.com/regen-network/regen-ledger/x/ecocredit/basket" +) + +func parseBasketCredits(clientCtx client.Context, creditsFile string) ([]*keeper.BasketCredit, error) { + credits := keeper.BasketCredits{} + + if creditsFile == "" { + return nil, fmt.Errorf("credits file path is empty") + } + + contents, err := ioutil.ReadFile(creditsFile) + if err != nil { + return nil, err + } + + if err := clientCtx.Codec.UnmarshalJSON(contents, &credits); err != nil { + return nil, err + } + + return credits.Credits, nil +} From ea63e518a6e9bef946e86c3df512deadf15a5a4f Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Thu, 10 Feb 2022 22:33:45 +0530 Subject: [PATCH 2/8] register add-to-basket command --- x/ecocredit/client/tx.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x/ecocredit/client/tx.go b/x/ecocredit/client/tx.go index 78a4d83da7..fe6bb7363c 100644 --- a/x/ecocredit/client/tx.go +++ b/x/ecocredit/client/tx.go @@ -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. @@ -39,6 +40,7 @@ func TxCmd(name string) *cobra.Command { TxUpdateClassMetadataCmd(), TxUpdateClassIssuersCmd(), TxUpdateClassAdminCmd(), + basketcli.TxAddToBasket(), ) return cmd } From d6b29ffd889a1958d338db7f736e88370da123c2 Mon Sep 17 00:00:00 2001 From: MD Aleem <72057206+aleem1314@users.noreply.github.com> Date: Fri, 11 Feb 2022 14:23:50 +0530 Subject: [PATCH 3/8] Update x/ecocredit/client/basket/tx.go Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> --- x/ecocredit/client/basket/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/ecocredit/client/basket/tx.go b/x/ecocredit/client/basket/tx.go index d523388c62..b4b7f2830b 100644 --- a/x/ecocredit/client/basket/tx.go +++ b/x/ecocredit/client/basket/tx.go @@ -20,7 +20,7 @@ func TxAddToBasket() *cobra.Command { Parameters: owner: account address of the owner of credits being put into the basket. - Note, the'--from' flag is ignored as it is implied from [owner] + Note, the '--from' flag is ignored as it is implied from [owner] basket_denom: basket denom is the basket denom to add credits to. Example: From 29cd6ce3c1890b22f3e85db1a83c2149fea414b6 Mon Sep 17 00:00:00 2001 From: MD Aleem <72057206+aleem1314@users.noreply.github.com> Date: Fri, 11 Feb 2022 14:24:04 +0530 Subject: [PATCH 4/8] Update x/ecocredit/client/basket/tx.go Co-authored-by: Robert Zaremba --- x/ecocredit/client/basket/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/ecocredit/client/basket/tx.go b/x/ecocredit/client/basket/tx.go index b4b7f2830b..30dc2e17f1 100644 --- a/x/ecocredit/client/basket/tx.go +++ b/x/ecocredit/client/basket/tx.go @@ -21,7 +21,7 @@ func TxAddToBasket() *cobra.Command { Parameters: owner: account address of the owner of credits being put into the basket. Note, the '--from' flag is ignored as it is implied from [owner] - basket_denom: basket denom is the basket denom to add credits to. + basket_denom: basket identifier Example: $regen tx ecocredit add-to-basket [owner] [basket_denom] [credits_json_file] From 410d357d202b5c322d172e7ed83221ca2093aa8f Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 11 Feb 2022 14:29:11 +0530 Subject: [PATCH 5/8] chore: remove owner param --- x/ecocredit/client/basket/tx.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/x/ecocredit/client/basket/tx.go b/x/ecocredit/client/basket/tx.go index 30dc2e17f1..e2d5e75481 100644 --- a/x/ecocredit/client/basket/tx.go +++ b/x/ecocredit/client/basket/tx.go @@ -9,22 +9,23 @@ 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" ) -func TxAddToBasket() *cobra.Command { +func TxPutInBasket() *cobra.Command { cmd := &cobra.Command{ - Use: "add-to-basket [owner] [basket_denom] [credits_json_file]", + Use: "put-in-basket [basket_denom] [credits_json_file]", Short: "add credits to the basket", Long: strings.TrimSpace(`add credits to the basket. Parameters: - owner: account address of the owner of credits being put into the basket. - Note, the '--from' flag is ignored as it is implied from [owner] basket_denom: basket identifier +Flags: + from: account address of the owner + Example: - $regen tx ecocredit add-to-basket [owner] [basket_denom] [credits_json_file] + $regen tx ecocredit put-in-basket [basket_denom] [credits_json_file] Where credits_json_file contains: @@ -41,22 +42,21 @@ Example: ] } `), - Args: cobra.ExactArgs(3), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - cmd.Flags().Set(flags.FlagFrom, args[0]) clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } - credits, err := parseBasketCredits(clientCtx, args[2]) + credits, err := parseBasketCredits(clientCtx, args[1]) if err != nil { return err } - msg := keeper.MsgPut{ + msg := basket.MsgPut{ Owner: clientCtx.FromAddress.String(), - BasketDenom: args[1], + BasketDenom: args[0], Credits: credits, } From ae414c191dcfb9671e461c49b6ec533604960be8 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 11 Feb 2022 15:26:01 +0530 Subject: [PATCH 6/8] chore: review changes --- go.mod | 123 +----------- go.sum | 8 +- proto/regen/ecocredit/basket/v1/types.proto | 6 - x/basket/spec/protobuf.md | 16 -- x/ecocredit/basket/types.pb.go | 200 +------------------- x/ecocredit/client/basket/tx.go | 26 ++- x/ecocredit/client/basket/util.go | 18 +- x/ecocredit/client/basket/util_test.go | 77 ++++++++ 8 files changed, 110 insertions(+), 364 deletions(-) create mode 100644 x/ecocredit/client/basket/util_test.go diff --git a/go.mod b/go.mod index 5d2f124ee1..c7024ab5f7 100644 --- a/go.mod +++ b/go.mod @@ -17,128 +17,11 @@ 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-20220209195652-db638375bc3a // 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 ( - filippo.io/edwards25519 v1.0.0-beta.2 // indirect - github.com/99designs/keyring v1.1.6 // indirect - github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect - github.com/DataDog/zstd v1.4.5 // indirect - github.com/Workiva/go-datastructures v1.0.52 // indirect - github.com/armon/go-metrics v0.3.9 // indirect - github.com/beorn7/perks v1.0.1 // indirect - github.com/bgentry/speakeasy v0.1.0 // indirect - github.com/btcsuite/btcd v0.22.0-beta // indirect - github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce // indirect - github.com/cespare/xxhash v1.1.0 // indirect - github.com/cespare/xxhash/v2 v2.1.1 // indirect - github.com/cockroachdb/apd/v2 v2.0.2 // indirect - github.com/coinbase/rosetta-sdk-go v0.6.10 // indirect - github.com/confio/ics23/go v0.6.6 // indirect - github.com/cosmos/cosmos-proto v1.0.0-alpha7 // indirect - github.com/cosmos/cosmos-sdk/api v0.1.0-alpha4 // indirect - github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.2 // indirect - github.com/cosmos/cosmos-sdk/orm v1.0.0-alpha.9 // indirect - github.com/cosmos/go-bip39 v1.0.0 // indirect - github.com/cosmos/iavl v0.17.1 // indirect - github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect - github.com/cosmos/ledger-go v0.9.2 // indirect - github.com/danieljoos/wincred v1.0.2 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect - github.com/dgraph-io/badger/v2 v2.2007.2 // indirect - github.com/dgraph-io/ristretto v0.0.3 // indirect - github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect - github.com/dustin/go-humanize v1.0.0 // indirect - github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect - github.com/enigmampc/btcutil v1.0.3-0.20200723161021-e2fb6adb2a25 // indirect - github.com/felixge/httpsnoop v1.0.1 // indirect - github.com/fsnotify/fsnotify v1.5.1 // indirect - github.com/go-kit/kit v0.10.0 // indirect - github.com/go-logfmt/logfmt v0.5.0 // indirect - github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect - github.com/gogo/gateway v1.1.0 // indirect - github.com/gogo/protobuf v1.3.3 // indirect - github.com/golang/protobuf v1.5.2 // indirect - github.com/golang/snappy v0.0.3 // indirect - github.com/google/btree v1.0.0 // indirect - github.com/google/orderedcode v0.0.1 // indirect - github.com/gorilla/handlers v1.5.1 // indirect - github.com/gorilla/websocket v1.4.2 // indirect - github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect - github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect - github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect - github.com/gtank/merlin v0.1.1 // indirect - github.com/gtank/ristretto255 v0.1.2 // indirect - github.com/hashicorp/go-immutable-radix v1.0.0 // indirect - github.com/hashicorp/golang-lru v0.5.4 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect - github.com/hdevalence/ed25519consensus v0.0.0-20210204194344-59a8610d2b87 // indirect - github.com/improbable-eng/grpc-web v0.14.1 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/ipfs/go-cid v0.0.7 // indirect - github.com/jmhodges/levigo v1.0.0 // indirect - github.com/keybase/go-keychain v0.0.0-20190712205309-48d3d31d256d // indirect - github.com/klauspost/compress v1.11.7 // indirect - github.com/lib/pq v1.10.2 // indirect - github.com/libp2p/go-buffer-pool v0.0.2 // indirect - github.com/magiconair/properties v1.8.5 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect - github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect - github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect - github.com/minio/highwayhash v1.0.1 // indirect - github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/mitchellh/mapstructure v1.4.2 // indirect - github.com/mr-tron/base58 v1.1.3 // indirect - github.com/mtibben/percent v0.2.1 // indirect - github.com/multiformats/go-base32 v0.0.3 // indirect - github.com/multiformats/go-base36 v0.1.0 // indirect - github.com/multiformats/go-multibase v0.0.3 // indirect - github.com/multiformats/go-multihash v0.0.13 // indirect - github.com/multiformats/go-varint v0.0.5 // indirect - github.com/pelletier/go-toml v1.9.4 // indirect - github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect - github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.11.0 // indirect - github.com/prometheus/client_model v0.2.0 // indirect - github.com/prometheus/common v0.29.0 // indirect - github.com/prometheus/procfs v0.6.0 // indirect - github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect - github.com/regen-network/cosmos-proto v0.3.1 // indirect - github.com/regen-network/regen-ledger/api v0.1.0 // indirect - github.com/regen-network/regen-ledger/orm v1.0.0-beta1 // indirect - github.com/rs/cors v1.7.0 // indirect - github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect - github.com/spaolacci/murmur3 v1.1.0 // indirect - github.com/spf13/afero v1.6.0 // indirect - github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect - github.com/spf13/viper v1.8.1 // indirect - github.com/subosito/gotenv v1.2.0 // indirect - github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca // indirect - github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect - github.com/tendermint/btcd v0.1.1 // indirect - github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect - github.com/tendermint/go-amino v0.16.0 // indirect - github.com/zondax/hid v0.9.0 // indirect - go.etcd.io/bbolt v1.3.6 // indirect - golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect - golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect - golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect - golang.org/x/text v0.3.7 // indirect - google.golang.org/grpc v1.44.0 // indirect - google.golang.org/protobuf v1.27.1 // indirect - gopkg.in/ini.v1 v1.63.2 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect - nhooyr.io/websocket v1.8.6 // indirect - sigs.k8s.io/yaml v1.1.0 // indirect -) +require golang.org/x/text v0.3.7 // indirect replace google.golang.org/grpc => google.golang.org/grpc v1.33.2 diff --git a/go.sum b/go.sum index 6123773638..b0c4b5c939 100644 --- a/go.sum +++ b/go.sum @@ -1149,8 +1149,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-20220209195652-db638375bc3a h1:atOEWVSedO4ksXBe/UrlbSLVxQQ9RxM/tT2Jy10IaHo= -golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a/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= @@ -1561,8 +1561,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= diff --git a/proto/regen/ecocredit/basket/v1/types.proto b/proto/regen/ecocredit/basket/v1/types.proto index 5b9d352da9..f83e0a363c 100644 --- a/proto/regen/ecocredit/basket/v1/types.proto +++ b/proto/regen/ecocredit/basket/v1/types.proto @@ -14,10 +14,4 @@ message BasketCredit { // Decimal values are acceptable within the precision of the corresponding // credit type for this batch. string amount = 2; -} - -// BasketCredits represents the slice of BasketCredit objects. -message BasketCredits { - // credits is the list of basket credits - repeated BasketCredit credits = 1; } \ No newline at end of file diff --git a/x/basket/spec/protobuf.md b/x/basket/spec/protobuf.md index 6a35bb663c..d0204ae4bc 100644 --- a/x/basket/spec/protobuf.md +++ b/x/basket/spec/protobuf.md @@ -6,7 +6,6 @@ - [regen/ecocredit/basket/v1/types.proto](#regen/ecocredit/basket/v1/types.proto) - [BasketCredit](#regen.ecocredit.basket.v1.BasketCredit) - - [BasketCredits](#regen.ecocredit.basket.v1.BasketCredits) - [regen/ecocredit/basket/v1/tx.proto](#regen/ecocredit/basket/v1/tx.proto) - [MsgCreate](#regen.ecocredit.basket.v1.MsgCreate) @@ -44,21 +43,6 @@ BasketCredit represents the information for a credit batch inside a basket. - - - -### BasketCredits -BasketCredits represents the slice of BasketCredit objects. - - -| Field | Type | Label | Description | -| ----- | ---- | ----- | ----------- | -| credits | [BasketCredit](#regen.ecocredit.basket.v1.BasketCredit) | repeated | credits is the list of basket credits | - - - - - diff --git a/x/ecocredit/basket/types.pb.go b/x/ecocredit/basket/types.pb.go index dfed916f92..c32674ae43 100644 --- a/x/ecocredit/basket/types.pb.go +++ b/x/ecocredit/basket/types.pb.go @@ -79,55 +79,8 @@ func (m *BasketCredit) GetAmount() string { return "" } -// BasketCredits represents the slice of BasketCredit objects. -type BasketCredits struct { - // credits is the list of basket credits - Credits []*BasketCredit `protobuf:"bytes,1,rep,name=credits,proto3" json:"credits,omitempty"` -} - -func (m *BasketCredits) Reset() { *m = BasketCredits{} } -func (m *BasketCredits) String() string { return proto.CompactTextString(m) } -func (*BasketCredits) ProtoMessage() {} -func (*BasketCredits) Descriptor() ([]byte, []int) { - return fileDescriptor_e6c256e957c69c4d, []int{1} -} -func (m *BasketCredits) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *BasketCredits) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_BasketCredits.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *BasketCredits) XXX_Merge(src proto.Message) { - xxx_messageInfo_BasketCredits.Merge(m, src) -} -func (m *BasketCredits) XXX_Size() int { - return m.Size() -} -func (m *BasketCredits) XXX_DiscardUnknown() { - xxx_messageInfo_BasketCredits.DiscardUnknown(m) -} - -var xxx_messageInfo_BasketCredits proto.InternalMessageInfo - -func (m *BasketCredits) GetCredits() []*BasketCredit { - if m != nil { - return m.Credits - } - return nil -} - func init() { proto.RegisterType((*BasketCredit)(nil), "regen.ecocredit.basket.v1.BasketCredit") - proto.RegisterType((*BasketCredits)(nil), "regen.ecocredit.basket.v1.BasketCredits") } func init() { @@ -135,7 +88,7 @@ func init() { } var fileDescriptor_e6c256e957c69c4d = []byte{ - // 229 bytes of a gzipped FileDescriptorProto + // 197 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x2d, 0x4a, 0x4d, 0x4f, 0xcd, 0xd3, 0x4f, 0x4d, 0xce, 0x4f, 0x2e, 0x4a, 0x4d, 0xc9, 0x2c, 0xd1, 0x4f, 0x4a, 0x2c, 0xce, 0x4e, 0x2d, 0xd1, 0x2f, 0x33, 0xd4, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, @@ -143,14 +96,12 @@ var fileDescriptor_e6c256e957c69c4d = []byte{ 0xe7, 0xe2, 0x71, 0x02, 0x73, 0x9c, 0xc1, 0x32, 0x42, 0xf2, 0x5c, 0xdc, 0x49, 0x89, 0x25, 0xc9, 0x19, 0xf1, 0x29, 0xa9, 0x79, 0xf9, 0xb9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x5c, 0x60, 0x21, 0x17, 0x90, 0x88, 0x90, 0x18, 0x17, 0x5b, 0x62, 0x6e, 0x7e, 0x69, 0x5e, 0x89, 0x04, 0x13, - 0x58, 0x0e, 0xca, 0x53, 0x0a, 0xe2, 0xe2, 0x45, 0x36, 0xa8, 0x58, 0xc8, 0x91, 0x8b, 0x1d, 0x62, - 0x5b, 0xb1, 0x04, 0xa3, 0x02, 0xb3, 0x06, 0xb7, 0x91, 0xba, 0x1e, 0x4e, 0x67, 0xe8, 0x21, 0x6b, - 0x0d, 0x82, 0xe9, 0x73, 0x0a, 0x3a, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, + 0x58, 0x0e, 0xca, 0x73, 0x0a, 0x3a, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, - 0x8b, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0xb0, 0xa9, 0xba, 0x79, - 0xa9, 0x25, 0xe5, 0xf9, 0x45, 0xd9, 0x50, 0x5e, 0x4e, 0x6a, 0x4a, 0x7a, 0x6a, 0x91, 0x7e, 0x05, - 0x46, 0xd0, 0x24, 0xb1, 0x81, 0x83, 0xc4, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x9b, 0x6b, 0xa4, - 0xd9, 0x3b, 0x01, 0x00, 0x00, + 0x8b, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0xb0, 0x43, 0x74, 0xf3, + 0x52, 0x4b, 0xca, 0xf3, 0x8b, 0xb2, 0xa1, 0xbc, 0x9c, 0xd4, 0x94, 0xf4, 0xd4, 0x22, 0xfd, 0x0a, + 0x0c, 0x6f, 0x24, 0xb1, 0x81, 0x9d, 0x6f, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0xcf, 0x71, 0xfc, + 0x2f, 0xe7, 0x00, 0x00, 0x00, } func (m *BasketCredit) Marshal() (dAtA []byte, err error) { @@ -190,43 +141,6 @@ func (m *BasketCredit) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *BasketCredits) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *BasketCredits) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *BasketCredits) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Credits) > 0 { - for iNdEx := len(m.Credits) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Credits[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTypes(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - } - } - return len(dAtA) - i, nil -} - func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { offset -= sovTypes(v) base := offset @@ -255,21 +169,6 @@ func (m *BasketCredit) Size() (n int) { return n } -func (m *BasketCredits) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if len(m.Credits) > 0 { - for _, e := range m.Credits { - l = e.Size() - n += 1 + l + sovTypes(uint64(l)) - } - } - return n -} - func sovTypes(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -393,93 +292,6 @@ func (m *BasketCredit) Unmarshal(dAtA []byte) error { } return nil } -func (m *BasketCredits) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: BasketCredits: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: BasketCredits: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Credits", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Credits = append(m.Credits, &BasketCredit{}) - if err := m.Credits[len(m.Credits)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func skipTypes(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/ecocredit/client/basket/tx.go b/x/ecocredit/client/basket/tx.go index e2d5e75481..c26e4512aa 100644 --- a/x/ecocredit/client/basket/tx.go +++ b/x/ecocredit/client/basket/tx.go @@ -1,4 +1,4 @@ -package basket +package basketclient import ( "strings" @@ -29,18 +29,16 @@ Example: Where credits_json_file contains: - { - "credits": [ - { - "batch_denom": "C01-20210101-20220101-001", - "amount": "10", - }, - { - "batch_denom": "C01-20210101-20220101-001", - "amount": "10.5", - } - ] - } + [ + { + "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 { @@ -49,7 +47,7 @@ Example: return err } - credits, err := parseBasketCredits(clientCtx, args[1]) + credits, err := parseBasketCredits(args[1]) if err != nil { return err } diff --git a/x/ecocredit/client/basket/util.go b/x/ecocredit/client/basket/util.go index f4e61e9bf6..36f03c2ef2 100644 --- a/x/ecocredit/client/basket/util.go +++ b/x/ecocredit/client/basket/util.go @@ -1,29 +1,27 @@ -package basket +package basketclient import ( + "encoding/json" "fmt" "io/ioutil" - "github.com/cosmos/cosmos-sdk/client" - - keeper "github.com/regen-network/regen-ledger/x/ecocredit/basket" + "github.com/regen-network/regen-ledger/x/ecocredit/basket" ) -func parseBasketCredits(clientCtx client.Context, creditsFile string) ([]*keeper.BasketCredit, error) { - credits := keeper.BasketCredits{} +func parseBasketCredits(creditsFile string) ([]*basket.BasketCredit, error) { + credits := []*basket.BasketCredit{} if creditsFile == "" { return nil, fmt.Errorf("credits file path is empty") } - contents, err := ioutil.ReadFile(creditsFile) + bz, err := ioutil.ReadFile(creditsFile) if err != nil { return nil, err } - - if err := clientCtx.Codec.UnmarshalJSON(contents, &credits); err != nil { + if err = json.Unmarshal(bz, &credits); err != nil { return nil, err } - return credits.Credits, nil + return credits, nil } diff --git a/x/ecocredit/client/basket/util_test.go b/x/ecocredit/client/basket/util_test.go new file mode 100644 index 0000000000..f37a53d012 --- /dev/null +++ b/x/ecocredit/client/basket/util_test.go @@ -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) + } + }) + } +} From 68573d14f225e66f361e7dc7c8e5041b4ebd0788 Mon Sep 17 00:00:00 2001 From: aleem1314 Date: Fri, 11 Feb 2022 15:32:42 +0530 Subject: [PATCH 7/8] revert proto changes --- x/ecocredit/basket/tx.pb.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/ecocredit/basket/tx.pb.go b/x/ecocredit/basket/tx.pb.go index a45878edec..ac3b94de19 100644 --- a/x/ecocredit/basket/tx.pb.go +++ b/x/ecocredit/basket/tx.pb.go @@ -62,8 +62,8 @@ type MsgCreate struct { // min_start_date is the earliest start date for batches of credits allowed // into the basket. MinStartDate *types.Timestamp `protobuf:"bytes,8,opt,name=min_start_date,json=minStartDate,proto3" json:"min_start_date,omitempty"` - // fee is the fee that the curator will pay to create the basket. It must be - // >= the required Params.basket_creation_fee. We include the fee explicitly + // fee is the fee that the curator will pay to create the basket. It must be >= + // the required Params.basket_creation_fee. We include the fee explicitly // here so that the curator explicitly acknowledges paying this fee and // is not surprised to learn that the paid a big fee and didn't know // beforehand. @@ -329,7 +329,7 @@ func (m *MsgPutResponse) GetAmountReceived() string { type MsgTake struct { // owner is the owner of the basket tokens. Owner string `protobuf:"bytes,1,opt,name=owner,proto3" json:"owner,omitempty"` - // basket_denom is the basket denom to take credits from. + // basket_denom is the basket bank denom to take credits from. BasketDenom string `protobuf:"bytes,2,opt,name=basket_denom,json=basketDenom,proto3" json:"basket_denom,omitempty"` // amount is the integer number of basket tokens to convert into credits. Amount string `protobuf:"bytes,3,opt,name=amount,proto3" json:"amount,omitempty"` @@ -2158,4 +2158,4 @@ var ( ErrInvalidLengthTx = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowTx = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroupTx = fmt.Errorf("proto: unexpected end of group") -) +) \ No newline at end of file From d39d010f1cf258ae2963424e15c44b3f28231b0f Mon Sep 17 00:00:00 2001 From: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> Date: Fri, 11 Feb 2022 15:41:27 -0800 Subject: [PATCH 8/8] Apply suggestions from code review --- proto/regen/ecocredit/basket/v1/types.proto | 2 +- x/ecocredit/basket/tx.pb.go | 2 +- x/ecocredit/client/tx.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/proto/regen/ecocredit/basket/v1/types.proto b/proto/regen/ecocredit/basket/v1/types.proto index f83e0a363c..b2d5143c41 100644 --- a/proto/regen/ecocredit/basket/v1/types.proto +++ b/proto/regen/ecocredit/basket/v1/types.proto @@ -14,4 +14,4 @@ message BasketCredit { // Decimal values are acceptable within the precision of the corresponding // credit type for this batch. string amount = 2; -} \ No newline at end of file +} diff --git a/x/ecocredit/basket/tx.pb.go b/x/ecocredit/basket/tx.pb.go index ac3b94de19..e7761519a4 100644 --- a/x/ecocredit/basket/tx.pb.go +++ b/x/ecocredit/basket/tx.pb.go @@ -2158,4 +2158,4 @@ var ( ErrInvalidLengthTx = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflowTx = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroupTx = fmt.Errorf("proto: unexpected end of group") -) \ No newline at end of file +) diff --git a/x/ecocredit/client/tx.go b/x/ecocredit/client/tx.go index fe6bb7363c..16be55aa3f 100644 --- a/x/ecocredit/client/tx.go +++ b/x/ecocredit/client/tx.go @@ -40,7 +40,7 @@ func TxCmd(name string) *cobra.Command { TxUpdateClassMetadataCmd(), TxUpdateClassIssuersCmd(), TxUpdateClassAdminCmd(), - basketcli.TxAddToBasket(), + basketcli.TxPutInBasket(), ) return cmd }