Skip to content

Commit

Permalink
feat(marketplace): QueryBuyOrders ORM (#919)
Browse files Browse the repository at this point in the history
* feat: query buy orders

* chore: address review

Co-authored-by: technicallyty <[email protected]>
Co-authored-by: MD Aleem <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2022
1 parent 88a4b31 commit b202c37
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 16 deletions.
4 changes: 2 additions & 2 deletions x/ecocredit/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package ecocredit
import (
"fmt"

ormapi "github.com/cosmos/cosmos-sdk/api/cosmos/orm/v1alpha1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ormapi "github.com/cosmos/cosmos-sdk/api/cosmos/orm/v1alpha1"

"github.com/regen-network/regen-ledger/types/math"
basketapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
marketApi "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1"
api "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/math"
)

const (
Expand Down
78 changes: 76 additions & 2 deletions x/ecocredit/server/marketplace/query_buy_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,83 @@ package marketplace
import (
"context"

"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/marketplace/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
"github.com/regen-network/regen-ledger/x/ecocredit/marketplace"
)

func (k Keeper) BuyOrders(context.Context, *marketplace.QueryBuyOrdersRequest) (*marketplace.QueryBuyOrdersResponse, error) {
panic("not implemented")
// BuyOrders queries all buy orders with optional pagination
func (k Keeper) BuyOrders(ctx context.Context, request *marketplace.QueryBuyOrdersRequest) (*marketplace.QueryBuyOrdersResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
}

it, err := k.stateStore.BuyOrderTable().List(ctx, api.BuyOrderPrimaryKey{}, ormlist.Paginate(pg))
if err != nil {
return nil, err
}
defer it.Close()

orders := make([]*marketplace.BuyOrder, 0, 10)
for it.Next() {
v, err := it.Value()
if err != nil {
return nil, err
}
var order marketplace.BuyOrder
if err = ormutil.PulsarToGogoSlow(v, &order); err != nil {
return nil, err
}
orders = append(orders, &order)
}

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

return &marketplace.QueryBuyOrdersResponse{BuyOrders: orders, Pagination: pr}, nil
}

// BuyOrdersByAddress queries all buy orders created by the given address with optional pagination
func (k Keeper) BuyOrdersByAddress(ctx context.Context, request *marketplace.QueryBuyOrdersByAddressRequest) (*marketplace.QueryBuyOrdersByAddressResponse, error) {
pg, err := ormutil.GogoPageReqToPulsarPageReq(request.Pagination)
if err != nil {
return nil, err
}

buyer, err := sdk.AccAddressFromBech32(request.Address)
if err != nil {
return nil, err
}

it, err := k.stateStore.BuyOrderTable().List(ctx, api.BuyOrderBuyerIndexKey{}.WithBuyer(buyer), ormlist.Paginate(pg))
if err != nil {
return nil, err
}
defer it.Close()

orders := make([]*marketplace.BuyOrder, 0, 10)
for it.Next() {
v, err := it.Value()
if err != nil {
return nil, err
}
var order marketplace.BuyOrder
if err = ormutil.PulsarToGogoSlow(v, &order); err != nil {
return nil, err
}
orders = append(orders, &order)
}

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

return &marketplace.QueryBuyOrdersByAddressResponse{BuyOrders: orders, Pagination: pr}, nil
}
66 changes: 66 additions & 0 deletions x/ecocredit/server/marketplace/query_buy_orders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package marketplace

import (
"testing"

"gotest.tools/v3/assert"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

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

func TestQueryBuyOrders(t *testing.T) {
t.Parallel()
s := setupBase(t)
assert.NilError(t, s.marketStore.BuyOrderTable().Insert(s.ctx, &api.BuyOrder{Buyer: s.addr}))
assert.NilError(t, s.marketStore.BuyOrderTable().Insert(s.ctx, &api.BuyOrder{Buyer: s.addr}))

res, err := s.k.BuyOrders(s.ctx, &marketplace.QueryBuyOrdersRequest{})
assert.NilError(t, err)
assert.Equal(t, 2, len(res.BuyOrders))

res, err = s.k.BuyOrders(s.ctx, &marketplace.QueryBuyOrdersRequest{Pagination: &query.PageRequest{Limit: 1, CountTotal: true}})
assert.NilError(t, err)
assert.Equal(t, 1, len(res.BuyOrders))
assert.Equal(t, uint64(2), res.Pagination.Total)
}

func TestQueryBuyOrdersByAddress(t *testing.T) {
t.Parallel()
s := setupBase(t)
_, _, otherAddr := testdata.KeyTestPubAddr()
_, _, noOrdersAddr := testdata.KeyTestPubAddr()

assert.NilError(t, s.marketStore.BuyOrderTable().Insert(s.ctx, &api.BuyOrder{Buyer: s.addr}))
assert.NilError(t, s.marketStore.BuyOrderTable().Insert(s.ctx, &api.BuyOrder{Buyer: otherAddr}))

// valid queries
res, err := s.k.BuyOrdersByAddress(s.ctx, &marketplace.QueryBuyOrdersByAddressRequest{Address: s.addr.String()})
assert.NilError(t, err)
assert.Equal(t, 1, len(res.BuyOrders))
assert.Equal(t, s.addr.String(), sdk.AccAddress(res.BuyOrders[0].Buyer).String())

res, err = s.k.BuyOrdersByAddress(s.ctx, &marketplace.QueryBuyOrdersByAddressRequest{Address: otherAddr.String()})
assert.NilError(t, err)
assert.Equal(t, 1, len(res.BuyOrders))
assert.Equal(t, otherAddr.String(), sdk.AccAddress(res.BuyOrders[0].Buyer).String())

res, err = s.k.BuyOrdersByAddress(s.ctx, &marketplace.QueryBuyOrdersByAddressRequest{Address: otherAddr.String(), Pagination: &query.PageRequest{CountTotal: true}})
assert.NilError(t, err)
assert.Equal(t, 1, len(res.BuyOrders))
assert.Equal(t, otherAddr.String(), sdk.AccAddress(res.BuyOrders[0].Buyer).String())
assert.Equal(t, uint64(1), res.Pagination.Total)

// empty slice for addr with no orders
res, err = s.k.BuyOrdersByAddress(s.ctx, &marketplace.QueryBuyOrdersByAddressRequest{Address: noOrdersAddr.String()})
assert.NilError(t, err)
assert.Equal(t, 0, len(res.BuyOrders))

// error on bad address
res, err = s.k.BuyOrdersByAddress(s.ctx, &marketplace.QueryBuyOrdersByAddressRequest{Address: "foobarasdfxxlck"})
assert.ErrorContains(t, err, "decoding bech32 failed")
}
11 changes: 0 additions & 11 deletions x/ecocredit/server/marketplace/query_orders_by_address.go

This file was deleted.

3 changes: 2 additions & 1 deletion x/ecocredit/server/marketplace/query_sell_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (

"gotest.tools/v3/assert"

"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/protobuf/types/known/timestamppb"

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/marketplace/v1"
"github.com/regen-network/regen-ledger/types/ormutil"
Expand Down

0 comments on commit b202c37

Please sign in to comment.