Skip to content

Commit

Permalink
[1658]: Reorganize keeper/orders.go. In SettleOrders, release the hol…
Browse files Browse the repository at this point in the history
…ds on the orders before doing the transfers, and put the hold back on the partial if there is one.
  • Loading branch information
SpicyLemon committed Oct 3, 2023
1 parent edb0d1a commit bc422e3
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 192 deletions.
27 changes: 22 additions & 5 deletions x/exchange/keeper/fulfillment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (k Keeper) FillBids(ctx sdk.Context, msg *exchange.MsgFillBidsRequest) erro
return err
}
seller := sdk.MustAccAddressFromBech32(msg.Seller)
if err := k.validateCanCreateAsk(ctx, marketID, seller); err != nil {
if err := k.validateUserCanCreateAsk(ctx, marketID, seller); err != nil {
return err
}
if err := validateCreateAskFees(store, marketID, msg.AskOrderCreationFee, msg.SellerSettlementFlatFee); err != nil {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (k Keeper) FillAsks(ctx sdk.Context, msg *exchange.MsgFillAsksRequest) erro
if serr != nil {
return fmt.Errorf("invalid buyer %q: %w", msg.Buyer, serr)
}
if err := k.validateCanCreateBid(ctx, marketID, buyer); err != nil {
if err := k.validateUserCanCreateBid(ctx, marketID, buyer); err != nil {
return err
}
if err := validateCreateBidFees(store, marketID, msg.BidOrderCreationFee, msg.TotalPrice, msg.BuyerSettlementFees); err != nil {
Expand Down Expand Up @@ -356,6 +356,20 @@ func (k Keeper) SettleOrders(ctx sdk.Context, marketID uint32, askOrderIDs, bidO
return errors.New("settlement unexpectedly resulted in all orders fully filled")
}

for _, order := range askOrders {
if err = k.releaseHoldOnOrder(ctx, order); err != nil {
errs = append(errs, err)
}
}
for _, order := range bidOrders {
if err = k.releaseHoldOnOrder(ctx, order); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return errors.Join(errs...)
}

transfers := exchange.BuildSettlementTransfers(fulfillments)

for _, transfer := range transfers.OrderTransfers {
Expand All @@ -369,9 +383,12 @@ func (k Keeper) SettleOrders(ctx sdk.Context, marketID uint32, askOrderIDs, bidO
}

if fulfillments.PartialOrder != nil {
if err = k.setOrderInStore(store, *fulfillments.PartialOrder.NewOrder); err != nil {
return fmt.Errorf("could not update partial %s order %d: %w",
fulfillments.PartialOrder.NewOrder.GetOrderType(), fulfillments.PartialOrder.NewOrder.OrderId, err)
order := fulfillments.PartialOrder.NewOrder
if err = k.setOrderInStore(store, *order); err != nil {
return fmt.Errorf("could not update partial %s order %d: %w", order.GetOrderType(), order.OrderId, err)
}
if err = k.placeHoldOnOrder(ctx, fulfillments.PartialOrder.NewOrder); err != nil {
return fmt.Errorf("could not replace hold on partial %s order %d: %w", order.GetOrderType(), order.OrderId, err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/exchange/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (k QueryServer) QueryGetMarketOrders(goCtx context.Context, req *exchange.Q

resp := &exchange.QueryGetMarketOrdersResponse{}
var err error
resp.Pagination, resp.Orders, err = k.GetPageOfOrdersFromIndex(store, req.Pagination, req.OrderType, req.AfterOrderId)
resp.Pagination, resp.Orders, err = k.getPageOfOrdersFromIndex(store, req.Pagination, req.OrderType, req.AfterOrderId)

if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error iterating orders for market %d: %v", req.MarketId, err)
Expand All @@ -126,7 +126,7 @@ func (k QueryServer) QueryGetOwnerOrders(goCtx context.Context, req *exchange.Qu

resp := &exchange.QueryGetOwnerOrdersResponse{}
var err error
resp.Pagination, resp.Orders, err = k.GetPageOfOrdersFromIndex(store, req.Pagination, req.OrderType, req.AfterOrderId)
resp.Pagination, resp.Orders, err = k.getPageOfOrdersFromIndex(store, req.Pagination, req.OrderType, req.AfterOrderId)

if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error iterating orders for owner %s: %v", req.Owner, err)
Expand All @@ -147,7 +147,7 @@ func (k QueryServer) QueryGetAssetOrders(goCtx context.Context, req *exchange.Qu

resp := &exchange.QueryGetAssetOrdersResponse{}
var err error
resp.Pagination, resp.Orders, err = k.GetPageOfOrdersFromIndex(store, req.Pagination, req.OrderType, req.AfterOrderId)
resp.Pagination, resp.Orders, err = k.getPageOfOrdersFromIndex(store, req.Pagination, req.OrderType, req.AfterOrderId)

if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error iterating orders for asset %s: %v", req.Asset, err)
Expand Down
Loading

0 comments on commit bc422e3

Please sign in to comment.