UNPKG

near-ca

Version:

An SDK for controlling Ethereum Accounts from a Near Account.

102 lines (101 loc) 3.49 kB
import { createPublicClient, http } from "viem"; import * as chains from "viem/chains"; import { CHAIN_INFO } from "./constants"; /** Custom RPC endpoint overrides for specific chain IDs */ const rpcOverrides = { 43114: "https://avalanche.drpc.org", 11155111: "https://ethereum-sepolia-rpc.publicnode.com", }; /** Map of all supported networks exported by viem */ const SUPPORTED_NETWORKS = createNetworkMap(Object.values(chains)); /** * Network class that provides access to network-specific data and functionality * Leverages network data provided through viem to make all relevant network fields * accessible dynamically by chain ID. */ export class Network { name; rpcUrl; chainId; scanUrl; client; icon; testnet; nativeCurrency; /** * Creates a new Network instance * * @param fields - Network configuration fields */ constructor({ name, rpcUrl, chainId, scanUrl, nativeCurrency, icon, }) { const network = SUPPORTED_NETWORKS[chainId]; this.name = name; this.rpcUrl = rpcUrl; this.chainId = chainId; this.scanUrl = scanUrl; this.client = createPublicClient({ transport: http(network.rpcUrl), }); this.testnet = network.testnet; this.nativeCurrency = nativeCurrency; this.icon = icon; } /** * Creates a Network instance from a chain ID * * @param chainId - The chain ID to create the network for * @param options - Optional configuration overrides * @returns A new Network instance * @throws Error if the chain ID is not supported */ static fromChainId(chainId, options = {}) { const networkFields = SUPPORTED_NETWORKS[chainId]; if (!networkFields) { throw new Error(`Network with chainId ${chainId} is not supported. Please reach out to the developers of https://github.com/bitteprotocol/near-ca`); } const mergedFields = { ...networkFields, // Manual Settings. rpcUrl: options.rpcUrl || networkFields.rpcUrl, scanUrl: options.scanUrl || networkFields.scanUrl, }; return new Network(mergedFields); } } /** * Creates a map of network configurations indexed by chain ID * * @param supportedNetworks - Array of Chain objects from viem * @returns A map of network configurations */ function createNetworkMap(supportedNetworks) { const networkMap = {}; supportedNetworks.forEach((network) => { const chainInfo = CHAIN_INFO[network.id]; const icon = chainInfo?.icon || `/${network.nativeCurrency.symbol}.svg`; networkMap[network.id] = { name: network.name, rpcUrl: rpcOverrides[network.id] || network.rpcUrls.default.http[0], chainId: network.id, scanUrl: network.blockExplorers?.default.url || "", icon, testnet: network.testnet || false, nativeCurrency: { ...network.nativeCurrency, wrappedAddress: chainInfo?.wrappedToken, icon: chainInfo?.currencyIcon || icon, }, }; }); return networkMap; } /** * Checks if a given chain ID corresponds to a test network * * @param chainId - The chain ID to check * @returns True if the network is a testnet, false otherwise */ export function isTestnet(chainId) { return Network.fromChainId(chainId).testnet; }