-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
72 lines (64 loc) · 1.87 KB
/
search.js
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
const bs58 = require('bs58');
const web3 = require('@solana/web3.js');
const { decodeTokenMetadata } = require('@nfteyez/sol-rayz');
const axios = require('axios');
const Promise = require('bluebird');
const { METADATA_PROGRAM, API_SOLANA } = require('./solana');
// using mainnet beta
let connection = new web3.Connection(API_SOLANA);
async function getJsonFromUrl(src) {
//simple function that return json from link
try {
const res = await axios.get(src);
return res;
} catch (err) {
return undefined;
}
}
const searchMetaTokenByParam = async text => {
const bytes = bs58.encode(Buffer.from(text));
const metaPubKey = new web3.PublicKey(METADATA_PROGRAM);
try {
const res = await connection.getProgramAccounts(metaPubKey, {
commitment: 'confirmed',
encoding: 'base64',
filters: [
{
memcmp: {
// offset determine what we will you search
// for example 1 means that you will start from 2nd symbol and will check next n bytes,
// and from 1 till 32 stored update authority, in that case you will get all tokens with that authority
// in most cases it will mean whole collection. In this case 1 + 32 + 32 + 4 means name of the token
offset: 1 + 32 + 32 + 4,
bytes,
},
},
],
});
return res;
} catch (err) {
//
}
};
const decodeNftsMetadata = async nfts => {
try {
const decodedArray = await Promise.all(nfts.map(acc => decodeTokenMetadata(acc?.account?.data)));
return decodedArray;
} catch (err) {
//
}
};
const getSingleNftMeta = async nft => {
try {
const jsonUri = nft?.data?.uri?.replace(/\0/g, '');
const fetchedJsonNft = await getJsonFromUrl(jsonUri);
return fetchedJsonNft;
} catch (err) {
//
}
};
module.exports = {
searchMetaTokenByParam,
decodeNftsMetadata,
getSingleNftMeta,
};