Last active
September 15, 2021 21:54
-
-
Save togosh/210608b05ab3d635b3a0c11c62cf2406 to your computer and use it in GitHub Desktop.
Circulating Supply & Current Holders of HEX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://codeakk.medium.com/hex-development-data-a1b1822446fa | |
// https://togosh.medium.com/hex-developer-guide-3b018a943a55 | |
test(); | |
async function test(){ | |
var { circulatingSupply, currentHolders } = await get_tokenHoldersData(); | |
console.log("Circulating Supply: " + Number(circulatingSupply).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0})); | |
console.log("Current Holders: " + Number(currentHolders).toLocaleString(undefined)); | |
} | |
async function get_tokenHoldersData(){ | |
var $lastNumeralIndex = 0; | |
var circulatingSum = 0; | |
var uniqueAddressList = []; | |
var uniqueAddressCount = 0; | |
while (true) { | |
var data = undefined; | |
var retrieveCount = 0; | |
while (true) { | |
try { | |
data = await get_tokenHolders($lastNumeralIndex); | |
break; | |
} catch (error) { | |
retrieveCount += 1; | |
await sleep(1000 * retrieveCount); | |
if (retrieveCount > 3){ | |
throw error; | |
} | |
} | |
} | |
if (data.count <= 0) { break; } | |
circulatingSum += data.circulatingHEX; | |
uniqueAddressList = uniqueAddressList.concat(data.uniqueAddresses); | |
$lastNumeralIndex = data.lastNumeralIndex; | |
console.log($lastNumeralIndex); | |
await sleep(100); | |
} | |
uniqueAddressCount = (new Set(uniqueAddressList)).size; | |
return { | |
circulatingSupply: circulatingSum, | |
currentHolders: uniqueAddressCount, | |
} | |
} | |
async function get_tokenHolders($lastNumeralIndex){ | |
return await fetch('https://api.thegraph.com/subgraphs/name/codeakk/hex', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ query: ` | |
query { | |
tokenHolders(first: 1000, orderBy: numeralIndex, | |
where: { | |
numeralIndex_gt: "` + $lastNumeralIndex + `", | |
tokenBalance_gt: 0 | |
} | |
) { | |
numeralIndex | |
tokenBalance | |
holderAddress | |
} | |
}` | |
}), | |
}) | |
.then(res => res.json()) | |
.then(res => { | |
var tokenHolders = Object.keys(res.data.tokenHolders).length; | |
if (tokenHolders <= 0) { | |
return { | |
count: 0 | |
}; | |
} | |
else { | |
var tokenHoldersReduced = res.data.tokenHolders.reduce(function(previousValue, currentValue) { | |
return { | |
tokenBalance: parseInt(previousValue.tokenBalance, 10) + parseInt(currentValue.tokenBalance, 10), | |
} | |
}); | |
var lastNumeralIndex = res.data.tokenHolders[(tokenHolders - 1)].numeralIndex; | |
var uniqueAddresses = res.data.tokenHolders.map(a => a.holderAddress).filter(onlyUnique); | |
var data = { | |
count: tokenHolders, | |
circulatingHEX: tokenHoldersReduced.tokenBalance / 100000000, | |
uniqueAddresses: uniqueAddresses, | |
lastNumeralIndex: lastNumeralIndex | |
}; | |
return data; | |
}}); | |
} | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
function onlyUnique(value, index, self) { | |
return self.indexOf(value) === index; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment