-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
52 lines (47 loc) · 1.88 KB
/
build.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
import dotenv from "dotenv";
import axios from 'axios';
import { wrappedSolTokenAddress } from "./config.js";
import fs from 'fs';
import { getDecimalByMintAddress } from "./decimal.js";
dotenv.config();
function getUniqueTokenIds(poolsData) {
const tokenIds = poolsData.map(poolData => [
poolData.relationships.base_token.data.id.replace('solana_', ''),
poolData.relationships.quote_token.data.id.replace('solana_', '')
]).flat();
// Filter out wSOL mint address and get unique token addresses.
const uniqueIds = [...new Set(tokenIds)];
return uniqueIds.filter(id => id !== wrappedSolTokenAddress);
}
function readOriginalTokens(filename) {
try {
const data = fs.readFileSync(filename, 'utf-8');
return JSON.parse(data);
} catch (error) {
console.error('Error reading the original tokens file:', error);
return []; // Return an empty array if the file doesn't exist or is empty
}
}
function writeToJsonFile(data, filename) {
fs.writeFileSync(filename, JSON.stringify(data, null, 2), 'utf-8');
}
async function getTopPools() {
axios.get('https://pro-api.coingecko.com/api/v3/onchain/networks/solana/trending_pools', {
headers: {
'accept': 'application/json',
'x-cg-pro-api-key': 'CG-ww38dvoPhso7kYTyXbLrMQ8h'
}
})
.then(response => {
const newTokenIds = getUniqueTokenIds(response.data.data);
const originalTokens = readOriginalTokens('token.json');
const combinedTokens = [...new Set([...originalTokens, ...newTokenIds])];
writeToJsonFile(combinedTokens, 'token.json');
console.log('Data written to token.json');
getDecimalByMintAddress();
})
.catch(error => {
console.error(error);
});
}
getTopPools();