Skip to content

Commit

Permalink
feat(x/ecocredit)!: add ecocredit projects msg & query server impleme…
Browse files Browse the repository at this point in the history
…ntations (#647)

* build: run make proto-gen

* feat: add create project msg

* add project id tests

* feat: fix msg tests

* feat: add msg & query server implementations

* chore: register msgs

* feat: fix cli

* fix: fix genesis import & export

* feat: add create project simulation

* fix: fix msg server tests

* chore: go mod tidy

* fix: fix simulations

* WIP: add cli commands

* feat: add project-info command

* chore: fix cli tx tests

* chore: fix error

* chore: update github action

* wip: fix tests

* chore: add logs to failing tests

* fix: fix failing tests

* Update .github/workflows/test.yml

* Update .github/workflows/test.yml

* update workflows/test.yml

* Update x/ecocredit/client/query.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>

* Update x/ecocredit/client/query.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>

* Update x/ecocredit/project.go

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>

* chore: review changes

* chore: run make proto-gen

* chore: go mod tidy

* fix build error

* chore: review changes

* feat: add genesis test

* Update x/ecocredit/server/genesis.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* chore: review changes

* chore: review changes

* chore: fix failing tests

* chore: add project id generator tests

* chore: fix test

* chore: fix test

* chore: fix test

* Update x/ecocredit/client/tx.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* Update x/ecocredit/project.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

* Update x/ecocredit/server/server_test.go

Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>

Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 3, 2022
1 parent a70bae8 commit 17331c0
Show file tree
Hide file tree
Showing 37 changed files with 14,951 additions and 6,127 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ jobs:
with:
flags: ${{matrix.envs}}
file: coverage.txt

6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.14
github.com/tendermint/tm-db v0.6.4
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211117155847-120650a500bb // indirect
)

require (
Expand Down Expand Up @@ -123,9 +120,12 @@ require (
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // 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/genproto v0.0.0-20211203200212-54befc351ae9 // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,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-20211117183948-ae814b36b871 h1:/pEO3GD/ABYAjuakUS6xSEmmlyVS4kxBNkeA9tLJiTI=
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e h1:MUP6MR3rJ7Gk9LEia0LP2ytiH6MuCfs7qYz+47jGdD8=
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/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 @@ -1544,8 +1544,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-20211117155847-120650a500bb h1:B1cc9lxfg3Zd0zoHxwyckY7YPprzkXKNWw9sxJ3/obk=
google.golang.org/genproto v0.0.0-20211117155847-120650a500bb/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9 h1:fU3FNfL/oBU2D5DvGqiuyVqqn40DdxvaTFHq7aivA3k=
google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
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
3 changes: 3 additions & 0 deletions proto/regen/ecocredit/v1alpha2/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ message GenesisState {
// project_info is the list of projects.
repeated ProjectInfo project_info = 7;

// project_seq_num is the project table orm.Sequence,
// it is used to generate the next project id.
uint64 project_seq_num = 8;
}

// Balance represents tradable or retired units of a credit batch with an
Expand Down
60 changes: 56 additions & 4 deletions x/ecocredit/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func QueryCmd(name string) *cobra.Command {
QueryBalanceCmd(),
QuerySupplyCmd(),
QueryCreditTypesCmd(),
QueryProjectsCmd(),
QueryProjectInfoCmd(),
QueryParamsCmd(),
QuerySellOrderCmd(),
QuerySellOrdersCmd(),
Expand Down Expand Up @@ -97,12 +99,62 @@ func QueryClassInfoCmd() *cobra.Command {
})
}

// QueryProjectsCmd returns a query command that retrieves projects.
func QueryProjectsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "projects [class_id]",
Short: "List all projects in the given class with pagination flags",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
if err != nil {
return err
}

pagination, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

res, err := c.Projects(cmd.Context(), &ecocredit.QueryProjectsRequest{
ClassId: args[0],
Pagination: pagination,
})
return print(ctx, res, err)
},
}
flags.AddPaginationFlagsToCmd(cmd, "projects")
return qflags(cmd)
}

