-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoOfStakedTokens.gs
More file actions
99 lines (86 loc) · 2.77 KB
/
noOfStakedTokens.gs
File metadata and controls
99 lines (86 loc) · 2.77 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Function to fetch total staked ROUTE data
function fetchTotalStakedROUTE() {
try {
const response = UrlFetchApp.fetch(
'https://hub.routerprotocol.com/_next/data/UKpFlD_rehphUPcO4uJNK/staking.json',
{ muteHttpExceptions: true }
);
const data = JSON.parse(response.getContentText());
return {
totalStaked: parseFloat(data.pageProps.totalStakedRoute),
apr: parseFloat(data.pageProps.apr),
inflationRate: parseFloat(data.pageProps.inflationRate),
unbondingPeriod: parseInt(data.pageProps.unbondingPeriod)
};
} catch (error) {
Logger.log('Error fetching staking data: ' + error);
return {
totalStaked: 0,
apr: 0,
inflationRate: 0,
unbondingPeriod: 0
};
}
}
// Main function to record staking data
function recordStakingData() {
try {
const ss = SpreadsheetApp.getActive();
let sheet = ss.getSheetByName('Total Staked ROUTE');
// Create sheet if it doesn't exist
if (!sheet) {
sheet = ss.insertSheet('Total Staked ROUTE');
// Add headers
sheet.getRange('A1:E1').setValues([[
'Timestamp',
'Total Staked ROUTE',
'APR (%)',
'Inflation Rate (%)',
'Unbonding Period (days)'
]]);
// Format headers
sheet.getRange('A1:E1')
.setBackground('#D3D3D3')
.setFontWeight('bold')
.setHorizontalAlignment('center');
}
// Fetch current data
const stakingData = fetchTotalStakedROUTE();
// Prepare row data
const rowData = [
new Date(),
stakingData.totalStaked,
stakingData.apr,
stakingData.inflationRate,
stakingData.unbondingPeriod
];
// 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');
sheet.getRange(nextRow, 3, 1, 2).setNumberFormat('#,##0.00"%"');
sheet.getRange(nextRow, 5).setNumberFormat('#,##0" days"');
// Auto-size columns
sheet.autoResizeColumns(1, rowData.length);
Logger.log('Staking data updated successfully');
} catch(error) {
Logger.log('Error recording staking data: ' + error);
}
}
// Create trigger to run every hour
function createHourlyStakingTrigger() {
// Delete any existing triggers
const triggers = ScriptApp.getProjectTriggers();
triggers.forEach(trigger => {
if(trigger.getHandlerFunction() === 'recordStakingData') {
ScriptApp.deleteTrigger(trigger);
}
});
// Create new hourly trigger
ScriptApp.newTrigger('recordStakingData')
.timeBased()
.everyHours(1)
.create();
}