Skip to content

Commit

Permalink
Use shallow copy
Browse files Browse the repository at this point in the history
  • Loading branch information
pxrl committed Jan 2, 2025
1 parent d8cf915 commit 75566e5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ export class CommonConfig {
ARWEAVE_GATEWAY,
} = env;

const updateConfig = (envVar: string, config: Record<string, unknown>) =>
Object.entries(JSON.parse(envVar ?? "{}")).forEach(([k, v]) => (config[k] = v));
const mergeConfig = <T>(config: T, envVar: string): T => {
const shallowCopy = { ...config };
Object.entries(JSON.parse(envVar ?? "{}")).forEach(([k, v]) => {
assert(
typeof v === typeof shallowCopy[k] || !isDefined(shallowCopy[k]),
`Invalid ${envVar} configuration on key ${k} (${typeof v} != ${typeof shallowCopy[k]})`
);
shallowCopy[k] = v;
});
return shallowCopy;
};

this.version = ACROSS_BOT_VERSION ?? "unknown";
this.hubPoolChainId = Number(HUB_CHAIN_ID ?? CHAIN_IDs.MAINNET);
Expand All @@ -60,8 +69,7 @@ export class CommonConfig {
this.maxConfigVersion = Number(ACROSS_MAX_CONFIG_VERSION ?? Constants.CONFIG_STORE_VERSION);
assert(!isNaN(this.maxConfigVersion), `Invalid maximum config version: ${this.maxConfigVersion}`);

this.blockRangeEndBlockBuffer = { ...Constants.BUNDLE_END_BLOCK_BUFFERS };
updateConfig(BLOCK_RANGE_END_BLOCK_BUFFER, this.blockRangeEndBlockBuffer);
this.blockRangeEndBlockBuffer = mergeConfig(Constants.BUNDLE_END_BLOCK_BUFFERS, BLOCK_RANGE_END_BLOCK_BUFFER);

this.ignoredAddresses = JSON.parse(IGNORED_ADDRESSES ?? "[]").map((address) => ethers.utils.getAddress(address));

Expand All @@ -71,8 +79,7 @@ export class CommonConfig {
this.spokePoolChainsOverride = JSON.parse(SPOKE_POOL_CHAINS_OVERRIDE ?? "[]");

// Inherit the default eth_getLogs block range config, then sub in any env-based overrides.
this.maxBlockLookBack = { ...Constants.CHAIN_MAX_BLOCK_LOOKBACK };
updateConfig(MAX_BLOCK_LOOK_BACK, this.maxBlockLookBack);
this.maxBlockLookBack = mergeConfig(Constants.CHAIN_MAX_BLOCK_LOOKBACK, MAX_BLOCK_LOOK_BACK);

this.sendingTransactionsEnabled = SEND_TRANSACTIONS === "true";

Expand Down

0 comments on commit 75566e5

Please sign in to comment.