Skip to content

Commit

Permalink
common: add viem client common functions (#1299)
Browse files Browse the repository at this point in the history
  • Loading branch information
kayleegeorge authored Aug 30, 2024
1 parent f08bd15 commit 01621b8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/daimo-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from "./chain";
export * from "./retryBackoff";
export * from "./cctp";
export * from "./sendPair";
export * from "./viemClient";
64 changes: 64 additions & 0 deletions packages/daimo-common/src/viemClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { extractChain, Chain } from "viem";
import {
mainnet,
sepolia,
base,
optimism,
polygon,
baseSepolia,
arbitrum,
arbitrumSepolia,
optimismSepolia,
avalanche,
avalancheFuji,
} from "viem/chains";

/** Retrieve the Viem chain config for a given chainId. */
export function getViemChainById(chainId: number): Chain {
if (!supportedChainIds.includes(chainId))
throw new Error(`Unsupported chainId ${chainId}`);

return extractChain({
chains: [
mainnet,
sepolia,
polygon,
arbitrum,
arbitrumSepolia,
base,
baseSepolia,
optimism,
optimismSepolia,
avalanche,
avalancheFuji,
],
id: chainId as any,
});
}

/** Get the RPC URL for a given chainId. */
export function getAlchemyTransportUrl(chainId: number, alchemyApiKey: string) {
const network = alchemyChainNames[chainId];
if (!network) {
throw new Error(`Invalid chainId: ${chainId}`);
}
const alchemyRpcUrl = `https://${network}.g.alchemy.com/v2/${alchemyApiKey}`;
return alchemyRpcUrl;
}

/**
* Mapping of chainID to Alchemy network name for RPC URLs.
* Note: Avax is not supported by Alchemy, nor do we support it.
*/
const alchemyChainNames: Record<number, string> = {
[mainnet.id]: "eth-mainnet",
[sepolia.id]: "eth-sepolia",
[polygon.id]: "polygon-mainnet",
[arbitrum.id]: "arb-mainnet",
[arbitrumSepolia.id]: "arb-sepolia",
[base.id]: "base-mainnet",
[baseSepolia.id]: "base-sepolia",
[optimism.id]: "opt-mainnet",
[optimismSepolia.id]: "opt-sepolia",
};
const supportedChainIds = Object.keys(alchemyChainNames).map(Number);

0 comments on commit 01621b8

Please sign in to comment.