Skip to content

Commit

Permalink
Merge pull request ubiquity#28 from Keyrxng/improved-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 authored Jun 21, 2024
2 parents d5b3446 + edd481c commit 30022f4
Show file tree
Hide file tree
Showing 23 changed files with 31,815 additions and 1,279 deletions.
4 changes: 2 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"version": "0.2",
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "lib", "dist"],
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "lib", "dist", "types/dynamic.ts"],
"useGitignore": true,
"language": "en",
"words": ["dataurl", "devpool", "outdir", "servedir"],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
"ignoreRegExpList": ["[0-9a-fA-F]{6}"],
"ignoreWords": ["Rpcs", "ethersproject", "publicnode", "WXDAI", "XDAI", "chainlist", "Knip", "LOCALSTORAGE"]
"ignoreWords": ["Rpcs", "ethersproject", "publicnode", "WXDAI", "XDAI", "chainlist", "Knip", "LOCALSTORAGE", "sonarjs", "cronos", "Cronos", "gochain"]
}
5 changes: 4 additions & 1 deletion .github/workflows/jest-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ jobs:
- name: Install dependencies
run: yarn install

- name: Setup tests
run: yarn pretest

- name: Build & Run test suite
run: |
yarn test | tee ./coverage.txt && exit ${PIPESTATUS[0]}
- name: Jest Coverage Comment
# Ensures this step is run even on previous step failure (e.g. test failed)
if: always()
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ yarn test
```

- This below will only work after `yarn build` has been run and will fail under test conditions

```bash
npx tsx tests/script-test.ts
```
```
56 changes: 56 additions & 0 deletions build/dynamic-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { appendFile, writeFile } from "fs/promises";
import { BlockExplorer, NativeToken } from "../types/handler";
import { generateChainData } from "../lib/chainlist/utils/fetch";

/**
* This produces dynamic types and constants for:
* - CHAINS_IDS
* - NETWORKS
* - NETWORK_FAUCETS
* - NETWORK_EXPLORERS
* - NETWORK_CURRENCIES
* - EXTRA_RPCS
*/

export async function createDynamicTypes() {
const data = await generateChainData();
const idToNetwork: Record<string, string> = {};
const networkToId: Record<string, number> = {};
const idToRpc: Record<string, string[]> = {};
const idToFaucet: Record<string, string[]> = {};
const idToExplorers = {} as Record<string, BlockExplorer[]>;
const idToNativeCurrency: Record<string, NativeToken> = {};
const extraRpcs: Record<string, string[]> = {};

for (const chain of data) {
let { name, chainId, networkId, rpc, faucets, explorers, nativeCurrency } = chain;
name = name.toLowerCase().replace(/\s/g, "-");
idToNetwork[chainId] = name;
networkToId[name] = networkId;
idToRpc[chainId] = rpc;
idToFaucet[chainId] = faucets;

if (explorers && explorers.length > 0) {
idToExplorers[chainId] = explorers;
}

if (rpc && rpc.length > 0) {
const rpcs = rpc.filter((rpc: { url: string }) => rpc.url);
extraRpcs[chainId] = rpcs.map((rpc: { url: string }) => rpc.url);
}

idToNativeCurrency[chainId] = nativeCurrency;
}

const filename = "types/dynamic.ts";

// Clear the file
await writeFile(filename, "/* eslint-disable sonarjs/no-duplicate-string */\n\n");

appendFile(filename, `\nexport const CHAINS_IDS = ${JSON.stringify(idToNetwork, null, 2)} as const;\n`);
appendFile(filename, `\nexport const NETWORKS = ${JSON.stringify(networkToId, null, 2)} as const;\n`);
appendFile(filename, `\nexport const NETWORK_FAUCETS = ${JSON.stringify(idToFaucet, null, 2)};\n`);
appendFile(filename, `\nexport const NETWORK_EXPLORERS = ${JSON.stringify(idToExplorers, null, 2)};\n`);
appendFile(filename, `\nexport const NETWORK_CURRENCIES = ${JSON.stringify(idToNativeCurrency, null, 2)};\n`);
appendFile(filename, `\nexport const EXTRA_RPCS = ${JSON.stringify(extraRpcs, null, 2)};\n`);
}
29 changes: 3 additions & 26 deletions build/esbuild-build-tests.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import esbuild from "esbuild";
import chainlist from "../lib/chainlist/constants/extraRpcs";
import chainIDList from "../lib/chainlist/constants/chainIds.json";
import path from "path";
import * as fs from "fs";
import { createDynamicTypes } from "./dynamic-types";

