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

refactor(x/ecocredit): update project id and batch denom #1046

Merged
merged 27 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eb12874
fix(x/ecocredit): project id and batch denom
ryanchristo Apr 21, 2022
dc98186
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 22, 2022
dc07ae8
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 22, 2022
d3ea05c
revised project id and batch denom
ryanchristo Apr 24, 2022
e66cc5a
revert implementation updates
ryanchristo Apr 24, 2022
b22a59a
revert implementation updates
ryanchristo Apr 24, 2022
2af559d
revise create batch steps
ryanchristo Apr 24, 2022
1db45d3
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 25, 2022
b23cbe9
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 25, 2022
482294d
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 26, 2022
34d47ad
update project id and batch denom
ryanchristo Apr 27, 2022
a4363c8
update project id and batch denom
ryanchristo Apr 27, 2022
cf96344
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 27, 2022
a4b6006
remove support for custom project ids
ryanchristo Apr 27, 2022
21c205d
remove support for custom project ids
ryanchristo Apr 27, 2022
9438b3a
update scenarios and clean up
ryanchristo Apr 27, 2022
63a57e8
clean up format comments
ryanchristo Apr 27, 2022
e58922e
clean up utils and regex
ryanchristo Apr 27, 2022
097df0a
clean up utils and regex
ryanchristo Apr 27, 2022
d740565
Update x/ecocredit/core/utils.go
ryanchristo Apr 28, 2022
80ac074
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 28, 2022
93415ff
fix jurisdiction error check
ryanchristo Apr 28, 2022
78d9884
update simulations
ryanchristo Apr 29, 2022
c49fc2d
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo Apr 29, 2022
03ee483
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo May 4, 2022
a52bf97
address review comment
ryanchristo May 4, 2022
39a603a
Merge branch 'master' into ryan/1032-project-id-batch-denom
ryanchristo May 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update scenarios and clean up
  • Loading branch information
