forked from solana-foundation/explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnftoken.ts
More file actions
139 lines (119 loc) · 4.25 KB
/
nftoken.ts
File metadata and controls
139 lines (119 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Connection, PublicKey } from '@solana/web3.js';
import axios from 'axios';
import bs58 from 'bs58';
import pLimit from 'p-limit';
import { fromHex } from '@/app/shared/lib/bytes';
import { NftokenTypes } from './nftoken-types';
export const NFTOKEN_ADDRESS = 'nftokf9qcHSYkVSP3P2gUMmV6d4AwjMueXgUu43HyLL';
const nftokenAccountDiscInHex = '21b45b35ec0f3f61';
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace NftokenFetcher {
export const getNftsInCollection = async ({
collection,
rpcUrl,
}: {
collection: string;
rpcUrl: string;
}): Promise<NftokenTypes.NftInfo[]> => {
const connection = new Connection(rpcUrl);
const accounts = await connection.getProgramAccounts(new PublicKey(NFTOKEN_ADDRESS), {
filters: [
{
memcmp: {
bytes: bs58.encode(fromHex(nftokenAccountDiscInHex)),
offset: 0,
},
},
{
memcmp: {
// authority_can_update
bytes: collection,
offset:
8 + // discriminator
1 + // version
32 + // holder
32 + // authority
1,
},
},
],
});
const parsed_accounts: NftokenTypes.NftAccount[] = accounts.flatMap(account => {
const parsed = NftokenTypes.nftAccountLayout.decode(account.account.data);
if (!parsed) {
return [];
}
return {
address: account.pubkey.toBase58(),
authority: parsed.authority,
authority_can_update: Boolean(parsed.authority_can_update),
collection: parsed.collection,
delegate: parsed.delegate,
holder: parsed.holder,
metadata_url: parsed.metadata_url,
};
});
const metadata_urls = parsed_accounts.map(a => a.metadata_url);
const metadataMap = await getMetadataMap({ urls: metadata_urls });
const nfts = parsed_accounts.map(account => ({
...account,
...metadataMap.get(account.metadata_url),
}));
nfts.sort();
return nfts.sort((a, b) => {
if (a.name && b.name) {
return a.name < b.name ? -1 : 1;
}
if (a.name) {
return 1;
}
if (b.name) {
return -1;
}
return a.address < b.address ? 1 : -1;
});
};
export const getMetadata = async ({
url,
}: {
url: string | null | undefined;
}): Promise<NftokenTypes.Metadata | null> => {
if (!url) {
return null;
}
const metadataMap = await getMetadataMap({
urls: [url],
});
return metadataMap.get(url) ?? null;
};
export const getMetadataMap = async ({
urls: _urls,
}: {
urls: Array<string | null | undefined>;
}): Promise<Map<string, NftokenTypes.Metadata | null>> => {
const urls = Array.from(new Set(_urls.filter((url): url is string => Boolean(url))));
const metadataMap = new Map<string, NftokenTypes.Metadata | null>();
const limit = pLimit(5);
const promises = urls.map(url =>
limit(async () => {
try {
const { data } = await axios.get(url, {
timeout: 5_000,
});
metadataMap.set(url, {
animation_url: data.animation_url ?? null,
description: data.description ?? null,
external_url: data.external_url ?? null,
image: data.image ?? '',
name: data.name ?? '',
traits: data.traits ?? [],
});
} catch {
metadataMap.set(url, null);
}
}),
);
await Promise.all(promises);
return metadataMap;
};
}