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

feat(x/ecocredit): query BatchInfo ORM #843

Merged
merged 5 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions x/ecocredit/server/core/query_batch_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core

import (
"context"
"github.com/regen-network/regen-ledger/x/ecocredit"
v1 "github.com/regen-network/regen-ledger/x/ecocredit/v1"
)

// BatchInfo queries for information on a credit batch.
func (k Keeper) BatchInfo(ctx context.Context, request *v1.QueryBatchInfoRequest) (*v1.QueryBatchInfoResponse, error) {
if err := ecocredit.ValidateDenom(request.BatchDenom); err != nil {
return nil, err
}

batch, err := k.stateStore.BatchInfoStore().GetByBatchDenom(ctx, request.BatchDenom)
if err != nil {
return nil, err
}

var bi v1.BatchInfo
if err = PulsarToGogoSlow(batch, &bi); err != nil {
return nil, err
}
return &v1.QueryBatchInfoResponse{Info: &bi}, nil
}
31 changes: 31 additions & 0 deletions x/ecocredit/server/core/query_batch_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core

import (
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
ecocreditv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
v1 "github.com/regen-network/regen-ledger/x/ecocredit/v1"
"gotest.tools/v3/assert"
"testing"
)

func TestQuery_BatchInfo(t *testing.T) {
t.Parallel()
s := setupBase(t)
batchDenom := "C01-20200101-20220101-001"
assert.NilError(t, s.stateStore.BatchInfoStore().Insert(s.ctx, &ecocreditv1.BatchInfo{
ProjectId: 1,
BatchDenom: batchDenom,
Metadata: nil,
StartDate: nil,
EndDate: nil,
}))

// invalid query
_, err := s.k.BatchInfo(s.ctx, &v1.QueryBatchInfoRequest{BatchDenom: "A00-00000000-00000000-000"})
assert.ErrorContains(t, err, ormerrors.NotFound.Error())

// good query
res, err := s.k.BatchInfo(s.ctx, &v1.QueryBatchInfoRequest{BatchDenom: batchDenom})
assert.NilError(t, err)
assert.Equal(t, uint64(1), res.Info.ProjectId)
}