Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow fulfillOrder to take an Order object from api/v2 response #1320

Merged
merged 1 commit into from
Dec 14, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opensea-js",
"version": "7.0.0",
"version": "7.0.1",
"description": "TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data",
"license": "MIT",
"author": "OpenSea Developers",
Expand Down
30 changes: 24 additions & 6 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,28 @@ type PartialParameters = {
zoneHash: string;
};

/**
* Criteria for collection or trait offers.
* @category API Response Types
*/
type Criteria = {
/** The collection for the criteria */
collection: CollectionCriteria;
contract?: ContractCriteria;
/** The contract for the criteria */
contract: ContractCriteria;
/** Represents a list of token ids which can be used to fulfill the criteria offer. */
encoded_token_ids?: string;
/** The trait for the criteria */
trait?: TraitCriteria;
};

/**
* Criteria for trait offers.
* @category API Response Types
*/
type TraitCriteria = {
type: string;
value: string;
};

type CollectionCriteria = {
Expand Down Expand Up @@ -66,16 +84,16 @@ export type Order = {
* Offer type.
* @category API Models
*/
export type Offer = Order;
export type Offer = Order & {
/** The criteria for the offer if it is a collection or trait offer. */
criteria?: Criteria;
};

/**
* Collection Offer type.
* @category API Models
*/
export type CollectionOffer = Offer & {
/** Defines which NFTs meet the criteria to fulfill the offer. */
criteria: Criteria;
};
export type CollectionOffer = Required<Pick<Offer, "criteria">> & Offer;

/**
* Price response.
Expand Down
8 changes: 6 additions & 2 deletions src/orders/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export type OrderProtocol = keyof OrderProtocolToProtocolData;
export type ProtocolData =
OrderProtocolToProtocolData[keyof OrderProtocolToProtocolData];

// Protocol agnostic order data
export type OrderType = "basic" | "english" | "criteria";
export enum OrderType {
BASIC = "basic",
ENGLISH = "english",
CRITERIA = "criteria",
}

type OrderFee = {
account: OpenSeaAccount;
basisPoints: string;
Expand Down
41 changes: 27 additions & 14 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
JsonRpcProvider,
} from "ethers";
import { OpenSeaAPI } from "./api/api";
import { CollectionOffer, NFT } from "./api/types";
import { CollectionOffer, Listing, NFT, Order } from "./api/types";
import {
INVERSE_BASIS_POINT,
DEFAULT_ZONE,
Expand All @@ -30,7 +30,7 @@ import {
getPrivateListingConsiderations,
getPrivateListingFulfillments,
} from "./orders/privateListings";
import { OrderV2 } from "./orders/types";
import { OrderType, OrderV2 } from "./orders/types";
import { DEFAULT_SEAPORT_CONTRACT_ADDRESS } from "./orders/utils";
import {
ERC1155__factory,
Expand Down Expand Up @@ -686,23 +686,38 @@ export class OpenSeaSDK {
domain,
overrides,
}: {
order: OrderV2;
order: OrderV2 | Order;
accountAddress: string;
recipientAddress?: string;
domain?: string;
overrides?: Overrides;
}): Promise<string> {
await this._requireAccountIsAvailable(accountAddress);
requireValidProtocol(order.protocolAddress);

const protocolAddress =
(order as OrderV2).protocolAddress ?? (order as Order).protocol_address;
requireValidProtocol(protocolAddress);

const orderHash =
(order as OrderV2).orderHash ?? (order as Order).order_hash;

const side =
(order as OrderV2).side ??
([OrderType.BASIC, OrderType.ENGLISH].includes((order as Listing).type)
? OrderSide.ASK
: OrderSide.BID);

let extraData: string | undefined = undefined;

if (order.orderHash) {
const protocolData =
(order as OrderV2).protocolData ?? (order as Order).protocol_data;

if (orderHash) {
const result = await this.api.generateFulfillmentData(
accountAddress,
order.orderHash,
order.protocolAddress,
order.side,
orderHash,
protocolAddress,
side,
);

// If the order is using offer protection, the extraData
Expand All @@ -711,29 +726,27 @@ export class OpenSeaSDK {
if ("orders" in inputData && "extraData" in inputData.orders[0]) {
extraData = (inputData.orders[0] as AdvancedOrder).extraData;
}

const signature = result.fulfillment_data.orders[0].signature;
order.clientSignature = signature;
order.protocolData.signature = signature;
protocolData.signature = signature;
}

const isPrivateListing = !!order.taker;
const isPrivateListing = "taker" in order ? !!order.taker : false;
if (isPrivateListing) {
if (recipientAddress) {
throw new Error(
"Private listings cannot be fulfilled with a recipient address",
);
}
return this.fulfillPrivateOrder({
order,
order: order as OrderV2,
accountAddress,
domain,
overrides,
});
}

const { executeAllActions } = await this.seaport_v1_5.fulfillOrder({
order: order.protocolData,
order: protocolData,
accountAddress,
recipientAddress,
extraData,
Expand Down