-3

I am currious of why Opensea can get all NFT contract information from blockchain? Not only does Opensea, but all the NFT marketplaces. I know that there are APIs from Etherscan or Polygonscan, but I don't find an API which returns all the contract addresses or token names. Or they just contact with Etherscan and get a private API for that fucntion? Are there any web3 experts can help with my question?

Thank you

Emily Li
  • 55
  • 7

1 Answers1

0

As per the ERC-721 and ERC-1155 standards, when a contract is minting or transferring NFTs, it needs to emit an event.

Marketplaces listen to these events containing collection addresses and token IDs. Combined with another set of standardized functions implemented by the collection contracts, and standardized metadata file format, they are able to automatically retrieve information about:

  • newly minted NFTs
  • their metadata files
  • their image files, name, description, and other attributes

and store this information in their own database.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • wow, thank you so much!!! May I know how to listen to theses events? Or if there are some documentations I can read? – Emily Li May 30 '22 at 08:00
  • @EmilyLi You can connect to a 3rd party node provider (Infura, Alchemy, ... or your own node) using websockets, and then subscribe to the events using a standardized [eth_newfilter](https://eth.wiki/json-rpc/API#eth_newfilter) RPC method... There are high-level wrappers for this, for example using `web3js` [Contract.events()](https://web3js.readthedocs.io/en/v1.7.3/web3-eth-contract.html#contract-events) or `ethers.js` [Contract.on()](https://docs.ethers.io/v5/api/contract/contract/#Contract-on) – Petr Hejda May 30 '22 at 08:29
  • I try to study on Contract.on() of ehters.js, and I found that it requires me to input contract address and ABI. How does marketplace get my contract address? And I found that my contract is not verified which means my contract abi can't be access, but I can still see my NFT on marketplace, why is that? – Emily Li Jun 01 '22 at 01:51
  • @EmilyLi They use a generic subscription handler that listens to all `Transfer` events from all addresses. I haven't found a way in the Ethers.js documentation but here's the [docs](https://web3js.readthedocs.io/en/v1.7.3/web3-eth-subscribe.html#subscribe-logs) for the generic function in web3.js... All contracts implementing the same interface use the same ABI JSON - at least for the part that implements the interface, not for its extensions. So they can use a generic ABI JSON of ERC721. Or in this case the ABI-encoded event log `topics` values. – Petr Hejda Jun 01 '22 at 07:57
  • Thank you! I have a cleaner concept of maketplace now! I think it should be `provider.on` function for ethers.js to do that listener things, but I am still doing tests on that. – Emily Li Jun 01 '22 at 09:05