|
| 1 | +import { expect, test } from 'bun:test' |
| 2 | + |
| 3 | +import * as chains from '~viem/chains/index.js' |
| 4 | +import type { Chain } from '~viem/types/chain.js' |
| 5 | +import { withTimeout } from '~viem/utils/promise/withTimeout.js' |
| 6 | +import { getSocket, rpc } from '~viem/utils/rpc.js' |
| 7 | + |
| 8 | +const defaultTimeout = 10_000 |
| 9 | + |
| 10 | +const chains_ = Object.values(chains) as readonly Chain[] |
| 11 | +chains_.forEach((chain) => { |
| 12 | + const httpRpcUrls = chain.rpcUrls.default.http |
| 13 | + if (httpRpcUrls) |
| 14 | + test( |
| 15 | + `${chain.name}: check http urls`, |
| 16 | + async () => { |
| 17 | + await assertHttpRpcUrls(chain.id, httpRpcUrls) |
| 18 | + }, |
| 19 | + { timeout: defaultTimeout }, |
| 20 | + ) |
| 21 | + |
| 22 | + const webSocketRpcUrls = chain.rpcUrls.default.webSocket |
| 23 | + if (webSocketRpcUrls) |
| 24 | + test( |
| 25 | + `${chain.name}: check web socket urls`, |
| 26 | + async () => { |
| 27 | + await assertWebSocketRpcUrls(chain.id, webSocketRpcUrls) |
| 28 | + }, |
| 29 | + { timeout: defaultTimeout }, |
| 30 | + ) |
| 31 | + |
| 32 | + const explorerUrl = chain.blockExplorers?.default.url |
| 33 | + if (explorerUrl) |
| 34 | + test( |
| 35 | + `${chain.name}: check block explorer`, |
| 36 | + async () => { |
| 37 | + await assertExplorerUrl(explorerUrl) |
| 38 | + }, |
| 39 | + { timeout: defaultTimeout }, |
| 40 | + ) |
| 41 | +}) |
| 42 | + |
| 43 | +function isLocalNetwork(url: string): boolean { |
| 44 | + const u = new URL(url) |
| 45 | + const localNetworkRegex = |
| 46 | + /^(127\.|10\.|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[0-1]\.|192\.168\.)/ |
| 47 | + |
| 48 | + return localNetworkRegex.test(u.hostname) || u.hostname === 'localhost' |
| 49 | +} |
| 50 | + |
| 51 | +async function assertHttpRpcUrls( |
| 52 | + chainId: number, |
| 53 | + rpcUrls: readonly string[], |
| 54 | +): Promise<void> { |
| 55 | + for (const url of rpcUrls) { |
| 56 | + if (isLocalNetwork(url)) continue |
| 57 | + const response = await rpc |
| 58 | + .http(url, { |
| 59 | + body: { method: 'eth_chainId' }, |
| 60 | + fetchOptions: { headers: { 'Content-Type': 'application/json' } }, |
| 61 | + timeout: defaultTimeout, |
| 62 | + }) |
| 63 | + .then((r) => r.result) |
| 64 | + expect(BigInt(response)).toBe(BigInt(chainId)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +async function assertWebSocketRpcUrls( |
| 69 | + chainId: number, |
| 70 | + rpcUrls: readonly string[], |
| 71 | +): Promise<void> { |
| 72 | + for (const url of rpcUrls) { |
| 73 | + if (isLocalNetwork(url)) continue |
| 74 | + |
| 75 | + const socket = await withTimeout(() => getSocket(url), { |
| 76 | + timeout: defaultTimeout, |
| 77 | + }) |
| 78 | + const response = await rpc |
| 79 | + .webSocketAsync(socket, { |
| 80 | + body: { method: 'eth_chainId' }, |
| 81 | + timeout: defaultTimeout, |
| 82 | + }) |
| 83 | + .then((r) => r.result) |
| 84 | + socket.close() |
| 85 | + expect(BigInt(response)).toBe(BigInt(chainId)) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +async function assertExplorerUrl(explorerUrl: string): Promise<void> { |
| 90 | + await fetch(explorerUrl, { method: 'HEAD' }) |
| 91 | +} |
0 commit comments