// QueryProjectInfoCmd returns a query command that retrieves project information.
func QueryProjectInfoCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "project-info [project_id]",
Short: "Retrive project 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.ProjectInfo(cmd.Context(), &ecocredit.QueryProjectInfoRequest{
ProjectId: args[0],
})
return print(ctx, res, err)
},
}

return qflags(cmd)
}

// QueryBatchesCmd returns a query command that retrieves credit batches for a
// given credit class.
// given project.
func QueryBatchesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "batches [class_id]",
Short: "List all credit batches in the given class with pagination flags",
Use: "batches [project_id]",
Short: "List all credit batches in the given project with pagination flags",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, ctx, err := mkQueryClient(cmd)
Expand All @@ -116,7 +168,7 @@ func QueryBatchesCmd() *cobra.Command {
}

res, err := c.Batches(cmd.Context(), &ecocredit.QueryBatchesRequest{
ClassId: args[0],
ProjectId: args[0],
Pagination: pagination,
})
return print(ctx, res, err)
Expand Down
58 changes: 29 additions & 29 deletions x/ecocredit/client/testsuite/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ func (s *IntegrationTestSuite) TestGetClasses() {
}{
{
"invalid path",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/class", val.APIAddress),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/class", val.APIAddress),
true,
0,
},
{
"valid query",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes", val.APIAddress),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/classes", val.APIAddress),
false,
4,
},
{
"valid query pagination",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes?pagination.limit=2", val.APIAddress),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/classes?pagination.limit=2", val.APIAddress),
false,
2,
},
Expand Down Expand Up @@ -69,21 +69,21 @@ func (s *IntegrationTestSuite) TestGetClass() {
}{
{
"invalid path",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/class", val.APIAddress),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/class", val.APIAddress),
true,
"Not Implemented",
"",
},
{
"class not found",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes/%s", val.APIAddress, "C999"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/classes/%s", val.APIAddress, "C999"),
true,
"not found",
"",
},
{
"valid class-id",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes/%s", val.APIAddress, "C01"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/classes/%s", val.APIAddress, "C01"),
false,
"",
"C01",
Expand Down Expand Up @@ -122,29 +122,29 @@ func (s *IntegrationTestSuite) TestGetBatches() {
errMsg string
}{
{
"invalid class-id",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes/%s/batches", val.APIAddress, "abcd"),
"invalid project-id",
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/projects/%s/batches", val.APIAddress, "abc-d"),
0,
true,
"class ID didn't match the format",
"invalid project id",
},
{
"no batches found",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes/%s/batches", val.APIAddress, "C100"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/projects/%s/batches", val.APIAddress, "P02"),
0,
false,
"",
},
{
"valid request",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes/%s/batches", val.APIAddress, "C01"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/projects/%s/batches", val.APIAddress, "P01"),
4,
false,
"",
},
{
"valid request with pagination",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/classes/%s/batches?pagination.limit=2", val.APIAddress, "C01"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/projects/%s/batches?pagination.limit=2", val.APIAddress, "P01"),
2,
false,
"",
Expand Down Expand Up @@ -177,32 +177,32 @@ func (s *IntegrationTestSuite) TestGetBatch() {
val := s.network.Validators[0]

testCases := []struct {
name string
url string
expErr bool
errMsg string
classID string
name string
url string
expErr bool
errMsg string
projectID string
}{
{
"invalid batch denom",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s", val.APIAddress, "C999"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s", val.APIAddress, "C999"),
true,
"invalid denom",
"",
},
{
"no batches found",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s", val.APIAddress, "A00-00000000-00000000-000"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s", val.APIAddress, "A00-00000000-00000000-000"),
true,
"not found",
"",
},
{
"valid request",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s", val.APIAddress, "C01-20210101-20210201-002"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s", val.APIAddress, "C01-20210101-20210201-002"),
false,
"",
"C01",
"P01",
},
}

