-
Notifications
You must be signed in to change notification settings - Fork 103
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5573917
feat(x/ecocredit): add BasketBalance query
aaronc cdf2b72
go mod tidy
aaronc b984101
add nil check
aaronc 5358246
Merge branch 'release/v2.2.x' into aaronc/732-query-basket-balance
aaronc b9d2881
go mod tidy
ryanchristo a40e85d
Merge branch 'release/v2.2.x' into aaronc/732-query-basket-balance
ryanchristo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that needed?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.