const typescriptEntries = ["tests/mocks/rpc-service.ts", "tests/mocks/rpc-handler.ts", "tests/mocks/handler.ts"];
export const entries = [...typescriptEntries];
const extraRpcs: Record<string, string[]> = {};
// this flattens all the rpcs into a single object, with key names that match the networkIds. The arrays are just of URLs per network ID.

Object.keys(chainlist).forEach((networkId) => {
const officialUrls = chainlist[networkId].rpcs.filter((rpc) => typeof rpc === "string");
const extraUrls: string[] = chainlist[networkId].rpcs.filter((rpc) => rpc.url !== undefined && rpc.tracking === "none").map((rpc) => rpc.url);

extraRpcs[networkId] = [...officialUrls, ...extraUrls].filter((rpc) => rpc.startsWith("https://"));
});

export const esBuildContext: esbuild.BuildOptions = {
entryPoints: entries,
bundle: true,

outdir: "dist",
define: createEnvDefines({ extraRpcs, chainIDList }),
};

async function main() {
Expand All @@ -34,8 +22,6 @@ async function main() {
}
}

main();

async function buildForEnvironments() {
ensureDistDir();

Expand All @@ -62,25 +48,16 @@ async function buildIndex() {
bundle: true,
format: "cjs",
outfile: "dist/index.js",
define: createEnvDefines({ extraRpcs, chainIDList }),
});

console.log("Index build complete.");
}

function createEnvDefines(generatedAtBuild: Record<string, unknown>): Record<string, string> {
const defines: Record<string, string> = {};

Object.keys(generatedAtBuild).forEach((key) => {
defines[key] = JSON.stringify(generatedAtBuild[key]);
});

return defines;
}

function ensureDistDir() {
const distPath = path.resolve(__dirname, "dist");
if (!fs.existsSync(distPath)) {
fs.mkdirSync(distPath, { recursive: true });
}
}

createDynamicTypes().then(main).catch(console.error);
27 changes: 3 additions & 24 deletions build/esbuild-build.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import esbuild from "esbuild";
import chainlist from "../lib/chainlist/constants/extraRpcs";
import chainIDList from "../lib/chainlist/constants/chainIds.json";
import path from "path";
import * as fs from "fs";
import { createDynamicTypes } from "./dynamic-types";

const typescriptEntries = ["index.ts"];
export const entries = [...typescriptEntries];
const extraRpcs: Record<string, string[]> = {};

// this flattens all the rpcs into a single object, with key names that match the networkIds. The arrays are just of URLs per network ID.
Object.keys(chainlist).forEach((networkId) => {
const officialUrls = chainlist[networkId].rpcs.filter((rpc) => typeof rpc === "string");
const extraUrls: string[] = chainlist[networkId].rpcs.filter((rpc) => rpc.url !== undefined && rpc.tracking === "none").map((rpc) => rpc.url);
extraRpcs[networkId] = [...officialUrls, ...extraUrls].filter((rpc) => rpc.startsWith("https://"));
});

export const esBuildContext: esbuild.BuildOptions = {
entryPoints: entries,
bundle: true,

outdir: "dist",
define: createEnvDefines({ extraRpcs, chainIDList }),
};

async function main() {
Expand All @@ -33,8 +22,6 @@ async function main() {
}
}

main();

async function buildForEnvironments() {
ensureDistDir();

Expand Down Expand Up @@ -76,24 +63,16 @@ async function buildIndex() {
bundle: true,
format: "cjs",
outfile: "dist/index.js",
define: createEnvDefines({ extraRpcs, chainIDList }),
});

console.log("Index build complete.");
}

function createEnvDefines(generatedAtBuild: Record<string, unknown>): Record<string, string> {
const defines: Record<string, string> = {};
Object.keys(generatedAtBuild).forEach((key) => {
defines[key] = JSON.stringify(generatedAtBuild[key]);
});

return defines;
}

function ensureDistDir() {
const distPath = path.resolve(__dirname, "dist");
if (!fs.existsSync(distPath)) {
fs.mkdirSync(distPath, { recursive: true });
}
}

createDynamicTypes().then(main).catch(console.error);
33 changes: 6 additions & 27 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,20 @@ export default async function getRPCHandler() {
}

import {
ChainId,
ChainNames,
NetworkId,
NetworkName,
HandlerConstructorConfig,
HandlerInterface,
NativeToken,
NetworkCurrencies,
NetworkExplorers,
NetworkIds,
NetworkNames,
NetworkRPCs,
Token,
Tokens,
ValidBlockData,
} from "./types/handler";

