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
28 changes: 25 additions & 3 deletions src/utils/fulfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,22 @@ export function isOrderFulfillable({
return !isCancelled && !(totalSize > 0n && totalFilled / totalSize === 1n)
}

/**
* Whether an order's endTime has already passed at `currentBlockTimestamp`.
*
* Seaport only accepts an order while `startTime <= block.timestamp < endTime`,
* and `fulfillAvailableAdvancedOrders` skips one outside that window just as it
* skips a cancelled one. Block time only moves forward, so an order that has
* expired at the latest block is still expired when the fulfillment lands. An
* order that has not started yet is left alone: it can become active by then.
*/
export function hasOrderExpired(
{ parameters: { endTime } }: Order,
currentBlockTimestamp: number,
): boolean {
return BigInt(endTime) <= BigInt(currentBlockTimestamp)
}

export type FulfillOrdersMetadata = {
order: Order
unitsToFill?: BigNumberish
Expand Down Expand Up @@ -672,9 +688,15 @@ export function fulfillAvailableOrders({

// Drop the orders that cannot be filled instead of throwing on the first one.
// Every array built below is indexed by position in this list, so the
// fulfillments and criteria resolvers are derived from it too.
// fulfillments and criteria resolvers are derived from it too. An expired
// order is skipped on chain too, so keeping it would only check its offerer's
// balance and charge the fulfiller for an order that will never settle.
const sanitizedOrdersMetadata = ordersMetadata
.filter(({ orderStatus }) => isOrderFulfillable(orderStatus))
.filter(
({ order, orderStatus }) =>
isOrderFulfillable(orderStatus) &&
!hasOrderExpired(order, currentBlockTimestamp),
)
.map(orderMetadata => ({
...orderMetadata,
unitsToFill: orderMetadata.unitsToFill
Expand All @@ -691,7 +713,7 @@ export function fulfillAvailableOrders({

if (sanitizedOrdersMetadata.length === 0) {
throw new Error(
"None of the orders can be fulfilled: every one is already filled or cancelled.",
"None of the orders can be fulfilled: every one is already filled, cancelled or expired.",
)
}

Expand Down
103 changes: 103 additions & 0 deletions test/fulfill-orders-availability.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,109 @@ describeWithFixture(
)
})

// Seaport skips an order whose endTime has passed, the same way it skips a
// cancelled one, so an expired order must not hold the rest of the batch
// to its offerer's balance or to the fulfiller paying for it.
const expiredListing = async (nftId: string, token?: string) => {
const { ethers, seaport } = fixture
const latest = await ethers.provider.getBlock("latest")
const endTime = latest!.timestamp + 1000

const order = await (
await seaport.createOrder({
...(await listingFor(nftId)),
endTime: endTime.toString(),
consideration: [
{
amount: price.toString(),
token,
recipient: await offerer.getAddress(),
},
],
})
).executeAllActions()

await ethers.provider.send("evm_setNextBlockTimestamp", [endTime + 1])
await ethers.provider.send("evm_mine", [])

return order
}

it("drops an expired order whose offerer no longer holds the item", async () => {
const { seaport, testErc721, ethers } = fixture
const expiredNftId = "3"
await testErc721.mint(await offerer.getAddress(), expiredNftId)

const expiredOrder = await expiredListing(expiredNftId)

// The listing lapsed, so the seller moved the token on.
const [, , , elsewhere] = await ethers.getSigners()
await testErc721
.connect(offerer)
.transferFrom(
await offerer.getAddress(),
await elsewhere.getAddress(),
expiredNftId,
)

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

const fulfillAction = actions[actions.length - 1]
const transaction =
await fulfillAction.transactionMethods.buildTransaction()
expect(transaction.value).to.eq(price)

await (await fulfillAction.transactionMethods.transact()).wait()

expect(await testErc721.ownerOf(secondNftId)).to.eq(
await fulfiller.getAddress(),
)
})

it("does not count an expired order against the fulfiller's balance", async () => {
const { seaport, testErc721, testErc20 } = fixture
const token = await testErc20.getAddress()
const expiredNftId = "3"
const liveNftId = "4"
await testErc721.mint(await offerer.getAddress(), expiredNftId)
await testErc721.mint(await offerer.getAddress(), liveNftId)

const expiredOrder = await expiredListing(expiredNftId, token)
const liveOrder = await (
await seaport.createOrder({
...(await listingFor(liveNftId)),
consideration: [
{
amount: price.toString(),
token,
recipient: await offerer.getAddress(),
},
],
})
).executeAllActions()

// Enough for the live order, not for both.
await testErc20.mint(await fulfiller.getAddress(), price)

const { executeAllActions } = await seaport.fulfillOrders({
fulfillOrderDetails: [{ order: expiredOrder }, { order: liveOrder }],
accountAddress: await fulfiller.getAddress(),
})

await (await executeAllActions()).wait()

expect(await testErc721.ownerOf(liveNftId)).to.eq(
await fulfiller.getAddress(),
)
expect(await testErc721.ownerOf(expiredNftId)).to.eq(
await offerer.getAddress(),
)
expect(await testErc20.balanceOf(await fulfiller.getAddress())).to.eq(0n)
})

it("still reports when nothing in the batch can be filled", async () => {
const { seaport } = fixture

Expand Down
28 changes: 28 additions & 0 deletions test/fulfill-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { FulfillOrdersMetadata } from "../src/utils/fulfill"
import {
generateFulfillOrdersFulfillments,
hasExtraData,
hasOrderExpired,
isOrderFulfillable,
offerItemsLandWithFulfiller,
shouldUseBasicFulfill,
Expand Down Expand Up @@ -453,3 +454,30 @@ describe("isOrderFulfillable", () => {
).to.equal(true)
})
})

// Seaport accepts an order while startTime <= block.timestamp < endTime, so an
// order is expired from the second its endTime is reached.
describe("hasOrderExpired", () => {
const orderEndingAt = (endTime: string): Order => {
const order = makeOrder({ offer: [erc721Item()] })
return { ...order, parameters: { ...order.parameters, endTime } }
}

it("is false before the endTime", () => {
expect(hasOrderExpired(orderEndingAt("1000"), 999)).to.equal(false)
})

it("is true at the endTime itself", () => {
expect(hasOrderExpired(orderEndingAt("1000"), 1000)).to.equal(true)
})

it("is true after the endTime", () => {
expect(hasOrderExpired(orderEndingAt("1000"), 1001)).to.equal(true)
})

it("is false for an order that has not started yet", () => {
const order = orderEndingAt("2000")
order.parameters.startTime = "1500"
expect(hasOrderExpired(order, 1000)).to.equal(false)
})
})
Loading