Skip to content

Commit

Permalink
feat: use BEACON_RPC_PROVIDER
Browse files Browse the repository at this point in the history
  • Loading branch information
tnunamak authored Oct 25, 2024
1 parent 4221b82 commit fa97dca
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions stats-client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const STATS_SERVER_URL = process.env.STATS_SERVER_URL;
const GETH_URL = process.env.GETH_URL;
const INSTANCE_NAME = process.env.INSTANCE_NAME;
const WS_SECRET = process.env.WS_SECRET;
const BEACON_NODE_API = process.env.BEACON_NODE_API;
const BEACON_RPC_PROVIDER = process.env.BEACON_RPC_PROVIDER || '127.0.0.1:4000';
const PUBLIC_KEY = process.env.PUBLIC_KEY;

// Define stats object
Expand Down Expand Up @@ -144,16 +144,16 @@ async function getContainerOsInfo(containerName) {
// Function to fetch beacon node stats
async function getBeaconNodeStats() {
try {
const response = await fetch(`${BEACON_NODE_API}`);
const response = await fetch(`http://${BEACON_RPC_PROVIDER}/eth/v1/node/syncing`);
if (!response.ok) {
throw new Error(`Beacon node API request failed with status ${response.status}`);
}
const data = await response.json();

return {
headSlot: data.head_slot,
finalizedEpoch: data.finalized_epoch,
currentEpoch: data.current_epoch,
headSlot: parseInt(data.data.head_slot),
finalizedEpoch: parseInt(data.data.finalized_epoch),
currentEpoch: parseInt(data.data.current_epoch),
};
} catch (err) {
console.error('Error fetching beacon node stats:', err);
Expand All @@ -164,30 +164,24 @@ async function getBeaconNodeStats() {
// Function to fetch validator data
async function getValidatorData() {
try {
const validatorResponse = await fetch(`${BEACON_NODE_API}/validators`);
const validatorResponse = await fetch(`http://${BEACON_RPC_PROVIDER}/eth/v1/beacon/states/head/validators/${PUBLIC_KEY}`);
if (!validatorResponse.ok) {
throw new Error(`Beacon node API request failed with status ${validatorResponse.status}`);
}
const validatorJson = await validatorResponse.json();

if (validatorJson.data && validatorJson.data.length > 0) {
const matchingValidator = validatorJson.data.find(v => v.validator.pubkey === PUBLIC_KEY);

if (matchingValidator) {
const { validator } = matchingValidator;
return {
balance: matchingValidator.balance,
status: matchingValidator.status,
effectiveBalance: validator.effective_balance,
slashed: validator.slashed,
activation_eligibility_epoch: validator.activation_eligibility_epoch,
activation_epoch: validator.activation_epoch,
exit_epoch: validator.exit_epoch,
withdrawable_epoch: validator.withdrawable_epoch,
};
} else {
console.error('Validator with public key not found.');
}
if (validatorJson.data) {
const validator = validatorJson.data;
return {
balance: parseInt(validator.balance),
status: validator.status,
effectiveBalance: parseInt(validator.validator.effective_balance),
slashed: validator.validator.slashed,
activation_eligibility_epoch: parseInt(validator.validator.activation_eligibility_epoch),
activation_epoch: parseInt(validator.validator.activation_epoch),
exit_epoch: parseInt(validator.validator.exit_epoch),
withdrawable_epoch: parseInt(validator.validator.withdrawable_epoch),
};
}
return null;
} catch (error) {
Expand Down

0 comments on commit fa97dca

Please sign in to comment.