Last active
January 17, 2024 11:05
-
-
Save jeluard/96d5143b1e2df815b78f6ed1b1405c02 to your computer and use it in GitHub Desktop.
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"/> | |
<title></title> | |
<script type="module"> | |
import {ApiError, getClient} from "https://esm.sh/@lodestar/api@1.14.0"; | |
import {createChainForkConfig} from "https://esm.sh/@lodestar/config@1.14.0"; | |
import {networksChainConfig} from "https://esm.sh/@lodestar/config@1.14.0/networks"; | |
import { | |
Lightclient, | |
LightclientEvent, | |
RunStatusCode | |
} from "https://esm.sh/@lodestar/light-client@1.14.0"; | |
import {LightClientRestTransport} from "https://esm.sh/@lodestar/light-client@1.14.0/transport"; | |
import {getLcLoggerConsole} from "https://esm.sh/@lodestar/light-client@1.14.0/utils"; | |
async function getGenesisData(api) { | |
const res = await api.beacon.getGenesis(); | |
ApiError.assert(res); | |
return { | |
genesisTime: Number(res.response.data.genesisTime), | |
genesisValidatorsRoot: res.response.data.genesisValidatorsRoot, | |
}; | |
} | |
async function getSyncCheckpoint(api) { | |
const res = await api.beacon.getStateFinalityCheckpoints("head"); | |
ApiError.assert(res); | |
return res.response.data.finalized.root; | |
} | |
const config = createChainForkConfig(networksChainConfig.mainnet); | |
const logger = getLcLoggerConsole({logDebug: true}); | |
const api = getClient({urls: ["https://lodestar-mainnet.chainsafe.io"]}, {config}); | |
const transport = new LightClientRestTransport(api); | |
const client = await Lightclient.initializeFromCheckpointRoot({ | |
config, | |
logger, | |
transport, | |
genesisData: await getGenesisData(api), | |
checkpointRoot: await getSyncCheckpoint(api), | |
opts: { | |
allowForcedUpdates: true, | |
updateHeadersOnForcedUpdate: true, | |
} | |
}); | |
const div = document.createElement('div'); | |
div.innerText = `Current slot: ${client.getHead().beacon.slot}`; | |
document.body.appendChild(div); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment