Skip to content

Commit

Permalink
feat: add chain info
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jan 18, 2024
1 parent edb876c commit 61a70c7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/nft/src/generators/onfinality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export const createOnfinalityNftGenerator: (options?: {
}
)

const chainEtherscan = new Map<string, string | undefined>()
const getEtherscanUrl = async (chainId: string, contractAddress: string, tokenId: string) => {
const chainEtherscan = new Map<string, { name: string; explorerUrl: string | undefined } | undefined>()
const getNftChainInfo = async (chainId: string, contractAddress: string, tokenId: string) => {
chainEtherscan.set(
chainId,
chainEtherscan.get(chainId) ??
Expand All @@ -59,15 +59,20 @@ export const createOnfinalityNftGenerator: (options?: {
)
)
.then(x => x.json())
.then(x => x.explorerUrl as string | undefined)
.catch())
)

if (!chainEtherscan.has(chainId)) {
return
}

return new URL(`./nft/${contractAddress}/${tokenId}`, chainEtherscan.get(chainId)).toString()
return {
name: chainEtherscan.get(chainId)?.name ?? '',
etherscanUrl: new URL(
`./nft/${contractAddress}/${tokenId}`,
chainEtherscan.get(chainId)?.explorerUrl
).toString(),
}
}

yield* await Promise.all(
Expand All @@ -81,22 +86,23 @@ export const createOnfinalityNftGenerator: (options?: {
} => x !== null && x !== undefined && x.collection !== null && x.collection !== undefined
)
.map(async nft => {
const etherscanUrl = await getEtherscanUrl(
const nftChainInfo = await getNftChainInfo(
nft.collection.networkId,
nft.collection.contractAddress,
nft.tokenId
)
return {
id: `${nft.collection.contractType}-${nft.collection.networkId}-${nft.collection.contractAddress}-${nft.tokenId}`.toLowerCase(),
type: nft.collection.contractType.toLowerCase() ?? '',
chain: nft.collection.networkId ?? '',
chain: nftChainInfo?.name,
name: nft.metadata?.name ?? undefined,
description: nft.metadata?.description ?? undefined,
media: nft.metadata?.imageUri ?? undefined,
thumbnail: nft.metadata?.imageUri ?? undefined,
serialNumber: Number(nft.tokenId),
properties: undefined,
externalLinks: etherscanUrl === undefined ? undefined : [{ name: 'Etherscan', url: etherscanUrl }],
externalLinks:
nftChainInfo === undefined ? undefined : [{ name: 'Etherscan', url: nftChainInfo.etherscanUrl }],
collection: {
id: nft.collection.contractAddress,
name: nft.collection.name,
Expand Down

0 comments on commit 61a70c7

Please sign in to comment.