Skip to content

Commit 0f8362d

Browse files
Xingchen LiaoCyson
Xingchen Liao
and
Cyson
authored
Add market order by nominal type (#295)
* Add market by nominal type * Fix gofmt * Resolve comment * Fix fmt Co-authored-by: Cyson <[email protected]>
1 parent 7769767 commit 0f8362d

File tree

11 files changed

+555
-150
lines changed

11 files changed

+555
-150
lines changed

proto/dex/enums.proto

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum OrderType {
1818
MARKET = 1;
1919
LIQUIDATION = 2;
2020
FOKMARKET = 3; // fill-or-kill market order
21+
FOKMARKETBYVALUE = 4; // fill-or-kill market by value order
2122
}
2223

2324
enum Unit {

proto/dex/order.proto

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ message Order {
5353
string statusDescription = 12 [
5454
(gogoproto.jsontag) = "status_description"
5555
];
56+
string nominal = 13 [
57+
(gogoproto.moretags) = "yaml:\"nominal\"",
58+
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
59+
(gogoproto.nullable) = false,
60+
(gogoproto.jsontag) = "nominal"
61+
];
5662
}
5763

5864
message Cancellation {

wasmbinding/test/encoder_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestEncodePlaceOrder(t *testing.T) {
3030
Price: sdk.MustNewDecFromStr("10"),
3131
Quantity: sdk.OneDec(),
3232
Data: "{\"position_effect\":\"OPEN\", \"leverage\":\"1\"}",
33+
Nominal: sdk.ZeroDec(),
3334
}
3435
fund := sdk.NewCoin("usei", sdk.NewInt(1000000000))
3536
msg := bindings.PlaceOrders{

x/dex/cache/order.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ func (o *BlockOrders) GetSortedMarketOrders(direction types.PositionDirection, i
4242
o.mu.Lock()
4343
defer o.mu.Unlock()
4444

45-
res := o.getOrdersByCriteria(types.OrderType_MARKET, direction)
46-
res = append(res, o.getOrdersByCriteria(types.OrderType_FOKMARKET, direction)...)
45+
orderTypes := map[types.OrderType]bool{
46+
types.OrderType_MARKET: true,
47+
types.OrderType_FOKMARKET: true,
48+
types.OrderType_FOKMARKETBYVALUE: true,
49+
}
4750
if includeLiquidationOrders {
48-
res = append(res, o.getOrdersByCriteria(types.OrderType_LIQUIDATION, direction)...)
51+
orderTypes[types.OrderType_LIQUIDATION] = true
4952
}
53+
res := o.getOrdersByCriteriaMap(orderTypes, map[types.PositionDirection]bool{direction: true})
54+
5055
sort.Slice(res, func(i, j int) bool {
5156
// a price of 0 indicates that there is no worst price for the order, so it should
5257
// always be ranked at the top.
@@ -86,3 +91,20 @@ func (o *BlockOrders) getOrdersByCriteria(orderType types.OrderType, direction t
8691
}
8792
return res
8893
}
94+
95+
func (o *BlockOrders) getOrdersByCriteriaMap(orderType map[types.OrderType]bool, direction map[types.PositionDirection]bool) []*types.Order {
96+
res := []*types.Order{}
97+
for _, order := range o.internal {
98+
if _, ok := orderType[order.OrderType]; !ok {
99+
continue
100+
}
101+
if _, ok := direction[order.PositionDirection]; !ok {
102+
continue
103+
}
104+
if order.Status == types.OrderStatus_FAILED_TO_PLACE {
105+
continue
106+
}
107+
res = append(res, order)
108+
}
109+
return res
110+
}

x/dex/client/cli/tx/tx_place_orders.go

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func CmdPlaceOrders() *cobra.Command {
5353
}
5454
newOrder.OrderType = argOrderType
5555
newOrder.Data = orderDetails[6]
56+
if newOrder.OrderType == types.OrderType_FOKMARKETBYVALUE {
57+
argNominal, err := sdk.NewDecFromStr(orderDetails[7])
58+
if err != nil {
59+
return err
60+
}
61+
newOrder.Nominal = argNominal
62+
}
5663
orders = append(orders, &newOrder)
5764
}
5865

x/dex/contract/market_order.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func getUnfulfilledPlacedMarketOrderIds(
4040
if order.Status == types.OrderStatus_FAILED_TO_PLACE {
4141
continue
4242
}
43-
if order.OrderType == types.OrderType_MARKET || order.OrderType == types.OrderType_LIQUIDATION || order.OrderType == types.OrderType_FOKMARKET {
43+
if order.OrderType == types.OrderType_MARKET || order.OrderType == types.OrderType_LIQUIDATION ||
44+
order.OrderType == types.OrderType_FOKMARKET || order.OrderType == types.OrderType_FOKMARKETBYVALUE {
4445
if settledQuantity, ok := orderIDToSettledQuantities[order.Id]; !ok || settledQuantity.LT(order.Quantity) {
4546
res = append(res, order.Id)
4647
}

0 commit comments

Comments
 (0)