Skip to content

Commit

Permalink
Adjust Tips Correctly for Partial Fills (#416)
Browse files Browse the repository at this point in the history
* Adjust tips correctly for partial fills

* Add ERC1155 <=> ETH adjust tips correctly with low denomination

* Add ERC1155 <=> ERC20 include tips correctly test
  • Loading branch information
sofianeOuafir authored Dec 20, 2023
1 parent 2b6788d commit e5b9453
Show file tree
Hide file tree
Showing 3 changed files with 358 additions and 3 deletions.
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

0 comments on commit e5b9453

Please sign in to comment.