Last active
February 14, 2025 11:41
-
-
Save Amuhar/03a3ab70bf41eb13d37966c400ba3a1c to your computer and use it in GitHub Desktop.
Lido active keys
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
#!/usr/bin/env node | |
const fs = require('fs'); | |
// Console colors for messages | |
const RED = '\x1b[31m'; | |
const GREEN = '\x1b[32m'; | |
const NC = '\x1b[0m'; // Reset color | |
// Ensure KEYS_API_URL is set | |
const KEYS_API_URL = process.env.KEYS_API_URL; | |
const FILE_OUTPUT = process.env.FILE_OUTPUT || 'filtered_keys.json'; | |
if (!KEYS_API_URL) { | |
console.error(`${RED}Error: KEYS_API_URL is not set.${NC}`); | |
process.exit(1); | |
} | |
async function fetchData(url) { | |
try { | |
console.log(`Fetching: ${url}`); | |
const response = await fetch(url); | |
if (!response.ok) { | |
throw new Error(`HTTP error! Status: ${response.status}`); | |
} | |
return await response.json(); | |
} catch (error) { | |
console.error(`${RED}Error fetching data from ${url}:${NC}`, error); | |
process.exit(1); | |
} | |
} | |
async function main() { | |
const operatorsJson = await fetchData(`${KEYS_API_URL}/v1/operators`); | |
const keysJson = await fetchData(`${KEYS_API_URL}/v1/keys?used=true`); | |
const OPERATORS_LAST_CHANGED_BLOCK_HASH = operatorsJson?.meta?.elBlockSnapshot?.lastChangedBlockHash; | |
const KEYS_LAST_CHANGED_BLOCK_HASH = keysJson?.meta?.elBlockSnapshot?.lastChangedBlockHash; | |
if (OPERATORS_LAST_CHANGED_BLOCK_HASH !== KEYS_LAST_CHANGED_BLOCK_HASH) { | |
console.error( | |
`${RED}Error: A key update occurred between the /operators and /keys requests. Please try running the script again.${NC}`, | |
); | |
process.exit(1); | |
} | |
const operators = operatorsJson.data | |
.flatMap((d) => d.operators) | |
.map(({ index, moduleAddress, stoppedValidators }) => ({ | |
index, | |
moduleAddress, | |
stoppedValidators, | |
})); | |
// Filter active keys | |
const filteredKeys = keysJson.data.filter((key) => | |
operators.some( | |
(op) => | |
key.used === true && | |
key.operatorIndex === op.index && | |
key.moduleAddress === op.moduleAddress && | |
key.index >= op.stoppedValidators, | |
), | |
); | |
fs.writeFileSync(FILE_OUTPUT, JSON.stringify(filteredKeys, null, 2), 'utf8'); | |
console.log(`${GREEN}Filtered keys saved to: ${FILE_OUTPUT}${NC}`); | |
console.log( | |
`${GREEN}Data was collected for block hash: ${keysJson?.meta?.elBlockSnapshot?.blockHash}, block number ${keysJson?.meta?.elBlockSnapshot?.blockNumber}${NC}`, | |
); | |
} | |
main().catch((error) => { | |
console.error(`${RED}Unhandled error in main:${NC}`, error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment