Skip to content

Commit

Permalink
Merge branch 'main' into aleem/1493-inf-dec
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanchristo authored Nov 7, 2022
2 parents add5a6b + e06a36b commit 5f53e32
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 51 deletions.
18 changes: 7 additions & 11 deletions x/ecocredit/basket/keeper/query_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,39 @@ package keeper
import (
"context"

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

"cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
regenerrors "github.com/regen-network/regen-ledger/errors"

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

func (k Keeper) BasketBalance(ctx context.Context, request *types.QueryBasketBalanceRequest) (*types.QueryBasketBalanceResponse, error) {
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, regenerrors.ErrInvalidArgument.Wrap("empty request")
}

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

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

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

return &types.QueryBasketBalanceResponse{Balance: balance.Balance}, nil
Expand Down
6 changes: 6 additions & 0 deletions x/ecocredit/basket/keeper/query_balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func TestKeeper_BasketBalance(t *testing.T) {
t.Parallel()
s := setupBase(t)

// nil query
_, err := s.k.BasketBalance(s.ctx, nil)
require.Error(t, err)
require.ErrorContains(t, err, "empty request: invalid argument")

// add a basket
basketDenom := testBasketDenom
batchDenom := "bar"
Expand Down Expand Up @@ -49,6 +54,7 @@ func TestKeeper_BasketBalance(t *testing.T) {
BatchDenom: basketDenom,
})
require.Error(t, err)
require.ErrorContains(t, err, "basket bar not found")

// add another basket
basketDenom = "foo1"
Expand Down
22 changes: 11 additions & 11 deletions x/ecocredit/basket/keeper/query_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,31 @@ package keeper
import (
"context"

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

"cosmossdk.io/errors"

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

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/basket/types/v1"
)

func (k Keeper) BasketBalances(ctx context.Context, request *types.QueryBasketBalancesRequest) (*types.QueryBasketBalancesResponse, error) {
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, regenerrors.ErrInvalidArgument.Wrap("empty request")
}

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

pulsarPageReq, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

it, err := k.stateStore.BasketBalanceTable().List(ctx, api.BasketBalancePrimaryKey{}.WithBasketId(basket.Id),
Expand Down Expand Up @@ -59,8 +55,12 @@ func (k Keeper) BasketBalances(ctx context.Context, request *types.QueryBasketBa
Balance: bal.Balance,
})
}
it.Close()
defer it.Close()

