From cf566b921d18b77fc5fc26d7c3329a87a1ea5f11 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Thu, 17 Mar 2022 13:32:21 -0700 Subject: [PATCH 1/4] feat: query sell order --- x/ecocredit/server/marketplace/keeper.go | 1 + .../server/marketplace/query_sell_order.go | 22 ++++++++ .../marketplace/query_sell_order_test.go | 56 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 x/ecocredit/server/marketplace/query_sell_order.go create mode 100644 x/ecocredit/server/marketplace/query_sell_order_test.go diff --git a/x/ecocredit/server/marketplace/keeper.go b/x/ecocredit/server/marketplace/keeper.go index 813a36cc3f..8607cd3429 100644 --- a/x/ecocredit/server/marketplace/keeper.go +++ b/x/ecocredit/server/marketplace/keeper.go @@ -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{} diff --git a/x/ecocredit/server/marketplace/query_sell_order.go b/x/ecocredit/server/marketplace/query_sell_order.go new file mode 100644 index 0000000000..14f23ab436 --- /dev/null +++ b/x/ecocredit/server/marketplace/query_sell_order.go @@ -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" + "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 +} diff --git a/x/ecocredit/server/marketplace/query_sell_order_test.go b/x/ecocredit/server/marketplace/query_sell_order_test.go new file mode 100644 index 0000000000..77f950dac2 --- /dev/null +++ b/x/ecocredit/server/marketplace/query_sell_order_test.go @@ -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) { + 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()) +} From 1f2133a8f47deeb5a18e3367dbbb191dc466c2d7 Mon Sep 17 00:00:00 2001 From: Tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 17 Mar 2022 16:35:12 -0700 Subject: [PATCH 2/4] Update x/ecocredit/server/marketplace/query_sell_order_test.go Co-authored-by: Ryan Christoffersen <12519942+ryanchristo@users.noreply.github.com> --- x/ecocredit/server/marketplace/query_sell_order_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/ecocredit/server/marketplace/query_sell_order_test.go b/x/ecocredit/server/marketplace/query_sell_order_test.go index 77f950dac2..1d4143d529 100644 --- a/x/ecocredit/server/marketplace/query_sell_order_test.go +++ b/x/ecocredit/server/marketplace/query_sell_order_test.go @@ -15,7 +15,7 @@ import ( "github.com/regen-network/regen-ledger/x/ecocredit/server/core" ) -func TestQuery_SelOrder(t *testing.T) { +func TestQuery_SellOrder(t *testing.T) { t.Parallel() s := setupBase(t) batchDenom := "C01-20200101-20200201-001" From e4c3bed9beda21364378e98dc6c0e847d9ddc717 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Thu, 17 Mar 2022 16:35:51 -0700 Subject: [PATCH 3/4] chore: v1 -> marketplace --- x/ecocredit/server/marketplace/query_sell_order.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/ecocredit/server/marketplace/query_sell_order.go b/x/ecocredit/server/marketplace/query_sell_order.go index 14f23ab436..dbae7c46cb 100644 --- a/x/ecocredit/server/marketplace/query_sell_order.go +++ b/x/ecocredit/server/marketplace/query_sell_order.go @@ -5,18 +5,18 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - v1 "github.com/regen-network/regen-ledger/x/ecocredit/marketplace" + "github.com/regen-network/regen-ledger/x/ecocredit/marketplace" "github.com/regen-network/regen-ledger/x/ecocredit/server/basket" ) -func (k Keeper) SellOrder(ctx context.Context, req *v1.QuerySellOrderRequest) (*v1.QuerySellOrderResponse, error) { +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 v1.SellOrder + var so marketplace.SellOrder if err = basket.PulsarToGogoSlow(order, &so); err != nil { return nil, err } - return &v1.QuerySellOrderResponse{SellOrder: &so}, nil + return &marketplace.QuerySellOrderResponse{SellOrder: &so}, nil } From 3bc98ca20d8965e8e850449b6500c9e71014c061 Mon Sep 17 00:00:00 2001 From: technicallyty <48813565+tytech3@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:08:41 -0700 Subject: [PATCH 4/4] chore: fix conflicts, fmt --- x/ecocredit/server/marketplace/keeper_test.go | 3 +-- x/ecocredit/server/marketplace/prune_test.go | 2 +- x/ecocredit/server/marketplace/query_sell_order.go | 4 ++-- x/ecocredit/server/marketplace/query_sell_order_test.go | 4 ++-- x/ecocredit/server/marketplace/sell.go | 1 + x/ecocredit/server/marketplace/sell_test.go | 5 ++--- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/x/ecocredit/server/marketplace/keeper_test.go b/x/ecocredit/server/marketplace/keeper_test.go index 9888e49ec6..b5e74a91a4 100644 --- a/x/ecocredit/server/marketplace/keeper_test.go +++ b/x/ecocredit/server/marketplace/keeper_test.go @@ -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 @@ -68,4 +68,3 @@ func setupBase(t *testing.T) *baseSuite { _, _, s.addr = testdata.KeyTestPubAddr() return s } - diff --git a/x/ecocredit/server/marketplace/prune_test.go b/x/ecocredit/server/marketplace/prune_test.go index fed6988461..8835746cb4 100644 --- a/x/ecocredit/server/marketplace/prune_test.go +++ b/x/ecocredit/server/marketplace/prune_test.go @@ -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: ¬Expired}, diff --git a/x/ecocredit/server/marketplace/query_sell_order.go b/x/ecocredit/server/marketplace/query_sell_order.go index dbae7c46cb..46c5eeb8ea 100644 --- a/x/ecocredit/server/marketplace/query_sell_order.go +++ b/x/ecocredit/server/marketplace/query_sell_order.go @@ -5,8 +5,8 @@ import ( 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" - "github.com/regen-network/regen-ledger/x/ecocredit/server/basket" ) func (k Keeper) SellOrder(ctx context.Context, req *marketplace.QuerySellOrderRequest) (*marketplace.QuerySellOrderResponse, error) { @@ -15,7 +15,7 @@ func (k Keeper) SellOrder(ctx context.Context, req *marketplace.QuerySellOrderRe return nil, sdkerrors.ErrInvalidRequest.Wrapf("could not get sell order with id %d: %s", req.SellOrderId, err.Error()) } var so marketplace.SellOrder - if err = basket.PulsarToGogoSlow(order, &so); err != nil { + if err = ormutil.PulsarToGogoSlow(order, &so); err != nil { return nil, err } return &marketplace.QuerySellOrderResponse{SellOrder: &so}, nil diff --git a/x/ecocredit/server/marketplace/query_sell_order_test.go b/x/ecocredit/server/marketplace/query_sell_order_test.go index 1d4143d529..ca2f780ede 100644 --- a/x/ecocredit/server/marketplace/query_sell_order_test.go +++ b/x/ecocredit/server/marketplace/query_sell_order_test.go @@ -10,9 +10,9 @@ import ( "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" - "github.com/regen-network/regen-ledger/x/ecocredit/server/core" ) func TestQuery_SellOrder(t *testing.T) { @@ -44,7 +44,7 @@ func TestQuery_SellOrder(t *testing.T) { assert.NilError(t, err) var gogoOrder marketplace.SellOrder - assert.NilError(t, core.PulsarToGogoSlow(&order, &gogoOrder)) + assert.NilError(t, ormutil.PulsarToGogoSlow(&order, &gogoOrder)) res, err := s.k.SellOrder(s.ctx, &marketplace.QuerySellOrderRequest{SellOrderId: id}) assert.NilError(t, err) diff --git a/x/ecocredit/server/marketplace/sell.go b/x/ecocredit/server/marketplace/sell.go index 1fd5c5b6d2..6b6506b707 100644 --- a/x/ecocredit/server/marketplace/sell.go +++ b/x/ecocredit/server/marketplace/sell.go @@ -2,6 +2,7 @@ package marketplace import ( "context" + "google.golang.org/protobuf/types/known/timestamppb" "github.com/cosmos/cosmos-sdk/orm/types/ormerrors" diff --git a/x/ecocredit/server/marketplace/sell_test.go b/x/ecocredit/server/marketplace/sell_test.go index 23c989fae5..e8a0bc75bd 100644 --- a/x/ecocredit/server/marketplace/sell_test.go +++ b/x/ecocredit/server/marketplace/sell_test.go @@ -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) @@ -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, @@ -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", }))