Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/seaport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ export class Seaport {
offererOperator,
fulfillerOperator,
recipientAddress,
fulfillerAddress,
domain,
overrides,
},
Expand Down Expand Up @@ -1102,6 +1103,7 @@ export class Seaport {
signer: fulfiller,
conduitKey,
recipientAddress,
fulfillerAddress,
domain,
exactApproval,
overrides,
Expand Down
21 changes: 15 additions & 6 deletions src/utils/balanceAndApprovalCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export const validateStandardFulfillBalancesAndApprovals = ({
timeBasedItemParams,
offererOperator,
fulfillerOperator,
offerItemsGoToFulfiller = true,
}: Pick<OrderParameters, "offer" | "consideration"> & {
offerCriteria: InputCriteria[]
considerationCriteria: InputCriteria[]
Expand All @@ -350,6 +351,7 @@ export const validateStandardFulfillBalancesAndApprovals = ({
timeBasedItemParams: TimeBasedItemParams
offererOperator: string
fulfillerOperator: string
offerItemsGoToFulfiller?: boolean
}) => {
validateOfferBalancesAndApprovals({
offer,
Expand All @@ -360,13 +362,20 @@ export const validateStandardFulfillBalancesAndApprovals = ({
operator: offererOperator,
})

// Requirement 2 above only holds while the offered items land with the
// fulfiller. When they are forwarded to someone else, the fulfiller never
// receives them and has to cover every consideration item out of what they
// already hold, so crediting the offer here would clear a fulfillment that
// reverts on transfer.
const fulfillerBalancesAndApprovalsAfterReceivingOfferedItems =
addToExistingBalances({
items: offer,
criterias: offerCriteria,
balancesAndApprovals: fulfillerBalancesAndApprovals,
timeBasedItemParams,
})
offerItemsGoToFulfiller
? addToExistingBalances({
items: offer,
criterias: offerCriteria,
balancesAndApprovals: fulfillerBalancesAndApprovals,
timeBasedItemParams,
})
: fulfillerBalancesAndApprovals

const { insufficientBalances, insufficientApprovals } =
getInsufficientBalanceAndApprovalAmounts({
Expand Down
29 changes: 29 additions & 0 deletions src/utils/fulfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ export function fulfillStandardOrder(
timeBasedItemParams,
conduitKey,
recipientAddress,
fulfillerAddress,
signer,
domain,
overrides,
Expand All @@ -365,6 +366,7 @@ export function fulfillStandardOrder(
fulfillerOperator: string
conduitKey: string
recipientAddress: string
fulfillerAddress: string
timeBasedItemParams: TimeBasedItemParams
signer: Signer
domain?: string
Expand Down Expand Up @@ -462,6 +464,10 @@ export function fulfillStandardOrder(
timeBasedItemParams,
offererOperator,
fulfillerOperator,
offerItemsGoToFulfiller: offerItemsLandWithFulfiller(
recipientAddress,
fulfillerAddress,
),
})

overrides = { ...overrides, value: totalNativeAmount }
Expand Down Expand Up @@ -536,6 +542,23 @@ export function fulfillStandardOrder(
}
}

/**
* Whether the order's offered items end up with the fulfiller.
*
* `recipientAddress` forwards them somewhere else, with the zero address
* meaning "leave them with the fulfiller". Naming the fulfiller explicitly is
* the same thing, so it counts as landing with them too.
*/
export function offerItemsLandWithFulfiller(
recipientAddress: string,
fulfillerAddress: string,
): boolean {
return (
recipientAddress === ethers.ZeroAddress ||
recipientAddress.toLowerCase() === fulfillerAddress.toLowerCase()
)
}

export function validateAndSanitizeFromOrderStatus(
order: Order,
orderStatus: OrderStatus,
Expand Down Expand Up @@ -580,6 +603,7 @@ export function fulfillAvailableOrders({
conduitKey,
signer,
recipientAddress,
fulfillerAddress,
exactApproval,
domain,
overrides,
Expand All @@ -593,6 +617,7 @@ export function fulfillAvailableOrders({
conduitKey: string
signer: Signer
recipientAddress: string
fulfillerAddress: string
exactApproval: boolean
domain?: string
overrides?: Overrides
Expand Down Expand Up @@ -738,6 +763,10 @@ export function fulfillAvailableOrders({
timeBasedItemParams,
offererOperator,
fulfillerOperator,
offerItemsGoToFulfiller: offerItemsLandWithFulfiller(
recipientAddress,
fulfillerAddress,
),
},
)

Expand Down
95 changes: 95 additions & 0 deletions test/gifting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,100 @@ describeWithFixture(
})
})
})

// Accepting a bid pays the fulfiller in the bid currency, and the fee on
// that bid is a consideration item the fulfiller settles. Those two net out
// while the fulfiller keeps the proceeds. Forward them to someone else and
// the fee has to come from the fulfiller's own balance instead.
describe("[Accept offer] I want to send the proceeds to another address", () => {
const bidNftId = "77"
const bidAmount = parseEther("10")
let bid: Awaited<ReturnType<typeof buildBid>>

const buildBid = async () => {
const { seaport, testErc20, testErc721 } = fixture

const input: CreateOrderInput = {
startTime: "0",
offer: [
{
token: await testErc20.getAddress(),
amount: bidAmount.toString(),
},
],
consideration: [
{
itemType: ItemType.ERC721,
token: await testErc721.getAddress(),
identifier: bidNftId,
recipient: await offerer.getAddress(),
},
],
fees: [{ recipient: await zone.getAddress(), basisPoints: 250 }],
}

return (
await seaport.createOrder(input, await offerer.getAddress())
).executeAllActions()
}

beforeEach(async () => {
const { testErc20, testErc721 } = fixture

// The fulfiller owns the NFT but holds none of the bid currency.
await testErc721.mint(await fulfiller.getAddress(), bidNftId)
await testErc20.mint(await offerer.getAddress(), bidAmount)

bid = await buildBid()
})

it("reports the shortfall when the proceeds go elsewhere", async () => {
const { seaport } = fixture

await expect(
seaport.fulfillOrder({
order: bid,
accountAddress: await fulfiller.getAddress(),
recipientAddress: await recipient.getAddress(),
}),
).to.be.rejectedWith("The fulfiller does not have the balances needed")
})

it("still lets the fee come out of the proceeds when they are kept", async () => {
const { seaport, testErc20 } = fixture

const { actions } = await seaport.fulfillOrder({
order: bid,
accountAddress: await fulfiller.getAddress(),
})

for (const action of actions) {
await (await action.transactionMethods.transact()).wait()
}

// 10 received, 0.25 paid out as the fee.
expect(await testErc20.balanceOf(await fulfiller.getAddress())).to.eq(
parseEther("9.75"),
)
})

it("treats naming yourself as the recipient the same as keeping them", async () => {
const { seaport, testErc20 } = fixture

const { actions } = await seaport.fulfillOrder({
order: bid,
accountAddress: await fulfiller.getAddress(),
recipientAddress: await fulfiller.getAddress(),
})

for (const action of actions) {
await (await action.transactionMethods.transact()).wait()
}

expect(await testErc20.balanceOf(await fulfiller.getAddress())).to.eq(
parseEther("9.75"),
)
})
})
},
)