diff --git a/src/seaport.ts b/src/seaport.ts index 4819a848..c7bc6e77 100644 --- a/src/seaport.ts +++ b/src/seaport.ts @@ -946,6 +946,7 @@ export class Seaport { offererOperator, fulfillerOperator, recipientAddress, + fulfillerAddress, domain, overrides, }, @@ -1102,6 +1103,7 @@ export class Seaport { signer: fulfiller, conduitKey, recipientAddress, + fulfillerAddress, domain, exactApproval, overrides, diff --git a/src/utils/balanceAndApprovalCheck.ts b/src/utils/balanceAndApprovalCheck.ts index 3b503f79..31ba1bc4 100644 --- a/src/utils/balanceAndApprovalCheck.ts +++ b/src/utils/balanceAndApprovalCheck.ts @@ -342,6 +342,7 @@ export const validateStandardFulfillBalancesAndApprovals = ({ timeBasedItemParams, offererOperator, fulfillerOperator, + offerItemsGoToFulfiller = true, }: Pick & { offerCriteria: InputCriteria[] considerationCriteria: InputCriteria[] @@ -350,6 +351,7 @@ export const validateStandardFulfillBalancesAndApprovals = ({ timeBasedItemParams: TimeBasedItemParams offererOperator: string fulfillerOperator: string + offerItemsGoToFulfiller?: boolean }) => { validateOfferBalancesAndApprovals({ offer, @@ -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({ diff --git a/src/utils/fulfill.ts b/src/utils/fulfill.ts index f15d251e..52c155c9 100644 --- a/src/utils/fulfill.ts +++ b/src/utils/fulfill.ts @@ -346,6 +346,7 @@ export function fulfillStandardOrder( timeBasedItemParams, conduitKey, recipientAddress, + fulfillerAddress, signer, domain, overrides, @@ -365,6 +366,7 @@ export function fulfillStandardOrder( fulfillerOperator: string conduitKey: string recipientAddress: string + fulfillerAddress: string timeBasedItemParams: TimeBasedItemParams signer: Signer domain?: string @@ -462,6 +464,10 @@ export function fulfillStandardOrder( timeBasedItemParams, offererOperator, fulfillerOperator, + offerItemsGoToFulfiller: offerItemsLandWithFulfiller( + recipientAddress, + fulfillerAddress, + ), }) overrides = { ...overrides, value: totalNativeAmount } @@ -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, @@ -580,6 +603,7 @@ export function fulfillAvailableOrders({ conduitKey, signer, recipientAddress, + fulfillerAddress, exactApproval, domain, overrides, @@ -593,6 +617,7 @@ export function fulfillAvailableOrders({ conduitKey: string signer: Signer recipientAddress: string + fulfillerAddress: string exactApproval: boolean domain?: string overrides?: Overrides @@ -738,6 +763,10 @@ export function fulfillAvailableOrders({ timeBasedItemParams, offererOperator, fulfillerOperator, + offerItemsGoToFulfiller: offerItemsLandWithFulfiller( + recipientAddress, + fulfillerAddress, + ), }, ) diff --git a/test/gifting.spec.ts b/test/gifting.spec.ts index 6166d70e..e4e472b2 100644 --- a/test/gifting.spec.ts +++ b/test/gifting.spec.ts @@ -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> + + 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"), + ) + }) + }) }, )