import {
LOCAL_HOST,
chainIDList,
extraRpcs,
getNetworkName,
networkCurrencies,
networkExplorers,
Expand All @@ -39,39 +34,23 @@ import {
networkRpcs,
nftAddress,
permit2Address,
tokens,
getNetworkId,
} from "./types/constants";

import { RPCHandler } from "./types/rpc-handler";

export {
LOCAL_HOST,
chainIDList,
extraRpcs,
getNetworkName,
networkCurrencies,
networkExplorers,
networkIds,
networkNames,
networkRpcs,
nftAddress,
permit2Address,
tokens,
};
export { LOCAL_HOST, getNetworkName, getNetworkId, networkCurrencies, networkExplorers, networkIds, networkNames, networkRpcs, nftAddress, permit2Address };

export type {
ChainId,
ChainNames,
NetworkId,
NetworkName,
HandlerConstructorConfig,
HandlerInterface,
NativeToken,
NetworkCurrencies,
NetworkExplorers,
NetworkIds,
NetworkNames,
NetworkRPCs,
Token,
Tokens,
ValidBlockData,
};
export { RPCHandler };
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@
"node": ">=20.10.0"
},
"scripts": {
"format": "run-s format:lint format:prettier format:cspell",
"format": "run-p format:*",
"format:lint": "eslint --fix .",
"format:prettier": "prettier --write .",
"format:cspell": "cspell **/*",
"knip": "knip",
"knip-ci": "knip --no-exit-code --reporter json",
"prepare": "husky install",
"build:types": "tsc --emitDeclarationOnly --declaration --outDir dist && rm -rf dist/src",
"build:tests": "tsx build/esbuild-build-tests.ts && tsc --emitDeclarationOnly --declaration --project tsconfig.tests.json",
"build": "rm -rf dist && tsx build/esbuild-build.ts",
"postbuild": "yarn build:types",
"postinstall": "git submodule update --init --recursive",
"test": "rm -rf dist && yarn build:tests && jest"
"build": "run-s clean build:types",
"build:types": "tsx build/esbuild-build.ts && tsc --emitDeclarationOnly --declaration --outDir dist",
"build:tests": "tsx build/esbuild-build-tests.ts && tsc --emitDeclarationOnly --declaration --project tsconfig.tests.json",
"postbuild": "rm -rf dist/src",
"pretest": "run-s clean build:tests",
"test": "jest",
"clean": "rm -rf dist"
},
"keywords": [
"typescript",
Expand Down Expand Up @@ -87,4 +89,4 @@
]
},
"packageManager": "[email protected]"
}
}
6 changes: 3 additions & 3 deletions src/services/rpc-service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ValidBlockData } from "../../types/handler";
import { NetworkId, ValidBlockData } from "../../types/handler";
import axios from "axios";
type PromiseResult = { success: boolean; rpcUrl: string; duration: number };

