Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created April 15, 2023 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamedmansour/0ec679393e0ea4f7a2a24c8f729cfd1e to your computer and use it in GitHub Desktop.
Save mohamedmansour/0ec679393e0ea4f7a2a24c8f729cfd1e to your computer and use it in GitHub Desktop.
Beacon Chain: Calculate HEAD Slot
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