Skip to content

Instantly share code, notes, and snippets.

@togosh
Last active November 5, 2021 02:02
Show Gist options
  • Save togosh/96f4bea299081ae6f421d34f8b024105 to your computer and use it in GitHub Desktop.
Save togosh/96f4bea299081ae6f421d34f8b024105 to your computer and use it in GitHub Desktop.
Average Payout per Tshare of HEX between Days - Codeak API - Fast
// https://codeakk.medium.com/hex-development-data-a1b1822446fa
// https://togosh.medium.com/hex-developer-guide-3b018a943a55
test()
async function test(){
var startDay = 700; // 2 or higher
var endDay = 702;
var data = await get_dailyDataUpdateData();
var payoutPerTshareArray = data.dailyDataUpdates.map(a => Number(a.payoutPerTShare));
const average = (array) => array.reduce((a, b) => a + b) / array.length;
console.log("Average Payout per Tshare: " + average(payoutPerTshareArray.slice(startDay - 2, endDay - 1)));
}
async function get_dailyDataUpdateData(){
var $lastEndDay = 0;
var dailyDataUpdates = [];
while (true) {
var data = await get_dailyDataUpdates($lastEndDay);
if (data.count <= 0) { break; }
else {
$lastEndDay = data.lastEndDay;
dailyDataUpdates = dailyDataUpdates.concat(data.dailyDataUpdates);
console.log($lastEndDay);
await sleep(100);
}
}
return {
dailyDataUpdates: dailyDataUpdates
};
}
async function get_dailyDataUpdates($lastEndDay){
return await fetch('https://api.thegraph.com/subgraphs/name/codeakk/hex', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `
query {
dailyDataUpdates(first: 1000, orderBy: endDay,
where: {
endDay_gt: ` + $lastEndDay + `,
}
) {
id
payout
shares
payoutPerTShare
endDay
}
}`
}),
})
.then(res => res.json())
.then(res => {
var dailyUpdatesCount = Object.keys(res.data.dailyDataUpdates).length;
if (dailyUpdatesCount <= 0){
return {
count: 0,
success: false
};
}
var lastEndDay = res.data.dailyDataUpdates[(dailyUpdatesCount - 1)].endDay;
return {
count: dailyUpdatesCount,
dailyDataUpdates: res.data.dailyDataUpdates,
lastEndDay: Number(lastEndDay),
success: true
}
});
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment