Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC Overrides #179

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
NEXT_PUBLIC_WALLET_CONNECT_ID=12345678901234567890123456789012
NEXT_PUBLIC_WALLET_CONNECT_ID=12345678901234567890123456789012
NEXT_PUBLIC_RPC_OVERRIDES='{"chain1":{"http":"https://..."}}'
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"next",
"next/core-web-vitals",
"next",
"next/core-web-vitals",
"prettier"
],
"rules": {
"no-console": ["warn"],
"no-console": ["error"],
"no-eval": ["error"],
"no-ex-assign": ["error"],
"no-constant-condition": ["off"],
Expand Down
3 changes: 3 additions & 0 deletions src/consts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const explorerApiKeys = JSON.parse(process?.env?.EXPLORER_API_KEYS || '{}');
const walletConnectProjectId = process?.env?.NEXT_PUBLIC_WALLET_CONNECT_ID || '';
const withdrawalWhitelist = process?.env?.NEXT_PUBLIC_BLOCK_WITHDRAWAL_WHITELIST || '';
const transferBlacklist = process?.env?.NEXT_PUBLIC_TRANSFER_BLACKLIST || '';
const rpcOverrides = process?.env?.NEXT_PUBLIC_RPC_OVERRIDES || '';

interface Config {
isDevMode: boolean; // Enables some debug features in the app
Expand All @@ -20,6 +21,7 @@ interface Config {
transferBlacklist: string; // comma-separated list of routes between which transfers are disabled. Expects Caip2Id-Caip2Id (e.g. ethereum:1-sealevel:1399811149)
enableExplorerLink: boolean; // Include a link to the hyperlane explorer in the transfer modal
addressBlacklist: string[]; // A list of addresses that are blacklisted and cannot be used in the app
rpcOverrides: string;
}

export const config: Config = Object.freeze({
Expand All @@ -34,4 +36,5 @@ export const config: Config = Object.freeze({
transferBlacklist,
enableExplorerLink: false,
addressBlacklist: ADDRESS_BLACKLIST.map((address) => address.toLowerCase()),
rpcOverrides,
});
27 changes: 25 additions & 2 deletions src/context/chains.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { z } from 'zod';

import { GithubRegistry, chainMetadata } from '@hyperlane-xyz/registry';
import { ChainMap, ChainMetadata, ChainMetadataSchema } from '@hyperlane-xyz/sdk';
import { ChainMap, ChainMetadata, ChainMetadataSchema, RpcUrlSchema } from '@hyperlane-xyz/sdk';

import { chains as ChainsTS } from '../consts/chains.ts';
import ChainsYaml from '../consts/chains.yaml';
import { config } from '../consts/config.ts';
import { cosmosDefaultChain } from '../features/chains/cosmosDefault';
import { tryParseJson } from '../utils/json.ts';
import { logger } from '../utils/logger';

export async function assembleChainMetadata() {
Expand All @@ -20,6 +21,12 @@ export async function assembleChainMetadata() {
logger.warn('Invalid chain config', result.error);
throw new Error(`Invalid chain config: ${result.error.toString()}`);
}

const rpcOverrides = z.record(RpcUrlSchema).safeParse(tryParseJson(config.rpcOverrides));
if (config.rpcOverrides && !rpcOverrides.success) {
logger.warn('Invalid RPC overrides config', rpcOverrides.error);
}

const customChainMetadata = result.data as ChainMap<ChainMetadata>;

const registry = new GithubRegistry({ uri: config.registryUrl });
Expand All @@ -34,6 +41,22 @@ export async function assembleChainMetadata() {
await registry.listRegistryContent();
}

const chains = { ...defaultChainMetadata, ...customChainMetadata };
const chains: ChainMap<ChainMetadata> = Object.entries({
...defaultChainMetadata,
...customChainMetadata,
}).reduce(
(accum, [name, chain]) => ({
...accum,
[name]:
rpcOverrides.success && rpcOverrides.data[name]
? {
...chain,
rpcUrls: [rpcOverrides.data[name]],
}
: chain,
}),
{} as ChainMap<ChainMetadata>,
);

return { chains, registry };
}
10 changes: 10 additions & 0 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { logger } from './logger';

export function tryParseJson(input: string): unknown | null {
try {
return JSON.parse(input);
} catch (e) {
logger.warn('unable to parse JSON', e);
return null;
}
}
Loading