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

fix(ecocredit/core): use legacy params in credit type query #966

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 15 additions & 18 deletions x/ecocredit/server/core/query_credit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ package core
import (
"context"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
"github.com/regen-network/regen-ledger/x/ecocredit"
"github.com/regen-network/regen-ledger/x/ecocredit/core"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// CreditTypes queries the list of allowed types that credit classes can have.
func (k Keeper) CreditTypes(ctx context.Context, _ *core.QueryCreditTypesRequest) (*core.QueryCreditTypesResponse, error) {
creditTypes := make([]*core.CreditType, 0)
it, err := k.stateStore.CreditTypeTable().List(ctx, api.CreditTypePrimaryKey{})
if err != nil {
return nil, err
}
defer it.Close()
for it.Next() {
ct, err := it.Value()
if err != nil {
return nil, err
}
var cType core.CreditType
if err = ormutil.PulsarToGogoSlow(ct, &cType); err != nil {
return nil, err
sdkCtx := sdk.UnwrapSDKContext(ctx)
var params ecocredit.Params
k.paramsKeeper.GetParamSet(sdkCtx, &params)

cTypes := make([]*core.CreditType, len(params.CreditTypes))
for i, ct := range params.CreditTypes {
cTypes[i] = &core.CreditType{
Abbreviation: ct.Abbreviation,
Name: ct.Name,
Unit: ct.Unit,
Precision: ct.Precision,
}
creditTypes = append(creditTypes, &cType)
}
return &core.QueryCreditTypesResponse{CreditTypes: creditTypes}, nil
return &core.QueryCreditTypesResponse{CreditTypes: cTypes}, nil
}
25 changes: 8 additions & 17 deletions x/ecocredit/server/core/query_credit_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,25 @@ package core
import (
"testing"

"github.com/golang/mock/gomock"
"github.com/regen-network/regen-ledger/x/ecocredit"
"gotest.tools/v3/assert"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/x/ecocredit/core"
)

func TestQuery_CreditTypes(t *testing.T) {
t.Parallel()
s := setupBase(t)

// insert a few credit types
assert.NilError(t, s.stateStore.CreditTypeTable().Insert(s.ctx, &api.CreditType{
Abbreviation: "C",
Name: "carbon",
Unit: "a ton",
Precision: 6,
}))
assert.NilError(t, s.stateStore.CreditTypeTable().Insert(s.ctx, &api.CreditType{
Abbreviation: "F",
Name: "foobar",
Unit: "foo per inch",
Precision: 18,
}))
gmAny := gomock.Any()
s.paramsKeeper.EXPECT().GetParamSet(gmAny, gmAny).Do(func(any interface{}, p *ecocredit.Params) {
p.CreditTypes = []*ecocredit.CreditType{{Name: "foobar", Abbreviation: "C", Unit: "tonne", Precision: 6}}
}).Times(1)

// base query should return all types
res, err := s.k.CreditTypes(s.ctx, &core.QueryCreditTypesRequest{})
assert.NilError(t, err)
assert.Equal(t, 2, len(res.CreditTypes))
assert.Equal(t, 1, len(res.CreditTypes))
assert.Equal(t, uint32(6), res.CreditTypes[0].Precision)
assert.Equal(t, "foobar", res.CreditTypes[1].Name)
assert.Equal(t, "foobar", res.CreditTypes[0].Name)
}