Skip to content

Commit

Permalink
feat!: Add dates as a top level field to credit batches (#393)
Browse files Browse the repository at this point in the history
Add start_date and end_date fields to BatchInfo and associated MsgCreateBatchRequest. These dates track the period during which the credit batch was quantified and verified.

Closes: #393
  • Loading branch information
ruhatch committed Jul 15, 2021
1 parent 91c9313 commit 0bd4cd9
Show file tree
Hide file tree
Showing 18 changed files with 628 additions and 208 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* add support for credit cancelling (#385)
* record retirement locations of ecocredit (#328)
* add dates as top level fields in credit batches (#393)

## [1.0.0] - 2021-04-13

Expand Down
6 changes: 6 additions & 0 deletions docs/modules/ecocredit/protobuf.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ EventCreateBatch is an event emitted when a credit batch is created.
| batch_denom | [string](#string) | | batch_denom is the unique ID of credit batch. |
| issuer | [string](#string) | | issuer is the account address of the issuer of the credit batch. |
| total_amount | [string](#string) | | total_amount is the total number of credits in the credit batch. |
| start_date | [string](#string) | | start_date is the beginning of the period during which this credit batch was quantified and verified. |
| end_date | [string](#string) | | end_date is the end of the period during which this credit batch was quantified and verified. |



Expand Down Expand Up @@ -185,6 +187,8 @@ BatchInfo represents the high-level on-chain information for a credit batch.
| total_amount | [string](#string) | | total_amount is the total number of active credits in the credit batch. Some of the issued credits may be cancelled and will be removed from total_amount and tracked in amount_cancelled. total_amount and amount_cancelled will always sum to the original amount of credits that were issued. |
| metadata | [bytes](#bytes) | | metadata is any arbitrary metadata to attached to the credit batch. |
| amount_cancelled | [string](#string) | | amount_cancelled is the number of credits in the batch that have been cancelled, effectively undoing there issuance. The sum of total_amount and amount_cancelled will always sum to the original amount of credits that were issued. |
| start_date | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | start_date is the beginning of the period during which this credit batch was quantified and verified. |
| end_date | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | end_date is the end of the period during which this credit batch was quantified and verified. |



Expand Down Expand Up @@ -493,6 +497,8 @@ MsgCreateBatchRequest is the Msg/CreateBatch request type.
| class_id | [string](#string) | | class_id is the unique ID of the class. |
| issuance | [MsgCreateBatchRequest.BatchIssuance](#regen.ecocredit.v1alpha1.MsgCreateBatchRequest.BatchIssuance) | repeated | issuance are the credits issued in the batch. |
| metadata | [bytes](#bytes) | | metadata is any arbitrary metadata to attached to the credit batch. |
| start_date | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | start_date is the beginning of the period during which this credit batch was quantified and verified. |
| end_date | [google.protobuf.Timestamp](#google.protobuf.Timestamp) | | end_date is the end of the period during which this credit batch was quantified and verified. |



Expand Down
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.11
github.com/tendermint/tm-db v0.6.4
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
google.golang.org/genproto v0.0.0-20210701191553-46259e63a0a9 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
google.golang.org/genproto v0.0.0-20210714021259-044028024a4f // indirect
)

replace google.golang.org/grpc => google.golang.org/grpc v1.33.2
Expand Down
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand Down Expand Up @@ -290,7 +289,6 @@ github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/gateway v1.1.0 h1:u0SuhL9+Il+UbjM9VIE3ntfRujKbvVpFvNB4HbjeVQ0=
github.com/gogo/gateway v1.1.0/go.mod h1:S7rR8FRQyG3QFESeSv4l2WnsyzlCLG0CzBbUUo/mbic=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -369,7 +367,6 @@ github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLe
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down Expand Up @@ -892,8 +889,8 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
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 @@ -1237,8 +1234,8 @@ google.golang.org/genproto v0.0.0-20210310155132-4ce2db91004e/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
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-20210701191553-46259e63a0a9 h1:HBPuvo39L0DgfVn9eHR3ki/RjZoUFWa+em77e7KFDfs=
google.golang.org/genproto v0.0.0-20210701191553-46259e63a0a9/go.mod h1:yiaVoXHpRzHGyxV3o4DktVWY4mSUErTKaeEOq6C3t3U=
google.golang.org/genproto v0.0.0-20210714021259-044028024a4f h1:UUQZ0BNnLgSI3v/uqq0hjdxwctexOPwnvKA5AA/Aze4=
google.golang.org/genproto v0.0.0-20210714021259-044028024a4f/go.mod h1:AxrInvYm1dci+enl5hChSFPOmmUF1+uAa/UsgNRWd7k=
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
8 changes: 8 additions & 0 deletions proto/regen/ecocredit/v1alpha1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ message EventCreateBatch {

// total_amount is the total number of credits in the credit batch.
string total_amount = 4 [ (gogoproto.moretags) = "yaml:\"total_amount\"" ];

// start_date is the beginning of the period during which this credit batch
// was quantified and verified.
string start_date = 5 [ (gogoproto.moretags) = "yaml:\"start_date\"" ];

// end_date is the end of the period during which this credit batch was
// quantified and verified.
string end_date = 6 [ (gogoproto.moretags) = "yaml:\"end_date\"" ];
}

// EventReceive is an event emitted when credits are received either upon
Expand Down
11 changes: 11 additions & 0 deletions proto/regen/ecocredit/v1alpha1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";
package regen.ecocredit.v1alpha1;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/regen-network/regen-ledger/x/ecocredit";

Expand Down Expand Up @@ -90,6 +91,16 @@ message MsgCreateBatchRequest {
// metadata is any arbitrary metadata to attached to the credit batch.
bytes metadata = 4;

// start_date is the beginning of the period during which this credit batch
// was quantified and verified.
google.protobuf.Timestamp start_date = 5
[(gogoproto.stdtime) = true, (gogoproto.moretags) = "yaml:\"start_date\""];

// end_date is the end of the period during which this credit batch was
// quantified and verified.
google.protobuf.Timestamp end_date = 6
[(gogoproto.stdtime) = true, (gogoproto.moretags) = "yaml:\"end_date\""];

// BatchIssuance represents the issuance of some credits in a batch to a
// single recipient.
message BatchIssuance {
Expand Down
13 changes: 12 additions & 1 deletion proto/regen/ecocredit/v1alpha1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package regen.ecocredit.v1alpha1;

import "cosmos/base/v1beta1/coin.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/regen-network/regen-ledger/x/ecocredit";

Expand Down Expand Up @@ -49,7 +50,17 @@ message BatchInfo {
// cancelled, effectively undoing there issuance. The sum of total_amount and
// amount_cancelled will always sum to the original amount of credits that
// were issued.
string amount_cancelled = 6;
string amount_cancelled = 6 [ (gogoproto.moretags) = "yaml:\"amount_cancelled\"" ];

// start_date is the beginning of the period during which this credit batch
// was quantified and verified.
google.protobuf.Timestamp start_date = 7
[(gogoproto.stdtime) = true, (gogoproto.moretags) = "yaml:\"start_date\""];

// end_date is the end of the period during which this credit batch was
// quantified and verified.
google.protobuf.Timestamp end_date = 8
[(gogoproto.stdtime) = true, (gogoproto.moretags) = "yaml:\"end_date\""];
}

// Params defines the updatable global parameters of the ecocredit module for
Expand Down
10 changes: 1 addition & 9 deletions x/data/query.pb.gw.go

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

41 changes: 29 additions & 12 deletions x/ecocredit/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,38 @@ Parameters:

func txCreateBatch() *cobra.Command {
return &cobra.Command{
Use: "create-batch [issuer] [class_id] [metadata] [issuance]",
Use: "create-batch [issuer] [class_id] [start_date] [end_date] [metadata] [issuance]",
Short: "Issues a new credit batch",
Long: `Issues a new credit batch.
Parameters:
issuer: issuer address
class_id: credit class
metadata: base64 encoded issuance metadata
issuance: YAML encode issuance list. Note: numerical values must be written in strings.
eg: '[{recipient: "xrn:sdgkjhs2345u79ghisodg", tradable_amount: "10", retired_amount: "2", retirement_location: "YY-ZZ 12345"}]'
Note: "tradable_amount" and "retired_amount" default to 0.
Note: "retirement_location" is only required when "retired_amount" is positive.`,
Args: cobra.ExactArgs(4),
issuer: issuer address
class_id: credit class
start_date: The beginning of the period during which this credit batch was
quantified and verified. Format: yyyy-mm-dd.
end_date: The end of the period during which this credit batch was
quantified and verified. Format: yyyy-mm-dd.
metadata: base64 encoded issuance metadata
issuance: YAML encode issuance list. Note: numerical values must be written in strings.
eg: '[{recipient: "xrn:sdgkjhs2345u79ghisodg", tradable_amount: "10", retired_amount: "2", retirement_location: "YY-ZZ 12345"}]'
Note: "tradable_amount" and "retired_amount" default to 0.
Note: "retirement_location" is only required when "retired_amount" is positive.`,
Args: cobra.ExactArgs(6),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := base64.StdEncoding.DecodeString(args[2])
startDate, err := parseDate("start_date", args[2])
if err != nil {
return err
}
endDate, err := parseDate("end_date", args[3])
if err != nil {
return err
}
b, err := base64.StdEncoding.DecodeString(args[4])
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "metadata is malformed, proper base64 string is required")
}
var issuance = []*ecocredit.MsgCreateBatchRequest_BatchIssuance{}
if err = yaml.Unmarshal([]byte(args[3]), &issuance); err != nil {
if err = yaml.Unmarshal([]byte(args[5]), &issuance); err != nil {
return err
}

Expand All @@ -109,7 +121,12 @@ Parameters:
return err
}
msg := ecocredit.MsgCreateBatchRequest{
Issuer: args[0], ClassId: args[1], Metadata: b, Issuance: issuance,
Issuer: args[0],
ClassId: args[1],
StartDate: &startDate,
EndDate: &endDate,
Metadata: b,
Issuance: issuance,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
Expand Down
14 changes: 12 additions & 2 deletions x/ecocredit/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"regexp"
"strings"
"time"

sdkclient "github.com/cosmos/cosmos-sdk/client"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -47,7 +49,7 @@ func parseCancelCreditsList(creditsListStr string) ([]*ecocredit.MsgCancelReques

cancelCreditsList := make([]*ecocredit.MsgCancelRequest_CancelCredits, len(creditsList))
for i, credits := range creditsList {
cancelCreditsList[i] = &ecocredit.MsgCancelRequest_CancelCredits {
cancelCreditsList[i] = &ecocredit.MsgCancelRequest_CancelCredits{
BatchDenom: credits.batchDenom,
Amount: credits.amount,
}
Expand Down Expand Up @@ -84,8 +86,16 @@ func parseCredits(creditsStr string) (credits, error) {
return credits{}, fmt.Errorf("invalid credit expression: %s", creditsStr)
}

return credits {
return credits{
batchDenom: matches[2],
amount: matches[1],
}, nil
}

func parseDate(field string, date string) (time.Time, error) {
t, err := time.Parse("2006-01-02", date)
if err != nil {
return t, sdkerrors.ErrInvalidRequest.Wrapf("%s must have format yyyy-mm-dd, but received %v", field, date)
}
return t, nil
}
Loading

0 comments on commit 0bd4cd9

Please sign in to comment.