Skip to content

Instantly share code, notes, and snippets.

@eccentricexit
Created February 4, 2022 22:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eccentricexit/58f41b5c57df81c509dd59820395d83c to your computer and use it in GitHub Desktop.
Save eccentricexit/58f41b5c57df81c509dd59820395d83c to your computer and use it in GitHub Desktop.
Querying NFT and NFT Collections
const fetch = require("node-fetch");
async function main() {
const collectionToCheck = `0xed5af388653567af2f388e6224dc7c4b3241c544`;
const nftCollectionsAddress = `0x2f19f817bbf800b487b7f2e51f24ad5ea0222463`;
console.info(
`Is the collection ${collectionToCheck} in the NFT registry (${nftCollectionsAddress})?`
);
console.info(
await isCollectionRegistered(collectionToCheck, nftCollectionsAddress)
);
const tokenID = `104910411686653679914571065004236135313385658342127918230679249181414300581889`;
const collectionAddress = `0x495f947276749ce646f68ac8c248420045cb7b5e`;
const nftRegistryAddress = `0xD5994f15BE9987104D9821AA99d1C97227c7C08c`;
console.info(
`Is the nft of tokenID ${tokenID}, of collection ${collectionAddress} in the NFT registry (${nftRegistryAddress})?`
);
console.info(
await isCollectionRegistered(collectionAddress, tokenID, nftRegistryAddress)
);
}
/**
* Example query check if a given NFT collection was accepted into the NFT
* collections list.
*
* Remember:
* - Addresses always in lowercase (not checksummed)
* - Registered items are in the Registered or Clearing Requested status.
* - The current subgraph API does not support mixing text serches with
* entity fields, so we must filter out after querying.
*
* @param collectionAddress - The address of the collection to check
*/
async function isCollectionRegistered(
collectionAddress,
nftCollectionsAddress
) {
let query = {
query: `{
itemSearch(text: "${collectionAddress.toLowerCase()}") {
id
registryAddress
status
disputed
}
}`,
};
const {
data: { itemSearch },
} = await (
await fetch(
"https://api.thegraph.com/subgraphs/name/eccentricexit/curate-xdai-ii",
{
method: "POST",
body: JSON.stringify(query),
}
)
).json();
// Filter out irrelevant items (subgraph does not currently support text searches
// with entity field parameters, so we must do this manually here).
// Only deal with items submitted to the NFT collection registry.
let result = itemSearch.filter(
(i) => i.registryAddress === nftCollectionsAddress
);
// Remove rejected and submitted items (those aren't registered).
result = result.filter(
(i) => i.status === "Registered" || i.status === `ClearingRequested`
);
return result.length > 0;
}
async function isNFTregistered(
nftCollectionAddress,
tokenID,
nftRegistryAddress
) {
let query = {
query: `{
itemSearch(text: "${nftCollectionAddress.toLowerCase()} & ${tokenID}") {
id
registryAddress
status
disputed
}
}`,
};
const {
data: { itemSearch },
} = await (
await fetch(
"https://api.thegraph.com/subgraphs/name/eccentricexit/curate-xdai-ii",
{
method: "POST",
body: JSON.stringify(query),
}
)
).json();
// Filter out irrelevant items (subgraph does not currently support text searches
// with entity field parameters, so we must do this manually here).
// Only deal with items submitted to the NFT collection registry.
let result = itemSearch.filter(
(i) => i.registryAddress === nftCollectionsAddress
);
// Remove rejected and submitted items (those aren't registered).
result = result.filter(
(i) => i.status === "Registered" || i.status === `ClearingRequested`
);
return result.length > 0;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment