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 24, 2025
2 parents 45210bf + 21bec90 commit c2407fe
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,14 @@ export function prepareOrdersForRound(
const orderbookBundledOrders: BundledOrders[] = [];
for (const [, ownerProfile] of ownersProfileMap) {
let remainingLimit = ownerProfile.limit;
const activeOrdersProfiles = Array.from(ownerProfile.orders).filter((v) => v[1].active);
let remainingOrdersPairs = activeOrdersProfiles.filter(
(v) => v[1].takeOrders.length > 0,
);
// reset if all orders are already consumed
if (remainingOrdersPairs.length === 0) {
for (const [, orderProfile] of activeOrdersProfiles) {
orderProfile.takeOrders.push(...orderProfile.consumedTakeOrders.splice(0));
}
remainingOrdersPairs = activeOrdersProfiles;
}
const ordersProfilesArr = Array.from(ownerProfile.orders);
// consume orders limits
for (const [orderHash, orderProfile] of remainingOrdersPairs) {
if (remainingLimit > 0) {
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);
Expand All @@ -325,21 +319,23 @@ export function prepareOrdersForRound(
// 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 activeOrdersProfiles) {
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 [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,
);
}
}
}
}
Expand Down

0 comments on commit c2407fe

Please sign in to comment.