export class RPCService {
static async testRpcPerformance(
networkId: number,
networkId: NetworkId,
latencies: Record<string, number>,
runtimeRpcs: string[],
rpcHeader: object,
Expand Down Expand Up @@ -58,7 +58,7 @@ export class RPCService {

return { latencies, runtimeRpcs };
}
static async findFastestRpc(latencies: Record<string, number>, networkId: number): Promise<string | null> {
static async findFastestRpc(latencies: Record<string, number>, networkId: NetworkId): Promise<string | null> {
try {
const validLatencies: Record<string, number> = Object.entries(latencies)
.filter(([key]) => key.startsWith(`${networkId}__`))
Expand Down
4 changes: 3 additions & 1 deletion src/services/storage-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NetworkId } from "types/handler";

export class StorageService {
static getLatencies(env: string, networkId: number): Record<string | number, number> {
static getLatencies(env: string, networkId: NetworkId): Record<string | number, number> {
if (env === "browser") {
if (this.bypassForTests()) return {};
const latencies: Record<string, number> = JSON.parse(localStorage.getItem("rpcLatencies") || "{}");
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JsonRpcProvider } from "@ethersproject/providers";
import { RPCHandler, HandlerConstructorConfig } from "../dist";

export const testConfig: HandlerConstructorConfig = {
networkId: 1,
networkId: "1",
autoStorage: false,
cacheRefreshCycles: 3,
networkName: null,
Expand Down
Loading

1 comment on commit 30022f4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 20%
20.72% (2105/10156) 13.06% (638/4884) 20.56% (314/1527)

JUnit

Tests Skipped Failures Errors Time
30 0 💤 0 ❌ 0 🔥 18.117s ⏱️
Coverage Report (20%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files20.7213.0620.5620.58 
dist20.4212.9520.2620.32 
   index.js20.4212.9520.2620.3246, 58, 66–67, 75, 82, 90, 95–97, 100–102, 106, 109, 112, 123, 127, 133–157, 160–215, 218–252, 262, 264, 286–291, 304, 310–322, 325, 328–330, 333–336, 340, 354–360, 482–483, 486, 492, 495, 502–528, 531–539, 542, 545–546, 550, 553–556, 559–567, 570–595, 600–625, 631–653, 657–680, 683–694, 697–706, 709, 712–715, 718–721, 724, 727, 730–733, 736–742, 745–746, 749–751, 754–756, 760–769, 772–773, 776–778, 781–783, 788–804, 807–808, 811–813, 816–818, 821–834, 837, 840–849, 853–892, 896–909, 912–958, 961–994, 997–1546, 1549–1586, 1590–1604, 1607–1612, 1615–1622, 1625–1627, 1631–1654, 1661–1667, 1670–1678, 1682–1692, 1695–1707, 1710–1714, 1717–1743, 1746–1748, 1751–1753, 1756, 1761, 1774–1775, 1780, 1783, 1786, 1789–1804, 1807–1834, 1837–1838, 1841–1883, 1886–1887, 1890, 1893, 1896, 1899, 1902–1909, 1912–1927, 1930, 1933–1948, 1951–1961, 1964–1984, 1987, 1990, 1993–1994, 1997, 2000–2027, 2030–2081, 2087–2158, 2161, 2164, 2167, 2170–2179, 2182–2191, 2194, 2197–2208, 2211, 2214–2272, 2279–2331, 2334–2363, 2366, 2369, 2372, 2375, 2378–2399, 2402, 2405–2424, 2427–2434, 2437–2454, 2457, 2460, 2463, 2466, 2469, 2472, 2475, 2478, 2481, 2484, 2487, 2490–2492, 2495–2496, 2499–2500, 2503–2504, 2507–2508, 2511–2512, 2515–2516, 2519–2520, 2523–2524, 2527–2529, 2532–2534, 2537–2539, 2542–2544, 2547–2549, 2552–2554, 2557–2559, 2562–2564, 2573–2577, 2580–2582, 2585–2606, 2609, 2612–2615, 2623–2646, 2650–2669, 2677, 2685, 2693–2704, 2707–2732, 2736–2737, 2740–2741, 2747–2750, 2753–2756, 2759–2764, 2767–2772, 2775–2780, 2783–2788, 2791–2792, 2795–2796, 2799–2800, 2803, 2806, 2809–2848, 2851–2856, 2860–2900, 2903–2904, 2907–2909, 2912–2925, 2929, 2932–2934, 2937–2951, 2954–2965, 2968–2969, 2985, 2991–2992, 3061–3062, 3066–3067, 3072, 3077, 3082, 3087, 3100, 3103, 3110, 3113, 3121–3124, 3128, 3136, 3139, 3183–3250, 3253–3269, 3272–3309, 3312–3320, 3323–3339, 3342–3365, 3368–3390, 3394–3424, 3428–3429, 3433–3605, 3611–3616, 3629–3630, 3633–3634, 3657–3664, 3679–3726, 3730–3733, 3737–3738, 3742–3749, 3753–3756, 3760–3775, 3779–3790, 3794–3809, 3813, 3817, 3821, 3825, 3829, 3833, 3837–3842, 3846–3848, 3852–3853, 3857–3866, 3870–3871, 3875–3886, 3890–3891, 3895–3896, 3900–3901, 3905, 3909–3910, 3923–3931, 3935–3951, 3954–3956, 3959–3990, 4002–4007, 4011, 4015, 4019, 4023, 4027, 4031, 4035, 4060–4070, 4079–4102, 4105–4108, 4198–4212, 4221–4254, 4257–4260, 4272–4275, 4293–4296, 4483–4505, 4514–4536, 4549–4633, 4636–4735, 4747–4750, 4776–4779, 4808–4812, 4821–4862, 4865–4904, 5244–5251, 5255–5265, 5268–5269, 5272–5273, 5305, 5311–5449, 5466, 5468–5474, 5476–5477, 31735–31746, 31767–31771, 31775, 31778–31781, 31826–31995, 32000, 32002–32037, 32055–32062, 32069, 32072, 32075, 32078, 32083, 32093–32102, 32105, 32108, 32111–32125, 32130–32163, 32170, 32176–32227, 32231, 32235–32275, 32279, 32281, 32284, 32292–32400, 32415, 32424–32555, 32561, 32565, 32574, 32578, 32581, 32585–32613, 32623, 32627, 32635, 32641, 32646–32663, 32672–32694, 32710–32736, 32749–32793, 32797–32798, 32809–32831, 32837, 32846–32871, 32881, 32885–32945, 32948, 32954–32957, 32960, 33047, 33061–33096, 33114–33119, 33134–33188, 33206, 33233–33257, 33265–33381, 33620–33710, 33718–33762, 33772–33876, 33885–33903, 33910, 33921–33971, 33977–33999, 34010–34015, 34035–34038, 34045–34127, 34131–34393, 34408, 34417–34490, 34502–34524, 34529–34876, 34894–34899, 34903–34904, 34907–34908, 34914–34941, 34945–34948, 34952–34955, 34959–34962, 34974–34993, 34997–35042, 35046–35048, 35053, 35057, 35065–35083, 35088, 35091, 35094–35120, 35123–35151, 35154–35275, 35279, 35282, 35285–35300, 35303, 35306–35310, 35313, 35316–35327, 35330–35335, 35338–35347, 35353–35360, 35366, 35369–35372, 35390–35397, 35404–35412, 35417–35450, 35457–35463, 35466–35520, 35526–35537, 35540–35550, 35553–35559, 35562–35605, 35610, 35613, 35616–35640, 35643–35645, 35657–35677, 35680–35682, 35685, 35688–35703, 35706–35717, 35720, 35723, 35726–35734, 35737–35742, 35745–35750, 35753, 35756–35775, 35778–35800, 35804, 35807–35813, 35816, 35819–35843, 35846–35869, 35872–35909, 35912–35919, 35925–35955, 35961–35992, 35995–36014, 36017–36046, 36049–36050, 36053–36063, 36066–36078, 36082–36084, 36087, 36104–36114, 36122–36128, 36270–36287, 36291–36300, 36303, 36306–36314, 36317–36329, 36332–36351, 36355–36361, 36365–36367, 36373–36375, 36381–36388, 36391–36399, 36402–36405, 36408–36409, 36412–36421, 36424–36427, 36430, 36433, 36436, 36440–36450, 36454–36486, 36489–36550, 36553–36573, 36578, 36583–36600, 36604, 36607, 36610, 36613–36630, 36634–36640, 36643–36683, 36687–36709, 36712–36729, 36732–36745, 36755, 36772–36775, 36779–36840, 36855–37096, 37119, 37225–37466, 37471–37474, 37480–37483, 37489–37496, 37504–37526, 37532–37570, 37577, 37582–37597, 37603–37645, 37657–37660, 37670–37725, 37730–38024, 38048, 38051–38054, 38062, 38072–38110, 38126–39229, 39236–39247, 39250, 39255–39311, 39321–39343, 39372–39588, 39595–39617, 39623–39740, 39745–39920, 39926–39938, 39961–39966, 39973, 39980, 39985–40248, 40285, 40316, 40321, 40353, 40362, 40366, 40387–40390, 40407, 40423–40429, 40432–40443, 40447, 40451–40456, 40461–40465, 40492, 40502–40504, 40522, 40532–40540, 40543–40562, 40631, 40642, 40702–40716, 40722–40811, 40817–40832, 40836, 40839–40843, 40850, 40856–40871, 40888–40915, 40930–40931, 40991–41052, 41063–41068, 41078, 41082, 41085, 41088, 41091–41092, 41096–41101, 41112, 41119, 41128–41131, 41186–41207, 41222–41228, 41232–41249, 41272, 41283, 41302–41354, 41362–41364, 41368, 41376, 41386–41392, 41425, 41478–41513, 41520–41538, 41547–41567, 41576–41600, 41607, 41617–41652, 41658, 41750, 41757–41761, 41765–41769, 41779–41896, 41937–41947, 41951–41952, 41956–41973, 41982–41983, 42012, 42017, 42022–42038, 42042–42043, 42072–42082, 42089, 42103–42108, 42175, 42184–42188, 42204, 42256, 42260, 42266–42267, 42279, 42290, 42294–42295, 42332, 42344–42345, 42359–42364, 42385–42389, 42393, 42399–42407, 42413–42419, 42425, 42430, 42435–42437, 42442, 42471, 42481–42485, 42489–42496, 42500, 42518–42525, 42535–42549, 42559–42566, 42661, 42745–42755, 42763–42787, 42793, 42800, 42835, 42837, 42840, 42850–42851, 42857, 42862–42863, 42866, 42872, 42877–42886, 42892–42898, 42904–42907, 42931, 42934, 42937, 42946–42948, 42960–42966
types10010085.71100 
   constants.ts10010085.71100 
   dynamic.ts100100100100 

Please sign in to comment.