Skip to content

Commit

Permalink
feat(x/ecocredit): query ClassIssuers ORM (#839)
Browse files Browse the repository at this point in the history
* feat: query class issuers

* Update x/ecocredit/server/core/query_class_issuers_test.go

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

* chore: v1beta1 -> v1

Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com>
Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 9, 2022
1 parent 6e19a4f commit 8c1f902
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
44 changes: 44 additions & 0 deletions x/ecocredit/server/core/query_class_issuers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package core

import (
"context"
"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
sdk "github.com/cosmos/cosmos-sdk/types"
ecocreditv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
v1 "github.com/regen-network/regen-ledger/x/ecocredit/v1"
)

// ClassIssuers returns a list of addresses that are allowed to issue batches from the given class.
func (k Keeper) ClassIssuers(ctx context.Context, request *v1.QueryClassIssuersRequest) (*v1.QueryClassIssuersResponse, error) {
pg, err := GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
}

classInfo, err := k.stateStore.ClassInfoStore().GetByName(ctx, request.ClassId)
if err != nil {
return nil, err
}

it, err := k.stateStore.ClassIssuerStore().List(ctx, ecocreditv1.ClassIssuerClassIdIssuerIndexKey{}.WithClassId(classInfo.Id), ormlist.Paginate(pg))
if err != nil {
return nil, err
}

issuers := make([]string, 0)
for it.Next() {
val, err := it.Value()
if err != nil {
return nil, err
}
issuers = append(issuers, sdk.AccAddress(val.Issuer).String())
}
pr, err := PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
}
return &v1.QueryClassIssuersResponse{
Issuers: issuers,
Pagination: pr,
}, nil
}
56 changes: 56 additions & 0 deletions x/ecocredit/server/core/query_class_issuers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package core

import (
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
"github.com/cosmos/cosmos-sdk/types/query"
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_ClassIssuers(t *testing.T) {
t.Parallel()
s := setupBase(t)

// make a class with 3 issuers.
addrs := genAddrs(2)
issuers := append(addrs, s.addr)
err := s.stateStore.ClassInfoStore().Insert(s.ctx, &ecocreditv1.ClassInfo{
Name: "C01",
Admin: s.addr,
Metadata: nil,
CreditType: "C",
})
assert.NilError(t, err)
assert.NilError(t, s.stateStore.ClassIssuerStore().Insert(s.ctx, &ecocreditv1.ClassIssuer{
ClassId: 1,
Issuer: s.addr,
}))
assert.NilError(t, s.stateStore.ClassIssuerStore().Insert(s.ctx, &ecocreditv1.ClassIssuer{
ClassId: 1,
Issuer: addrs[0],
}))
assert.NilError(t, s.stateStore.ClassIssuerStore().Insert(s.ctx, &ecocreditv1.ClassIssuer{
ClassId: 1,
Issuer: addrs[1],
}))

// base request
res, err := s.k.ClassIssuers(s.ctx, &v1.QueryClassIssuersRequest{ClassId: "C01"})
assert.NilError(t, err)
assert.Equal(t, len(issuers), len(res.Issuers))

// bad request
_, err = s.k.ClassIssuers(s.ctx, &v1.QueryClassIssuersRequest{ClassId: "F01"})
assert.ErrorContains(t, err, ormerrors.NotFound.Error())

// paginated request
res, err = s.k.ClassIssuers(s.ctx, &v1.QueryClassIssuersRequest{
ClassId: "C01",
Pagination: &query.PageRequest{Limit: 1, CountTotal: true},
})
assert.NilError(t, err)
assert.Equal(t, 1, len(res.Issuers))
assert.Equal(t, uint64(3), res.Pagination.Total)
}

0 comments on commit 8c1f902

Please sign in to comment.