How do I get a lift of all the current owners of an NFT collection and the token id owned by each one of them?
3 Answers
All other answers give valid suggestions. If you are interested in how to query directly from the smart contract, you can leverage ERC-721's ownerOf
methods.
This is a sample script base on web3JS:
const Web3 = require('web3');
const url = 'https://fill-in-your-endpoint-url-here';
const web3 = new Web3(url);
var abi = [{ "inputs": [{ "internalType": "uint256", "name": "tokenId", "type": "uint256" }], "name": "ownerOf", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }]
var contract = new web3.eth.Contract(abi, "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D");
var startingIndex = 0
async function getOwner(index) {
try {
result = await contract.methods.ownerOf(index).call()
console.log("NFT index:" + index + "; Owner address:" + result)
getOwner(index + 1)
} catch (err) {
console.log(err)
console.log("Likely reache the end")
}
}
getOwner(startingIndex)
To use it, create an empty folder. Copy this code into an empty javascript file, for example go.js
. Fill in the URL
at line 2. (BTW chainstack is a great RPC provider, our developer plan gives 3 million request for free).
run :
npm init
npm install web3
node go
to see the result.
Hope that helps, happy coding.
-
As is stated in the help documentation you shouldn't use a header, tagline nor signature. ethereum.stackexchange.com/help/behavior.– Ismael ♦Feb 7 at 19:40
-
Thanks for pointing this out. I didn't really go through this page before! Feb 8 at 4:15
You can get all the owners of a collection using Moralis NFT API.
Here is a tutorial step-by-step: https://docs.moralis.io/web3-data-api/evm/how-to-get-the-owners-of-an-nft-collection
And this is an example of the results you can get. Let me know if you have any questions!
"result": [
{
"token_address": "0xa186d739ca2b3022b966194004c6b01855d59571",
"token_id": "692",
"owner_of": "0xd1fa414bbd0b71292e097d18a9d5ccbb04806c31",
"block_number": "15846571",
"block_number_minted": "15846571",
"token_hash": "22c43a90364cd3639c565470effc46db",
"amount": "1",
"contract_type": "ERC721",
"name": "NFTPass",
"symbol": "ATTPASS",
"token_uri": "https://metadata.atticc.xyz/692",
"metadata": "{\"attributes\":[{\"trait_type\":\"Name\",\"value\":\"Atticc Early Adopter Pass\"},{\"trait_type\":\"Category\",\"value\":\"Early Adopter Pass\"},{\"trait_type\":\"Number of Seats\",\"value\":\"5555\",\"display_type\":\"number\"},{\"trait_type\":\"Royalty\",\"value\":\"0\",\"display_type\":\"number\"}],\"image\":\"https://media.atticc.xyz/Pass692.webp\"}",
"last_token_uri_sync": "2022-10-28T12:30:53.470Z",
"last_metadata_sync": "2022-10-28T12:31:12.113Z",
"minter_address": "0xd1fa414bbd0b71292e097d18a9d5ccbb04806c31"
},
{
"token_address": "0xa186d739ca2b3022b966194004c6b01855d59571",
"token_id": "691",
"owner_of": "0x5e899294aa47ff846ca1f1bb096692deb0636ec0",
"block_number": "15845243",
"block_number_minted": "15845243",
"token_hash": "37341fba448ccb6bcd2776aef50a69d2",
"amount": "1",
"contract_type": "ERC721",
"name": "NFTPass",
"symbol": "ATTPASS",
"token_uri": "https://metadata.atticc.xyz/691",
"metadata": "{\"attributes\":[{\"trait_type\":\"Name\",\"value\":\"Atticc Early Adopter Pass\"},{\"trait_type\":\"Category\",\"value\":\"Early Adopter Pass\"},{\"trait_type\":\"Number of Seats\",\"value\":\"5555\",\"display_type\":\"number\"},{\"trait_type\":\"Royalty\",\"value\":\"0\",\"display_type\":\"number\"}],\"image\":\"https://media.atticc.xyz/Pass691.webp\"}",
"last_token_uri_sync": "2022-10-28T08:04:34.923Z",
"last_metadata_sync": "2022-10-28T08:04:49.994Z",
"minter_address": "0x42678905eb6fb9f0f3a1334cb23b9d393b53c138"
}
]
Disclosure: I work at Moralis
That depends on which NFT standard are we talking about here. And which blockchain. But in general, if we are talking about ETH, Solana, BNB, or any of the other big blockchains, there are usually third party tools/servers you can use for this. Like Alchemy or Infura.
If you wouldn't like to use such a service you can query the blockchain nodes yourself.