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(marketplace): QuerySellOrder ORM #906

Merged
merged 8 commits into from
Mar 18, 2022
1 change: 1 addition & 0 deletions x/ecocredit/server/marketplace/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ func NewKeeper(ss marketplacev1.StateStore, cs ecocreditv1.StateStore, bk ecocre

// TODO: uncomment when impl
// var _ v1.MsgServer = Keeper{}
// var _ v1.QueryServer = Keeper{}
22 changes: 22 additions & 0 deletions x/ecocredit/server/marketplace/query_sell_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package marketplace

import (
"context"

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

v1 "github.com/regen-network/regen-ledger/x/ecocredit/marketplace"
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket"
)

func (k Keeper) SellOrder(ctx context.Context, req *v1.QuerySellOrderRequest) (*v1.QuerySellOrderResponse, error) {
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())
}
var so v1.SellOrder
if err = basket.PulsarToGogoSlow(order, &so); err != nil {
return nil, err
}
return &v1.QuerySellOrderResponse{SellOrder: &so}, nil
}
56 changes: 56 additions & 0 deletions x/ecocredit/server/marketplace/query_sell_order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package marketplace

import (
"testing"

"gotest.tools/v3/assert"

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

func TestQuery_SelOrder(t *testing.T) {
technicallyty marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
s := setupBase(t)
batchDenom := "C01-20200101-20200201-001"
start, end := timestamppb.Now(), timestamppb.Now()
ask := sdk.NewInt64Coin("ufoo", 10)
creditType := ecocredit.CreditType{
Name: "carbon",
Abbreviation: "C",
Unit: "tonnes",
Precision: 6,
}
testSellSetup(t, s, batchDenom, ask.Denom, ask.Denom[1:], "C01", start, end, creditType)

// make a sell order
order := api.SellOrder{
Seller: s.addr,
BatchId: 1,
Quantity: "15.32",
MarketId: 1,
AskPrice: "100",
DisableAutoRetire: false,
Expiration: nil,
Maker: false,
}
id, err := s.marketStore.SellOrderTable().InsertReturningID(s.ctx, &order)
assert.NilError(t, err)

var gogoOrder marketplace.SellOrder
assert.NilError(t, core.PulsarToGogoSlow(&order, &gogoOrder))

res, err := s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: id})
assert.NilError(t, err)
assert.DeepEqual(t, *res.SellOrder, gogoOrder)

// invalid order id should fail
_, err = s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: 404})
assert.ErrorContains(t, err, ormerrors.NotFound.Error())
}