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): add BasketBalance query #751

Merged
merged 6 commits into from
Feb 10, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ require (
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/regen-network/regen-ledger/api v0.1.0 // indirect
github.com/regen-network/regen-ledger/api v0.2.0 // indirect
github.com/regen-network/regen-ledger/orm v1.0.0-beta1 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,9 @@ github.com/regen-network/cosmos-sdk v0.44.2-regen-1 h1:Sm7rU4rMwLpEpgrQkF/4w/V/y
github.com/regen-network/cosmos-sdk v0.44.2-regen-1/go.mod h1:fwQJdw+aECatpTvQTo1tSfHEsxACdZYU80QCZUPnHr4=
github.com/regen-network/protobuf v1.3.2-alpha.regen.4 h1:c9jEnU+xm6vqyrQe3M94UFWqiXxRIKKnqBOh2EACmBE=
github.com/regen-network/protobuf v1.3.2-alpha.regen.4/go.mod h1:/J8/bR1T/NXyIdQDLUaq15LjNE83nRzkyrLAMcPewig=
github.com/regen-network/regen-ledger/api v0.1.0 h1:RXLRatYYW5D7uciqz3dnV+a/SjE4VZX5o0hB7Uf2hUs=
github.com/regen-network/regen-ledger/api v0.1.0/go.mod h1:kWly2pK0u3fDfyoYYilpEoTEg61pCjV/f5XSY0KzJQY=
github.com/regen-network/regen-ledger/api v0.2.0 h1:+h8r6BEwHFJO75pN700YB3GreUbwOZjuMOwPifjzHcI=
github.com/regen-network/regen-ledger/api v0.2.0/go.mod h1:kWly2pK0u3fDfyoYYilpEoTEg61pCjV/f5XSY0KzJQY=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down Expand Up @@ -1038,6 +1039,8 @@ github.com/tendermint/tm-db v0.6.4/go.mod h1:dptYhIpJ2M5kUuenLr+Yyf3zQOv1SgBZcl8
github.com/tendermint/tm-db v0.6.6 h1:EzhaOfR0bdKyATqcd5PNeyeq8r+V4bRPHBfyFdD9kGM=
github.com/tendermint/tm-db v0.6.6/go.mod h1:wP8d49A85B7/erz/r4YbKssKw6ylsO/hKtFk7E1aWZI=
github.com/tetafro/godot v1.4.9/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8=
github.com/thanhpk/randstr v1.0.4 h1:IN78qu/bR+My+gHCvMEXhR/i5oriVHcTB/BJJIRTsNo=
github.com/thanhpk/randstr v1.0.4/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U=
github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down
4 changes: 3 additions & 1 deletion x/ecocredit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/pkg/errors v0.9.1
github.com/regen-network/regen-ledger/api v0.1.0
github.com/regen-network/regen-ledger/api v0.2.0
github.com/regen-network/regen-ledger/orm v1.0.0-beta1
github.com/regen-network/regen-ledger/types v1.0.0
github.com/spf13/cobra v1.2.1
Expand Down Expand Up @@ -64,6 +64,7 @@ require (
github.com/gogo/gateway v1.1.0 // indirect
github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
Expand Down Expand Up @@ -124,6 +125,7 @@ require (
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gotest.tools/v3 v3.1.0 // indirect
nhooyr.io/websocket v1.8.6 // indirect
)

Expand Down
4 changes: 2 additions & 2 deletions x/ecocredit/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,8 @@ github.com/regen-network/cosmos-sdk v0.44.2-regen-1 h1:Sm7rU4rMwLpEpgrQkF/4w/V/y
github.com/regen-network/cosmos-sdk v0.44.2-regen-1/go.mod h1:fwQJdw+aECatpTvQTo1tSfHEsxACdZYU80QCZUPnHr4=
github.com/regen-network/protobuf v1.3.3-alpha.regen.1 h1:OHEc+q5iIAXpqiqFKeLpu5NwTIkVXUs48vFMwzqpqY4=
github.com/regen-network/protobuf v1.3.3-alpha.regen.1/go.mod h1:2DjTFR1HhMQhiWC5sZ4OhQ3+NtdbZ6oBDKQwq5Ou+FI=
github.com/regen-network/regen-ledger/api v0.1.0 h1:RXLRatYYW5D7uciqz3dnV+a/SjE4VZX5o0hB7Uf2hUs=
github.com/regen-network/regen-ledger/api v0.1.0/go.mod h1:kWly2pK0u3fDfyoYYilpEoTEg61pCjV/f5XSY0KzJQY=
github.com/regen-network/regen-ledger/api v0.2.0 h1:+h8r6BEwHFJO75pN700YB3GreUbwOZjuMOwPifjzHcI=
github.com/regen-network/regen-ledger/api v0.2.0/go.mod h1:kWly2pK0u3fDfyoYYilpEoTEg61pCjV/f5XSY0KzJQY=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down
5 changes: 3 additions & 2 deletions x/ecocredit/server/basket/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ import (

// Keeper is the basket keeper.
type Keeper struct {
basketStore basketv1.StateStore
stateStore basketv1.StateStore
bankKeeper BankKeeper
ecocreditKeeper EcocreditKeeper
}

var _ baskettypes.MsgServer = Keeper{}
var _ baskettypes.QueryServer = Keeper{}

// NewKeeper returns a new keeper instance.
func NewKeeper(db ormdb.ModuleDB, ecocreditKeeper EcocreditKeeper, bankKeeper BankKeeper) Keeper {
basketStore, err := basketv1.NewStateStore(db)
if err != nil {
panic(err)
}
return Keeper{bankKeeper: bankKeeper, ecocreditKeeper: ecocreditKeeper, basketStore: basketStore}
return Keeper{bankKeeper: bankKeeper, ecocreditKeeper: ecocreditKeeper, stateStore: basketStore}
}

// EcocreditKeeper abstracts over methods that the main eco-credit keeper
Expand Down
28 changes: 28 additions & 0 deletions x/ecocredit/server/basket/query_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package basket

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

func (k Keeper) BasketBalance(ctx context.Context, request *baskettypes.QueryBasketBalanceRequest) (*baskettypes.QueryBasketBalanceResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be checking if the request is nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its done in all our other queries (https://github.com/regen-network/regen-ledger/blob/master/x/ecocredit/server/query_server.go#L20). Unless this is being called from somewhere else and being checked there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll add. @AmauryM can you confirm whether this is needed or not in grpc?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All proto types are pointers . Request can't be nil - the deserialization is checked before a RPC method is called.

if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

basket, err := k.stateStore.BasketStore().GetByBasketDenom(ctx, request.BasketDenom)
if err != nil {
return nil, err
}

balance, err := k.stateStore.BasketBalanceStore().Get(ctx, basket.Id, request.BatchDenom)
if err != nil {
return nil, err
}

return &baskettypes.QueryBasketBalanceResponse{Balance: balance.Balance}, nil
}
65 changes: 65 additions & 0 deletions x/ecocredit/server/basket/query_balance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package basket_test

import (
"testing"

baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"

"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
"github.com/cosmos/cosmos-sdk/orm/testing/ormtest"

basketv1 "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"

"github.com/cosmos/cosmos-sdk/orm/model/ormdb"
"github.com/golang/mock/gomock"
"github.com/regen-network/regen-ledger/x/ecocredit/server"
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket/mocks"
"github.com/stretchr/testify/require"
)

func TestKeeper_BasketBalance(t *testing.T) {
// prepare database
db, err := ormdb.NewModuleDB(server.ModuleSchema, ormdb.ModuleDBOptions{})
stateStore, err := basketv1.NewStateStore(db)
require.NoError(t, err)
ctx := ormtable.WrapContextDefault(ormtest.NewMemoryBackend())

// setup test keeper
ctrl := gomock.NewController(t)
require.NoError(t, err)
bankKeeper := mocks.NewMockBankKeeper(ctrl)
ecocreditKeeper := mocks.NewMockEcocreditKeeper(ctrl)
k := basket.NewKeeper(db, ecocreditKeeper, bankKeeper)

// add a basket
basketDenom := "foo"
batchDenom := "bar"
balance := "5.3"
id, err := stateStore.BasketStore().InsertReturningID(ctx, &basketv1.Basket{
BasketDenom: basketDenom,
})
require.NoError(t, err)

// add a balance
require.NoError(t, stateStore.BasketBalanceStore().Insert(ctx, &basketv1.BasketBalance{
BasketId: id,
BatchDenom: batchDenom,
Balance: balance,
}))

// query
res, err := k.BasketBalance(ctx, &baskettypes.QueryBasketBalanceRequest{
BasketDenom: basketDenom,
BatchDenom: batchDenom,
})
require.NoError(t, err)
require.Equal(t, balance, res.Balance)

// bad query
res, err = k.BasketBalance(ctx, &baskettypes.QueryBasketBalanceRequest{
BasketDenom: batchDenom,
BatchDenom: basketDenom,
})
require.Error(t, err)
}
12 changes: 12 additions & 0 deletions x/ecocredit/server/basket/query_balances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package basket

import (
"context"

baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

func (k Keeper) BasketBalances(ctx context.Context, request *baskettypes.QueryBasketBalancesRequest) (*baskettypes.QueryBasketBalancesResponse, error) {
//TODO implement me
panic("implement me")
}
12 changes: 12 additions & 0 deletions x/ecocredit/server/basket/query_basket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package basket

import (
"context"

baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

func (k Keeper) Basket(ctx context.Context, request *baskettypes.QueryBasketRequest) (*baskettypes.QueryBasketResponse, error) {
//TODO implement me
panic("implement me")
}
12 changes: 12 additions & 0 deletions x/ecocredit/server/basket/query_baskets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package basket

import (
"context"

baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
)

func (k Keeper) Baskets(ctx context.Context, request *baskettypes.QueryBasketsRequest) (*baskettypes.QueryBasketsResponse, error) {
//TODO implement me
panic("implement me")
}