Created
July 6, 2021 11:49
-
-
Save mattdesl/b5bf0e6af28af9a0a30012d85c9edca7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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