ryanchristo committed Apr 27, 2022
commit 9438b3a9a54a9796f0cb5470c3773f41bb381a5e
8 changes: 4 additions & 4 deletions x/ecocredit/marketplace/msg_sell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMsgSell(t *testing.T) {
Owner: a1.String(),
Orders: []*MsgSell_Order{
{
BatchDenom: "A00-001-00000000-00000000-000",
BatchDenom: "A00-000-00000000-00000000-000",
Quantity: "1.5",
AskPrice: &sdk.Coin{
Denom: "uregen",
Expand All @@ -44,7 +44,7 @@ func TestMsgSell(t *testing.T) {
Owner: "foobar",
Orders: []*MsgSell_Order{
{
BatchDenom: "A00-001-00000000-00000000-000",
BatchDenom: "A00-000-00000000-00000000-000",
Quantity: "1.5",
AskPrice: &sdk.Coin{
Denom: "uregen",
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestMsgSell(t *testing.T) {
Owner: a1.String(),
Orders: []*MsgSell_Order{
{
BatchDenom: "A00-001-00000000-00000000-000",
BatchDenom: "A00-000-00000000-00000000-000",
Quantity: "-1.5",
AskPrice: &sdk.Coin{
Denom: "uregen",
Expand All @@ -95,7 +95,7 @@ func TestMsgSell(t *testing.T) {
Owner: a1.String(),
Orders: []*MsgSell_Order{
{
BatchDenom: "A00-001-00000000-00000000-000",
BatchDenom: "A00-000-00000000-00000000-000",
Quantity: "1.5",
AskPrice: &sdk.Coin{
Denom: "uregen",
Expand Down
10 changes: 5 additions & 5 deletions x/ecocredit/server/core/create_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ func (k Keeper) CreateProject(ctx context.Context, req *core.MsgCreateProject) (
// genProjectID generates a projectID when no projectID was given for CreateProject.
// The ID is generated by concatenating the classID and a sequence number.
func (k Keeper) genProjectID(ctx context.Context, classKey uint64, classID string) (string, error) {
var nextID uint64
var nextSeq uint64
projectSeqNo, err := k.stateStore.ProjectSequenceTable().Get(ctx, classKey)
switch err {
case ormerrors.NotFound:
nextID = 1
nextSeq = 1
case nil:
nextID = projectSeqNo.NextSequence
nextSeq = projectSeqNo.NextSequence
default:
return "", err
}

if err = k.stateStore.ProjectSequenceTable().Save(ctx, &api.ProjectSequence{
ClassKey: classKey,
NextSequence: nextID + 1,
NextSequence: nextSeq + 1,
}); err != nil {
return "", err
}

return core.FormatProjectId(classID, nextID), nil
return core.FormatProjectId(classID, nextSeq), nil
}
18 changes: 10 additions & 8 deletions x/ecocredit/server/core/features/msg_create_batch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ Feature: CreateBatch

Rule: A credit batch denom is always unique

Background:
Scenario: multiple credit batches from the same project
Given a credit type exists with abbreviation "A"
And alice has created a credit class with credit type "A"
And alice has created a project with credit class id "A01"
And alice has created a credit batch with project id "A01-001"
When alice creates a credit batch with project id "A01-001"
Then the credit batch exists with denom "A01-001-20200101-20210101-002"

Scenario: multiple credit batches from different projects
Given a credit type exists with abbreviation "A"
And a credit type exists with abbreviation "B"
And alice has created a credit class with credit type "A"
And alice has created a credit class with credit type "B"
And alice has created a project with credit class id "A01"
And alice has created a project with credit class id "B01"

Scenario: credit batches with one project
When alice creates a credit batch with project id "A01-001"
Then the credit batch exists with denom "A01-001-20200101-20210101-001"

Scenario: credit batches with multiple projects
Given alice has created a credit batch with project id "A01-001"
And alice has created a credit batch with project id "A01-001"
When alice creates a credit batch with project id "B01-001"
Then the credit batch exists with denom "B01-001-20200101-20210101-001"
21 changes: 19 additions & 2 deletions x/ecocredit/server/core/features/msg_create_project.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ Feature: CreateProject

Rule: A project id is always unique

Scenario Outline: the project id is auto-generated
Scenario: two projects from the same credit class
Given a credit type exists with abbreviation "A"
And alice has created a credit class with credit type "A"
And the project sequence number is "<next_sequence>"
And alice has created a project with credit class id "A01"
When alice creates a project with credit class id "A01"
Then the project exists with project id "A01-002"

Scenario: two projects from different credit classes
Given a credit type exists with abbreviation "A"
And a credit type exists with abbreviation "B"
And alice has created a credit class with credit type "A"
And alice has created a credit class with credit type "B"
And alice has created a project with credit class id "A01"
And alice has created a project with credit class id "B01"
When alice creates a project with credit class id "B01"
Then the project exists with project id "B01-001"

Scenario Outline: project id is formatted correctly
Given a credit type exists with abbreviation "A"
And alice has created a credit class with credit type "A"
And the project sequence for credit class "A01" is "<next_sequence>"
When alice creates a project with credit class id "A01"
Then the project exists with project id "<project_id>"

Expand Down
9 changes: 3 additions & 6 deletions x/ecocredit/server/core/msg_create_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (

type createBatchSuite struct {
*baseSuite
alice sdk.AccAddress
classId string
err error
alice sdk.AccAddress
err error
}

func TestCreateBatch(t *testing.T) {
Expand Down Expand Up @@ -54,15 +53,13 @@ func (s *createBatchSuite) AliceHasCreatedACreditClassWithCreditType(a string) {

s.bankKeeper.EXPECT().BurnCoins(gmAny, gmAny, gmAny).Return(nil).AnyTimes()

res, err := s.k.CreateClass(s.ctx, &core.MsgCreateClass{
_, err := s.k.CreateClass(s.ctx, &core.MsgCreateClass{
Admin: s.alice.String(),
Issuers: []string{s.alice.String()},
CreditTypeAbbrev: a,
Fee: &fee,
})
require.NoError(s.t, err)

s.classId = res.ClassId
}

func (s *createBatchSuite) AliceHasCreatedAProjectWithCreditClassId(a string) {
Expand Down
21 changes: 13 additions & 8 deletions x/ecocredit/server/core/msg_create_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (

type createProjectSuite struct {
*baseSuite
alice sdk.AccAddress
classId string
err error
alice sdk.AccAddress
err error
}

func TestCreateProject(t *testing.T) {
Expand Down Expand Up @@ -54,22 +53,28 @@ func (s *createProjectSuite) AliceHasCreatedACreditClassWithCreditType(a string)

s.bankKeeper.EXPECT().BurnCoins(gmAny, gmAny, gmAny).Return(nil).AnyTimes()

res, err := s.k.CreateClass(s.ctx, &core.MsgCreateClass{
_, err := s.k.CreateClass(s.ctx, &core.MsgCreateClass{
Admin: s.alice.String(),
Issuers: []string{s.alice.String()},
CreditTypeAbbrev: a,
Fee: &fee,
})
require.NoError(s.t, err)
}

s.classId = res.ClassId
func (s *createProjectSuite) AliceHasCreatedAProjectWithCreditClassId(a string) {
_, s.err = s.k.CreateProject(s.ctx, &core.MsgCreateProject{
Issuer: s.alice.String(),
ClassId: a,
Jurisdiction: "US",
})
}

func (s *createProjectSuite) TheProjectSequenceNumberIs(a string) {
nextSequence, err := strconv.ParseUint(a, 10, 32)
func (s *createProjectSuite) TheProjectSequenceForCreditClassIs(a, b string) {
class, err := s.k.stateStore.ClassTable().GetById(s.ctx, a)
require.NoError(s.t, err)

class, err := s.k.stateStore.ClassTable().GetById(s.ctx, s.classId)
nextSequence, err := strconv.ParseUint(b, 10, 32)
require.NoError(s.t, err)

err = s.k.stateStore.ProjectSequenceTable().Insert(s.ctx, &api.ProjectSequence{
Expand Down