diff --git a/x/dex/cache/order.go b/x/dex/cache/order.go index 2817538b79..f4d93bdc72 100644 --- a/x/dex/cache/order.go +++ b/x/dex/cache/order.go @@ -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. diff --git a/x/dex/cache/order_test.go b/x/dex/cache/order_test.go index bebe056c0a..29d1528ea6 100644 --- a/x/dex/cache/order_test.go +++ b/x/dex/cache/order_test.go @@ -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) {