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{}
3 changes: 1 addition & 2 deletions x/ecocredit/server/marketplace/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type baseSuite struct {
t *testing.T
db ormdb.ModuleDB
coreStore ecocreditv1.StateStore
marketStore marketApi.StateStore
marketStore marketApi.StateStore
ctx context.Context
k Keeper
ctrl *gomock.Controller
Expand Down Expand Up @@ -68,4 +68,3 @@ func setupBase(t *testing.T) *baseSuite {
_, _, s.addr = testdata.KeyTestPubAddr()
return s
}

2 changes: 1 addition & 1 deletion x/ecocredit/server/marketplace/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestSell_Prune(t *testing.T) {
assert.NilError(t, err)

res, err := s.k.Sell(s.ctx, &v1.MsgSell{
Owner: s.addr.String(),
Owner: s.addr.String(),
Orders: []*v1.MsgSell_Order{
{BatchDenom: batchDenom, Quantity: "10", AskPrice: &ask, Expiration: &expired},
{BatchDenom: batchDenom, Quantity: "10", AskPrice: &ask, Expiration: &notExpired},
Expand Down
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"

"github.com/regen-network/regen-ledger/types/ormutil"
"github.com/regen-network/regen-ledger/x/ecocredit/marketplace"
)

func (k Keeper) SellOrder(ctx context.Context, req *marketplace.QuerySellOrderRequest) (*marketplace.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 marketplace.SellOrder
if err = ormutil.PulsarToGogoSlow(order, &so); err != nil {
return nil, err
}
return &marketplace.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/types/ormutil"
"github.com/regen-network/regen-ledger/x/ecocredit"
"github.com/regen-network/regen-ledger/x/ecocredit/marketplace"
)

func TestQuery_SellOrder(t *testing.T) {
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, ormutil.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())
}
1 change: 1 addition & 0 deletions x/ecocredit/server/marketplace/sell.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package marketplace

import (
"context"

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

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
Expand Down
5 changes: 2 additions & 3 deletions x/ecocredit/server/marketplace/sell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func assertCoinsEscrowed(t *testing.T, balanceBefore, balanceAfter *ecocreditv1.
supplyBefore.EscrowedAmount, supplyAfter.EscrowedAmount)
assert.NilError(t, err)
balBeforeTradable, balAfterTradable, balBeforeEscrowed, balAfterEscrowed, supBeforeTradable, supAfterTradable,
supBeforeEscrowed, supAfterEscrowed := decs[0], decs[1], decs[2], decs[3], decs[4], decs[5], decs[6], decs[7]
supBeforeEscrowed, supAfterEscrowed := decs[0], decs[1], decs[2], decs[3], decs[4], decs[5], decs[6], decs[7]

// check the resulting balance -> tradableBefore - orderAmt = tradableAfter
calculatedTradable, err := balBeforeTradable.Sub(orderAmt)
Expand All @@ -193,7 +193,6 @@ func assertCoinsEscrowed(t *testing.T, balanceBefore, balanceAfter *ecocreditv1.
assert.Check(t, calculatedESupply.Equal(supAfterEscrowed))
}


func testSellSetup(t *testing.T, s *baseSuite, batchDenom, bankDenom, displayDenom, classId string, start, end *timestamppb.Timestamp, creditType ecocredit.CreditType) {
assert.NilError(t, s.coreStore.BatchInfoTable().Insert(s.ctx, &ecocreditv1.BatchInfo{
ProjectId: 1,
Expand Down Expand Up @@ -226,7 +225,7 @@ func testSellSetup(t *testing.T, s *baseSuite, batchDenom, bankDenom, displayDen
Retired: "100",
}))
assert.NilError(t, s.k.coreStore.BatchSupplyTable().Insert(s.ctx, &ecocreditv1.BatchSupply{
BatchId: 1,
BatchId: 1,
TradableAmount: "100",
RetiredAmount: "100",
}))
Expand Down