-
Notifications
You must be signed in to change notification settings - Fork 2
/
thalesIO.js
182 lines (155 loc) · 5.93 KB
/
thalesIO.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { redisClient, connectDefaultRedisClient } = require("./redis/client");
require("dotenv").config();
const KEYS = require("./redis/redis-keys");
const fetch = require("node-fetch");
const { delay } = require("./services/utils");
let thalesIODuneDataMap = new Map();
let thalesIOWeeklyDuneDataMap = new Map();
let dailyStatsDisableFirstRunExecution = true;
let weeklyStatsDisableFirstRunExecution = true;
(async () => {
console.log("Start processing...");
await connectDefaultRedisClient();
if (process.env.REDIS_URL && process.env.DUNE_API_KEY) {
const thalesIOMapRaw = await redisClient.get(KEYS.THALES_IO_DAILY_STATS);
if (thalesIOMapRaw) {
thalesIODuneDataMap = new Map(JSON.parse(thalesIOMapRaw));
}
const thalesIOMapRawWeekly = await redisClient.get(KEYS.THALES_IO_WEEKLY_STATS);
if (thalesIOMapRawWeekly) {
thalesIOWeeklyDuneDataMap = new Map(JSON.parse(thalesIOMapRawWeekly));
}
setTimeout(async () => {
while (true) {
try {
if (!dailyStatsDisableFirstRunExecution) {
console.log("fetch daily thales io data from dune");
await fetchDailyDuneData();
} else {
dailyStatsDisableFirstRunExecution = false;
}
} catch (error) {
console.log("error fetching daily thales io data from dune: ", error);
}
await delay(24 * 60 * 60 * 1000); // 24h 24 * 60 * 60 * 1000
}
}, 3000);
setTimeout(async () => {
while (true) {
try {
if (!weeklyStatsDisableFirstRunExecution) {
console.log("fetch weekly thales io data from dune");
await fetchWeeklyDuneData();
} else {
weeklyStatsDisableFirstRunExecution = false;
}
} catch (error) {
console.log("error fetching weekly thales io data from dune: ", error);
}
await delay(60 * 60 * 1000); // 1h 60 * 60 * 1000
}
}, 3000);
}
})();
async function fetchDailyDuneData() {
try {
// executing queries costs credits, our API KEY has 2500 credits per month (80 daily)
// current credits being spent daily: 20
// current credits being spent weekly: 160
// execute queries
// 10 credits
await fetch("https://api.dune.com/api/v1/query/3350848/execute?api_key=" + process.env.DUNE_API_KEY, {
method: "POST",
});
// 10 credits
await fetch("https://api.dune.com/api/v1/query/3387824/execute?api_key=" + process.env.DUNE_API_KEY, {
method: "POST",
});
await delay(5 * 60 * 1000); // 5 minutes
//fetch data
const thalesStats = await fetch(
"https://api.dune.com/api/v1/query/3350848/results?api_key=" + process.env.DUNE_API_KEY,
);
const thalesStatsJson = await thalesStats.json();
const thalesTVLStats = await fetch(
"https://api.dune.com/api/v1/query/3387824/results?api_key=" + process.env.DUNE_API_KEY,
);
const thalesTVLStatsJson = await thalesTVLStats.json();
// Optional chaining not available in version 12 of node
if (
(((thalesStatsJson || {}).result || {}).rows || [])[0] &&
(((thalesTVLStatsJson || {}).result || {}).rows || [])[0]
) {
const allThalesStats = {
...thalesStatsJson.result.rows[0],
...thalesTVLStatsJson.result.rows.reduce((prev, curr) => {
return { ...prev, [toSnakeCase(curr.category + " tvl")]: curr.tvl };
}, {}),
};
thalesIODuneDataMap = new Map(Object.entries(allThalesStats));
redisClient.set(KEYS.THALES_IO_DAILY_STATS, JSON.stringify([...thalesIODuneDataMap]));
}
} catch (e) {
console.log(e);
}
}
async function fetchWeeklyDuneData() {
try {
// executing queries costs credits, our API KEY has 2500 credits per month (80 daily)
// current credits being spent daily: 20
// current credits being spent weekly: 160
// execute queries
// 10 credits
// check if current day is Wednesday and between 6 PM and 7 PM UTC in order to refresh CCIP data and save Dune credits
const now = new Date();
if (now.getDay() == 3 && now.getUTCHours() == 18) {
console.log("Thursday, time for fetching CCIP rev share data");
await fetch("https://api.dune.com/api/v1/query/3099522/execute?api_key=" + process.env.DUNE_API_KEY, {
method: "POST",
});
// 10 credits
await fetch("https://api.dune.com/api/v1/query/3400360/execute?api_key=" + process.env.DUNE_API_KEY, {
method: "POST",
});
await delay(5 * 60 * 1000); // 5 minutes
}
//fetch data
const thalesSafeboxFees = await fetch(
"https://api.dune.com/api/v1/query/3099522/results?api_key=" + process.env.DUNE_API_KEY,
);
const thalesSafeboxFeesJson = await thalesSafeboxFees.json();
const thalesRevShare = await fetch(
"https://api.dune.com/api/v1/query/3400360/results?api_key=" + process.env.DUNE_API_KEY,
);
const thalesRevShareJson = await thalesRevShare.json();
// Optional chaining not available in version 12 of node
if (
(((thalesSafeboxFeesJson || {}).result || {}).rows || [])[0] &&
(((thalesRevShareJson || {}).result || {}).rows || [])[0]
) {
const thalesSafeboxFeesPerWeekResponse = [];
thalesSafeboxFeesJson.result.rows.reduce(function (res, value) {
if (!res[value.w]) {
res[value.w] = {
fee: 0,
w: value.w,
};
thalesSafeboxFeesPerWeekResponse.push(res[value.w]);
}
res[value.w].fee += value.fee;
return res;
}, {});
const thalesSafeboxFeesPerWeek = Object.values(thalesSafeboxFeesPerWeekResponse);
thalesIOWeeklyDuneDataMap = new Map([
["safebox", thalesSafeboxFeesPerWeek],
["revShare", thalesRevShareJson.result.rows],
]);
redisClient.set(KEYS.THALES_IO_WEEKLY_STATS, JSON.stringify([...thalesIOWeeklyDuneDataMap]));
}
} catch (e) {
console.log(e);
}
}
function toSnakeCase(str) {
return str.replace(/ /g, "_").toLowerCase();
}