Created
April 15, 2023 21:58
-
-
Save mohamedmansour/0ec679393e0ea4f7a2a24c8f729cfd1e to your computer and use it in GitHub Desktop.
Beacon Chain: Calculate HEAD Slot
This file contains hidden or 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
const GENESIS_TIME = "2020-12-01T12:00:23Z" | |
const MILLISECONDS_PER_SLOT = 12000 | |
function toUTCTimestamp(time) { | |
return Date.UTC( | |
time.getUTCFullYear(), | |
time.getUTCMonth(), | |
time.getUTCDate(), | |
time.getUTCHours(), | |
time.getUTCMinutes(), | |
time.getUTCSeconds(), | |
time.getUTCMilliseconds() | |
) | |
} | |
function currentSlot() { | |
const genesisTimestamp = toUTCTimestamp(new Date(GENESIS_TIME)) | |
const currentTime = toUTCTimestamp(new Date) | |
return Math.floor((currentTime - genesisTimestamp) / MILLISECONDS_PER_SLOT) | |
} | |
console.log(currentSlot()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment