-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-staking.js
60 lines (50 loc) · 1.75 KB
/
fetch-staking.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
const fs = require('fs');
const comchat_vali_addr = "5D4o6H19z6ctWjS9HzxBpMxqhuzCCCsgXk49AqXGPUqZEpRt"
const https_provider_url = "https://commune-api-node-0.communeai.net/public-http"
const fetchStaking = async () => {
// Check staking amount
const payload = {
"id": 1,
"jsonrpc": "2.0",
"method": "subspace_getModuleInfo",
"params": [comchat_vali_addr, 0]
}
const response = await fetch(https_provider_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json' // This tells the API you're sending JSON
},
body: JSON.stringify(payload) // Converts your data object to JSON string
});
if (!response.ok) {
return new NextResponse(`Unable to fetch staking info. Please relax a bit while.`, {
status: 400,
});
}
const responseData = await response.json();
const stake_from = responseData["result"]["stats"]["stake_from"]
var result = {}
if (Array.isArray(stake_from)) {
for (const index in stake_from) {
const key = stake_from[index][0]
const staking = stake_from[index][1]
result[key] = staking
}
} else {
result = stake_from
}
// Convert the result object to a JSON string
const jsonString = JSON.stringify(result);
// Write the JSON string to a file
fs.writeFile('public/staking.json', jsonString, (err) => {
if (err) {
console.error('Error writing to file', err);
} else {
console.log('Successfully wrote to staking.json');
}
});
}
// Run fetchStaking every 1 minute (60000 milliseconds)
setInterval(fetchStaking, 60000);
// Initial call to fetchStaking to run it immediately
fetchStaking();