Skip to content

Commit

Permalink
add more stuff to graph query
Browse files Browse the repository at this point in the history
  • Loading branch information
bvalosek committed Dec 7, 2021
1 parent 4287614 commit b30d705
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/hypervibes/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface GetIndexedInfusionsInput {
export interface GetIndexedInfusionsOutput {
nft: string;
tokenId: BigNumber;
tokenUri: string;
currentOwner: string;
infusions: Array<{
realm: {
id: BigNumber;
Expand Down Expand Up @@ -81,25 +83,33 @@ const infusionsQuery = gql`
}
`;

const computeNftId = (datum: { nft: string; tokenId: BigNumber }) =>
`${datum.nft}-${datum.tokenId.toNumber()}`;

// get indexed information about a batch of NFTs
//
// can be used when there is a list of NFTs presented in a UI (eg, a token grid
// UX) and you want to know if it has been infused in ANY realm
export const batchGetIndexedInfusions = async (
batch: GetIndexedInfusionsInput[],
chainId: number
): Promise<void> => {
): Promise<GetIndexedInfusionsOutput[]> => {
const endpoint = SUBGRAPH_ENDPOINTS[chainId];
if (endpoint == null) {
throw new Error(`unsupported network: ${chainId}`);
}

// WARN: the assumption about the ID format isn't great here
const ids = batch.map(datum => `${datum.nft}-${datum.tokenId.toNumber()}`);
const ids = batch.map(computeNftId);
const { nfts } = await request(endpoint, infusionsQuery, { ids });

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const projected = nfts.map((item: any) => {
const projected: GetIndexedInfusionsOutput[] = nfts.map((item: any) => {
const mapped: GetIndexedInfusionsOutput = {
nft: item.collection.address,
tokenId: BigNumber.from(item.tokenId),
tokenUri: item.tokenUri,
currentOwner: item.owner.address,
infusions: [],
};

Expand Down Expand Up @@ -132,5 +142,12 @@ export const batchGetIndexedInfusions = async (
return mapped;
});

return projected;
const outputLookup = new Map(projected.map(nft => [computeNftId(nft), nft]));
const ordered = batch.map(datum => {
const projected = outputLookup.get(computeNftId(datum));
if (!projected) throw new Error('batch input output mismatch');
return projected;
});

return ordered;
};

0 comments on commit b30d705

Please sign in to comment.