Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Created July 6, 2021 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdesl/b5bf0e6af28af9a0a30012d85c9edca7 to your computer and use it in GitHub Desktop.
Save mattdesl/b5bf0e6af28af9a0a30012d85c9edca7 to your computer and use it in GitHub Desktop.
const SUBGRAPH = "https://api.thegraph.com/subgraphs/name/artblocks/art-blocks";
// Utility to query from subgraph
async function query(url, query) {
const response = await fetch(url, {
method: "post",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ query }),
});
const result = await response.json();
if (result.errors) throw new Error(result.errors[0].message);
return result.data;
}
// Fetches up to 100 owned tokens for the given wallet address
async function fetchTokensOwnedByAddress(ownerAddress, opts = {}) {
ownerAddress = ownerAddress.toLowerCase();
const { limit = 100 } = opts;
const { tokens } = await query(
SUBGRAPH,
`{
tokens(first: ${limit}, orderBy: createdAt, orderDirection: desc, where: { owner: "${ownerAddress}"}) {
id
project {
name
artistName
}
}
}`
);
return tokens;
}
(async () => {
const tokens = await fetchTokensOwnedByAddress("... wallet address ...");
console.log(tokens);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment