-
Notifications
You must be signed in to change notification settings - Fork 0
/
dex_script.jsx
59 lines (44 loc) · 1.92 KB
/
dex_script.jsx
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
const { MongoClient } = require('mongodb');
const uri = 'mongodb+srv://liltest:[email protected]/';
const dbName = 'vaults';
async function updateStats() {
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
try {
await client.connect();
const database = client.db(dbName);
const statsCollection = database.collection('stats');
const tvlCollection = database.collection('mantle');
const tvlMantaCollection = database.collection('manta-pacific');
const statsDocuments = await statsCollection.find({}).toArray();
for (const doc of statsDocuments) {
const chainNameStats = doc.chain;
const dex = doc.dex;
const tvlCount = await tvlCollection.countDocuments({ 'chain': chainNameStats, dex });
const tvlMantaCount = await tvlMantaCollection.countDocuments({ 'chain': chainNameStats, dex });
const tvlDocuments = await tvlCollection.find({ 'chain': chainNameStats, dex }).toArray();
const tvlMantaDocuments = await tvlMantaCollection.find({ 'chain': chainNameStats, dex }).toArray();
const tvlSum = tvlDocuments.reduce((sum, tvlDoc) => sum + (tvlDoc.tvlUSD || 0), 0);
const tvlMantaSum = tvlMantaDocuments.reduce((sum, tvlMantaDoc) => sum + (tvlMantaDoc.tvlUSD || 0), 0);
const totalSum = tvlSum + tvlMantaSum;
await statsCollection.updateOne(
{ chain: chainNameStats, dex },
{
$set: {
totalVaults: tvlCount + tvlMantaCount,
tvlUSD: totalSum
}
}
);
}
console.log('Stats collection updated successfully.');
} catch (error) {
console.error('Error updating stats collection:', error);
} finally {
client.close();
}
}
updateStats();
setInterval(() => {
console.log('Running the updateStats function...');
updateStats();
}, 10 * 60 * 1000);