From dee2b157b230a958d4c5ac8f9603d4b55ca539fa Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Sat, 25 Jan 2025 22:56:07 +0000 Subject: [PATCH 1/2] init --- src/order.ts | 52 ++++++++++++----------------------------- src/types.ts | 2 +- test/orders.test.js | 56 ++++++++++++++++++++++++++++++++------------- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/order.ts b/src/order.ts index 16fec624..e3051273 100644 --- a/src/order.ts +++ b/src/order.ts @@ -175,7 +175,6 @@ export async function handleAddOrderbookOwnersProfileMap( tokens, orderDetails, ), - consumedTakeOrders: [], }); } else { if (!order.active) order.active = true; @@ -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 { @@ -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); } @@ -302,42 +301,21 @@ 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 = Array.from(ownerProfile.orders).flatMap( + ([, orderProfile]) => orderProfile.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) { diff --git a/src/types.ts b/src/types.ts index 2f3ed8da..f73328de 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; diff --git a/test/orders.test.js b/test/orders.test.js index 980438b9..f298ca4a 100644 --- a/test/orders.test.js +++ b/test/orders.test.js @@ -665,7 +665,6 @@ describe("Test order", async function () { { active: true, order: orderStruct1, - consumedTakeOrders: [], takeOrders: [ { buyToken: orderStruct1.validInputs[0].token, @@ -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, @@ -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); }); From 4dfffe9ceb5e85f1da7dfb4387b29abc3b5b434d Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Sun, 26 Jan 2025 03:46:42 +0000 Subject: [PATCH 2/2] Update order.ts --- src/order.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/order.ts b/src/order.ts index e3051273..be1216c7 100644 --- a/src/order.ts +++ b/src/order.ts @@ -302,9 +302,8 @@ export function prepareOrdersForRound( for (const [, ownerProfile] of ownersProfileMap) { let remainingLimit = ownerProfile.limit; // consume orders limits - const allOrders = Array.from(ownerProfile.orders).flatMap( - ([, orderProfile]) => orderProfile.takeOrders, - ); + 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;