-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { BigNumber } from 'ethers'; | ||
import request, { gql } from 'graphql-request'; | ||
import { SUBGRAPH_ENDPOINTS } from './constants'; | ||
|
||
export interface GetIndexedInfusionsInput { | ||
nft: string; | ||
tokenId: BigNumber; | ||
} | ||
|
||
export interface GetIndexedInfusionsOutput { | ||
nft: string; | ||
tokenId: BigNumber; | ||
tokenUri: string; | ||
currentOwner: string; | ||
infusions: Array<{ | ||
realm: { | ||
id: BigNumber; | ||
name: string; | ||
dailyRate: BigNumber; | ||
token: { | ||
address: string; | ||
symbol: string; | ||
name: string; | ||
decimals: number; | ||
}; | ||
}; | ||
initialInfusionBy: string; | ||
initialInfusionAmount: BigNumber; | ||
currentBalance: BigNumber; | ||
lastClaimTimestamp: BigNumber; | ||
}>; | ||
} | ||
|
||
const infusionsQuery = gql` | ||
query getInfusions($ids: [ID!]!) { | ||
nfts(where: { id_in: $ids }) { | ||
id | ||
tokenId | ||
tokenUri | ||
owner { | ||
id | ||
address | ||
} | ||
collection { | ||
id | ||
address | ||
name | ||
symbol | ||
} | ||
infusions { | ||
id | ||
balance | ||
lastClaimAtTimestamp | ||
realm { | ||
id | ||
name | ||
description | ||
dailyRate | ||
token { | ||
id | ||
address | ||
name | ||
symbol | ||
decimals | ||
} | ||
} | ||
events(first: 1, orderBy: createdAtTimestamp, orderDirection: asc) { | ||
id | ||
eventType | ||
createdAtTimestamp | ||
createdAtBlock | ||
msgSender { | ||
address | ||
} | ||
target { | ||
address | ||
} | ||
amount | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
// WARN: the assumption about the ID format isn't great here | ||
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<GetIndexedInfusionsOutput[]> => { | ||
const endpoint = SUBGRAPH_ENDPOINTS[chainId]; | ||
if (endpoint == null) { | ||
throw new Error(`unsupported network: ${chainId}`); | ||
} | ||
|
||
const ids = batch.map(computeNftId); | ||
const { nfts } = await request(endpoint, infusionsQuery, { ids }); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-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: [], | ||
}; | ||
|
||
for (const infusion of item.infusions) { | ||
const { token } = infusion.realm; | ||
|
||
const [event] = infusion.events; | ||
if (event == null) throw new Error('missing initial infusion event'); | ||
|
||
const entry: GetIndexedInfusionsOutput['infusions'][0] = { | ||
realm: { | ||
id: BigNumber.from(infusion.realm.id), | ||
name: infusion.realm.name, | ||
dailyRate: BigNumber.from(infusion.realm.dailyRate), | ||
token: { | ||
address: token.address ?? '', | ||
symbol: token.symbol ?? '', | ||
name: token.name ?? '', | ||
decimals: token.decimals, | ||
}, | ||
}, | ||
currentBalance: BigNumber.from(infusion.balance), | ||
lastClaimTimestamp: BigNumber.from(infusion.lastClaimAtTimestamp), | ||
initialInfusionAmount: BigNumber.from(event.amount), | ||
initialInfusionBy: event.target.address, | ||
}; | ||
mapped.infusions.push(entry); | ||
} | ||
|
||
return mapped; | ||
}); | ||
|
||
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; | ||
}; |