Skip to content

Instantly share code, notes, and snippets.

@togosh
Last active November 21, 2022 19:27
Show Gist options
  • Save togosh/49ba03b73d1f76ae0415b259a234dcd0 to your computer and use it in GitHub Desktop.
Save togosh/49ba03b73d1f76ae0415b259a234dcd0 to your computer and use it in GitHub Desktop.
Circulating & Staked HEX and Total Tshares from Etherscan API - https://etherscan.io/apis
// https://codeakk.medium.com/hex-development-data-a1b1822446fa
// https://togosh.medium.com/hex-developer-guide-3b018a943a55
// https://etherscan.io/apis
test();
async function test(){
var { circulatingHEX, stakedHEX, totalTshares, penaltiesHEX } = await getGlobalInfo();
console.log("Circulating Supply: " + Number(circulatingHEX).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
console.log("Staked Supply: " + Number(stakedHEX).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
console.log("Total T-Shares: " + Number(totalTshares).toLocaleString(undefined, {minimumFractionDigits:2, maximumFractionDigits:2}));
console.log("Total Penalties: " + Number(penaltiesHEX).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
}
async function getGlobalInfo(){
const HEX_CONTRACT_ADDRESS = "0x2b591e99afe9f32eaa6214f7b7629768c40eeb39";
const HEX_CONTRACT_GLOBALINFO = "0xf04b5fa0";
const ETHERSCAN_APIKEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
var etherScanURL =
"https://api.etherscan.io/api?" +
"module=proxy&action=eth_call" +
"&to=" + HEX_CONTRACT_ADDRESS +
"&data=" + HEX_CONTRACT_GLOBALINFO +
"&apikey=" + ETHERSCAN_APIKEY;
return await fetch(etherScanURL, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(res => {
var chunks = chunkSubstr(res.result.substring(2), 64);
var circulatingSupply = parseInt(chunks[11], 16).toString();
circulatingSupply = circulatingSupply.substring(0, circulatingSupply.length - 8);
var stakedHEX = parseInt(chunks[0], 16).toString();
stakedHEX = stakedHEX.substring(0, stakedHEX.length - 8);
var totalTshares = parseInt(chunks[5], 16).toString();
totalTshares = totalTshares.substring(0, totalTshares.length - 12) + "." + totalTshares.substring(totalTshares.length - 12);
var penalties = parseInt(chunks[3], 16).toString();
penalties = penalties.substring(0, penalties.length - 8) + "." + penalties.substring(penalties.length - 8);
penalties = parseFloat(penalties) * 2.0;
return {
circulatingHEX: parseInt(circulatingSupply),
stakedHEX: parseInt(stakedHEX),
totalTshares: parseFloat(totalTshares),
penaltiesHEX: penalties,
};
});
}
function chunkSubstr(str, size) {
const numChunks = Math.ceil(str.length / size);
const chunks = new Array(numChunks);
for (let i = 0, o = 0; i < numChunks; ++i, o += size) {
chunks[i] = str.substr(o, size);
}
return chunks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment