Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Tips Correctly for Partial Fills #416

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
9 changes: 8 additions & 1 deletion src/utils/fulfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
areAllCurrenciesSame,
mapOrderAmountsFromFilledStatus,
mapOrderAmountsFromUnitsToFill,
adjustTipsForPartialFills,
totalItemsAmount,
} from "./order";
import {
Expand Down Expand Up @@ -377,11 +378,17 @@ export function fulfillStandardOrder(
totalSize,
});

let adjustedTips: ConsiderationItem[] = [];

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

const {
parameters: { offer, consideration },
} = orderWithAdjustedFills;

const considerationIncludingTips = [...consideration, ...tips];
const considerationIncludingTips = [...consideration, ...adjustedTips];

const offerCriteriaItems = offer.filter(({ itemType }) =>
isCriteriaItem(itemType),
Expand Down
26 changes: 26 additions & 0 deletions src/utils/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,32 @@ export const mapOrderAmountsFromUnitsToFill = (
};
};

export function adjustTipsForPartialFills(
tips: ConsiderationItem[],
unitsToFill: BigNumberish,
totalSize: bigint,
): ConsiderationItem[] {
const unitsToFillBn = BigInt(unitsToFill);

if (unitsToFillBn <= 0n) {
throw new Error("Units to fill must be greater than 0");
}

return tips.map((tip) => ({
...tip,
startAmount: multiplyDivision(
tip.startAmount,
unitsToFillBn,
totalSize,
).toString(),
endAmount: multiplyDivision(
tip.endAmount,
unitsToFillBn,
totalSize,
).toString(),
}));
}

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