@xyo-network/crypto-nft-collection-witness-plugin 2.79.2 → 2.79.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,17 @@
1
1
  import { BaseProvider } from '@ethersproject/providers'
2
+ import { exists } from '@xylabs/exists'
2
3
  import { AxiosJson } from '@xyo-network/axios'
3
4
  import { NftInfo, NftMetadata, NftSchema, TokenType, toTokenType } from '@xyo-network/crypto-nft-payload-plugin'
4
5
  import { ERC721Enumerable__factory, ERC721URIStorage__factory, ERC1155Supply__factory } from '@xyo-network/open-zeppelin-typechain'
6
+ import { checkIpfsUrl, getErc1967Status } from '@xyo-network/witness-blockchain-abstract'
5
7
 
6
8
  import { tokenTypes } from './tokenTypes'
7
9
  import { tryCall } from './tryCall'
8
10
 
9
11
  const ipfsGateway = '5d7b6582.beta.decentralnetworkservices.com'
10
12
 
11
- /**
12
- * Returns the equivalent IPFS gateway URL for the supplied URL.
13
- * @param urlToCheck The URL to check
14
- * @returns If the supplied URL is an IPFS URL, it converts the URL to the
15
- * equivalent IPFS gateway URL. Otherwise, returns the original URL.
16
- */
17
- export const checkIpfsUrl = (urlToCheck: string, ipfsGateway: string) => {
18
- const url = new URL(urlToCheck)
19
- let protocol = url.protocol
20
- let host = url.host
21
- let path = url.pathname
22
- const query = url.search
23
- if (protocol === 'ipfs:') {
24
- protocol = 'https:'
25
- host = ipfsGateway
26
- path = url.host === 'ipfs' ? `ipfs${path}` : `ipfs/${url.host}${path}`
27
- const root = `${protocol}//${host}/${path}`
28
- return query?.length > 0 ? `${root}?${query}` : root
29
- } else {
30
- return urlToCheck
31
- }
13
+ function range(size: number, startAt: number = 0): ReadonlyArray<number> {
14
+ return [...Array(size).keys()].map((i) => i + startAt)
32
15
  }
33
16
 
34
17
  export const getNftCollectionNfts = async (
@@ -49,45 +32,56 @@ export const getNftCollectionNfts = async (
49
32
  maxNfts = 100,
50
33
  ): Promise<NftInfo[]> => {
51
34
  try {
35
+ //Check if ERC-1967 Upgradeable
36
+ const { implementation } = await getErc1967Status(provider, contractAddress)
37
+
52
38
  const axios = new AxiosJson({ timeout: 2000 })
53
- const enumerable = ERC721Enumerable__factory.connect(contractAddress, provider)
54
- const storage = ERC721URIStorage__factory.connect(contractAddress, provider)
55
- const supply1155 = ERC1155Supply__factory.connect(contractAddress, provider)
56
- const finalTypes = types ?? (await tokenTypes(provider, contractAddress))
57
- const result: NftInfo[] = []
39
+ const enumerable = ERC721Enumerable__factory.connect(implementation, provider)
40
+ const storage = ERC721URIStorage__factory.connect(implementation, provider)
41
+ const supply1155 = ERC1155Supply__factory.connect(implementation, provider)
42
+ const finalTypes = types ?? (await tokenTypes(provider, implementation))
58
43
 
59
- for (let i = 0; i < maxNfts; i++) {
60
- const tokenId = await tryCall(async () => (await enumerable.tokenByIndex(i)).toHexString())
61
- if (tokenId !== undefined) {
62
- const supply = finalTypes.includes(toTokenType('ERC1155'))
63
- ? (await tryCall(async () => (await supply1155.totalSupply(tokenId)).toHexString())) ?? '0x01'
64
- : '0x01'
65
- const metadataUri = await tryCall(async () => await storage.tokenURI(tokenId))
66
- const checkedMetaDataUri = metadataUri ? checkIpfsUrl(metadataUri, ipfsGateway) : undefined
67
- let metadata: NftMetadata | undefined = undefined
68
- if (checkedMetaDataUri !== undefined) {
69
- try {
70
- metadata = (await axios.get(checkedMetaDataUri)).data
71
- } catch (ex) {
72
- const error = ex as Error
73
- console.error(`Get Metadata failed: ${error.message}`)
74
- }
75
- }
44
+ const maxNftsArray = range(maxNfts)
76
45
 
77
- const info: NftInfo = {
78
- address: contractAddress,
79
- chainId: provider.network.chainId,
80
- metadata,
81
- metadataUri,
82
- schema: NftSchema,
83
- supply,
84
- tokenId,
85
- type: finalTypes.at(0),
86
- types: finalTypes,
87
- }
88
- result.push(info)
89
- }
90
- }
46
+ const result: NftInfo[] = (
47
+ await Promise.all(
48
+ maxNftsArray.map(async (_value, i) => {
49
+ const tokenId = (await tryCall(async () => (await enumerable.tokenByIndex(i)).toHexString())) ?? `${i}`
50
+ if (tokenId !== undefined) {
51
+ const supply = finalTypes.includes(toTokenType('ERC1155'))
52
+ ? (await tryCall(async () => (await supply1155.totalSupply(tokenId)).toHexString())) ?? '0x01'
53
+ : '0x01'
54
+ const metadataUri = await tryCall(async () => await storage.tokenURI(tokenId))
55
+ const checkedMetaDataUri = metadataUri ? checkIpfsUrl(metadataUri, ipfsGateway) : undefined
56
+ let metadata: NftMetadata | undefined = undefined
57
+ if (checkedMetaDataUri !== undefined) {
58
+ try {
59
+ metadata = (await axios.get(checkedMetaDataUri)).data
60
+ } catch (ex) {
61
+ const error = ex as Error
62
+ console.error(`Get Metadata failed: ${error.message}`)
63
+ }
64
+ }
65
+
66
+ const info: NftInfo = {
67
+ address: contractAddress,
68
+ chainId: provider.network.chainId,
69
+ metadata,
70
+ metadataUri,
71
+ schema: NftSchema,
72
+ supply,
73
+ tokenId,
74
+ type: finalTypes.at(0),
75
+ types: finalTypes,
76
+ }
77
+ if (implementation !== contractAddress) {
78
+ info.implementation = implementation
79
+ }
80
+ return info
81
+ }
82
+ }),
83
+ )
84
+ ).filter(exists)
91
85
  return result
92
86
  } catch (ex) {
93
87
  const error = ex as Error