Last active
December 10, 2024 22:47
-
-
Save cordt-sei/1213d199fb2fde39d1e3322598704832 to your computer and use it in GitHub Desktop.
queryOwnersByPointer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from "ethers"; | |
// Use your provided RPC and contract address | |
const provider = new ethers.JsonRpcProvider("https://evm-rpc.basementnodes.ca:8545"); | |
const contractAddress = "0xdfa4ec4246f569d730edd9044965d3afeea980eb"; | |
const abi = [ | |
"function totalSupply() public view returns (uint256)", | |
"function ownerOf(uint256 tokenId) public view returns (address)" | |
]; | |
const contract = new ethers.Contract(contractAddress, abi, provider); | |
async function fetchAllTokensParallel() { | |
try { | |
// Query the total number of tokens from the contract | |
const totalTokens = await contract.totalSupply(); | |
console.log(`Total Tokens: ${totalTokens.toString()}`); | |
// Convert BigInt to Number (safe for reasonably sized collections) | |
const totalTokensNumber = Number(totalTokens); | |
// Create an array of promises for token IDs | |
const tokenPromises = Array.from({ length: totalTokensNumber }, (_, i) => | |
contract.ownerOf(i) | |
.then(owner => ({ tokenId: i + 1, owner })) // Resolve with tokenId and owner | |
.catch(() => null) // Catch errors for non-existent tokens | |
); | |
// Wait for all promises to resolve | |
const results = await Promise.all(tokenPromises); | |
// Filter out null results (invalid tokens) | |
return results.filter(token => token !== null); | |
} catch (error) { | |
console.error("Error fetching tokens:", error); | |
return []; | |
} | |
} | |
// Fetch and log all tokens | |
fetchAllTokensParallel() | |
.then(tokens => { | |
console.log(`Total Tokens Retrieved: ${tokens.length}`); | |
console.log("All Tokens:", JSON.stringify(tokens, null, 2)); // Pretty print the entire output | |
}) | |
.catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ercnft", | |
"version": "1.0.0", | |
"main": "index.js", | |
"type": "module", | |
"license": "MIT", | |
"dependencies": { | |
"ethers": "^6.13.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment