/** * NFTs — Collections, balances, ownership, metadata. * * Run: npx tsx examples/07-nfts.ts */ import { Spectrum } from '@spectrumnodes/sdk'; const spectrum = new Spectrum({ api: process.env.SPECTRUM_API! }); const BAYC = '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D'; // ERC-721 const OPENSEA_ERC1155 = '0x495f947276749ce646f68ac8c248420045cb7b5e'; // ERC-1155 (OpenSea Shared Storefront) const WALLET = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; async function main() { // --- getCollection --- const collection = await spectrum.nfts.getCollection('ethereum', BAYC); console.log(`Collection: ${collection.name ?? '(unnamed)'}`); console.log(` Symbol: ${collection.symbol ?? '(none)'}`); console.log(` Total supply: ${collection.totalSupply ?? 'unknown'}`); if (collection.standard) { console.log(` Standard: ${collection.standard}`); } // --- getBalance --- const balance = await spectrum.nfts.getBalance('ethereum', BAYC, WALLET); console.log(`\nVitalik's BAYC balance: ${balance.balance}`); // --- getOwnedTokens --- const owned = await spectrum.nfts.getOwnedTokens('ethereum', BAYC, WALLET); console.log(`Owned BAYC tokens: ${owned.tokenIds.length}`); for (const id of owned.tokenIds.slice(0, 3)) { console.log(` Token #${id}`); } // --- getTokenBalance (specific token ID) --- // Uses ERC-1155 `balanceOf(address, uint256)`. Pass an ERC-1155 contract here; // calling this on an ERC-721 (like BAYC) reverts because the signature differs. const tokenBal = await spectrum.nfts.getTokenBalance('ethereum', OPENSEA_ERC1155, WALLET, '1'); // `balance` is `string | number` — coerce before comparing. const balanceNum = typeof tokenBal.balance === 'string' ? Number(tokenBal.balance) : tokenBal.balance; console.log( `\nDoes wallet own token #1 of the ERC-1155 collection? ${balanceNum > 0 ? 'Yes' : 'No'}`, ); // --- getTokenOwner --- const owner = await spectrum.nfts.getTokenOwner('ethereum', BAYC, '1'); console.log(`BAYC #1 owner: ${owner.owner}`); // --- getTokenMetadata --- // The token's `metadata` payload (name/image/attributes) is opaque (`unknown`) // because shape varies per collection. Cast to your expected shape if needed. const meta = await spectrum.nfts.getTokenMetadata('ethereum', BAYC, '1'); console.log(`BAYC #1 tokenURI: ${meta.tokenURI ?? '(none)'}`); if (meta.metadata) { const m = meta.metadata as { name?: string; image?: string }; console.log(` Name: ${m.name ?? '(none)'}`); console.log(` Image: ${m.image ?? '(none)'}`); } // --- getBatchBalance --- // ERC-1155 `balanceOfBatch` — same constraint as above: pass an ERC-1155. const batchBal = await spectrum.nfts.getBatchBalance('ethereum', OPENSEA_ERC1155, { addresses: [WALLET, '0x0000000000000000000000000000000000000001'], tokenIds: ['1', '2'], }); console.log(`\nBatch balance results: ${batchBal.results.length}`); } main().catch(console.error);