Skip to content

Instantly share code, notes, and snippets.

@SilviaMargaritaOcegueda
Last active July 22, 2023 23:29
Show Gist options
  • Save SilviaMargaritaOcegueda/b5f18cf563a268f9c96841b04b5e7332 to your computer and use it in GitHub Desktop.
Save SilviaMargaritaOcegueda/b5f18cf563a268f9c96841b04b5e7332 to your computer and use it in GitHub Desktop.
getAccountsForUser
const { request, gql } = require("graphql-request");
// Function to query the subgraph and retrieve 6551-accounts for the given user
async function checkAccountForUser(contract, id, chainId) {
const endpoint =
"https://api.thegraph.com/subgraphs/name/silviamargaritaocegueda/mock-wallets";
const query = gql`
query MyQuery($contract: Bytes, $id: BigInt, $chainId: BigInt) {
accounts(
where: {
and: [
{ tokenContract: $contract }
{ tokenID: $id }
{ chainID: $chainId }
]
}
) {
id
}
}
`;
try {
const data = await request(endpoint, query, { contract, id, chainId });
console.log("Data:", data);
return data.accounts;
} catch (error) {
console.error("Error fetching data:", error);
return [];
}
}
async function getAccountsForUser(contracts, ids, chainId) {
const result = contracts
.slice(0, ids.length)
.map((contract, index) => [contract, ids[index]]);
const accounts = [];
await Promise.all(
result.map(([contract, id]) => checkAccountForUser(contract, id, chainId))
)
.then((accountResults) => {
for (const account of accountResults) {
accounts.push(account[0]);
}
})
.catch((error) => {
console.error("Error:", error);
});
return accounts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment