Skip to content

Instantly share code, notes, and snippets.

@togosh
Created August 26, 2021 08:23
Show Gist options
  • Save togosh/ead4497058ff771c7677617fa8e44d4e to your computer and use it in GitHub Desktop.
Save togosh/ead4497058ff771c7677617fa8e44d4e to your computer and use it in GitHub Desktop.
Ethereum Block by HEX Day
// https://codeakk.medium.com/hex-development-data-a1b1822446fa
// https://togosh.medium.com/hex-developer-guide-3b018a943a55
test();
async function test(){
var blockNumber = await getEthereumBlock(622);
console.log("Ethereum Block Number: " + Number(blockNumber).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0}));
}
async function getEthereumBlock(day){
var startTime = 1575417600 + ((day - 2) * 86400);
return await fetch('https://api.thegraph.com/subgraphs/name/blocklytics/ethereum-blocks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `
query {
blocks(
first: 1,
orderBy: timestamp,
orderDirection: asc,
where: {
timestamp_gt: ` + startTime + `
}
){
id
number
timestamp
}
}`
}),
})
.then(res => res.json())
.then(res => {
var block = res.data.blocks[0];
return block.number;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment