Skip to content

Commit

Permalink
Add market order by nominal type (#295)
Browse files Browse the repository at this point in the history
* Add market by nominal type

* Fix gofmt

* Resolve comment

* Fix fmt

Co-authored-by: Cyson <[email protected]>
  • Loading branch information
LCyson and Cyson authored Oct 10, 2022
1 parent 7769767 commit 0f8362d
Show file tree
Hide file tree
Showing 11 changed files with 555 additions and 150 deletions.
1 change: 1 addition & 0 deletions proto/dex/enums.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum OrderType {
MARKET = 1;
LIQUIDATION = 2;
FOKMARKET = 3; // fill-or-kill market order
FOKMARKETBYVALUE = 4; // fill-or-kill market by value order
}

enum Unit {
Expand Down
6 changes: 6 additions & 0 deletions proto/dex/order.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ message Order {
string statusDescription = 12 [
(gogoproto.jsontag) = "status_description"
];
string nominal = 13 [
(gogoproto.moretags) = "yaml:\"nominal\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "nominal"
];
}

message Cancellation {
Expand Down
1 change: 1 addition & 0 deletions wasmbinding/test/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestEncodePlaceOrder(t *testing.T) {
Price: sdk.MustNewDecFromStr("10"),
Quantity: sdk.OneDec(),
Data: "{\"position_effect\":\"OPEN\", \"leverage\":\"1\"}",
Nominal: sdk.ZeroDec(),
}
fund := sdk.NewCoin("usei", sdk.NewInt(1000000000))
msg := bindings.PlaceOrders{
Expand Down
28 changes: 25 additions & 3 deletions x/dex/cache/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ func (o *BlockOrders) GetSortedMarketOrders(direction types.PositionDirection, i
o.mu.Lock()
defer o.mu.Unlock()

res := o.getOrdersByCriteria(types.OrderType_MARKET, direction)
res = append(res, o.getOrdersByCriteria(types.OrderType_FOKMARKET, direction)...)
orderTypes := map[types.OrderType]bool{
types.OrderType_MARKET: true,
types.OrderType_FOKMARKET: true,
types.OrderType_FOKMARKETBYVALUE: true,
}
if includeLiquidationOrders {
res = append(res, o.getOrdersByCriteria(types.OrderType_LIQUIDATION, direction)...)
orderTypes[types.OrderType_LIQUIDATION] = true
}
res := o.getOrdersByCriteriaMap(orderTypes, map[types.PositionDirection]bool{direction: true})

sort.Slice(res, func(i, j int) bool {
// a price of 0 indicates that there is no worst price for the order, so it should
// always be ranked at the top.
Expand Down Expand Up @@ -86,3 +91,20 @@ func (o *BlockOrders) getOrdersByCriteria(orderType types.OrderType, direction t
}
return res
}

func (o *BlockOrders) getOrdersByCriteriaMap(orderType map[types.OrderType]bool, direction map[types.PositionDirection]bool) []*types.Order {
res := []*types.Order{}
for _, order := range o.internal {
if _, ok := orderType[order.OrderType]; !ok {
continue
}
if _, ok := direction[order.PositionDirection]; !ok {
continue
}
if order.Status == types.OrderStatus_FAILED_TO_PLACE {
continue
}
res = append(res, order)
}
return res
}
7 changes: 7 additions & 0 deletions x/dex/client/cli/tx/tx_place_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func CmdPlaceOrders() *cobra.Command {
}
newOrder.OrderType = argOrderType
newOrder.Data = orderDetails[6]
if newOrder.OrderType == types.OrderType_FOKMARKETBYVALUE {
argNominal, err := sdk.NewDecFromStr(orderDetails[7])
if err != nil {
return err
}
newOrder.Nominal = argNominal
}
orders = append(orders, &newOrder)
}

Expand Down
3 changes: 2 additions & 1 deletion x/dex/contract/market_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func getUnfulfilledPlacedMarketOrderIds(
if order.Status == types.OrderStatus_FAILED_TO_PLACE {
continue
}
if order.OrderType == types.OrderType_MARKET || order.OrderType == types.OrderType_LIQUIDATION || order.OrderType == types.OrderType_FOKMARKET {
if order.OrderType == types.OrderType_MARKET || order.OrderType == types.OrderType_LIQUIDATION ||
order.OrderType == types.OrderType_FOKMARKET || order.OrderType == types.OrderType_FOKMARKETBYVALUE {
if settledQuantity, ok := orderIDToSettledQuantities[order.Id]; !ok || settledQuantity.LT(order.Quantity) {
res = append(res, order.Id)
}
Expand Down
Loading

0 comments on commit 0f8362d

Please sign in to comment.