Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions proto/dex/enums.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum PositionEffect {
enum OrderType {
LIMIT = 0;
MARKET = 1;
LIQUIDATION = 2;
}

enum Denom {
Expand Down
1 change: 1 addition & 0 deletions proto/dex/settlement.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ message SettlementEntry {
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "leverage"
];
string orderType = 10 [(gogoproto.jsontag) = "order_type"];
}

message Settlements {
Expand Down
42 changes: 20 additions & 22 deletions x/dex/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,16 @@ func NewOrders() *Orders {
}

type OrderPlacement struct {
Id uint64
Price sdk.Dec
Quantity sdk.Dec
Creator string
OrderType types.OrderType
Direction types.PositionDirection
Effect types.PositionEffect
PriceDenom types.Denom
AssetDenom types.Denom
Leverage sdk.Dec
Liquidation bool
Id uint64
Price sdk.Dec
Quantity sdk.Dec
Creator string
OrderType types.OrderType
Direction types.PositionDirection
Effect types.PositionEffect
PriceDenom types.Denom
AssetDenom types.Denom
Leverage sdk.Dec
}

type OrderPlacements struct {
Expand Down Expand Up @@ -211,17 +210,16 @@ func FromLiquidationOrder(liquidationOrder types.LiquidationOrder, orderId uint6
panic(err)
}
return OrderPlacement{
Id: orderId,
Price: types.ConvertDecToStandard(priceUnit, price),
Quantity: types.ConvertDecToStandard(assetUnit, liquidationOrder.Quantity),
Creator: liquidationOrder.Account,
OrderType: types.OrderType_MARKET,
Direction: direction,
Effect: types.PositionEffect_CLOSE,
PriceDenom: priceDenom,
AssetDenom: assetDenom,
Leverage: liquidationOrder.Leverage,
Liquidation: true,
Id: orderId,
Price: types.ConvertDecToStandard(priceUnit, price),
Quantity: types.ConvertDecToStandard(assetUnit, liquidationOrder.Quantity),
Creator: liquidationOrder.Account,
OrderType: types.OrderType_LIQUIDATION,
Direction: direction,
Effect: types.PositionEffect_CLOSE,
PriceDenom: priceDenom,
AssetDenom: assetDenom,
Leverage: liquidationOrder.Leverage,
}
}

Expand Down
34 changes: 34 additions & 0 deletions x/dex/exchange/limit_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestMatchSingleOrder(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -77,6 +78,7 @@ func TestMatchSingleOrder(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
}

Expand Down Expand Up @@ -290,6 +292,7 @@ func TestMatchSingleOrderFromShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -299,6 +302,7 @@ func TestMatchSingleOrderFromShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
}

Expand Down Expand Up @@ -356,6 +360,7 @@ func TestMatchSingleOrderFromLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -365,6 +370,7 @@ func TestMatchSingleOrderFromLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
}

Expand Down Expand Up @@ -443,6 +449,7 @@ func TestMatchSingleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(95),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -452,6 +459,7 @@ func TestMatchSingleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(95),
ExpectedCostOrProceed: sdk.NewDec(90),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[2], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -461,6 +469,7 @@ func TestMatchSingleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[3], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -470,6 +479,7 @@ func TestMatchSingleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[4], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -479,6 +489,7 @@ func TestMatchSingleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[5], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -488,6 +499,7 @@ func TestMatchSingleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "ghi",
OrderType: types.OrderType_LIMIT,
})
}

Expand Down Expand Up @@ -566,6 +578,7 @@ func TestMatchSingleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(105),
ExpectedCostOrProceed: sdk.NewDec(110),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -575,6 +588,7 @@ func TestMatchSingleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(105),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[2], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -584,6 +598,7 @@ func TestMatchSingleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[3], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -593,6 +608,7 @@ func TestMatchSingleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[4], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -602,6 +618,7 @@ func TestMatchSingleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "ghi",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[5], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -611,6 +628,7 @@ func TestMatchSingleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
}

Expand Down Expand Up @@ -718,6 +736,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(97),
ExpectedCostOrProceed: sdk.NewDec(104),
Account: "jkl",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -727,6 +746,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(97),
ExpectedCostOrProceed: sdk.NewDec(90),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[2], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -736,6 +756,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(95),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[3], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -745,6 +766,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(95),
ExpectedCostOrProceed: sdk.NewDec(90),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[4], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -754,6 +776,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[5], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -763,6 +786,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "def",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[6], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -772,6 +796,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[7], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -781,6 +806,7 @@ func TestMatchMultipleOrderFromMultipleShortBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "ghi",
OrderType: types.OrderType_LIMIT,
})
}

Expand Down Expand Up @@ -888,6 +914,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(103),
ExpectedCostOrProceed: sdk.NewDec(110),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[1], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -897,6 +924,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(103),
ExpectedCostOrProceed: sdk.NewDec(96),
Account: "jkl",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[2], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -906,6 +934,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(105),
ExpectedCostOrProceed: sdk.NewDec(110),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[3], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -915,6 +944,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(105),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[4], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -924,6 +954,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[5], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -933,6 +964,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[6], types.Settlement{
Direction: types.PositionDirection_LONG,
Expand All @@ -942,6 +974,7 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "ghi",
OrderType: types.OrderType_LIMIT,
})
assert.Equal(t, *settlements[7], types.Settlement{
Direction: types.PositionDirection_SHORT,
Expand All @@ -951,5 +984,6 @@ func TestMatchMultipleOrderFromMultipleLongBook(t *testing.T) {
ExecutionCostOrProceed: sdk.NewDec(100),
ExpectedCostOrProceed: sdk.NewDec(100),
Account: "abc",
OrderType: types.OrderType_LIMIT,
})
}
16 changes: 15 additions & 1 deletion x/dex/exchange/market_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,21 @@ func MatchMarketOrders(
executed.Mul(existingOrder.GetPrice()),
)
dirtyPrices.Add(existingOrder.GetPrice())
takerSettlements, makerSettlements := Settle(marketOrder.FormattedCreatorWithSuffix(), executed, existingOrder, direction, marketOrder.WorstPrice)

var orderType types.OrderType
if marketOrder.IsLiquidation {
orderType = types.OrderType_LIQUIDATION
} else {
orderType = types.OrderType_MARKET
}
takerSettlements, makerSettlements := Settle(
marketOrder.FormattedCreatorWithSuffix(),
executed,
existingOrder,
direction,
marketOrder.WorstPrice,
orderType,
)
*settlements = append(*settlements, makerSettlements...)
// taker settlements' clearing price will need to be adjusted after all market order executions finish
allTakerSettlements = append(allTakerSettlements, takerSettlements...)
Expand Down
Loading