-
Notifications
You must be signed in to change notification settings - Fork 0
/
mant.jsx
95 lines (73 loc) · 2.84 KB
/
mant.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
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
const { MongoClient } = require('mongodb');
const { ethers } = require('ethers');
const mntABI = require('./ABI.jsx');
const provider = new ethers.providers.JsonRpcProvider("https://pacific-rpc.manta.network/http");
// MongoDB configuration
const dbUrl = 'mongodb+srv://liltest:[email protected]/';
const dbName = 'backend';
async function connectToDatabase() {
const client = new MongoClient(dbUrl, { useUnifiedTopology: true });
try {
await client.connect();
return client.db(dbName);
} catch (error) {
console.error('Error connecting to MongoDB:', error);
throw error;
}
}
// array of objects where each object contains an address and a corresponding function
const addressFunctions = [
{
address: "0x713C1300f82009162cC908dC9D82304A51F05A3E",
processFunction: (totalAssets) => totalAssets/1
},
{
address: "0x0DB2BA00bCcf4F5e20b950bF954CAdF768D158Aa",
processFunction: (totalAssets) => totalAssets/1
},
{
address: "0xDc63179CC57783493DD8a4Ffd7367DF489Ae93BF",
processFunction: (totalAssets) => totalAssets/1
},
{
address: "0x5f247B216E46fD86A09dfAB377d9DBe62E9dECDA",
processFunction: (totalAssets) => totalAssets/1
},
];
// function to fetch totalAssets for an address and apply the corresponding function
async function fetchTotalAssetsWithFunction(address, processFunction) {
try {
const contract = new ethers.Contract(address, mntABI, provider);
const totalAssets = await contract.totalAssets();
const processedValue = processFunction(totalAssets);
const db = await connectToDatabase();
const collection = db.collection('tvl_manta');
// Update the existing document with the new TVL value
const filter = { "Vault Address": address };
const updateDocument = {
$set: {
totalAssets: processedValue,
},
};
const result = await collection.updateOne(filter, updateDocument, { upsert: true });
if (result.modifiedCount === 1) {
console.log(`Total Assets for ${address} (processed) updated in MongoDB:`, processedValue);
} else {
console.error(`Document for ${address} not found in MongoDB.`);
}
} catch (error) {
console.error(`Error fetching and updating total assets for ${address}:`, error);
}
}
// calling function for each address-function pair in the array
async function updateTotalAssets() {
for (const { address, processFunction } of addressFunctions) {
await fetchTotalAssetsWithFunction(address, processFunction);
}
}
// run the function immediately to start the process
updateTotalAssets();
// schedule the function to run every ten minutes
const interval = setInterval(async () => {
await updateTotalAssets();
}, 10 * 60 * 1000); // 10 minutes in milliseconds