Skip to content

Commit

Permalink
Merge branch 'master' into ryan/1158-default-orm-params
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanchristo authored Jul 23, 2022
2 parents 61ca737 + d97c576 commit 9fdbad4
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1278](https://github.com/regen-network/regen-ledger/pull/1278) Fix `open` not set in `Msg/CreateBatch`
- [#1281](https://github.com/regen-network/regen-ledger/pull/1281) Fix curator address in state migrations
- [#1284](https://github.com/regen-network/regen-ledger/pull/1284) Fix basket state migration
- [#1290](https://github.com/regen-network/regen-ledger/pull/1290) Fix error message when basket not found.
- [#1290](https://github.com/regen-network/regen-ledger/pull/1290) Return empty balance when basket balance not found.

## [v4.0.0-rc1](https://github.com/regen-network/regen-ledger/releases/tag/v4.0.0-rc1) - 2022-07-15

Expand Down
21 changes: 19 additions & 2 deletions x/ecocredit/server/basket/query_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

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

Expand All @@ -16,12 +19,26 @@ func (k Keeper) BasketBalance(ctx context.Context, request *baskettypes.QueryBas

basket, err := k.stateStore.BasketTable().GetByBasketDenom(ctx, request.BasketDenom)
if err != nil {
return nil, err
if ormerrors.IsNotFound(err) {
return nil, sdkerrors.Wrapf(err, "basket %s not found", request.BasketDenom)
}
return nil, sdkerrors.Wrapf(err, "failed to get basket %s", request.BasketDenom)
}

found, err := k.coreStore.BatchTable().HasByDenom(ctx, request.BatchDenom)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to get credit batch %s", request.BatchDenom)
}
if !found {
return nil, ormerrors.NotFound.Wrapf("credit batch %s not found", request.BatchDenom)
}

balance, err := k.stateStore.BasketBalanceTable().Get(ctx, basket.Id, request.BatchDenom)
if err != nil {
return nil, err
if ormerrors.IsNotFound(err) {
return &baskettypes.QueryBasketBalanceResponse{Balance: "0"}, nil
}
return nil, sdkerrors.Wrapf(err, "failed to get basket balance for %s", request.BasketDenom)
}

return &baskettypes.QueryBasketBalanceResponse{Balance: balance.Balance}, nil
Expand Down
25 changes: 24 additions & 1 deletion x/ecocredit/server/basket/query_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"

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

Expand All @@ -22,6 +23,11 @@ func TestKeeper_BasketBalance(t *testing.T) {
})
require.NoError(t, err)

err = s.coreStore.BatchTable().Insert(s.ctx, &coreapi.Batch{
Denom: batchDenom,
})
require.NoError(t, err)

// add a balance
require.NoError(t, s.stateStore.BasketBalanceTable().Insert(s.ctx, &api.BasketBalance{
BasketId: id,
Expand All @@ -38,9 +44,26 @@ func TestKeeper_BasketBalance(t *testing.T) {
require.Equal(t, balance, res.Balance)

// bad query
res, err = s.k.BasketBalance(s.ctx, &baskettypes.QueryBasketBalanceRequest{
_, err = s.k.BasketBalance(s.ctx, &baskettypes.QueryBasketBalanceRequest{
BasketDenom: batchDenom,
BatchDenom: basketDenom,
})
require.Error(t, err)

// add another basket
basketDenom = "foo1"
basketName := "foo1.bar"
err = s.stateStore.BasketTable().Insert(s.ctx, &api.Basket{
BasketDenom: basketDenom,
Name: basketName,
})
require.NoError(t, err)

// expect empty basket balance
res, err = s.k.BasketBalance(s.ctx, &baskettypes.QueryBasketBalanceRequest{
BasketDenom: basketDenom,
BatchDenom: batchDenom,
})
require.NoError(t, err)
require.Equal(t, res.Balance, "0")
}
7 changes: 6 additions & 1 deletion x/ecocredit/server/basket/query_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/orm/model/ormlist"
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
Expand All @@ -20,7 +22,10 @@ func (k Keeper) BasketBalances(ctx context.Context, request *baskettypes.QueryBa

basket, err := k.stateStore.BasketTable().GetByBasketDenom(ctx, request.BasketDenom)
if err != nil {
return nil, err
if ormerrors.IsNotFound(err) {
return nil, sdkerrors.Wrapf(err, "basket %s not found", request.BasketDenom)
}
return nil, sdkerrors.Wrapf(err, "failed to get basket %s", request.BasketDenom)
}

pulsarPageReq, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
Expand Down
2 changes: 1 addition & 1 deletion x/ecocredit/server/basket/query_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestQueryBalances(t *testing.T) {
require.Equal(t, "4.20", res.Balances[1].Balance)

// bad query
res, err = s.k.BasketBalances(s.ctx, &baskettypes.QueryBasketBalancesRequest{BasketDenom: "nope"})
_, err = s.k.BasketBalances(s.ctx, &baskettypes.QueryBasketBalancesRequest{BasketDenom: "nope"})
require.Error(t, err)
require.Contains(t, err.Error(), "not found")
}
8 changes: 7 additions & 1 deletion x/ecocredit/server/basket/query_basket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
baskettypes "github.com/regen-network/regen-ledger/x/ecocredit/basket"
Expand All @@ -19,7 +22,10 @@ func (k Keeper) Basket(ctx context.Context, request *baskettypes.QueryBasketRequ

basket, err := k.stateStore.BasketTable().GetByBasketDenom(ctx, request.BasketDenom)
if err != nil {
return nil, err
if ormerrors.IsNotFound(err) {
return nil, sdkerrors.Wrapf(err, "basket %s not found", request.BasketDenom)
}
return nil, sdkerrors.Wrapf(err, "failed to get basket %s", request.BasketDenom)
}

basketGogo := &baskettypes.Basket{}
Expand Down
9 changes: 8 additions & 1 deletion x/ecocredit/server/basket/query_basket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestKeeper_Basket(t *testing.T) {
require.Equal(t, basketDenom, res.Basket.BasketDenom)

// bad query
res, err = s.k.Basket(s.ctx, &baskettypes.QueryBasketRequest{
_, err = s.k.Basket(s.ctx, &baskettypes.QueryBasketRequest{
BasketDenom: batchDenom,
})
require.Error(t, err)
Expand Down Expand Up @@ -61,4 +61,11 @@ func TestKeeper_BasketClasses(t *testing.T) {
require.NoError(t, err)
require.Equal(t, basketDenom, res.Basket.BasketDenom)
require.Equal(t, []string{classId}, res.Classes)

// query unknown basket
_, err = s.k.Basket(s.ctx, &baskettypes.QueryBasketRequest{
BasketDenom: "unknown",
})
require.Error(t, err)
require.ErrorContains(t, err, "basket unknown not found")
}
6 changes: 3 additions & 3 deletions x/ecocredit/server/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ func (s *IntegrationTestSuite) TestBasketScenario() {
BasketDenom: basketDenom,
BatchDenom: batchDenom,
})
require.Error(err)
require.Contains(err.Error(), "not found")
require.Nil(bRes)
require.NoError(err)
require.NotNil(bRes)
require.Equal(bRes.Balance, "0")

// basket token balance of user2 should be empty now
endBal := s.getUserBalance(user2, basketDenom)
Expand Down

0 comments on commit 9fdbad4

Please sign in to comment.