From f64d2208f36aedf689571563e4d2d6cb7b27d305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Fri, 11 Oct 2024 03:44:58 +0900 Subject: [PATCH] chore: update configurations and refactor RPC service references --- .eslintrc | 4 +- .github/workflows/publish-package.yml | 2 +- .github/workflows/sync-template.yml | 2 +- .prettierignore | 1 + CHANGELOG.md | 65 +++++++++++++-------------- index.ts | 51 +++++++++++++-------- knip.ts | 6 +-- package.json | 7 +-- types/handler.ts | 4 +- types/rpc-handler.ts | 28 ++++++------ types/rpc-service.ts | 6 +-- 11 files changed, 89 insertions(+), 87 deletions(-) create mode 100644 .prettierignore diff --git a/.eslintrc b/.eslintrc index f2369c2..d7b5ce8 100644 --- a/.eslintrc +++ b/.eslintrc @@ -15,8 +15,8 @@ "jest.config.ts", "knip.ts", "build", - "./lib", - "./dist" + "./dist", + "lib/**/*" ], "rules": { "filename-rules/match": [2, "/^(e2e\\.ts$|.*\\/e2e\\.ts$|[a-z0-9]+(?:[-._a-z0-9]+)*\\.ts|\\.[a-z0-9]+)$/"], diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index e832e13..f7dad16 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -20,7 +20,7 @@ jobs: with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.APP_PRIVATE_KEY }} - + - uses: googleapis/release-please-action@v4 id: release with: diff --git a/.github/workflows/sync-template.yml b/.github/workflows/sync-template.yml index 274b2ea..8b6c435 100644 --- a/.github/workflows/sync-template.yml +++ b/.github/workflows/sync-template.yml @@ -3,7 +3,7 @@ name: Sync branch to template on: workflow_dispatch: schedule: - - cron: '14 0 1 * *' + - cron: "14 0 1 * *" jobs: sync: diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..f1ff06d --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +lib/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 861af96..c3d28dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,61 +2,56 @@ ## [1.2.3](https://github.com/ubiquity/rpc-handler/compare/v1.2.2...v1.2.3) (2024-07-31) - ### Miscellaneous Chores -* release 1.2.3 ([0b44fdf](https://github.com/ubiquity/rpc-handler/commit/0b44fdfacb50c8f59576d1cf6faf79c3f19c72e4)) +- release 1.2.3 ([0b44fdf](https://github.com/ubiquity/rpc-handler/commit/0b44fdfacb50c8f59576d1cf6faf79c3f19c72e4)) ## [1.2.2](https://github.com/ubiquity/rpc-handler/compare/v1.2.1...v1.2.2) (2024-07-31) - ### Bug Fixes -* changed env variable for npm login ([69804d8](https://github.com/ubiquity/rpc-handler/commit/69804d8d244b50c089aea0aac84daca102877554)) +- changed env variable for npm login ([69804d8](https://github.com/ubiquity/rpc-handler/commit/69804d8d244b50c089aea0aac84daca102877554)) ## [1.2.1](https://github.com/ubiquity/rpc-handler/compare/v1.1.0...v1.2.1) (2024-07-31) - ### Miscellaneous Chores -* release 1.2.1 ([feffe04](https://github.com/ubiquity/rpc-handler/commit/feffe04efdff124a51adc07045e9a35116230a71)) +- release 1.2.1 ([feffe04](https://github.com/ubiquity/rpc-handler/commit/feffe04efdff124a51adc07045e9a35116230a71)) ## [1.1.0](https://github.com/ubiquity/rpc-handler/compare/v1.0.0...v1.1.0) (2024-07-31) - ### Features -* add debug configuration for vscode ([2eb0f97](https://github.com/ubiquity/rpc-handler/commit/2eb0f977254991707d4d19fbb3741b54145cab5f)) -* add tracking & protocol config options ([70b6e4f](https://github.com/ubiquity/rpc-handler/commit/70b6e4ffe40f0aca65f62196492f6d6293f51ed4)) -* filter rpc by tracking info ([fc493ae](https://github.com/ubiquity/rpc-handler/commit/fc493aecd97851d349a8033ef4e802aa48e4de19)) -* index with exports ([097f7fc](https://github.com/ubiquity/rpc-handler/commit/097f7fcd4465addd3985faa6fc1a102c3624896e)) -* proxy handler ([c50fd06](https://github.com/ubiquity/rpc-handler/commit/c50fd068685248f2809a6f9430c6748a695217b7)) -* removed bump script and added release-please ([5b2f026](https://github.com/ubiquity/rpc-handler/commit/5b2f02618df036e030c043ffc17a2a6819c66531)) -* use rpcs original object with extra info ([837fd86](https://github.com/ubiquity/rpc-handler/commit/837fd86c472cb12e71b54caf1c0433d7553ccfc3)) -* Web3 RPC Handler (npm package) ([#1](https://github.com/ubiquity/rpc-handler/issues/1)) ([#4](https://github.com/ubiquity/rpc-handler/issues/4)) ([0d964b0](https://github.com/ubiquity/rpc-handler/commit/0d964b09871b09fa5ce441d3339254d171ecae62)) -* workflow ([300603c](https://github.com/ubiquity/rpc-handler/commit/300603cbdc788b44c80a366021142f99842590ba)) - +- add debug configuration for vscode ([2eb0f97](https://github.com/ubiquity/rpc-handler/commit/2eb0f977254991707d4d19fbb3741b54145cab5f)) +- add tracking & protocol config options ([70b6e4f](https://github.com/ubiquity/rpc-handler/commit/70b6e4ffe40f0aca65f62196492f6d6293f51ed4)) +- filter rpc by tracking info ([fc493ae](https://github.com/ubiquity/rpc-handler/commit/fc493aecd97851d349a8033ef4e802aa48e4de19)) +- index with exports ([097f7fc](https://github.com/ubiquity/rpc-handler/commit/097f7fcd4465addd3985faa6fc1a102c3624896e)) +- proxy handler ([c50fd06](https://github.com/ubiquity/rpc-handler/commit/c50fd068685248f2809a6f9430c6748a695217b7)) +- removed bump script and added release-please ([5b2f026](https://github.com/ubiquity/rpc-handler/commit/5b2f02618df036e030c043ffc17a2a6819c66531)) +- use rpcs original object with extra info ([837fd86](https://github.com/ubiquity/rpc-handler/commit/837fd86c472cb12e71b54caf1c0433d7553ccfc3)) +- Web3 RPC Handler (npm package) ([#1](https://github.com/ubiquity/rpc-handler/issues/1)) ([#4](https://github.com/ubiquity/rpc-handler/issues/4)) ([0d964b0](https://github.com/ubiquity/rpc-handler/commit/0d964b09871b09fa5ce441d3339254d171ecae62)) +- workflow ([300603c](https://github.com/ubiquity/rpc-handler/commit/300603cbdc788b44c80a366021142f99842590ba)) ### Bug Fixes -* attempting to close timeouts ([7b31c5f](https://github.com/ubiquity/rpc-handler/commit/7b31c5f82a50a0d4c33c9205643cce19c41d3152)) -* built export fixes ([2a6103a](https://github.com/ubiquity/rpc-handler/commit/2a6103a7d9b14ffa66355016ed6f4b2af9a64531)) -* eslint parsing tests ([d039977](https://github.com/ubiquity/rpc-handler/commit/d039977b2e9d77866c18f231bf6e0b5184963525)) -* improved log tests ([5427159](https://github.com/ubiquity/rpc-handler/commit/54271597bc74ee945a12d584caea386b52354267)) -* increased timeout for tests ([38751c0](https://github.com/ubiquity/rpc-handler/commit/38751c02b54febec1c653af49b8cef10b14ffcbb)) -* increased timeout for tests ([5e0bb40](https://github.com/ubiquity/rpc-handler/commit/5e0bb40625bf44ee79194ea26d4c7c5984d5e9c7)) -* log ([5a25b29](https://github.com/ubiquity/rpc-handler/commit/5a25b29e668398abefa97b44a11846f7cdda8571)) -* make tracking property optional ([4fbe2f1](https://github.com/ubiquity/rpc-handler/commit/4fbe2f14f9171dc4bc8eabb29add44dda4beebe3)) -* readme ([a068543](https://github.com/ubiquity/rpc-handler/commit/a068543dd0c21b723876bc05a58272240ecbac01)) -* remove protocol config options ([e3cd0f0](https://github.com/ubiquity/rpc-handler/commit/e3cd0f00ad1325b8c24f8288e5d110f637110ac3)) -* remove yarn build from wf ([b028986](https://github.com/ubiquity/rpc-handler/commit/b028986b63cd5bd52e7260c6aa220b8d14a5b184)) -* removed comments from Jest testing and added Action comment dashboard instead ([85dab3f](https://github.com/ubiquity/rpc-handler/commit/85dab3fb961fcb802c7e5d5be95c1025d6148528)) -* rethrow error, finish tests ([1a8eb87](https://github.com/ubiquity/rpc-handler/commit/1a8eb874902085c8978eddc978de97cf80d94f59)) -* update rpc type definition ([ec9b91c](https://github.com/ubiquity/rpc-handler/commit/ec9b91cdca285b9800ed3305795d89f6e7d5d31f)) -* use correct network rpcs ([19cea2a](https://github.com/ubiquity/rpc-handler/commit/19cea2ac65c5982c76c12e2c092d2e0664275573)) -* use correct types ([5ef09a0](https://github.com/ubiquity/rpc-handler/commit/5ef09a0bdb5733f7627470cd2e5d02ad89774442)) -* use max safe ([f9495d3](https://github.com/ubiquity/rpc-handler/commit/f9495d3d3b5703a9d4052432d66e5f7fb0c95860)) -* workflow ([5f7b03f](https://github.com/ubiquity/rpc-handler/commit/5f7b03fd9fae8c3abfecef89043949420a12049a)) +- attempting to close timeouts ([7b31c5f](https://github.com/ubiquity/rpc-handler/commit/7b31c5f82a50a0d4c33c9205643cce19c41d3152)) +- built export fixes ([2a6103a](https://github.com/ubiquity/rpc-handler/commit/2a6103a7d9b14ffa66355016ed6f4b2af9a64531)) +- eslint parsing tests ([d039977](https://github.com/ubiquity/rpc-handler/commit/d039977b2e9d77866c18f231bf6e0b5184963525)) +- improved log tests ([5427159](https://github.com/ubiquity/rpc-handler/commit/54271597bc74ee945a12d584caea386b52354267)) +- increased timeout for tests ([38751c0](https://github.com/ubiquity/rpc-handler/commit/38751c02b54febec1c653af49b8cef10b14ffcbb)) +- increased timeout for tests ([5e0bb40](https://github.com/ubiquity/rpc-handler/commit/5e0bb40625bf44ee79194ea26d4c7c5984d5e9c7)) +- log ([5a25b29](https://github.com/ubiquity/rpc-handler/commit/5a25b29e668398abefa97b44a11846f7cdda8571)) +- make tracking property optional ([4fbe2f1](https://github.com/ubiquity/rpc-handler/commit/4fbe2f14f9171dc4bc8eabb29add44dda4beebe3)) +- readme ([a068543](https://github.com/ubiquity/rpc-handler/commit/a068543dd0c21b723876bc05a58272240ecbac01)) +- remove protocol config options ([e3cd0f0](https://github.com/ubiquity/rpc-handler/commit/e3cd0f00ad1325b8c24f8288e5d110f637110ac3)) +- remove yarn build from wf ([b028986](https://github.com/ubiquity/rpc-handler/commit/b028986b63cd5bd52e7260c6aa220b8d14a5b184)) +- removed comments from Jest testing and added Action comment dashboard instead ([85dab3f](https://github.com/ubiquity/rpc-handler/commit/85dab3fb961fcb802c7e5d5be95c1025d6148528)) +- rethrow error, finish tests ([1a8eb87](https://github.com/ubiquity/rpc-handler/commit/1a8eb874902085c8978eddc978de97cf80d94f59)) +- update rpc type definition ([ec9b91c](https://github.com/ubiquity/rpc-handler/commit/ec9b91cdca285b9800ed3305795d89f6e7d5d31f)) +- use correct network rpcs ([19cea2a](https://github.com/ubiquity/rpc-handler/commit/19cea2ac65c5982c76c12e2c092d2e0664275573)) +- use correct types ([5ef09a0](https://github.com/ubiquity/rpc-handler/commit/5ef09a0bdb5733f7627470cd2e5d02ad89774442)) +- use max safe ([f9495d3](https://github.com/ubiquity/rpc-handler/commit/f9495d3d3b5703a9d4052432d66e5f7fb0c95860)) +- workflow ([5f7b03f](https://github.com/ubiquity/rpc-handler/commit/5f7b03fd9fae8c3abfecef89043949420a12049a)) ## 1.0.0 (2024-03-22) diff --git a/index.ts b/index.ts index 565371d..8a0b5d4 100644 --- a/index.ts +++ b/index.ts @@ -1,17 +1,24 @@ import { - NetworkId, - NetworkName, HandlerConstructorConfig, HandlerInterface, NativeToken, NetworkCurrencies, NetworkExplorers, - NetworkRPCs, + NetworkId, + NetworkName, + NetworkRpcs, Token, ValidBlockData, } from "./types/handler"; import { + getNetworkCurrency, + getNetworkData, + getNetworkExplorer, + getNetworkFaucets, + getNetworkId, + getNetworkName, + getNetworkRpcs, LOCAL_HOST, networkCurrencies, networkExplorers, @@ -20,33 +27,41 @@ import { networkRpcs, nftAddress, permit2Address, - getNetworkId, - getNetworkFaucets, - getNetworkExplorer, - getNetworkName, - getNetworkRpcs, - getNetworkCurrency, - getNetworkData, } from "./types/constants"; -import { RPCHandler } from "./types/rpc-handler"; import { PrettyLogs } from "./types/logs"; +import { RpcHandler } from "./types/rpc-handler"; +import { RpcService } from "./types/rpc-service"; import { StorageService } from "./types/storage-service"; -import { RPCService } from "./types/rpc-service"; -export { LOCAL_HOST, networkCurrencies, networkExplorers, networkIds, networkNames, networkRpcs, nftAddress, permit2Address }; -export { getNetworkId, getNetworkFaucets, getNetworkExplorer, getNetworkName, getNetworkRpcs, getNetworkCurrency, getNetworkData }; +export { + getNetworkCurrency, + getNetworkData, + getNetworkExplorer, + getNetworkFaucets, + getNetworkId, + getNetworkName, + getNetworkRpcs, + LOCAL_HOST, + networkCurrencies, + networkExplorers, + networkIds, + networkNames, + networkRpcs, + nftAddress, + permit2Address, +}; +export { PrettyLogs, RpcHandler as RPCHandler, RpcService as RPCService, StorageService }; export type { - NetworkId, - NetworkName, HandlerConstructorConfig, HandlerInterface, NativeToken, NetworkCurrencies, NetworkExplorers, - NetworkRPCs, + NetworkId, + NetworkName, + NetworkRpcs as NetworkRPCs, Token, ValidBlockData, }; -export { RPCHandler, PrettyLogs, StorageService, RPCService }; diff --git a/knip.ts b/knip.ts index a512caf..1846e2d 100644 --- a/knip.ts +++ b/knip.ts @@ -5,11 +5,7 @@ const config: KnipConfig = { project: ["src/**/*.ts"], ignore: ["src/types/config.ts", "tests/*", ".github/workflows/**"], ignoreExportsUsedInFile: true, - ignoreDependencies: ["axios", "ts-node", "node-fetch", "eslint-config-prettier", "eslint-plugin-prettier", "@types/jest", "@types/node-fetch"], - jest: { - config: ["jest.config.ts"], - entry: ["tests/*.ts"], - }, + ignoreDependencies: ["axios", "ts-node", "node-fetch", "eslint-config-prettier", "eslint-plugin-prettier", "@types/node-fetch"], }; export default config; diff --git a/package.json b/package.json index d98f2eb..ddb7500 100644 --- a/package.json +++ b/package.json @@ -40,9 +40,7 @@ "open-source" ], "dependencies": { - "@ethersproject/providers": "5.7.2", - "axios": "^1.7.1", - "node-fetch": "^3.3.2" + "@ethersproject/providers": "5.7.2" }, "devDependencies": { "@actions/core": "^1.11.1", @@ -59,12 +57,10 @@ "@octokit/rest": "^21.0.2", "@types/jest": "^29.5.12", "@types/node": "^20.11.19", - "@types/node-fetch": "^2.6.11", "@typescript-eslint/eslint-plugin": "^7.0.1", "@typescript-eslint/parser": "^7.0.1", "cspell": "^8.4.0", "cypress": "13.6.6", - "esbuild": "^0.20.1", "eslint": "^8.56.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-filename-rules": "^1.3.1", @@ -80,7 +76,6 @@ "prettier": "^3.3.3", "simple-git": "^3.27.0", "ts-jest": "29.2.3", - "ts-node": "10.9.2", "tsx": "^4.7.1", "typescript": "^5.3.3" }, diff --git a/types/handler.ts b/types/handler.ts index 9ab53a2..6d6b661 100644 --- a/types/handler.ts +++ b/types/handler.ts @@ -1,7 +1,7 @@ import { JsonRpcProvider } from "@ethersproject/providers"; import { networkCurrencies, networkExplorers, networkRpcs } from "./constants"; -import { CHAINS_IDS, EXTRA_RPCS } from "../dynamic"; import { LogInterface, PrettyLogs, PrettyLogsWithOk } from "./logs"; +import { CHAINS_IDS, EXTRA_RPCS } from "../build/dynamic-types"; export type BlockExplorer = { name: string; @@ -65,7 +65,7 @@ export type HandlerConstructorConfig = { proxySettings: ProxySettings; // settings for the proxy }; -export type NetworkRPCs = typeof networkRpcs; +export type NetworkRpcs = typeof networkRpcs; export type NetworkCurrencies = typeof networkCurrencies; export type NetworkExplorers = typeof networkExplorers; diff --git a/types/rpc-handler.ts b/types/rpc-handler.ts index 36bad20..47827e0 100644 --- a/types/rpc-handler.ts +++ b/types/rpc-handler.ts @@ -1,8 +1,8 @@ import { JsonRpcProvider } from "@ethersproject/providers"; -import { LOCAL_HOST, networkRpcs, networkIds, LOCAL_HOST_2 } from "./constants"; -import { HandlerInterface, HandlerConstructorConfig, NetworkId, NetworkName, Rpc, Tracking, getRpcUrls } from "./handler"; +import { LOCAL_HOST, LOCAL_HOST_2, networkIds, networkRpcs } from "./constants"; +import { HandlerConstructorConfig, HandlerInterface, NetworkId, NetworkName, Rpc, Tracking, getRpcUrls } from "./handler"; import { Metadata, PrettyLogs, PrettyLogsWithOk } from "./logs"; -import { RPCService } from "./rpc-service"; +import { RpcService } from "./rpc-service"; import { StorageService } from "./storage-service"; const NO_RPCS_AVAILABLE = "No RPCs available"; @@ -14,8 +14,8 @@ function shuffleArray(array: object[]) { } } -export class RPCHandler implements HandlerInterface { - private static _instance: RPCHandler | null = null; +export class RpcHandler implements HandlerInterface { + private static _instance: RpcHandler | null = null; private _provider: JsonRpcProvider | null = null; private _networkId: NetworkId; private _networkName: NetworkName; @@ -70,7 +70,7 @@ export class RPCHandler implements HandlerInterface { shuffleArray(rpcList); for (const rpc of rpcList) { try { - const result = await RPCService.makeRpcRequest(rpc.url, this._rpcTimeout, { "Content-Type": "application/json" }); + const result = await RpcService.makeRpcRequest(rpc.url, this._rpcTimeout, { "Content-Type": "application/json" }); if (result.success) { return new JsonRpcProvider({ url: rpc.url, skipFetchSetup: true }, Number(this._networkId)); } else { @@ -111,7 +111,7 @@ export class RPCHandler implements HandlerInterface { * If proxySettings.disabled, it will return the provider as is and * any retry or RPC reselection logic will be down to the user to implement */ - createProviderProxy(provider: JsonRpcProvider, handler: RPCHandler): JsonRpcProvider { + createProviderProxy(provider: JsonRpcProvider, handler: RpcHandler): JsonRpcProvider { /** * It is not recommended to disable this feature * unless you are handling retries and RPC reselection yourself @@ -258,7 +258,7 @@ export class RPCHandler implements HandlerInterface { await this._testRpcPerformance(); - const fastestRpcUrl = await RPCService.findFastestRpc(this._latencies, this._networkId); + const fastestRpcUrl = await RpcService.findFastestRpc(this._latencies, this._networkId); if (!fastestRpcUrl) { throw this.log( @@ -304,19 +304,19 @@ export class RPCHandler implements HandlerInterface { return this._provider; } - public static getInstance(config: HandlerConstructorConfig): RPCHandler { - if (!RPCHandler._instance) { + public static getInstance(config: HandlerConstructorConfig): RpcHandler { + if (!RpcHandler._instance) { if (!config) { throw new Error("Config is required to initialize RPCHandler"); } - RPCHandler._instance = new RPCHandler(config); + RpcHandler._instance = new RpcHandler(config); } - return RPCHandler._instance; + return RpcHandler._instance; } public clearInstance(): void { - RPCHandler._instance = null; + RpcHandler._instance = null; } public getRuntimeRpcs(): string[] { @@ -348,7 +348,7 @@ export class RPCHandler implements HandlerInterface { } private async _testRpcPerformance(): Promise { - const { latencies, runtimeRpcs } = await RPCService.testRpcPerformance( + const { latencies, runtimeRpcs } = await RpcService.testRpcPerformance( this._networkId, this._latencies, this._runtimeRpcs, diff --git a/types/rpc-service.ts b/types/rpc-service.ts index ad7af3b..f24956f 100644 --- a/types/rpc-service.ts +++ b/types/rpc-service.ts @@ -1,5 +1,5 @@ -import { NetworkId, ValidBlockData } from "./handler"; import axios, { AxiosError } from "axios"; +import { NetworkId, ValidBlockData } from "./handler"; type PromiseResult = { success: boolean; rpcUrl: string; duration: number; error?: string }; const rpcBody = JSON.stringify({ @@ -9,7 +9,7 @@ const rpcBody = JSON.stringify({ id: 1, }); -export class RPCService { +export class RpcService { static async makeRpcRequest(rpcUrl: string, rpcTimeout: number, rpcHeader: object): Promise { const instance = axios.create({ timeout: rpcTimeout, @@ -51,7 +51,7 @@ export class RPCService { ): Promise<{ latencies: Record; runtimeRpcs: string[] }> { async function requestEndpoint(rpcUrl: string) { try { - return await RPCService.makeRpcRequest(rpcUrl, rpcTimeout, rpcHeader); + return await RpcService.makeRpcRequest(rpcUrl, rpcTimeout, rpcHeader); } catch (err) { console.error(`Failed to reach endpoint. ${err}`); throw new Error(rpcUrl);