Skip to content

Commit

Permalink
Merge branch 'master' into 2025-01-20-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt authored Jan 27, 2025
2 parents c2407fe + 37757c6 commit 231a013
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
51 changes: 14 additions & 37 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ export async function handleAddOrderbookOwnersProfileMap(
tokens,
orderDetails,
),
consumedTakeOrders: [],
});
} else {
if (!order.active) order.active = true;
Expand All @@ -195,11 +194,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 @@ -214,12 +213,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 @@ -144,10 +144,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 @@ -274,7 +274,6 @@ describe("Test order", async function () {
{
active: true,
order: orderStruct1,
consumedTakeOrders: [],
takeOrders: [
{
buyToken: orderStruct1.validInputs[0].token,
Expand All @@ -284,16 +283,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 @@ -303,38 +306,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

0 comments on commit 231a013

Please sign in to comment.