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
46 changes: 40 additions & 6 deletions src/utils/fulfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ export function fulfillAvailableOrders({

let totalNativeAmount = 0n
const totalInsufficientApprovals: InsufficientApprovals = []
// What the fulfiller owes across the WHOLE batch, per lowercased token and
// identifier. Each order's own requirement is checked separately below, but
// an ERC20 approval is one allowance shared by every order in the batch, so
// the amount to approve is this total rather than any single order's share.
const cumulativeFulfillerAmounts: Record<string, Record<string, bigint>> = {}
const criteriaOffersAndConsiderations = sanitizedOrdersMetadata
.flatMap(orderMetadata => [
orderMetadata.order.parameters.offer,
Expand Down Expand Up @@ -719,13 +724,25 @@ export function fulfillAvailableOrders({
isConsiderationItem: true,
}

const summedConsideration = getSummedTokenAndIdentifierAmounts({
items: considerationIncludingTips,
criterias: considerationCriteria,
timeBasedItemParams,
})

totalNativeAmount =
totalNativeAmount +
(getSummedTokenAndIdentifierAmounts({
items: considerationIncludingTips,
criterias: considerationCriteria,
timeBasedItemParams,
})[ethers.ZeroAddress]?.["0"] ?? 0n)
(summedConsideration[ethers.ZeroAddress]?.["0"] ?? 0n)

for (const [token, identifierToAmount] of Object.entries(
summedConsideration,
)) {
for (const [identifier, amount] of Object.entries(identifierToAmount)) {
cumulativeFulfillerAmounts[token] ??= {}
cumulativeFulfillerAmounts[token][identifier] =
(cumulativeFulfillerAmounts[token][identifier] ?? 0n) + amount
}
}

const insufficientApprovals = validateStandardFulfillBalancesAndApprovals(
{
Expand Down Expand Up @@ -764,8 +781,25 @@ export function fulfillAvailableOrders({

overrides = { ...overrides, value: totalNativeAmount }

// An approval is deduped to one action per token and operator, and with
// exactApproval that action approves `requiredApprovedAmount`. Left as the
// first order's share it under-approves the batch and the fulfillment reverts.
// Raise it to the batch total, never lower it: an order whose allowance
// already covers it contributes no entry here at all, so summing the entries
// that are present would still undercount.
const approvalsForBatch = totalInsufficientApprovals.map(approval => {
const cumulative =
cumulativeFulfillerAmounts[approval.token.toLowerCase()]?.[
approval.identifierOrCriteria
]
return cumulative !== undefined &&
cumulative > approval.requiredApprovedAmount
? { ...approval, requiredApprovedAmount: cumulative }
: approval
})

const approvalActions = getApprovalActions(
totalInsufficientApprovals,
approvalsForBatch,
exactApproval,
signer,
)
Expand Down
78 changes: 78 additions & 0 deletions test/fulfill-orders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,84 @@ describeWithFixture(
await secondOfferer.getAddress(),
)
})

it("approves the batch total for one ERC20, not the first order's share", async () => {
const { seaport, testErc721, testErc20 } = fixture

// Two listings priced in the same ERC20, 10 and 20, so the batch needs 30.
await testErc721.mint(await offerer.getAddress(), nftId)
await testErc721.mint(await secondOfferer.getAddress(), nftId2)
await testErc20.mint(
await fulfiller.getAddress(),
parseEther("30").toString(),
)

const listing = async (
seller: HardhatEthersSigner,
identifier: string,
price: string,
) =>
(
await seaport.createOrder(
{
offer: [
{
itemType: ItemType.ERC721,
token: await testErc721.getAddress(),
identifier,
},
],
consideration: [
{
amount: parseEther(price).toString(),
token: await testErc20.getAddress(),
recipient: await seller.getAddress(),
},
],
},
await seller.getAddress(),
)
).executeAllActions()

const firstOrder = await listing(offerer, nftId, "10")
const secondOrder = await listing(secondOfferer, nftId2, "20")

const { actions } = await seaport.fulfillOrders({
fulfillOrderDetails: [{ order: firstOrder }, { order: secondOrder }],
accountAddress: await fulfiller.getAddress(),
exactApproval: true,
})

const erc20Address = await testErc20.getAddress()
const erc20Approvals = actions.filter(
(action): action is ApprovalAction =>
action.type === "approval" && action.token === erc20Address,
)
// One allowance is shared by the whole batch, so one action is correct.
expect(erc20Approvals.length).to.equal(1)

for (const action of actions.filter(a => a.type === "approval")) {
await action.transactionMethods.transact()
}

// The allowance has to cover 10 + 20, not just the first order's 10.
// Read the spender off the action rather than recomputing the conduit.
expect(
await testErc20.allowance(
await fulfiller.getAddress(),
erc20Approvals[0].operator,
),
).to.equal(parseEther("30"))

await actions[actions.length - 1].transactionMethods.transact()

expect(await testErc721.ownerOf(nftId)).to.equal(
await fulfiller.getAddress(),
)
expect(await testErc721.ownerOf(nftId2)).to.equal(
await fulfiller.getAddress(),
)
})
})
// TODO
describe("Special use cases", () => {
Expand Down