Skip to content

Instantly share code, notes, and snippets.

@togosh
Created October 28, 2021 17:39
Show Gist options
  • Save togosh/97cfa35e8355fa2b446f72839150d557 to your computer and use it in GitHub Desktop.
Save togosh/97cfa35e8355fa2b446f72839150d557 to your computer and use it in GitHub Desktop.
Total Value Locked of HEX
// https://codeakk.medium.com/hex-development-data-a1b1822446fa
// https://togosh.medium.com/hex-developer-guide-3b018a943a55
// https://thegraph.com/legacy-explorer/subgraph/uniswap/uniswap-v2
// https://thegraph.com/legacy-explorer/subgraph/uniswap/uniswap-v3
// https://etherscan.io/apis
// NOTE: REPLACE XXXXXXXX with Etherscan API Key
test();
async function test(){
var priceUV2 = await getUniswapV2HEXDailyPrice();
console.log("HEX Price Uniswap V2: " + Number(priceUV2).toLocaleString(undefined, {minimumFractionDigits:4, maximumFractionDigits:4}));
var { stakedHEX } = await getGlobalInfo();
console.log("Staked Supply: " + Number(stakedHEX).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
var totalValueLocked = priceUV2 * stakedHEX;
console.log("Total Value Locked: " + Number(totalValueLocked).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
}
async function getUniswapV2HEXDailyPrice(){
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `
query {
tokenDayDatas (
first: 1,
orderBy: date,
orderDirection: desc,
where: {
token: "0x2b591e99afe9f32eaa6214f7b7629768c40eeb39"
})
{
date
token { symbol }
priceUSD
}
}`
}),
})
.then(res => res.json())
.then(res => {
var tokenDayData = res.data.tokenDayDatas[0];
return parseFloat(tokenDayData.priceUSD);
});
}
async function getGlobalInfo(){
const HEX_CONTRACT_ADDRESS = "0x2b591e99afe9f32eaa6214f7b7629768c40eeb39";
const HEX_CONTRACT_GLOBALINFO = "0xf04b5fa0";
const ETHERSCAN_APIKEY = "XXXXXXXX"
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);
return {
circulatingHEX: parseInt(circulatingSupply),
stakedHEX: parseInt(stakedHEX),
totalTshares: parseFloat(totalTshares),
};
});
}
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