UNPKG

opensea-js

Version:

TypeScript SDK for the OpenSea marketplace helps developers build new experiences using NFTs and our marketplace data

181 lines 7.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeTokenIds = exports.getSeaportVersion = exports.getSeaportInstance = exports.requireValidProtocol = exports.isValidProtocol = exports.getAddressAfterRemappingSharedStorefrontAddressToLazyMintAdapterAddress = exports.getAssetItemType = void 0; const constants_1 = require("@opensea/seaport-js/lib/constants"); const ethers_1 = require("ethers"); const constants_2 = require("../constants"); /** * Gets the appropriate ItemType for a given token standard. * @param tokenStandard The token standard (ERC20, ERC721, ERC1155) * @returns The corresponding ItemType from Seaport */ const getAssetItemType = (tokenStandard) => { switch (tokenStandard) { case "ERC20": return constants_1.ItemType.ERC20; case "ERC721": return constants_1.ItemType.ERC721; case "ERC1155": return constants_1.ItemType.ERC1155; default: throw new Error(`Unknown schema name: ${tokenStandard}`); } }; exports.getAssetItemType = getAssetItemType; /** * Checks if the token address is the shared storefront address and if so replaces * that address with the lazy mint adapter address. Otherwise, returns the input token address * @param tokenAddress token address * @returns input token address or lazy mint adapter address */ const getAddressAfterRemappingSharedStorefrontAddressToLazyMintAdapterAddress = (tokenAddress) => { return constants_2.SHARED_STOREFRONT_ADDRESSES.includes(tokenAddress.toLowerCase()) ? constants_2.SHARED_STOREFRONT_LAZY_MINT_ADAPTER_CROSS_CHAIN_ADDRESS : tokenAddress; }; exports.getAddressAfterRemappingSharedStorefrontAddressToLazyMintAdapterAddress = getAddressAfterRemappingSharedStorefrontAddressToLazyMintAdapterAddress; /** * Returns if a protocol address is valid. * @param protocolAddress The protocol address */ const isValidProtocol = (protocolAddress) => { try { const checkSumAddress = ethers_1.ethers.getAddress(protocolAddress); const validProtocolAddresses = [ constants_1.CROSS_CHAIN_SEAPORT_V1_6_ADDRESS, constants_2.GUNZILLA_SEAPORT_1_6_ADDRESS, ].map((address) => ethers_1.ethers.getAddress(address)); return validProtocolAddresses.includes(checkSumAddress); } catch { return false; } }; exports.isValidProtocol = isValidProtocol; /** * Throws an error if the protocol address is not valid. * @param protocolAddress The protocol address */ const requireValidProtocol = (protocolAddress) => { if (!(0, exports.isValidProtocol)(protocolAddress)) { throw new Error(`Unsupported protocol address: ${protocolAddress}`); } }; exports.requireValidProtocol = requireValidProtocol; /** * Get the Seaport instance for a given protocol address. * This is a shared utility to avoid duplicating the logic across multiple SDK manager classes. * @param protocolAddress The protocol address * @param seaport The Seaport instance * @returns The Seaport instance for the given protocol address * @throws Error if the protocol address is not supported */ const getSeaportInstance = (protocolAddress, seaport) => { try { const checksummedProtocolAddress = ethers_1.ethers.getAddress(protocolAddress); switch (checksummedProtocolAddress) { case constants_1.CROSS_CHAIN_SEAPORT_V1_6_ADDRESS: case constants_2.GUNZILLA_SEAPORT_1_6_ADDRESS: return seaport; default: throw new Error(`Unsupported protocol address: ${protocolAddress}`); } } catch (error) { // If ethers.getAddress fails, it's an invalid address format if (error instanceof Error && error.message.includes("Unsupported protocol")) { throw error; } throw new Error(`Unsupported protocol address: ${protocolAddress}`); } }; exports.getSeaportInstance = getSeaportInstance; /** * Get the Seaport version string for a given protocol address. * @param protocolAddress The protocol address * @returns The version string (e.g., "1.6") * @throws Error if the protocol address is not supported */ const getSeaportVersion = (protocolAddress) => { try { const protocolAddressChecksummed = ethers_1.ethers.getAddress(protocolAddress); switch (protocolAddressChecksummed) { case constants_1.CROSS_CHAIN_SEAPORT_V1_6_ADDRESS: case constants_2.GUNZILLA_SEAPORT_1_6_ADDRESS: return "1.6"; default: throw new Error(`Unknown or unsupported protocol address: ${protocolAddress}`); } } catch (error) { if (error instanceof Error && error.message.includes("Unknown or unsupported protocol")) { throw error; } throw new Error(`Unknown or unsupported protocol address: ${protocolAddress}`); } }; exports.getSeaportVersion = getSeaportVersion; /** * Decodes an encoded string of token IDs into an array of individual token IDs using bigint for precise calculations. * * The encoded token IDs can be in the following formats: * 1. Single numbers: '123' => ['123'] * 2. Comma-separated numbers: '1,2,3,4' => ['1', '2', '3', '4'] * 3. Ranges of numbers: '5:8' => ['5', '6', '7', '8'] * 4. Combinations of single numbers and ranges: '1,3:5,8' => ['1', '3', '4', '5', '8'] * 5. Wildcard '*' (matches all token IDs): '*' => ['*'] * * @param encodedTokenIds - The encoded string of token IDs to be decoded. * @returns An array of individual token IDs after decoding the input. * * @throws {Error} If the input is not correctly formatted or if bigint operations fail. * * @example * const encoded = '1,3:5,8'; * const decoded = decodeTokenIds(encoded); // Output: ['1', '3', '4', '5', '8'] * * @example * const encodedWildcard = '*'; * const decodedWildcard = decodeTokenIds(encodedWildcard); // Output: ['*'] * * @example * const emptyEncoded = ''; * const decodedEmpty = decodeTokenIds(emptyEncoded); // Output: [] */ const decodeTokenIds = (encodedTokenIds) => { if (encodedTokenIds === "*") { return ["*"]; } if (encodedTokenIds === "") { return []; } const validFormatRegex = /^(\d+(:\d+)?)(,\d+(:\d+)?)*$/; if (!validFormatRegex.test(encodedTokenIds)) { throw new Error("Invalid input format. Expected a valid comma-separated list of numbers and ranges."); } const ranges = encodedTokenIds.split(","); const tokenIds = []; for (const range of ranges) { if (range.includes(":")) { const [startStr, endStr] = range.split(":"); const start = BigInt(startStr); const end = BigInt(endStr); const diff = end - start + 1n; if (diff <= 0) { throw new Error(`Invalid range. End value: ${end} must be greater than or equal to the start value: ${start}.`); } for (let i = 0n; i < diff; i += 1n) { tokenIds.push((start + i).toString()); } } else { const tokenId = BigInt(range); tokenIds.push(tokenId.toString()); } } return tokenIds; }; exports.decodeTokenIds = decodeTokenIds; //# sourceMappingURL=protocol.js.map