Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FOK back to order match result #768

Merged
merged 1 commit into from
May 10, 2023
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 x/dex/cache/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (o *BlockOrders) getKVsToSet(failedOrdersMap map[uint64]wasm.UnsuccessfulOr
func (o *BlockOrders) GetSortedMarketOrders(direction types.PositionDirection) []*types.Order {
res := o.getOrdersByCriteria(types.OrderType_MARKET, direction)
res = append(res, o.getOrdersByCriteria(types.OrderType_FOKMARKET, direction)...)
res = append(res, o.getOrdersByCriteria(types.OrderType_FOKMARKETBYVALUE, direction)...)
sort.SliceStable(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
120 changes: 114 additions & 6 deletions x/dex/cache/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,126 @@ func TestGetSortedMarketOrders(t *testing.T) {
OrderType: types.OrderType_LIMIT,
Price: sdk.MustNewDecFromStr("100"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 9,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_LONG,
OrderType: types.OrderType_FOKMARKET,
Price: sdk.MustNewDecFromStr("100"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 10,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_LONG,
OrderType: types.OrderType_FOKMARKET,
Price: sdk.MustNewDecFromStr("0"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 11,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_LONG,
OrderType: types.OrderType_FOKMARKET,
Price: sdk.MustNewDecFromStr("150"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 12,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_FOKMARKET,
Price: sdk.MustNewDecFromStr("0"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 13,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_FOKMARKET,
Price: sdk.MustNewDecFromStr("150"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 14,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_FOKMARKET,
Price: sdk.MustNewDecFromStr("100"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 15,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_LONG,
OrderType: types.OrderType_FOKMARKETBYVALUE,
Price: sdk.MustNewDecFromStr("100"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 16,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_LONG,
OrderType: types.OrderType_FOKMARKETBYVALUE,
Price: sdk.MustNewDecFromStr("0"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 17,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_LONG,
OrderType: types.OrderType_FOKMARKETBYVALUE,
Price: sdk.MustNewDecFromStr("150"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 18,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_FOKMARKETBYVALUE,
Price: sdk.MustNewDecFromStr("0"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 19,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_FOKMARKETBYVALUE,
Price: sdk.MustNewDecFromStr("150"),
})
stateOne.GetBlockOrders(ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).Add(&types.Order{
Id: 20,
Account: "test",
ContractAddr: TEST_CONTRACT,
PositionDirection: types.PositionDirection_SHORT,
OrderType: types.OrderType_FOKMARKETBYVALUE,
Price: sdk.MustNewDecFromStr("100"),
})

marketBuys := stateOne.GetBlockOrders(
ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).GetSortedMarketOrders(types.PositionDirection_LONG)
require.Equal(t, uint64(3), marketBuys[0].Id)
require.Equal(t, uint64(1), marketBuys[1].Id)
require.Equal(t, uint64(2), marketBuys[2].Id)
require.Equal(t, uint64(16), marketBuys[0].Id)
require.Equal(t, uint64(10), marketBuys[1].Id)
require.Equal(t, uint64(3), marketBuys[2].Id)
require.Equal(t, uint64(1), marketBuys[3].Id)
require.Equal(t, uint64(11), marketBuys[4].Id)
require.Equal(t, uint64(17), marketBuys[5].Id)
require.Equal(t, uint64(2), marketBuys[6].Id)
require.Equal(t, uint64(9), marketBuys[7].Id)
require.Equal(t, uint64(15), marketBuys[8].Id)

marketSells := stateOne.GetBlockOrders(
ctx, utils.ContractAddress(TEST_CONTRACT), utils.PairString(TEST_PAIR)).GetSortedMarketOrders(types.PositionDirection_SHORT)
require.Equal(t, uint64(6), marketSells[0].Id)
require.Equal(t, uint64(5), marketSells[1].Id)
require.Equal(t, uint64(4), marketSells[2].Id)
require.Equal(t, uint64(18), marketSells[0].Id)
require.Equal(t, uint64(12), marketSells[1].Id)
require.Equal(t, uint64(6), marketSells[2].Id)
require.Equal(t, uint64(5), marketSells[3].Id)
require.Equal(t, uint64(4), marketSells[4].Id)
require.Equal(t, uint64(14), marketSells[5].Id)
require.Equal(t, uint64(20), marketSells[6].Id)
require.Equal(t, uint64(13), marketSells[7].Id)
require.Equal(t, uint64(19), marketSells[8].Id)
}

func TestGetTriggeredOrders(t *testing.T) {
Expand Down