Skip to content
Merged
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Many of the main core flows return _use cases_. What this means is that if you w
#### Listing an ERC-721 for 10 ETH and fulfilling it

```js
import { ItemType } from "@opensea/seaport-js";

const offerer = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
const fulfiller = "0x70997970c51812dc3a010c7d01b50e0d17dc79c8";
const { executeAllActions } = await seaport.createOrder(
Expand Down Expand Up @@ -126,13 +128,15 @@ const transaction = await executeAllFulfillActions();
#### Making an offer for an ERC-721 for 10 WETH and fulfilling it

```js
import { ItemType } from "@opensea/seaport-js";

const offerer = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
const fulfiller = "0x70997970c51812dc3a010c7d01b50e0d17dc79c8";
const { executeAllActions } = await seaport.createOrder(
{
offer: [
{
amount: parseEther("10").toString(),
amount: ethers.parseEther("10").toString(),
// WETH
token: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
},
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ItemType } from "./constants"
import { Seaport } from "./seaport"
import { getMaximumSizeForOrder } from "./utils/item"

export { Seaport }
export { getMaximumSizeForOrder, ItemType, Seaport }
69 changes: 48 additions & 21 deletions src/seaport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ export class Seaport {
this.config = {
ascendingAmountFulfillmentBuffer,
balanceAndApprovalChecksOnOrderCreation,
conduitKeyToConduit: {
...KNOWN_CONDUIT_KEYS_TO_CONDUIT,
[NO_CONDUIT]: seaportContractAddress,
...conduitKeyToConduit,
},
// Keyed lower case so a conduit key still resolves when it arrives in a
// different hex case; see _getConduit.
conduitKeyToConduit: Object.fromEntries(
Object.entries({
...KNOWN_CONDUIT_KEYS_TO_CONDUIT,
[NO_CONDUIT]: seaportContractAddress,
...conduitKeyToConduit,
}).map(([key, conduit]) => [key.toLowerCase(), conduit]),
),
seaportVersion,
}

Expand Down Expand Up @@ -353,7 +357,7 @@ export class Seaport {

const totalCurrencyAmount = totalItemsAmount(currencies)

const operator = this.config.conduitKeyToConduit[conduitKey]
const operator = this._getConduit(conduitKey)

const orderType = this._getOrderTypeFromOrderOptions({
allowPartialFills,
Expand All @@ -363,14 +367,24 @@ export class Seaport {
const considerationItemsWithFees = [
...deductFees(considerationItems, fees),
...(currencies.length
? (fees?.map(fee =>
feeToConsiderationItem({
fee,
token: currencies[0].token,
baseAmount: totalCurrencyAmount.startAmount,
baseEndAmount: totalCurrencyAmount.endAmount,
}),
) ?? [])
? (fees
?.map(fee =>
feeToConsiderationItem({
fee,
token: currencies[0].token,
baseAmount: totalCurrencyAmount.startAmount,
baseEndAmount: totalCurrencyAmount.endAmount,
}),
)
// A fee small enough to floor to zero on both ends carries nothing,
// and Seaport rejects a zero-amount item with MissingItemAmount, so
// including it would leave the order permanently unfulfillable. An
// item that is only zero at one end still pays over the rest of its
// range and is kept.
.filter(
({ startAmount, endAmount }) =>
BigInt(startAmount) > 0n || BigInt(endAmount) > 0n,
) ?? [])
: []),
]

Expand Down Expand Up @@ -424,6 +438,19 @@ export class Seaport {
return { orderComponents, approvalActions }
}

/**
* Resolves the operator to source approvals from for a conduit key.
*
* A conduit key is a bytes32, so the same key can be written in any hex
* case and still be the same value on chain. Looking it up as a raw string
* would miss on a key that only differs in case, leaving the operator
* undefined and failing later with an error that names neither the conduit
* nor the key.
*/
private _getConduit(conduitKey: string): string {
return this.config.conduitKeyToConduit[conduitKey.toLowerCase()]
}

private async _getSigner(
accountAddress?: string,
): Promise<Signer | JsonRpcSigner> {
Expand Down Expand Up @@ -834,10 +861,9 @@ export class Seaport {

const fulfillerAddress = await fulfiller.getAddress()

const offererOperator =
this.config.conduitKeyToConduit[orderParameters.conduitKey]
const offererOperator = this._getConduit(orderParameters.conduitKey)

const fulfillerOperator = this.config.conduitKeyToConduit[conduitKey]
const fulfillerOperator = this._getConduit(conduitKey)

const tipConsiderationItems = tips.map(tip => ({
...mapInputItemToOfferItem(tip),
Expand Down Expand Up @@ -946,6 +972,7 @@ export class Seaport {
offererOperator,
fulfillerOperator,
recipientAddress,
fulfillerAddress,
domain,
overrides,
},
Expand Down Expand Up @@ -1001,12 +1028,11 @@ export class Seaport {

const fulfillerAddress = await fulfiller.getAddress()

const allOffererOperators = fulfillOrderDetails.map(
({ order }) =>
this.config.conduitKeyToConduit[order.parameters.conduitKey],
const allOffererOperators = fulfillOrderDetails.map(({ order }) =>
this._getConduit(order.parameters.conduitKey),
)

const fulfillerOperator = this.config.conduitKeyToConduit[conduitKey]
const fulfillerOperator = this._getConduit(conduitKey)

const allOfferItems = fulfillOrderDetails.flatMap(
({ order }) => order.parameters.offer,
Expand Down Expand Up @@ -1102,6 +1128,7 @@ export class Seaport {
signer: fulfiller,
conduitKey,
recipientAddress,
fulfillerAddress,
domain,
exactApproval,
overrides,
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BigNumberish, ContractTransaction } from "ethers"
import type { BigNumberish, TransactionResponse } from "ethers"
import type { ItemType, OrderType } from "./constants"
import type { TestERC20, TestERC721 } from "./typechain-types/index"
import type { Seaport as SeaportContract } from "./typechain-types/seaport/contracts/Seaport"
Expand Down Expand Up @@ -226,7 +226,9 @@ export type OrderUseCase<
? OrderWithCounter
: T extends CreateBulkOrdersAction
? OrderWithCounter[]
: ContractTransaction
: // An exchange use case ends by sending the transaction, so this
// resolves to what transact() hands back, receipt and all.
TransactionResponse
>
/**
* Performs the approval transactions only, skipping the final action. Use
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
Loading