Skip to content

Instantly share code, notes, and snippets.

@togosh
Last active November 4, 2021 23:03
Show Gist options
  • Save togosh/db6cd8d974f4242ee77479e8a3b2002b to your computer and use it in GitHub Desktop.
Save togosh/db6cd8d974f4242ee77479e8a3b2002b to your computer and use it in GitHub Desktop.
Total Tshares, Daily Payout & Payout per Tshare of HEX
// https://codeakk.medium.com/hex-development-data-a1b1822446fa
// https://togosh.medium.com/hex-developer-guide-3b018a943a55
test();
async function test(){
var day = 622;
var { dailyPayoutHEX, totalTshares, success } = await get_dailyDataUpdate(day);
if (success) {
console.log("Total Tshares: " + Number(totalTshares).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
console.log("Daily Payout: " + Number(dailyPayoutHEX).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
console.log("Payout per Tshare: " + Number(dailyPayoutHEX / totalTshares).toLocaleString(undefined, {minimumFractionDigits:3, maximumFractionDigits:3}));
}
}
async function get_dailyDataUpdate(currentDay){
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: 1,
orderDirection: desc,
orderBy: timestamp,
where: {
endDay: ` + currentDay + `,
}
) {
id
payout
shares
payoutPerTShare
endDay
}
}`
}),
})
.then(res => res.json())
.then(res => {
if (Object.keys(res.data.dailyDataUpdates).length <= 0){
return {
success: false
};
}
var payout = res.data.dailyDataUpdates[0].payout;
payout = payout.substring(0, payout.length - 8) + "." + payout.substring(payout.length - 8);
var totalTshares = res.data.dailyDataUpdates[0].shares;
if (totalTshares == 0) {
totalTshares = "0";
} else {
totalTshares = totalTshares.substring(0, totalTshares.length - 12) + "." + totalTshares.substring(totalTshares.length - 12);
}
return {
dailyPayoutHEX: parseFloat(payout),
totalTshares: parseFloat(totalTshares),
success: true
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment