Created
August 26, 2021 08:23
-
-
Save togosh/ead4497058ff771c7677617fa8e44d4e to your computer and use it in GitHub Desktop.
Ethereum Block by HEX Day
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 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