res.Pagination, err = ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
return res, err
if err != nil {
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return res, nil
}
5 changes: 5 additions & 0 deletions x/ecocredit/basket/keeper/query_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func TestQueryBalances(t *testing.T) {
t.Parallel()
s := setupBase(t)

// nil request query
_, err := s.k.BasketBalances(s.ctx, nil)
require.Error(t, err)
require.ErrorContains(t, err, "invalid argument")

// add some baskets
batchDenoms := []string{"bar", "baz", "qux"}
require.NoError(t, s.stateStore.BasketTable().Insert(s.ctx, &api.Basket{
Expand Down
14 changes: 8 additions & 6 deletions x/ecocredit/basket/keeper/query_baskets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ package keeper
import (
"context"

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

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

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/basket/types/v1"
)

func (k Keeper) Baskets(ctx context.Context, request *types.QueryBasketsRequest) (*types.QueryBasketsResponse, error) {
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, regenerrors.ErrInvalidArgument.Wrap("empty request")
}

pulsarPageReq, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}
it, err := k.stateStore.BasketTable().List(ctx, api.BasketPrimaryKey{},
ormlist.Paginate(pulsarPageReq),
Expand Down Expand Up @@ -67,5 +65,9 @@ func (k Keeper) Baskets(ctx context.Context, request *types.QueryBasketsRequest)
it.Close()

res.Pagination, err = ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
return res, err
if err != nil {
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return res, nil
}
6 changes: 3 additions & 3 deletions x/ecocredit/basket/keeper/query_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"

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

regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
types "github.com/regen-network/regen-ledger/x/ecocredit/basket/types/v1"
)

func (k Keeper) BasketFee(ctx context.Context, req *types.QueryBasketFeeRequest) (*types.QueryBasketFeeResponse, error) {
basketFee, err := k.stateStore.BasketFeeTable().Get(ctx)
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

var fee sdk.Coin
Expand All @@ -22,7 +22,7 @@ func (k Keeper) BasketFee(ctx context.Context, req *types.QueryBasketFeeRequest)
var ok bool
fee, ok = regentypes.ProtoCoinToCoin(basketFee.Fee)
if !ok {
return nil, sdkerrors.ErrInvalidType.Wrapf("failed to parse basket fee")
return nil, regenerrors.ErrInternal.Wrap("failed to parse basket fee")
}
}

Expand Down
9 changes: 7 additions & 2 deletions x/ecocredit/marketplace/keeper/query_allowed_denoms.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (
"github.com/cosmos/cosmos-sdk/orm/model/ormlist"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/types/v1"
)

func (k Keeper) AllowedDenoms(ctx context.Context, req *types.QueryAllowedDenomsRequest) (*types.QueryAllowedDenomsResponse, error) {
if req == nil {
return nil, regenerrors.ErrInvalidArgument.Wrap("empty request")
}

pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

it, err := k.stateStore.AllowedDenomTable().List(ctx, &api.AllowedDenomPrimaryKey{}, ormlist.Paginate(pg))
Expand All @@ -36,7 +41,7 @@ func (k Keeper) AllowedDenoms(ctx context.Context, req *types.QueryAllowedDenoms
}
pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}
return &types.QueryAllowedDenomsResponse{AllowedDenoms: allowedDenoms, Pagination: pr}, nil
}
12 changes: 11 additions & 1 deletion x/ecocredit/marketplace/keeper/query_allowed_denoms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"gotest.tools/v3/assert"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/stretchr/testify/require"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
Expand All @@ -16,12 +17,21 @@ func TestQueryAllowedDenoms(t *testing.T) {
t.Parallel()
s := setupBase(t, 0)

// nil request
_, err := s.k.AllowedDenoms(s.ctx, nil)
require.Error(t, err)
require.ErrorContains(t, err, "invalid argument")

// no allowed denoms present
_, err = s.k.AllowedDenoms(s.ctx, &types.QueryAllowedDenomsRequest{})
assert.NilError(t, err)

allowedDenom := api.AllowedDenom{
BankDenom: "uregen",
DisplayDenom: "regen",
Exponent: 18,
}
err := s.marketStore.AllowedDenomTable().Insert(s.ctx, &allowedDenom)
err = s.marketStore.AllowedDenomTable().Insert(s.ctx, &allowedDenom)
assert.NilError(t, err)

allowedDenomOsmo := api.AllowedDenom{
Expand Down
12 changes: 8 additions & 4 deletions x/ecocredit/marketplace/keeper/query_sell_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@ import (
"context"

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

regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
types "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/types/v1"
)

func (k Keeper) SellOrder(ctx context.Context, req *types.QuerySellOrderRequest) (*types.QuerySellOrderResponse, error) {
if req == nil {
return nil, regenerrors.ErrInvalidArgument.Wrap("empty request")
}

order, err := k.stateStore.SellOrderTable().Get(ctx, req.SellOrderId)
if err != nil {
return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get sell order with id %d: %s", req.SellOrderId, err.Error())
return nil, regenerrors.ErrNotFound.Wrapf("could not get sell order with id %d: %s", req.SellOrderId, err.Error())
}

seller := sdk.AccAddress(order.Seller)

batch, err := k.baseStore.BatchTable().Get(ctx, order.BatchKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("could not get batch with key: %d: %s", order.BatchKey, err.Error())
}

market, err := k.stateStore.MarketTable().Get(ctx, order.MarketId)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("could not get market with id: %d: %s", order.MarketId, err.Error())
}

info := types.SellOrderInfo{
Expand Down
5 changes: 5 additions & 0 deletions x/ecocredit/marketplace/keeper/query_sell_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func TestQuery_SellOrder(t *testing.T) {
expiration, err := regentypes.ParseDate("expiration", "2030-01-01")
require.NoError(s.t, err)

// nil request
_, err = s.k.SellOrder(s.ctx, nil)
require.Error(t, err)
require.ErrorContains(t, err, "invalid argument")

// make a sell order (with expiration)
order1 := api.SellOrder{
Seller: s.addrs[0],
Expand Down
13 changes: 9 additions & 4 deletions x/ecocredit/marketplace/keeper/query_sell_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1"
regenerrors "github.com/regen-network/regen-ledger/errors"
regentypes "github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/ormutil"
types "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/types/v1"
)

// SellOrders queries all sell orders in state with optional pagination
func (k Keeper) SellOrders(ctx context.Context, req *types.QuerySellOrdersRequest) (*types.QuerySellOrdersResponse, error) {
if req == nil {
return nil, regenerrors.ErrInvalidArgument.Wrap("empty request")
}

pg, err := ormutil.GogoPageReqToPulsarPageReq(req.Pagination)
if err != nil {
return nil, err
return nil, regenerrors.ErrInvalidArgument.Wrap(err.Error())
}

it, err := k.stateStore.SellOrderTable().List(ctx, api.SellOrderSellerIndexKey{}, ormlist.Paginate(pg))
Expand All @@ -36,12 +41,12 @@ func (k Keeper) SellOrders(ctx context.Context, req *types.QuerySellOrdersReques

batch, err := k.baseStore.BatchTable().Get(ctx, order.BatchKey)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("unable to get batch with key: %d: %s", order.BatchKey, err.Error())
}

market, err := k.stateStore.MarketTable().Get(ctx, order.MarketId)
if err != nil {
return nil, err
return nil, regenerrors.ErrNotFound.Wrapf("unable to get market with id: %d: %s", order.MarketId, err.Error())
}

info := types.SellOrderInfo{
Expand All @@ -60,7 +65,7 @@ func (k Keeper) SellOrders(ctx context.Context, req *types.QuerySellOrdersReques

pr, err := ormutil.PulsarPageResToGogoPageRes(it.PageResponse())
if err != nil {
return nil, err
return nil, regenerrors.ErrInternal.Wrap(err.Error())
}

return &types.QuerySellOrdersResponse{SellOrders: orders, Pagination: pr}, nil
Expand Down
Loading

0 comments on commit 5f53e32

Please sign in to comment.