Skip to content
Projects

About

A TypeScript library to interface with the Seaport marketplace.

Developer Tools

How these counts are measured

Measured star growth

in 24h
0
in 7 days
+1
in 15 days
+1
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

OpenSea

Version npm Test CI Code Coverage License Docs Discussions

Seaport.js

Seaport is a marketplace protocol for safely and efficiently buying and selling NFTs. This is a TypeScript library intended to make interfacing with the contract reasonable and easy.

Synopsis

This is a TypeScript library to help interface with Seaport. It includes various helper methods and constants that makes interfacing with Seaport easier. These include creating orders, fulfilling orders, doing the necessary balance and approval checks, and more. We recommend taking a look at the Seaport docs to get a better understanding of how the Seaport marketplace works.

Seaport.js talks to the Seaport contracts directly and knows nothing about OpenSea. To list, buy, or make offers on OpenSea, use the OpenSea SDK (@opensea/sdk) instead. It builds on this library and also handles the OpenSea API, including posting orders and fetching listings and offers.

Installation

Seaport.js runs on viem 2.24 or newer, which is a peer dependency. Node.js 22 or newer is required.

npm install @opensea/seaport-js viem

The package ships ESM and CommonJS builds.

Getting started

Create a Seaport instance from a viem public client, plus a wallet client when you need to sign orders or send transactions:

import { Seaport } from "@opensea/seaport-js"
import { createPublicClient, createWalletClient, http } from "viem"
import { privateKeyToAccount } from "viem/accounts"
import { mainnet } from "viem/chains"

const transport = http("https://<network>.g.alchemy.com/v2/YOUR-API-KEY")

const seaport = new Seaport({
  publicClient: createPublicClient({ chain: mainnet, transport }),
  walletClient: createWalletClient({
    chain: mainnet,
    transport,
    account: privateKeyToAccount(PRIVATE_KEY),
  }),
})

The public client does every read: balances, approvals, counters, order status, simulations and gas estimates. When its chain has a Multicall3 address (most chains in viem/chains have one), each create or fulfill call sends its contract reads as one Multicall3 call; otherwise they go out in parallel. To also batch the remaining JSON-RPC calls (the native balance, the block, the chain id), create the transport with http(url, { batch: true }). The wallet client signs orders and sends transactions; without one, you can still build, hash and check orders. Methods that sign or send act as the wallet client's account unless you pass account (an address or a viem Account).

To connect through a browser wallet, see Examples. The public methods and their parameters are documented in src/seaport.ts.

Values

Amounts, token identifiers, times, salts and counters are bigint. Addresses are viem Address values and byte strings (zoneHash, conduitKey, signatures, proofs) are Hex. Orders that seaport-js returns use the same types, so convert them before JSON.stringify with orderToJson, and read an order from JSON (for example an API response) with orderFromJson.

Configuration

Besides publicClient and walletClient, the constructor takes optional settings. Every setting has a default, so you only pass the ones you want to change. examples/configuration.ts shows each of them in use.

  • ascendingAmountFulfillmentBuffer (default 300) is the number of seconds added to the current block time when computing how much to pay for an item whose amount rises over time, so the fill still covers it if the transaction confirms later.
  • balanceAndApprovalChecksOnOrderCreation (default true) turns the offerer's balance and approval checks in createOrder and createBulkOrders on or off. When false, both skip the checks and return no approval actions.
  • conduitKeyToConduit (default: the OpenSea conduit only) adds mappings from conduit key to conduit address. Add one for each custom conduit you use, so approvals are checked against the right address.
  • seaportAddress (default 0x0000000000000068F116a894984e2DB1123eB395) is the Seaport contract address. The default is Seaport 1.6, deployed at the same address on every supported chain.
  • seaportVersion (default "1.6") is the Seaport version in the EIP-712 domain used for signing. Set it to match seaportAddress.
  • domainRegistryAddress (default 0x000000000DaD0DE04D2B2D4a5A74581EBA94124A) is the domain registry contract address.
  • defaultConduitKey (default: the zero hash, meaning no conduit) is the conduit key used when you don't pass conduitKey to createOrder, createBulkOrders, fulfillOrder or fulfillOrders. With no conduit, approvals go to the Seaport contract itself.

Use cases

Most of the main flows return a use case. When you create an order with createOrder, the library checks the offerer's balances and approvals for the order's offer. If the offerer still needs to approve an asset contract, the use case's actions include an approval action to run first, so the trade can succeed later. executeAllActions runs every action in order, waiting for each approval to be mined, and returns the result of the last one: the signed order for a create, or the transaction hash for a fulfillment.

const hash = await (await seaport.fulfillOrder({ order })).executeAllActions()
const receipt = await seaport.publicClient.waitForTransactionReceipt({ hash })

Both take optional { confirmations, timeout } for the approval receipts.

executeAllActions sends the approvals and the fulfillment without asking again, for whatever the order says. When an order comes from an API or another party, show its offer, consideration and recipients to the user (or check them in code) before calling it. orderFromJson checks that an order is well-formed, not that its terms are what you expect.

To create an order the offerer approves onchain instead of by signature, as a smart contract wallet may have to, run executeApprovals() and pass the create action's orderComponents to validate(). Neither asks the wallet for a signature.

Each transaction action has transactionMethods with transact() (send, resolves to the hash), simulate(), estimateGas() and buildTransaction(). A Seaport revert is thrown as viem's ContractFunctionExecutionError, whose ContractFunctionRevertedError cause names the Seaport error. When one of the library's own checks fails first (an unknown conduit, a missing balance, an order already filled), it throws a SeaportError with a code to branch on.

Exports

  • @opensea/seaport-js has Seaport, the types, the constants (ItemType, OrderType, Seaport and conduit addresses, EIP_712_ORDER_TYPE) and helpers such as getOrderHash, getMaximumSizeForOrder, generateRandomSalt, MerkleTree, orderToJson and orderFromJson, and SeaportError.
  • @opensea/seaport-js/abi has seaportAbi, conduitControllerAbi, conduitAbi, domainRegistryAbi, erc20Abi, erc721Abi and erc1155Abi, typed as const for use with viem.

Examples

Each example is a TypeScript file in examples/ that CI type-checks against the library source, so it stays in step with the API.

Run npm run typecheck:examples to check them locally.

Contributing

See the contributing guide for detailed instructions on how to get started with this project.

Security

Found a vulnerability? Report it through OpenSea's Bugcrowd program at https://bugcrowd.com/engagements/opensea rather than opening a public issue. See SECURITY.md.

License

MIT Copyright 2022 Ozone Networks, Inc.

Explore related projects

Loading…