Skip to content

Commit

Permalink
ref(relay-utils): Refactor getNetworkConfig func to use beaconApi class
Browse files Browse the repository at this point in the history
  • Loading branch information
kkirkov authored and Dimo99 committed Dec 19, 2024
1 parent 05fd36b commit 1d9405c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 87 deletions.
73 changes: 22 additions & 51 deletions relay/utils/get_current_network_config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BeaconApi, getBeaconApi } from '@/implementations/beacon-api';

interface Config {
NETWORK_NAME: string;
BEACON_REST_API: string[];
Expand Down Expand Up @@ -40,60 +42,29 @@ export async function getNetworkConfig(
): Promise<Config> {
let config: Config = { ...defaultConfig, NETWORK_NAME: network };
config.NETWORK_NAME = network;
switch (network) {
case 'pratter': {
config.BEACON_REST_API[0] =
process.env.BEACON_REST_API_PRATER || 'default_prater_rest_api_url';
break;
}
case 'mainnet': {
config.BEACON_REST_API[0] =
process.env.BEACON_REST_API_MAINNET || 'default_mainnet_rest_api_url';
break;
}
case 'sepolia': {
config.BEACON_REST_API[0] =
process.env.BEACON_REST_API_SEPOLIA || 'default_sepolia_rest_api_url';
break;
}
case 'chiado': {
config.BEACON_REST_API[0] =
process.env.BEACON_REST_API_CHIADO || 'default_chiado_rest_api_url';
break;
}
case 'gnosis': {
config.BEACON_REST_API[0] =
process.env.BEACON_REST_API_GNOSIS || 'default_gnosis_rest_api_url';
break;
}
default: {
throw new Error('Network not supported');
break;
}
}

const response = await fetch(config.BEACON_REST_API + '/eth/v1/config/spec');
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
const responseGenesis = await fetch(
config.BEACON_REST_API + '/eth/v1/beacon/genesis',
);
if (!responseGenesis.ok) {
throw new Error(
'Network response was not ok ' + responseGenesis.statusText,
);
const envVarName = `BEACON_REST_API_${network.toUpperCase()}`;
const envVarValue = process.env[envVarName];

if (!envVarValue) {
throw new Error(`${envVarName} is not defined`);
}
const config_ = await response.json();
const config_genesis = await responseGenesis.json();

config.SLOTS_PER_EPOCH = config_.data.SLOTS_PER_EPOCH;
config.EPOCHS_PER_SYNC_COMMITTEE_PERIOD =
config_.data.EPOCHS_PER_SYNC_COMMITTEE_PERIOD;
config.GENESIS_FORK_VERSION = config_.data.GENESIS_FORK_VERSION;
config.FORK_VERSION = config_.data.DENEB_FORK_VERSION;
config.DOMAIN_SYNC_COMMITTEE = config_.data.DOMAIN_SYNC_COMMITTEE;
config.GENESIS_VALIDATORS_ROOT = config_genesis.data.genesis_validators_root;
config.BEACON_REST_API = envVarValue.split(',');

const beaconApi = await getBeaconApi(config.BEACON_REST_API);

const config_genesis = await beaconApi.getGenesisData();

config.SLOTS_PER_EPOCH = Number(await beaconApi.getSlotsPerEpoch());
config.EPOCHS_PER_SYNC_COMMITTEE_PERIOD = Number(
await beaconApi.getSlotsPerSyncCommitteePeriod(),
);
config.GENESIS_FORK_VERSION = config_genesis.genesisForkVersion.toString();
config.FORK_VERSION = await beaconApi.getForkVersion();
config.DOMAIN_SYNC_COMMITTEE = await beaconApi.getDomainSyncCommittee();
config.GENESIS_VALIDATORS_ROOT =
config_genesis.genesisValidatorsRoot.toString();

return config;
}
72 changes: 36 additions & 36 deletions yarn-project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -130,61 +130,61 @@ let
"bufferutil@npm:4.0.8" \
${isolated."bufferutil@npm:4.0.8"} \
".yarn/unplugged/bufferutil-npm-4.0.8-8005ed6210/node_modules/bufferutil"
echo 'injecting build for utf-8-validate'
echo 'injecting build for secp256k1'
yarn nixify inject-build \
"utf-8-validate@npm:5.0.10" \
${isolated."utf-8-validate@npm:5.0.10"} \
".yarn/unplugged/utf-8-validate-npm-5.0.10-93e9b6f750/node_modules/utf-8-validate"
"secp256k1@npm:4.0.3" \
${isolated."secp256k1@npm:4.0.3"} \
".yarn/unplugged/secp256k1-npm-4.0.3-b4e9ce065b/node_modules/secp256k1"
echo 'injecting build for keccak'
yarn nixify inject-build \
"keccak@npm:3.0.4" \
${isolated."keccak@npm:3.0.4"} \
".yarn/unplugged/keccak-npm-3.0.4-a84763aab8/node_modules/keccak"
echo 'injecting build for secp256k1'
echo 'injecting build for utf-8-validate'
yarn nixify inject-build \
"secp256k1@npm:4.0.3" \
${isolated."secp256k1@npm:4.0.3"} \
".yarn/unplugged/secp256k1-npm-4.0.3-b4e9ce065b/node_modules/secp256k1"
echo 'injecting build for bcrypto'
"utf-8-validate@npm:5.0.7" \
${isolated."utf-8-validate@npm:5.0.7"} \
".yarn/unplugged/utf-8-validate-npm-5.0.7-88d731f8ad/node_modules/utf-8-validate"
echo 'injecting build for utf-8-validate'
yarn nixify inject-build \
"bcrypto@npm:5.5.2" \
${isolated."bcrypto@npm:5.5.2"} \
".yarn/unplugged/bcrypto-npm-5.5.2-f3838b92ce/node_modules/bcrypto"
"utf-8-validate@npm:5.0.10" \
${isolated."utf-8-validate@npm:5.0.10"} \
".yarn/unplugged/utf-8-validate-npm-5.0.10-93e9b6f750/node_modules/utf-8-validate"
echo 'injecting build for blake-hash'
yarn nixify inject-build \
"blake-hash@npm:2.0.0" \
${isolated."blake-hash@npm:2.0.0"} \
".yarn/unplugged/blake-hash-npm-2.0.0-c63b9a2c2d/node_modules/blake-hash"
echo 'injecting build for bcrypt'
echo 'injecting build for bufferutil'
yarn nixify inject-build \
"bcrypt@npm:5.0.1" \
${isolated."bcrypt@npm:5.0.1"} \
".yarn/unplugged/bcrypt-npm-5.0.1-6815be1cfe/node_modules/bcrypt"
"bufferutil@npm:4.0.5" \
${isolated."bufferutil@npm:4.0.5"} \
".yarn/unplugged/bufferutil-npm-4.0.5-88cc521694/node_modules/bufferutil"
echo 'injecting build for leveldown'
yarn nixify inject-build \
"leveldown@npm:6.1.0" \
${isolated."leveldown@npm:6.1.0"} \
".yarn/unplugged/leveldown-npm-6.1.0-c2d7a4250d/node_modules/leveldown"
echo 'injecting build for keccak'
yarn nixify inject-build \
"keccak@npm:3.0.1" \
${isolated."keccak@npm:3.0.1"} \
".yarn/unplugged/keccak-npm-3.0.1-9f0a714d5c/node_modules/keccak"
echo 'injecting build for bufferutil'
yarn nixify inject-build \
"bufferutil@npm:4.0.5" \
${isolated."bufferutil@npm:4.0.5"} \
".yarn/unplugged/bufferutil-npm-4.0.5-88cc521694/node_modules/bufferutil"
echo 'injecting build for keccak'
yarn nixify inject-build \
"keccak@npm:3.0.2" \
${isolated."keccak@npm:3.0.2"} \
".yarn/unplugged/keccak-npm-3.0.2-6e9dec8765/node_modules/keccak"
echo 'injecting build for leveldown'
echo 'injecting build for bcrypt'
yarn nixify inject-build \
"leveldown@npm:6.1.0" \
${isolated."leveldown@npm:6.1.0"} \
".yarn/unplugged/leveldown-npm-6.1.0-c2d7a4250d/node_modules/leveldown"
echo 'injecting build for utf-8-validate'
"bcrypt@npm:5.0.1" \
${isolated."bcrypt@npm:5.0.1"} \
".yarn/unplugged/bcrypt-npm-5.0.1-6815be1cfe/node_modules/bcrypt"
echo 'injecting build for bcrypto'
yarn nixify inject-build \
"utf-8-validate@npm:5.0.7" \
${isolated."utf-8-validate@npm:5.0.7"} \
".yarn/unplugged/utf-8-validate-npm-5.0.7-88d731f8ad/node_modules/utf-8-validate"
"bcrypto@npm:5.5.2" \
${isolated."bcrypto@npm:5.5.2"} \
".yarn/unplugged/bcrypto-npm-5.5.2-f3838b92ce/node_modules/bcrypto"
echo 'injecting build for redis-commander'
yarn nixify inject-build \
"redis-commander@npm:0.8.0" \
Expand Down Expand Up @@ -249,16 +249,16 @@ let

isolated."msgpackr-extract@npm:3.0.2" = optionalOverride (args.overrideMsgpackrExtractAttrs or null) (mkIsolatedBuild { pname = "msgpackr-extract"; version = "3.0.2"; reference = "npm:3.0.2"; });
isolated."bufferutil@npm:4.0.8" = optionalOverride (args.overrideBufferutilAttrs or null) (mkIsolatedBuild { pname = "bufferutil"; version = "4.0.8"; reference = "npm:4.0.8"; });
isolated."utf-8-validate@npm:5.0.10" = optionalOverride (args.overrideUtf8ValidateAttrs or null) (mkIsolatedBuild { pname = "utf-8-validate"; version = "5.0.10"; reference = "npm:5.0.10"; });
isolated."keccak@npm:3.0.4" = optionalOverride (args.overrideKeccakAttrs or null) (mkIsolatedBuild { pname = "keccak"; version = "3.0.4"; reference = "npm:3.0.4"; });
isolated."secp256k1@npm:4.0.3" = optionalOverride (args.overrideSecp256k1Attrs or null) (mkIsolatedBuild { pname = "secp256k1"; version = "4.0.3"; reference = "npm:4.0.3"; });
isolated."bcrypto@npm:5.5.2" = optionalOverride (args.overrideBcryptoAttrs or null) (mkIsolatedBuild { pname = "bcrypto"; version = "5.5.2"; reference = "npm:5.5.2"; });
isolated."keccak@npm:3.0.4" = optionalOverride (args.overrideKeccakAttrs or null) (mkIsolatedBuild { pname = "keccak"; version = "3.0.4"; reference = "npm:3.0.4"; });
isolated."utf-8-validate@npm:5.0.7" = optionalOverride (args.overrideUtf8ValidateAttrs or null) (mkIsolatedBuild { pname = "utf-8-validate"; version = "5.0.7"; reference = "npm:5.0.7"; });
isolated."utf-8-validate@npm:5.0.10" = optionalOverride (args.overrideUtf8ValidateAttrs or null) (mkIsolatedBuild { pname = "utf-8-validate"; version = "5.0.10"; reference = "npm:5.0.10"; });
isolated."blake-hash@npm:2.0.0" = optionalOverride (args.overrideBlakeHashAttrs or null) (mkIsolatedBuild { pname = "blake-hash"; version = "2.0.0"; reference = "npm:2.0.0"; });
isolated."bcrypt@npm:5.0.1" = optionalOverride (args.overrideBcryptAttrs or null) (mkIsolatedBuild { pname = "bcrypt"; version = "5.0.1"; reference = "npm:5.0.1"; });
isolated."keccak@npm:3.0.1" = optionalOverride (args.overrideKeccakAttrs or null) (mkIsolatedBuild { pname = "keccak"; version = "3.0.1"; reference = "npm:3.0.1"; });
isolated."bufferutil@npm:4.0.5" = optionalOverride (args.overrideBufferutilAttrs or null) (mkIsolatedBuild { pname = "bufferutil"; version = "4.0.5"; reference = "npm:4.0.5"; });
isolated."keccak@npm:3.0.2" = optionalOverride (args.overrideKeccakAttrs or null) (mkIsolatedBuild { pname = "keccak"; version = "3.0.2"; reference = "npm:3.0.2"; });
isolated."leveldown@npm:6.1.0" = optionalOverride (args.overrideLeveldownAttrs or null) (mkIsolatedBuild { pname = "leveldown"; version = "6.1.0"; reference = "npm:6.1.0"; });
isolated."utf-8-validate@npm:5.0.7" = optionalOverride (args.overrideUtf8ValidateAttrs or null) (mkIsolatedBuild { pname = "utf-8-validate"; version = "5.0.7"; reference = "npm:5.0.7"; });
isolated."keccak@npm:3.0.1" = optionalOverride (args.overrideKeccakAttrs or null) (mkIsolatedBuild { pname = "keccak"; version = "3.0.1"; reference = "npm:3.0.1"; });
isolated."keccak@npm:3.0.2" = optionalOverride (args.overrideKeccakAttrs or null) (mkIsolatedBuild { pname = "keccak"; version = "3.0.2"; reference = "npm:3.0.2"; });
isolated."bcrypt@npm:5.0.1" = optionalOverride (args.overrideBcryptAttrs or null) (mkIsolatedBuild { pname = "bcrypt"; version = "5.0.1"; reference = "npm:5.0.1"; });
isolated."bcrypto@npm:5.5.2" = optionalOverride (args.overrideBcryptoAttrs or null) (mkIsolatedBuild { pname = "bcrypto"; version = "5.5.2"; reference = "npm:5.5.2"; });
isolated."redis-commander@npm:0.8.0" = optionalOverride (args.overrideRedisCommanderAttrs or null) (mkIsolatedBuild { pname = "redis-commander"; version = "0.8.0"; reference = "npm:0.8.0"; });
in overriddenProject

0 comments on commit 1d9405c

Please sign in to comment.