Skip to content

Commit

Permalink
chore: update configurations and refactor RPC service references
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 10, 2024
1 parent b1a84b2 commit f64d220
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 87 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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]+)$/"],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Sync branch to template
on:
workflow_dispatch:
schedule:
- cron: '14 0 1 * *'
- cron: "14 0 1 * *"

jobs:
sync:
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
65 changes: 30 additions & 35 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
51 changes: 33 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 };
6 changes: 1 addition & 5 deletions knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions types/handler.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down
28 changes: 14 additions & 14 deletions types/rpc-handler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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[] {
Expand Down Expand Up @@ -348,7 +348,7 @@ export class RPCHandler implements HandlerInterface {
}

private async _testRpcPerformance(): Promise<void> {
const { latencies, runtimeRpcs } = await RPCService.testRpcPerformance(
const { latencies, runtimeRpcs } = await RpcService.testRpcPerformance(
this._networkId,
this._latencies,
this._runtimeRpcs,
Expand Down
6 changes: 3 additions & 3 deletions types/rpc-service.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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<PromiseResult> {
const instance = axios.create({
timeout: rpcTimeout,
Expand Down Expand Up @@ -51,7 +51,7 @@ export class RPCService {
): Promise<{ latencies: Record<string, number>; 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);
Expand Down

0 comments on commit f64d220

Please sign in to comment.