I’m new to Ethereum, and I’m working on a side project I’m curious
How can I discover every NFT on the Ethereum blockchain?
Once I’ve identified the NFT, how do I fetch the metadata?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI’m new to Ethereum, and I’m working on a side project I’m curious
How can I discover every NFT on the Ethereum blockchain?
Once I’ve identified the NFT, how do I fetch the metadata?
First, you can't do this in a general way.
In my speech presented at NFT.nyc, I deployed on Ethereum Mainnet a smart contract which created 2^255 NFTs using the ERC-721 protocol. (Thank you, work sponsored by NFT.nyc and Chain 76. Link at http://nft.life.)
If you were planning to make a general database which held like (contract address, token ID, owner) then you would need more atoms than the universe has to store that in your database.
If you want to avoid "spam" projects like this as well as "fake" projects, and even "old versions" of projects (e.g. see how Su Squares re-deployed the project several times to new contract addresses)... then you are excluding a lot. Everybody I know who is working on that problem has decided to start with an allowlist and only check those tokens.
But, assume you really wanted a general solution, and you were willing to blocklist some NFTs which are impractical to index. And you only cared about ERC-721 (repeat similar approach for other standards). You would do this by one of these two approaches:
Create a list of all contracts on the network (i.e. CODESIZE
> 0) and interrogate them using ERC-165 for the ERC-721 interface ID.
Play every block on the blockchain starting with when ERC-721 was finalized (June 22, 2018) and listen for the Transfer(address, address, uint256)
event. Then filter that list using #1 above.
0xcert has created a tool which does exactly what you are describing and unfortunately it does require more than one beefy server to run. It is at https://github.com/0xcert/ethereum-scanner and is not publicly available. They may be willing to license the tool out or sell it. (I advise 0xcert as one of my clients.)
Other commercial options like OpenSea, Etherscan, Azure Blockchain (announced EOL), and Mathematica have a great API available. Everyone of them includes limitations and does not have open source.
For metadata, you just query every token for the tokenURI
and record that information. Normalizing that data and making it useful would be one full time job. This is, again, why commercial providers like OpenSea will select only a relatively few token contracts to support.
As William was saying, there is no easy way to do this. You would have to build up your own database and scan through the blockchain for the mint events in ERC721 and ERC1155 contracts.
We stumbled upon this problem ourselves and we built a solution that you can use if you want. It doesn't quite get you the entire way, it will allow you to get all the NFTs owned by a specific address. So you could easily get all NFTs (and the metadata), from the current metamask user.
You can check it out at moralis.io, you can use the JS SDK to get all NFTs by a specific user. It's free to use.
const userEthNFTs = await Moralis.Web3.getNFTs();
Or you could use the REST API
GET /nft/wallet/{address}
Then you can get the metadata using
/nft/contract/{contract_address}/metadata
Huge disclaimer: I work at moralis :)