Skip to content
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
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