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

fix prepare orders issue #307

Merged
merged 2 commits into from
Jan 26, 2025
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
51 changes: 14 additions & 37 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export async function handleAddOrderbookOwnersProfileMap(
tokens,
orderDetails,
),
consumedTakeOrders: [],
});
} else {
if (!order.active) order.active = true;
Expand All @@ -192,11 +191,11 @@ export async function handleAddOrderbookOwnersProfileMap(
tokens,
orderDetails,
),
consumedTakeOrders: [],
});
orderbookOwnerProfileItem.set(orderStruct.owner.toLowerCase(), {
limit: ownerLimits?.[orderStruct.owner.toLowerCase()] ?? DEFAULT_OWNER_LIMIT,
orders: ordersProfileMap,
lastIndex: 0,
});
}
} else {
Expand All @@ -211,12 +210,12 @@ export async function handleAddOrderbookOwnersProfileMap(
tokens,
orderDetails,
),
consumedTakeOrders: [],
});
const ownerProfileMap: OwnersProfileMap = new Map();
ownerProfileMap.set(orderStruct.owner.toLowerCase(), {
limit: ownerLimits?.[orderStruct.owner.toLowerCase()] ?? DEFAULT_OWNER_LIMIT,
orders: ordersProfileMap,
lastIndex: 0,
});
orderbooksOwnersProfileMap.set(orderbook, ownerProfileMap);
}
Expand Down Expand Up @@ -302,42 +301,20 @@ export function prepareOrdersForRound(
const orderbookBundledOrders: BundledOrders[] = [];
for (const [, ownerProfile] of ownersProfileMap) {
let remainingLimit = ownerProfile.limit;
const ordersProfilesArr = Array.from(ownerProfile.orders);
// consume orders limits
for (const [orderHash, orderProfile] of ordersProfilesArr) {
if (
remainingLimit > 0 &&
orderProfile.active &&
orderProfile.takeOrders.length > 0
) {
const consumingOrderPairs = orderProfile.takeOrders.splice(0, remainingLimit);
remainingLimit -= consumingOrderPairs.length;
orderProfile.consumedTakeOrders.push(...consumingOrderPairs);
gatherPairs(orderbook, orderHash, consumingOrderPairs, orderbookBundledOrders);
}
const allOrders: Pair[] = [];
ownerProfile.orders.forEach((v) => allOrders.push(...v.takeOrders));
const consumingOrders = allOrders.splice(ownerProfile.lastIndex, remainingLimit);
remainingLimit -= consumingOrders.length;
ownerProfile.lastIndex += consumingOrders.length;
if (remainingLimit) {
ownerProfile.lastIndex = 0;
const remainingConsumingOrders = allOrders.splice(0, remainingLimit);
ownerProfile.lastIndex += remainingConsumingOrders.length;
consumingOrders.push(...remainingConsumingOrders);
}
// if all orders are consumed and still there is limit remaining,
// reset and start consuming again from top until limit is reached
if (remainingLimit > 0) {
for (const [orderHash, orderProfile] of ordersProfilesArr) {
if (orderProfile.active) {
orderProfile.takeOrders.push(...orderProfile.consumedTakeOrders.splice(0));
if (remainingLimit > 0) {
const consumingOrderPairs = orderProfile.takeOrders.splice(
0,
remainingLimit,
);
remainingLimit -= consumingOrderPairs.length;
orderProfile.consumedTakeOrders.push(...consumingOrderPairs);
gatherPairs(
orderbook,
orderHash,
consumingOrderPairs,
orderbookBundledOrders,
);
}
}
}
for (const order of consumingOrders) {
gatherPairs(orderbook, order.takeOrder.id, [order], orderbookBundledOrders);
}
}
if (shuffle) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ export type OrderProfile = {
active: boolean;
order: Order;
takeOrders: Pair[];
consumedTakeOrders: Pair[];
};
export type OwnerProfile = {
limit: number;
lastIndex: number;
orders: OrdersProfileMap;
};
export type OrdersProfileMap = Map<string, OrderProfile>;
Expand Down
56 changes: 40 additions & 16 deletions test/orders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ describe("Test order", async function () {
{
active: true,
order: orderStruct1,
consumedTakeOrders: [],
takeOrders: [
{
buyToken: orderStruct1.validInputs[0].token,
Expand All @@ -675,16 +674,20 @@ describe("Test order", async function () {
sellTokenSymbol: order1.outputs[0].token.symbol,
sellTokenDecimals: orderStruct1.validOutputs[0].decimals,
takeOrder: {
order: orderStruct1,
inputIOIndex: 0,
outputIOIndex: 0,
signedContext: [],
id: order1.orderHash.toLowerCase(),
takeOrder: {
order: orderStruct1,
inputIOIndex: 0,
outputIOIndex: 0,
signedContext: [],
},
},
},
],
},
],
]),
lastIndex: 0,
});
ownerMap.set(order2.owner.toLowerCase(), {
limit: 25,
Expand All @@ -694,38 +697,59 @@ describe("Test order", async function () {
{
active: true,
order: orderStruct2,
consumedTakeOrders: [],
takeOrders: [
{
buyToken: orderStruct2.validInputs[0].token,
buyTokenSymbol: order2.inputs[0].token.symbol,
buyTokenDecimals: orderStruct2.validInputs[0].decimals,
buyToken: orderStruct2.validInputs[1].token,
buyTokenSymbol: order2.inputs[1].token.symbol,
buyTokenDecimals: orderStruct2.validInputs[1].decimals,
sellToken: orderStruct2.validOutputs[0].token,
sellTokenSymbol: order2.outputs[0].token.symbol,
sellTokenDecimals: orderStruct2.validOutputs[0].decimals,
takeOrder: {
order: orderStruct2,
inputIOIndex: 0,
outputIOIndex: 0,
signedContext: [],
id: order2.orderHash.toLowerCase(),
takeOrder: {
order: orderStruct2,
inputIOIndex: 1,
outputIOIndex: 0,
signedContext: [],
},
},
},
{
buyToken: orderStruct2.validInputs[0].token,
buyTokenSymbol: order2.inputs[0].token.symbol,
buyTokenDecimals: orderStruct2.validInputs[0].decimals,
sellToken: orderStruct2.validOutputs[1].token,
sellTokenSymbol: order2.outputs[1].token.symbol,
sellTokenDecimals: orderStruct2.validOutputs[1].decimals,
takeOrder: {
id: order2.orderHash.toLowerCase(),
takeOrder: {
order: orderStruct2,
inputIOIndex: 0,
outputIOIndex: 1,
signedContext: [],
},
},
},
],
},
],
]),
lastIndex: 0,
});
const expected = new Map([]);
expected.set(`0x${"2".repeat(40)}`, ownerMap);

const resultAsArray = Array.from(result).map((v) => [
v[0],
Array.from(v[1]).map((e) => [e[0], Array.from(e[1])]),
Array.from(v[1]).map((e) => [e[0], { ...e[1], orders: Array.from(e[1].orders) }]),
]);
const expectedAsArray = Array.from(result).map((v) => [
const expectedAsArray = Array.from(expected).map((v) => [
v[0],
Array.from(v[1]).map((e) => [e[0], Array.from(e[1])]),
Array.from(v[1]).map((e) => [e[0], { ...e[1], orders: Array.from(e[1].orders) }]),
]);

assert.deepEqual(resultAsArray, expectedAsArray);
});

Expand Down
Loading