-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocols.js
109 lines (95 loc) · 2.31 KB
/
protocols.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
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
const fs = require('fs');
const axios = require('axios');
const filename = './data.json';
let protocols = [];
let pools = {};
function getProtocols() {
return protocols;
}
function getPools() {
return pools;
}
function getProtocol(protocolName) {
try {
const data = fs.readFileSync(filename, 'utf8');
const protocolJSON = JSON.parse(data);
let protocolData, protocolParent;
for (const protocol of protocolJSON.protocols) {
if (protocol.name === protocolName) {
protocolData = protocol;
// If there is a protocol parent that exists, we want to return it.
if (protocolData.parentProtocol) {
for (const parentProtocol of protocolJSON.protocols.parentProtocols) {
if (parentProtocol.id === protocolData.parentProtocol) {
protocolParent = parentProtocol;
}
}
return [protocolData, protocolParent];
}
else {
return [protocolData];
}
}
}
return [protocolData, protocolParent];
}
catch (err) {
console.error(`Error reading file from disk: ${err}`);
}
}
function getPool(poolId) {
try {
// Check if the pool data is available in the pool map
if (pools[poolId]) {
return pools[poolId];
}
return null;
}
catch (err) {
console.error(`Error retrieving pool: ${err}`);
}
}
function getPoolIDBySymbol(key) {
const pool = pools[key];
return pool ? pool.pool : null;
}
function updateKeys() {
const JSONData = JSON.parse(fs.readFileSync(filename, 'utf8'));
protocols = JSONData.protocols.protocols.map(protocol => protocol.name);
pools = {};
JSONData.pools.data.forEach(pool => {
const key = `${pool.symbol} (${pool.project})`;
pools[key] = pool;
pools[pool.pool] = pool;
});
}
// Gets protocols & pools data to be used for autocomplete
async function syncData() {
try {
const [protocolsResponse, poolsResponse] = await Promise.all([
axios.get('https://api.llama.fi/lite/protocols2'),
axios.get('https://yields.llama.fi/pools'),
]);
fs.writeFileSync(filename, JSON.stringify({
protocols: protocolsResponse.data,
pools: poolsResponse.data,
}));
updateKeys();
console.log('protocols:', protocols);
console.log('pools:', pools);
}
catch (error) {
console.error(error);
return false;
}
return true;
}
module.exports = {
syncData,
updateKeys,
getProtocol,
getPool,
getProtocols,
getPools,
getPoolIDBySymbol,
};