Skip to content

Commit

Permalink
bugfix: handle undefined unitsToFill in fulfillOrders() (#580)
Browse files Browse the repository at this point in the history
* chore: add tests to prove the existence of issue #578

* bugfix: handle undefined unitsToFill in fulfillOrders.
  • Loading branch information
naveen-imtb authored Jun 25, 2024
1 parent 4cc7350 commit 32750d4
Show file tree
Hide file tree
Showing 3 changed files with 650 additions and 12 deletions.
24 changes: 14 additions & 10 deletions src/utils/fulfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import {
areAllCurrenciesSame,
mapOrderAmountsFromFilledStatus,
mapOrderAmountsFromUnitsToFill,
adjustTipsForPartialFills,
mapTipAmountsFromFilledStatus,
mapTipAmountsFromUnitsToFill,
totalItemsAmount,
} from "./order";
import {
Expand Down Expand Up @@ -382,7 +383,7 @@ export function fulfillStandardOrder(
let adjustedTips: ConsiderationItem[] = [];

if (tips.length > 0) {
adjustedTips = adjustTipsForPartialFills(tips, unitsToFill, totalSize);
adjustedTips = mapTipAmountsFromUnitsToFill(tips, unitsToFill, totalSize);
}

const {
Expand Down Expand Up @@ -589,14 +590,17 @@ export function fulfillAvailableOrders({
return [];
}

// Max total amount to fulfill for scaling
const maxUnits = getMaximumSizeForOrder(orderMetadata.order);

return adjustTipsForPartialFills(
orderMetadata.tips,
orderMetadata.unitsToFill || 1,
maxUnits,
);
return orderMetadata.unitsToFill
? mapTipAmountsFromUnitsToFill(
orderMetadata.tips,
orderMetadata.unitsToFill,
orderMetadata.orderStatus.totalSize,
)
: mapTipAmountsFromFilledStatus(
orderMetadata.tips,
orderMetadata.orderStatus.totalFilled,
orderMetadata.orderStatus.totalSize,
);
};

const ordersMetadataWithAdjustedFills = sanitizedOrdersMetadata.map(
Expand Down
24 changes: 22 additions & 2 deletions src/utils/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export const mapOrderAmountsFromFilledStatus = (

// i.e if totalFilled is 3 and totalSize is 4, there are 1 / 4 order amounts left to fill.
const basisPoints =
totalSize - (totalFilled * ONE_HUNDRED_PERCENT_BP) / totalSize;
((totalSize - totalFilled) * ONE_HUNDRED_PERCENT_BP) / totalSize;

return {
parameters: {
Expand Down Expand Up @@ -273,7 +273,7 @@ export const mapOrderAmountsFromUnitsToFill = (
};
};

export function adjustTipsForPartialFills(
export function mapTipAmountsFromUnitsToFill(
tips: ConsiderationItem[],
unitsToFill: BigNumberish,
totalSize: bigint,
Expand All @@ -299,6 +299,26 @@ export function adjustTipsForPartialFills(
}));
}

export function mapTipAmountsFromFilledStatus(
tips: ConsiderationItem[],
totalFilled: bigint,
totalSize: bigint,
): ConsiderationItem[] {
if (totalFilled === 0n || totalSize === 0n) {
return tips;
}

// i.e if totalFilled is 3 and totalSize is 4, there are 1 / 4 order amounts left to fill.
const basisPoints =
((totalSize - totalFilled) * ONE_HUNDRED_PERCENT_BP) / totalSize;

return tips.map((tip) => ({
...tip,
startAmount: multiplyBasisPoints(tip.startAmount, basisPoints).toString(),
endAmount: multiplyBasisPoints(tip.endAmount, basisPoints).toString(),
}));
}

export const generateRandomSalt = (domain?: string) => {
if (domain) {
return toBeHex(
Expand Down
Loading

0 comments on commit 32750d4

Please sign in to comment.