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 2 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://..."}}'
32 changes: 30 additions & 2 deletions src/context/chains.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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';

const RPC_OVERRIDES = process.env['NEXT_PUBLIC_RPC_OVERRIDES'];
AlexBHarley marked this conversation as resolved.
Show resolved Hide resolved

export async function assembleChainMetadata() {
// Chains must include a cosmos chain or CosmosKit throws errors
const result = z.record(ChainMetadataSchema).safeParse({
Expand All @@ -20,6 +23,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(RPC_OVERRIDES ?? ''));
if (RPC_OVERRIDES && !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 +43,25 @@ export async function assembleChainMetadata() {
await registry.listRegistryContent();
}

const chains = { ...defaultChainMetadata, ...customChainMetadata };
const chains: ChainMap<ChainMetadata> = Object.entries({
...defaultChainMetadata,
...customChainMetadata,
}).reduce((accum, [name, chain]) => {
const privateRpc = rpcOverrides.success ? rpcOverrides.data[name] : null;
if (privateRpc) {
return {
...accum,
[name]: {
...chain,
rpurls: chain.rpcUrls,
AlexBHarley marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
return {
...accum,
[name]: chain,
};
}, {});

return { chains, registry };
}
8 changes: 8 additions & 0 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function tryParseJson(input: string): unknown | null {
try {
return JSON.parse(input);
} catch (e) {
console.warn('unable to parse JSON', e);
AlexBHarley marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}
Loading