-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoOfStakedRoute-lcd-api.gs
More file actions
76 lines (64 loc) · 2.15 KB
/
noOfStakedRoute-lcd-api.gs
File metadata and controls
76 lines (64 loc) · 2.15 KB
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
// Function to fetch and calculate total staked ROUTE
function fetchTotalRouteStaked() {
try {
const response = UrlFetchApp.fetch(
'https://sentry.lcd.routerprotocol.com/cosmos/staking/v1beta1/pool',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return Number(data.pool.bonded_tokens) / Math.pow(10, 18);
} catch (error) {
Logger.log('Error fetching staked data: ' + error);
return 0;
}
}
// Main function to record total staked data
function recordTotalStakedData() {
try {
const ss = SpreadsheetApp.getActive();
let sheet = ss.getSheetByName('Total Route Staked');
// Create sheet if it doesn't exist
if (!sheet) {
sheet = ss.insertSheet('Total Route Staked');
// Add headers
sheet.getRange('A1:B1').setValues([[
'Timestamp',
'Total Route Staked'
]]);
// Format headers
sheet.getRange('A1:B1')
.setBackground('#D3D3D3')
.setFontWeight('bold')
.setHorizontalAlignment('center');
}
// Fetch and prepare data
const totalStaked = fetchTotalRouteStaked();
const rowData = [new Date(), totalStaked];
// Add new row of data
const nextRow = sheet.getLastRow() + 1;
sheet.getRange(nextRow, 1, 1, rowData.length).setValues([rowData]);
// Format the new row
sheet.getRange(nextRow, 1).setNumberFormat('yyyy-mm-dd hh:mm:ss');
sheet.getRange(nextRow, 2).setNumberFormat('#,##0.000000');
// Auto-size columns
sheet.autoResizeColumns(1, 2);
Logger.log('Total staked data updated successfully');
} catch(error) {
Logger.log('Error recording total staked data: ' + error);
}
}
// Create trigger to run every hour
function createHourlyStakedDataTrigger() {
// Delete any existing triggers
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(trigger => {
if(trigger.getHandlerFunction() === 'recordTotalStakedData') {
ScriptApp.deleteTrigger(trigger);
}
});
// Create new hourly trigger
ScriptApp.newTrigger('recordTotalStakedData')
.timeBased()
.everyHours(1)
.create();
}