opensea-js
Version:
TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data
685 lines • 35.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenSeaSDK = void 0;
const EventEmitter = require("events");
const seaport_js_1 = require("@opensea/seaport-js");
const ethers_1 = require("ethers");
const api_1 = require("./api/api");
const assets_1 = require("./sdk/assets");
const cancellation_1 = require("./sdk/cancellation");
const fulfillment_1 = require("./sdk/fulfillment");
const orders_1 = require("./sdk/orders");
const tokens_1 = require("./sdk/tokens");
const types_1 = require("./types");
const utils_1 = require("./utils/utils");
/**
* The OpenSea SDK main class.
* @category Main Classes
*/
class OpenSeaSDK {
/**
* Create a new instance of OpenSeaSDK.
* @param signerOrProvider Signer or provider to use for transactions. For example:
* `new ethers.providers.JsonRpcProvider('https://mainnet.infura.io')` or
* `new ethers.Wallet(privKey, provider)`
* @param apiConfig configuration options, including `chain`
* @param logger optional function for logging debug strings. defaults to no logging
*/
constructor(signerOrProvider, apiConfig = {}, logger) {
/** Internal cache of decimals for payment tokens to save network requests */
this._cachedPaymentTokenDecimals = {};
// API config
apiConfig.chain ?? (apiConfig.chain = types_1.Chain.Mainnet);
this.chain = apiConfig.chain;
this.api = new api_1.OpenSeaAPI(apiConfig);
this.provider = (signerOrProvider.provider ??
signerOrProvider);
this._signerOrProvider = signerOrProvider ?? this.provider;
const defaultConduit = (0, utils_1.getDefaultConduit)(this.chain);
const seaportAddress = (0, utils_1.getSeaportAddress)(this.chain);
this.seaport = new seaport_js_1.Seaport(this._signerOrProvider, {
conduitKeyToConduit: {
[defaultConduit.key]: defaultConduit.address,
},
overrides: {
defaultConduitKey: defaultConduit.key,
contractAddress: seaportAddress,
},
});
// Emit events
this._emitter = new EventEmitter();
// Logger: default to no logging if fn not provided
this.logger = logger ?? ((arg) => arg);
// Cache decimals for offer and listing payment tokens to skip network request
const offerPaymentToken = (0, utils_1.getOfferPaymentToken)(this.chain).toLowerCase();
const listingPaymentToken = (0, utils_1.getListingPaymentToken)(this.chain).toLowerCase();
this._cachedPaymentTokenDecimals[offerPaymentToken] = 18;
this._cachedPaymentTokenDecimals[listingPaymentToken] = 18;
// Create shared context for all managers
const context = {
chain: this.chain,
signerOrProvider: this._signerOrProvider,
provider: this.provider,
api: this.api,
seaport: this.seaport,
logger: this.logger,
dispatch: this._dispatch.bind(this),
confirmTransaction: this._confirmTransaction.bind(this),
requireAccountIsAvailable: this._requireAccountIsAvailable.bind(this),
};
// Initialize manager instances
this._tokensManager = new tokens_1.TokensManager(context);
this._assetsManager = new assets_1.AssetsManager(context);
this._cancellationManager = new cancellation_1.CancellationManager(context);
this._ordersManager = new orders_1.OrdersManager(context, this._getPriceParameters.bind(this));
this._fulfillmentManager = new fulfillment_1.FulfillmentManager(context, this._ordersManager);
}
/**
* Add a listener for events emitted by the SDK.
* @param event The {@link EventType} to listen to.
* @param listener A callback that will accept an object with {@link EventData}\
* @param once Whether the listener should only be called once, or continue listening until removed.
*/
addListener(event, listener, once = false) {
if (once) {
this._emitter.once(event, listener);
}
else {
this._emitter.addListener(event, listener);
}
}
/**
* Remove an event listener by calling `.removeListener()` on an event and listener.
* @param event The {@link EventType} to remove a listener for\
* @param listener The listener to remove
*/
removeListener(event, listener) {
this._emitter.removeListener(event, listener);
}
/**
* Remove all event listeners. This should be called when you're unmounting
* a component that listens to events to make UI updates.
* @param event Optional EventType to remove listeners for
*/
removeAllListeners(event) {
this._emitter.removeAllListeners(event);
}
/**
* Wrap native asset into wrapped native asset (e.g. ETH into WETH, POL into WPOL).
* Wrapped native assets are needed for making offers.
* @param options
* @param options.amountInEth Amount of native asset to wrap
* @param options.accountAddress Address of the user's wallet containing the native asset
*/
async wrapEth({ amountInEth, accountAddress, }) {
return this._tokensManager.wrapEth({ amountInEth, accountAddress });
}
/**
* Unwrap wrapped native asset into native asset (e.g. WETH into ETH, WPOL into POL).
* Emits the `UnwrapWeth` event when the transaction is prompted.
* @param options
* @param options.amountInEth How much wrapped native asset to unwrap
* @param options.accountAddress Address of the user's wallet containing the wrapped native asset
*/
async unwrapWeth({ amountInEth, accountAddress, }) {
return this._tokensManager.unwrapWeth({ amountInEth, accountAddress });
}
/**
* Create and submit an offer on an asset.
* @param options
* @param options.asset The asset to trade. tokenAddress and tokenId must be defined.
* @param options.accountAddress Address of the wallet making the offer.
* @param options.amount Amount in decimal format (e.g., "1.5" for 1.5 ETH, not wei). Automatically converted to base units.
* @param options.quantity Number of assets to bid for. Defaults to 1.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in salt.
* @param options.salt Arbitrary salt. Auto-generated if not provided.
* @param options.expirationTime Expiration time for the order, in UTC seconds
* @param options.paymentTokenAddress ERC20 address for the payment token in the order. If unspecified, defaults to WETH
* @param options.zone Zone for order protection. Defaults to chain's signed zone.
*
* @returns The {@link OrderV2} that was created.
*
* @throws Error if the asset does not contain a token id.
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if the amount is not greater than 0.
* @throws Error if paymentTokenAddress is not WETH on anything other than Ethereum mainnet.
*/
async createOffer({ asset, accountAddress, amount, quantity = 1, domain, salt, expirationTime, paymentTokenAddress, zone, }) {
return this._ordersManager.createOffer({
asset,
accountAddress,
amount,
quantity,
domain,
salt,
expirationTime,
paymentTokenAddress,
zone,
});
}
/**
* Create and submit a listing for an asset.
* @param options
* @param options.asset The asset to trade. tokenAddress and tokenId must be defined.
* @param options.accountAddress Address of the wallet making the listing
* @param options.amount Amount in decimal format (e.g., "1.5" for 1.5 ETH, not wei). Automatically converted to base units.
* @param options.quantity Number of assets to list. Defaults to 1.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in salt.
* @param options.salt Arbitrary salt. Auto-generated if not provided.
* @param options.listingTime Optional time when the order will become fulfillable, in UTC seconds. Undefined means it will start now.
* @param options.expirationTime Expiration time for the order, in UTC seconds.
* @param options.paymentTokenAddress ERC20 address for the payment token in the order. If unspecified, defaults to ETH
* @param options.buyerAddress Optional address that's allowed to purchase this item. If specified, no other address will be able to take the order, unless its value is the null address.
* @param options.includeOptionalCreatorFees If true, optional creator fees will be included in the listing. Default: false.
* @param options.zone Zone for order protection. Defaults to no zone.
* @returns The {@link OrderV2} that was created.
*
* @throws Error if the asset does not contain a token id.
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if the amount is not greater than 0.
* @throws Error if paymentTokenAddress is not WETH on anything other than Ethereum mainnet.
*/
async createListing({ asset, accountAddress, amount, quantity = 1, domain, salt, listingTime, expirationTime, paymentTokenAddress, buyerAddress, includeOptionalCreatorFees = false, zone, }) {
return this._ordersManager.createListing({
asset,
accountAddress,
amount,
quantity,
domain,
salt,
listingTime,
expirationTime,
paymentTokenAddress,
buyerAddress,
includeOptionalCreatorFees,
zone,
});
}
/**
* Create and submit multiple listings using Seaport's bulk order creation.
* This method uses a single signature for all listings and submits them individually to the OpenSea API with rate limit handling.
* All listings must be from the same account address.
*
* Note: If only one listing is provided, this method will use a normal order signature instead of a bulk signature,
* as bulk signatures are more expensive to decode on-chain due to the merkle proof verification.
*
* @param options
* @param options.listings Array of listing parameters. Each listing requires asset, amount, and optionally other listing parameters.
* @param options.accountAddress Address of the wallet making the listings
* @param options.continueOnError If true, continue submitting remaining listings even if some fail. Default: false (throw on first error).
* @param options.onProgress Optional callback for progress updates. Called after each listing is submitted (successfully or not).
* @returns {@link BulkOrderResult} containing successful orders and any failures.
*
* @throws Error if listings array is empty
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if any asset does not contain a token id.
* @throws Error if continueOnError is false and any submission fails.
*/
async createBulkListings({ listings, accountAddress, continueOnError, onProgress, }) {
return this._ordersManager.createBulkListings({
listings,
accountAddress,
continueOnError,
onProgress,
});
}
/**
* Create and submit multiple offers using Seaport's bulk order creation.
* This method uses a single signature for all offers and submits them individually to the OpenSea API with rate limit handling.
* All offers must be from the same account address.
*
* Note: If only one offer is provided, this method will use a normal order signature instead of a bulk signature,
* as bulk signatures are more expensive to decode on-chain due to the merkle proof verification.
*
* @param options
* @param options.offers Array of offer parameters. Each offer requires asset, amount, and optionally other offer parameters.
* @param options.accountAddress Address of the wallet making the offers
* @param options.continueOnError If true, continue submitting remaining offers even if some fail. Default: false (throw on first error).
* @param options.onProgress Optional callback for progress updates. Called after each offer is submitted (successfully or not).
* @returns {@link BulkOrderResult} containing successful orders and any failures.
*
* @throws Error if offers array is empty
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if any asset does not contain a token id.
* @throws Error if continueOnError is false and any submission fails.
*/
async createBulkOffers({ offers, accountAddress, continueOnError, onProgress, }) {
return this._ordersManager.createBulkOffers({
offers,
accountAddress,
continueOnError,
onProgress,
});
}
/**
* Create and submit a collection offer.
* @param options
* @param options.collectionSlug Identifier for the collection.
* @param options.accountAddress Address of the wallet making the offer.
* @param options.amount Amount in decimal format (e.g., "1.5" for 1.5 ETH, not wei). Automatically converted to base units.
* @param options.quantity Number of assets to bid for.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in salt.
* @param options.salt Arbitrary salt. Auto-generated if not provided.
* @param options.expirationTime Expiration time (UTC seconds).
* @param options.paymentTokenAddress Payment token address. Defaults to WETH.
* @param options.offerProtectionEnabled Use signed zone for protection against disabled items. Default: true.
* @param options.traitType If defined, the trait name to create the collection offer for.
* @param options.traitValue If defined, the trait value to create the collection offer for.
* @param options.traits If defined, an array of traits to create the multi-trait collection offer for.
* @returns The {@link CollectionOffer} that was created.
*/
async createCollectionOffer({ collectionSlug, accountAddress, amount, quantity, domain, salt, expirationTime, paymentTokenAddress, offerProtectionEnabled = true, traitType, traitValue, traits, }) {
return this._ordersManager.createCollectionOffer({
collectionSlug,
accountAddress,
amount,
quantity,
domain,
salt,
expirationTime,
paymentTokenAddress,
offerProtectionEnabled,
traitType,
traitValue,
traits,
});
}
/**
* Fulfill an order for an asset. The order can be either a listing or an offer.
* Uses the OpenSea API to generate fulfillment transaction data and executes it directly.
* @param options
* @param options.order The order to fulfill, a.k.a. "take"
* @param options.accountAddress Address of the wallet taking the offer.
* @param options.assetContractAddress Optional address of the NFT contract for criteria offers (e.g., collection offers). Required when fulfilling collection offers.
* @param options.tokenId Optional token ID for criteria offers (e.g., collection offers). Required when fulfilling collection offers.
* @param options.unitsToFill Optional number of units to fill. Defaults to 1 for both listings and offers.
* @param options.recipientAddress Optional recipient address for the NFT when fulfilling a listing. Not applicable for offers.
* @param options.includeOptionalCreatorFees Whether to include optional creator fees in the fulfillment. If creator fees are already required, this is a no-op. Defaults to false.
* @param options.overrides Transaction overrides, ignored if not set.
* @returns Transaction hash of the order.
*
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if the order's protocol address is not supported by OpenSea. See {@link isValidProtocol}.
* @throws Error if a signer is not provided (read-only providers cannot fulfill orders).
* @throws Error if the order hash is not available.
*/
async fulfillOrder({ order, accountAddress, assetContractAddress, tokenId, unitsToFill, recipientAddress, includeOptionalCreatorFees = false, overrides, }) {
return this._fulfillmentManager.fulfillOrder({
order,
accountAddress,
assetContractAddress,
tokenId,
unitsToFill,
recipientAddress,
includeOptionalCreatorFees,
overrides,
});
}
/**
* Cancel multiple orders onchain, preventing them from being fulfilled.
* This method accepts either full OrderV2 objects, OrderComponents, or order hashes with protocol address.
*
* **Event Behavior**: For backwards compatibility with the singular `cancelOrder` method,
* this method dispatches a `CancelOrder` event for the first order only, and only when
* an OrderV2 object is available (either provided directly or fetched via orderHashes).
* No event is dispatched when using OrderComponents directly, as they lack the full order data.
*
* @param options
* @param options.orders Array of orders to cancel. Can be OrderV2 objects or OrderComponents.
* @param options.orderHashes Optional array of order hashes to cancel. Must provide protocolAddress if using this.
* @param options.accountAddress The account address cancelling the orders.
* @param options.protocolAddress Required when using orderHashes. The Seaport protocol address for the orders.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in calldata.
* @param options.overrides Transaction overrides, ignored if not set.
* @returns Transaction hash of the cancellation.
*
* @throws Error if orderHashes is provided without protocolAddress.
* @throws Error if neither orders nor orderHashes is provided.
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if the order's protocol address is not supported by OpenSea. See {@link isValidProtocol}.
*/
async cancelOrders({ orders, orderHashes, accountAddress, protocolAddress, domain, overrides, }) {
return this._cancellationManager.cancelOrders({
orders,
orderHashes,
accountAddress,
protocolAddress,
domain,
overrides,
});
}
/**
* Cancel an order onchain, preventing it from ever being fulfilled.
* This method accepts either a full OrderV2 object or an order hash with protocol address.
*
* @param options
* @param options.order The order to cancel (OrderV2 object)
* @param options.orderHash Optional order hash to cancel. Must provide protocolAddress if using this.
* @param options.accountAddress The account address that will be cancelling the order.
* @param options.protocolAddress Required when using orderHash. The Seaport protocol address for the order.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in calldata.
*
* @throws Error if neither order nor orderHash is provided.
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if the order's protocol address is not supported by OpenSea. See {@link isValidProtocol}.
*
* @example
* // Cancel using OrderV2 object
* await sdk.cancelOrder({
* order: orderV2Object,
* accountAddress: "0x..."
* });
*
* @example
* // Cancel using order hash
* await sdk.cancelOrder({
* orderHash: "0x123...",
* protocolAddress: "0xabc...",
* accountAddress: "0x..."
* });
*/
async cancelOrder({ order, orderHash, accountAddress, protocolAddress, domain, }) {
return this._cancellationManager.cancelOrder({
order,
orderHash,
accountAddress,
protocolAddress,
domain,
});
}
/**
* Offchain cancel an order, offer or listing, by its order hash when protected by the SignedZone.
* Protocol and Chain are required to prevent hash collisions.
* Please note cancellation is only assured if a fulfillment signature was not vended prior to cancellation.
* @param protocolAddress The Seaport address for the order.
* @param orderHash The order hash, or external identifier, of the order.
* @param chain The chain where the order is located.
* @param offererSignature An EIP-712 signature from the offerer of the order.
* If this is not provided, the API key used to initialize the SDK must belong to the order's offerer.
* The signature must be a EIP-712 signature consisting of the order's Seaport contract's
* name, version, address, and chain. The struct to sign is `OrderHash` containing a
* single bytes32 field.
* @param useSignerToDeriveOffererSignature Derive the offererSignature from the Ethers signer passed into this sdk.
* @returns The response from the API.
*/
async offchainCancelOrder(protocolAddress, orderHash, chain = this.chain, offererSignature, useSignerToDeriveOffererSignature) {
return this._cancellationManager.offchainCancelOrder(protocolAddress, orderHash, chain, offererSignature, useSignerToDeriveOffererSignature);
}
/**
* Returns whether an order is fulfillable.
* An order may not be fulfillable if a target item's transfer function
* is locked for some reason, e.g. an item is being rented within a game
* or trading has been locked for an item type.
* @param options
* @param options.order Order to check
* @param options.accountAddress The account address that will be fulfilling the order
* @returns True if the order is fulfillable, else False.
*
* @throws Error if the order's protocol address is not supported by OpenSea. See {@link isValidProtocol}.
*/
async isOrderFulfillable({ order, accountAddress, }) {
return this._fulfillmentManager.isOrderFulfillable({
order,
accountAddress,
});
}
/**
* Get an account's balance of any Asset. This asset can be an ERC20, ERC1155, or ERC721.
* @param options
* @param options.accountAddress Account address to check
* @param options.asset The Asset to check balance for. tokenStandard must be set.
* @returns The balance of the asset for the account.
*
* @throws Error if the token standard does not support balanceOf.
*/
async getBalance({ accountAddress, asset, }) {
return this._assetsManager.getBalance({ accountAddress, asset });
}
/**
* Transfer an asset. This asset can be an ERC20, ERC1155, or ERC721.
* @param options
* @param options.asset The Asset to transfer. tokenStandard must be set.
* @param options.amount Amount of asset to transfer. Not used for ERC721.
* @param options.fromAddress The address to transfer from
* @param options.toAddress The address to transfer to
* @param options.overrides Transaction overrides, ignored if not set.
*/
async transfer({ asset, amount, fromAddress, toAddress, overrides, }) {
return this._assetsManager.transfer({
asset,
amount,
fromAddress,
toAddress,
overrides,
});
}
/**
* Bulk transfer multiple assets using OpenSea's TransferHelper contract.
* This method is more gas-efficient than calling transfer() multiple times.
* Note: All assets must be approved for transfer to the OpenSea conduit before calling this method.
* @param options
* @param options.assets Array of assets to transfer. Each asset must have tokenStandard set.
* @param options.fromAddress The address to transfer from
* @param options.overrides Transaction overrides, ignored if not set.
* @returns Transaction hash of the bulk transfer
*
* @throws Error if any asset is missing required fields (tokenId for NFTs, amount for ERC20/ERC1155).
* @throws Error if any asset is not approved for transfer to the OpenSea conduit.
* @throws Error if the fromAddress is not available through wallet or provider.
*/
async bulkTransfer({ assets, fromAddress, overrides, }) {
return this._assetsManager.bulkTransfer({
assets,
fromAddress,
overrides,
});
}
/**
* Batch approve multiple assets for transfer to the OpenSea conduit.
* This method checks which assets need approval and batches them efficiently:
* - 0 approvals needed: Returns early
* - 1 approval needed: Sends single transaction
* - 2+ approvals needed: Uses Multicall3 to batch all approvals in one transaction
*
* @param options
* @param options.assets Array of assets to approve for transfer
* @param options.fromAddress The address that owns the assets
* @param options.overrides Transaction overrides, ignored if not set.
* @returns Transaction hash of the approval transaction, or undefined if no approvals needed
*
* @throws Error if the fromAddress is not available through wallet or provider.
*/
async batchApproveAssets({ assets, fromAddress, overrides, }) {
return this._assetsManager.batchApproveAssets({
assets,
fromAddress,
overrides,
});
}
/**
* Instead of signing an off-chain order, this methods allows you to approve an order
* with an on-chain transaction.
* @param order Order to approve
* @param domain An optional domain to be hashed and included at the end of fulfillment calldata. This can be used for on-chain order attribution to assist with analytics.
* @returns Transaction hash of the approval transaction
*
* @throws Error if the accountAddress is not available through wallet or provider.
* @throws Error if the order's protocol address is not supported by OpenSea. See {@link isValidProtocol}.
*/
async approveOrder(order, domain) {
return this._fulfillmentManager.approveOrder(order, domain);
}
/**
* Validates an order onchain using Seaport's validate() method. This submits the order onchain
* and pre-validates the order using Seaport, which makes it cheaper to fulfill since a signature
* is not needed to be verified during fulfillment for the order, but is not strictly required
* and the alternative is orders can be submitted to the API for free instead of sent onchain.
* @param orderComponents Order components to validate onchain
* @param accountAddress Address of the wallet that will pay the gas to validate the order
* @returns Transaction hash of the validation transaction
*
* @throws Error if the accountAddress is not available through wallet or provider.
*/
async validateOrderOnchain(orderComponents, accountAddress) {
return this._fulfillmentManager.validateOrderOnchain(orderComponents, accountAddress);
}
/**
* Create and validate a listing onchain. Combines order building with onchain validation.
* Validation costs gas upfront but makes fulfillment cheaper (no signature verification needed).
* @param options
* @param options.asset The asset to trade. tokenAddress and tokenId must be defined.
* @param options.accountAddress Address of the wallet making the listing
* @param options.amount Amount in decimal format (e.g., "1.5" for 1.5 ETH, not wei). Automatically converted to base units.
* @param options.quantity Number of assets to list. Defaults to 1.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in salt.
* @param options.salt Arbitrary salt. Auto-generated if not provided.
* @param options.listingTime When order becomes fulfillable (UTC seconds). Defaults to now.
* @param options.expirationTime Expiration time (UTC seconds).
* @param options.paymentTokenAddress Payment token address. Defaults to ETH.
* @param options.buyerAddress Optional buyer restriction. Only this address can purchase.
* @param options.includeOptionalCreatorFees Include optional creator fees. Default: false.
* @param options.zone Zone for order protection. Defaults to no zone.
* @returns Transaction hash
*
* @throws Error if asset missing token id or accountAddress unavailable.
*/
async createListingAndValidateOnchain({ asset, accountAddress, amount, quantity = 1, domain, salt, listingTime, expirationTime, paymentTokenAddress, buyerAddress, includeOptionalCreatorFees = false, zone, }) {
return this._fulfillmentManager.createListingAndValidateOnchain({
asset,
accountAddress,
amount,
quantity,
domain,
salt,
listingTime,
expirationTime,
paymentTokenAddress,
buyerAddress,
includeOptionalCreatorFees,
zone,
});
}
/**
* Create and validate an offer onchain. Combines order building with onchain validation.
* Validation costs gas upfront but makes fulfillment cheaper (no signature verification needed).
* @param options
* @param options.asset The asset to trade. tokenAddress and tokenId must be defined.
* @param options.accountAddress Address of the wallet making the offer.
* @param options.amount Amount in decimal format (e.g., "1.5" for 1.5 ETH, not wei). Automatically converted to base units.
* @param options.quantity Number of assets to bid for. Defaults to 1.
* @param options.domain Optional domain for on-chain attribution. Hashed and included in salt.
* @param options.salt Arbitrary salt. Auto-generated if not provided.
* @param options.expirationTime Expiration time (UTC seconds).
* @param options.paymentTokenAddress Payment token address. Defaults to WETH.
* @param options.zone Zone for order protection. Defaults to chain's signed zone.
* @returns Transaction hash
*
* @throws Error if asset missing token id or accountAddress unavailable.
*/
async createOfferAndValidateOnchain({ asset, accountAddress, amount, quantity = 1, domain, salt, expirationTime, paymentTokenAddress, zone, }) {
return this._fulfillmentManager.createOfferAndValidateOnchain({
asset,
accountAddress,
amount,
quantity,
domain,
salt,
expirationTime,
paymentTokenAddress,
zone,
});
}
/**
* Compute the `basePrice` parameter to be used to price an order.
* Also validates the price and token address.
* @param tokenAddress Address of the ERC-20 token to use for trading. Use the null address for ETH.
* @param amount The value for the order, in the token's main units (e.g. ETH instead of wei)
*/
async _getPriceParameters(orderSide, tokenAddress, amount) {
tokenAddress = tokenAddress.toLowerCase();
const isEther = tokenAddress === ethers_1.ethers.ZeroAddress;
let decimals = 18;
if (!isEther) {
if (tokenAddress in this._cachedPaymentTokenDecimals) {
decimals = this._cachedPaymentTokenDecimals[tokenAddress];
}
else {
const paymentToken = await this.api.getPaymentToken(tokenAddress);
this._cachedPaymentTokenDecimals[tokenAddress] = paymentToken.decimals;
decimals = paymentToken.decimals;
}
}
const amountWei = ethers_1.ethers.parseUnits(amount.toString(), decimals);
const basePrice = amountWei;
// Validation
if (amount == null || amountWei < 0) {
throw new Error("Starting price must be a number >= 0");
}
if (isEther && orderSide === types_1.OrderSide.OFFER) {
throw new Error("Offers must use wrapped ETH or an ERC-20 token.");
}
return { basePrice };
}
_dispatch(event, data) {
this._emitter.emit(event, data);
}
/** Get the accounts available from the signer or provider. */
async _getAvailableAccounts() {
const availableAccounts = [];
try {
if ("address" in this._signerOrProvider) {
availableAccounts.push(this._signerOrProvider.address);
}
else if ("listAccounts" in this._signerOrProvider) {
const addresses = (await this._signerOrProvider.listAccounts()).map((acct) => acct.address);
availableAccounts.push(...addresses);
}
else if ("getAddress" in this._signerOrProvider) {
availableAccounts.push(await this._signerOrProvider.getAddress());
}
}
catch (error) {
// If we can't get accounts (e.g., RPC error), treat as no accounts available
this.logger(`Failed to get available accounts: ${error instanceof Error ? error.message : error}`);
}
return availableAccounts;
}
/**
* Throws an error if an account is not available through the provider.
* @param accountAddress The account address to check is available.
*/
async _requireAccountIsAvailable(accountAddress) {
const accountAddressChecksummed = ethers_1.ethers.getAddress(accountAddress);
const availableAccounts = await this._getAvailableAccounts();
if (availableAccounts.includes(accountAddressChecksummed)) {
return;
}
throw new Error(`Specified accountAddress is not available through wallet or provider: ${accountAddressChecksummed}. Accounts available: ${availableAccounts.length > 0 ? availableAccounts.join(", ") : "none"}`);
}
/**
* Wait for a transaction to confirm and log the success or failure.
* @param transactionHash The transaction hash to wait for.
* @param event The event type to log.
* @param description The description of the transaction.
*/
async _confirmTransaction(transactionHash, event, description) {
const transactionEventData = { transactionHash, event };
this.logger(`Transaction started: ${description}`);
try {
this._dispatch(types_1.EventType.TransactionCreated, transactionEventData);
await this.provider.waitForTransaction(transactionHash);
this.logger(`Transaction succeeded: ${description}`);
this._dispatch(types_1.EventType.TransactionConfirmed, transactionEventData);
}
catch (error) {
this.logger(`Transaction failed: ${description}`);
this._dispatch(types_1.EventType.TransactionFailed, {
...transactionEventData,
error,
});
throw error;
}
}
}
exports.OpenSeaSDK = OpenSeaSDK;
//# sourceMappingURL=sdk.js.map