Expand All @@ -222,7 +222,7 @@ func (s *IntegrationTestSuite) TestGetBatch() {
} else {
require.NoError(err)
require.NotNil(batch.Info)
require.Equal(batch.Info.ClassId, tc.classID)
require.Equal(batch.Info.ProjectId, tc.projectID)
}
})
}
Expand All @@ -232,7 +232,7 @@ func (s *IntegrationTestSuite) TestCreditTypes() {
require := s.Require()
val := s.network.Validators[0]

url := fmt.Sprintf("%s/regen/ecocredit/v1alpha1/credit-types", val.APIAddress)
url := fmt.Sprintf("%s/regen/ecocredit/v1alpha2/credit-types", val.APIAddress)
resp, err := rest.GetRequest(url)
require.NoError(err)

Expand All @@ -256,19 +256,19 @@ func (s *IntegrationTestSuite) TestGetBalance() {
}{
{
"invalid batch-denom",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s/balance/%s", val.APIAddress, "abcd", val.Address.String()),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s/balance/%s", val.APIAddress, "abcd", val.Address.String()),
true,
"invalid denom",
},
{
"invalid account address",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s/balance/%s", val.APIAddress, "C01-20210101-20210201-001", "abcd"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s/balance/%s", val.APIAddress, "C01-20210101-20210201-001", "abcd"),
true,
"decoding bech32 failed",
},
{
"valid request",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s/balance/%s", val.APIAddress, "C01-20210101-20210201-002", val.Address.String()),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s/balance/%s", val.APIAddress, "C01-20210101-20210201-002", val.Address.String()),
false,
"",
},
Expand Down Expand Up @@ -308,13 +308,13 @@ func (s *IntegrationTestSuite) TestGetSupply() {
}{
{
"invalid batch-denom",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s/supply", val.APIAddress, "abcd"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s/supply", val.APIAddress, "abcd"),
true,
"invalid denom",
},
{
"valid request",
fmt.Sprintf("%s/regen/ecocredit/v1alpha1/batches/%s/supply", val.APIAddress, "C01-20210101-20210201-001"),
fmt.Sprintf("%s/regen/ecocredit/v1alpha2/batches/%s/supply", val.APIAddress, "C01-20210101-20210201-001"),
false,
"",
},
Expand Down Expand Up @@ -347,7 +347,7 @@ func (s *IntegrationTestSuite) TestGRPCQueryParams() {
val := s.network.Validators[0]
require := s.Require()

resp, err := rest.GetRequest(fmt.Sprintf("%s/regen/ecocredit/v1alpha1/params", val.APIAddress))
resp, err := rest.GetRequest(fmt.Sprintf("%s/regen/ecocredit/v1alpha2/params", val.APIAddress))
require.NoError(err)

var params ecocredit.QueryParamsResponse
Expand Down
16 changes: 8 additions & 8 deletions x/ecocredit/client/testsuite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,20 @@ func (s *IntegrationTestSuite) TestQueryBatches() {
expectedErrMsg: "Error: accepts 1 arg(s), received 2",
},
{
name: "invalid class id",
args: []string{"abcde"},
name: "invalid project id",
args: []string{"abcd-e"},
expectErr: true,
expectedErrMsg: "class ID didn't match the format",
expectedErrMsg: "invalid project id",
},
{
name: "existing class no batches",
args: []string{"C02"},
name: "existing project no batches",
args: []string{"P02"},
expectErr: false,
expectedBatchDenoms: []string{},
},
{
name: "no pagination flags",
args: []string{"C01"},
args: []string{"P01"},
expectErr: false,
expectedBatchDenoms: []string{
"C01-20210101-20210201-001",
Expand All @@ -192,7 +192,7 @@ func (s *IntegrationTestSuite) TestQueryBatches() {
{
name: "limit 2",
args: []string{
"C01",
"P01",
fmt.Sprintf("--%s=2", flags.FlagLimit),
},
expectErr: false,
Expand All @@ -204,7 +204,7 @@ func (s *IntegrationTestSuite) TestQueryBatches() {
{
name: "limit 2, offset 2",
args: []string{
"C01",
"P01",
fmt.Sprintf("--%s=2", flags.FlagLimit),
fmt.Sprintf("--%s=2", flags.FlagOffset),
},
Expand Down
Loading

0 comments on commit 17331c0

Please sign in to comment.