Skip to content

Commit

Permalink
feat: use rpcs original object with extra info
Browse files Browse the repository at this point in the history
  • Loading branch information
EresDev committed Jun 14, 2024
1 parent d99097f commit 837fd86
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build/esbuild-build-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function buildIndex() {
bundle: true,
format: "cjs",
outfile: "dist/index.js",
define: createEnvDefines({ extraRpcs, chainIDList }),
define: createEnvDefines({ extraRpcs, chainIDList, extraRpcsOriginal: chainlist }),
});

console.log("Index build complete.");
Expand Down
2 changes: 1 addition & 1 deletion build/esbuild-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function buildIndex() {
bundle: true,
format: "cjs",
outfile: "dist/index.js",
define: createEnvDefines({ extraRpcs, chainIDList }),
define: createEnvDefines({ extraRpcs, chainIDList, extraRpcsOriginal: chainlist }),
});

console.log("Index build complete.");
Expand Down
4 changes: 3 additions & 1 deletion build/shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import chainlist from "../lib/chainlist/constants/extraRpcs";

export function prepareExtraRpcs(chainlist) {
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.
Expand All @@ -15,7 +17,7 @@ export function prepareBuildOptions(entries, extraRpcs, chainIDList) {
bundle: true,

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

Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
networkIds,
networkNames,
networkRpcs,
networkRpcsOriginal,
nftAddress,
permit2Address,
tokens,
Expand All @@ -54,6 +55,7 @@ export {
networkIds,
networkNames,
networkRpcs,
networkRpcsOriginal,
nftAddress,
permit2Address,
tokens,
Expand Down
5 changes: 4 additions & 1 deletion tests/benchmark.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { RPCHandler, HandlerConstructorConfig } from "../dist";
import { RPCHandler } from "../dist";
import { HandlerConstructorConfig } from "../types/handler";

export const testConfig: HandlerConstructorConfig = {
networkId: 1,
Expand All @@ -9,6 +10,8 @@ export const testConfig: HandlerConstructorConfig = {
networkRpcs: null,
rpcTimeout: 1500,
runtimeRpcs: null,
tracking: "yes",
protocol: "all",
};

describe("RPCHandler", () => {
Expand Down
13 changes: 7 additions & 6 deletions tests/mocks/rpc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { LOCAL_HOST, networkRpcs, networkNames } from "../../types/constants";
import { LOCAL_HOST, networkRpcs, networkNames, networkRpcsOriginal } from "../../types/constants";
import { HandlerInterface, HandlerConstructorConfig } from "../../types/handler";

import { RPCService } from "./rpc-service";
import { StorageService } from "./storage-service";
import { RpcType, getRpcUrls } from "../../types/shared";

export class RPCHandler implements HandlerInterface {
private static _instance: RPCHandler | null = null;
Expand All @@ -18,10 +19,10 @@ export class RPCHandler implements HandlerInterface {
private _autoStorage: boolean = false;
private _runtimeRpcs: string[] = [];
private _latencies: Record<string, number> = {};
private _networkRpcs: string[] = [];
private _networkRpcs: RpcType[] = [];
constructor(config: HandlerConstructorConfig) {
this._networkId = config.networkId;
this._networkRpcs = networkRpcs[this._networkId];
this._networkRpcs = networkRpcsOriginal[this._networkId];
this._networkName = networkNames[this._networkId];
this._initialize(config);
}
Expand Down Expand Up @@ -50,7 +51,7 @@ export class RPCHandler implements HandlerInterface {
Object.keys(this._latencies).filter((rpc) => rpc.startsWith(`${this._networkId}__`)).length <= 1 || this._refreshLatencies >= this._cacheRefreshCycles;

if (shouldRefreshRpcs) {
this._runtimeRpcs = networkRpcs[this._networkId];
this._runtimeRpcs = getRpcUrls(networkRpcs[this._networkId]);
this._refreshLatencies = 0;
} else {
this._runtimeRpcs = Object.keys(this._latencies).map((rpc) => {
Expand Down Expand Up @@ -96,7 +97,7 @@ export class RPCHandler implements HandlerInterface {
public clearInstance(): void {
RPCHandler._instance = null;
}
public getRuntimeRpcs(): string[] {
public getRuntimeRpcs(): RpcType[] {
return this._runtimeRpcs;
}
public getNetworkId(): number {
Expand All @@ -105,7 +106,7 @@ export class RPCHandler implements HandlerInterface {
public getNetworkName(): string {
return this._networkName;
}
public getNetworkRpcs(): string[] {
public getNetworkRpcs(): RpcType[] {
return this._networkRpcs;
}
public getLatencies(): Record<string, number> {
Expand Down
8 changes: 5 additions & 3 deletions tests/rpc-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { RPCHandler, networkRpcs } from "../dist";
import { networkRpcsOriginal, RPCHandler } from "../dist";
import { HandlerConstructorConfig } from "../types/handler";
import { getRpcUrls } from "../types/shared";

export const testConfig: HandlerConstructorConfig = {
networkId: 100,
Expand Down Expand Up @@ -46,7 +47,7 @@ describe("RPCHandler", () => {
expect(rpcHandler["_latencies"]).toEqual({});
});
it("should initialize with correct networkRpcs", () => {
expect(rpcHandler["_networkRpcs"]).toEqual(networkRpcs[testConfig.networkId]);
expect(rpcHandler["_networkRpcs"]).toEqual(networkRpcsOriginal[testConfig.networkId]);
});
it("should initialize with null provider", () => {
const provider = rpcHandler["_provider"];
Expand All @@ -71,8 +72,9 @@ describe("RPCHandler", () => {
const latArrLen = Array.from(Object.entries(latencies)).length;
const runtime = rpcHandler.getRuntimeRpcs();
expect(runtime.length).toBeGreaterThan(0);

expect(runtime.length).toBe(latArrLen);
expect(runtime.length).toBeLessThanOrEqual(networkRpcs[testConfig.networkId].length);
expect(runtime.length).toBeLessThanOrEqual(getRpcUrls(networkRpcsOriginal[testConfig.networkId]).length);

expect(latArrLen).toBeGreaterThan(1);

Expand Down
9 changes: 9 additions & 0 deletions types/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ChainNames, NativeToken, Token } from "./handler";
import { RpcType } from "./shared";

export declare const chainIDList: Record<string, string>;
export declare const extraRpcs: Record<ChainId, string[]>;
export declare const extraRpcsOriginal: Record<ChainId, { rpcs: RpcType[] }>;

export type ChainId<T extends string | number = number> = T extends keyof typeof chainIDList ? (typeof chainIDList)[T] : T;

Expand Down Expand Up @@ -31,6 +33,13 @@ export const networkRpcs: Record<ChainId, string[]> = Object.fromEntries(
})
);

export const networkRpcsOriginal: Record<ChainId, RpcType[]> = Object.fromEntries(
Object.entries(networkIds).map(([, value]) => {
const chainRpcs = extraRpcsOriginal[value] || [];
return [value, chainRpcs.rpcs];
})
);

export const tokens: Record<ChainId, Record<Token["symbol"], Token>> = {
[networkIds.Mainnet]: {
DAI: {
Expand Down
3 changes: 2 additions & 1 deletion types/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { ChainId, networkCurrencies, networkExplorers, networkIds, networkNames, networkRpcs, tokens } from "./constants";
import { ChainId, networkCurrencies, networkExplorers, networkIds, networkNames, networkRpcs, networkRpcsOriginal, tokens } from "./constants";

export type ValidBlockData = {
jsonrpc: string;
Expand Down Expand Up @@ -42,6 +42,7 @@ export type HandlerConstructorConfig = {
};

export type NetworkRPCs = typeof networkRpcs;
export type NetworkRPCsOriginal = typeof networkRpcsOriginal;
export type NetworkNames = typeof networkNames;
export type NetworkCurrencies = typeof networkCurrencies;
export type Tokens = typeof tokens;
Expand Down
13 changes: 7 additions & 6 deletions types/rpc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { LOCAL_HOST, networkRpcs, networkNames } from "./constants";
import { LOCAL_HOST, networkRpcs, networkNames, networkRpcsOriginal } from "./constants";
import { HandlerInterface, HandlerConstructorConfig } from "./handler";

import { RPCService } from "../src/services/rpc-service";
import { StorageService } from "../src/services/storage-service";
import { RpcType, getRpcUrls } from "./shared";

export class RPCHandler implements HandlerInterface {
private static _instance: RPCHandler | null = null;
Expand All @@ -20,11 +21,11 @@ export class RPCHandler implements HandlerInterface {
private _runtimeRpcs: string[] = [];
private _latencies: Record<string, number> = {};

private _networkRpcs: string[] = [];
private _networkRpcs: RpcType[];

constructor(config: HandlerConstructorConfig) {
this._networkId = config.networkId;
this._networkRpcs = networkRpcs[this._networkId];
this._networkRpcs = networkRpcsOriginal[this._networkId];
this._networkName = networkNames[this._networkId];
this._initialize(config);
}
Expand Down Expand Up @@ -55,7 +56,7 @@ export class RPCHandler implements HandlerInterface {
Object.keys(this._latencies).filter((rpc) => rpc.startsWith(`${this._networkId}__`)).length <= 1 || this._refreshLatencies >= this._cacheRefreshCycles;

if (shouldRefreshRpcs) {
this._runtimeRpcs = networkRpcs[this._networkId];
this._runtimeRpcs = getRpcUrls(networkRpcs[this._networkId]);
this._refreshLatencies = 0;
} else {
this._runtimeRpcs = Object.keys(this._latencies).map((rpc) => {
Expand Down Expand Up @@ -107,7 +108,7 @@ export class RPCHandler implements HandlerInterface {
RPCHandler._instance = null;
}

public getRuntimeRpcs(): string[] {
public getRuntimeRpcs(): RpcType[] {
return this._runtimeRpcs;
}

Expand All @@ -119,7 +120,7 @@ export class RPCHandler implements HandlerInterface {
return this._networkName;
}

public getNetworkRpcs(): string[] {
public getNetworkRpcs(): RpcType[] {
return this._networkRpcs;
}

Expand Down
21 changes: 21 additions & 0 deletions types/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type RpcType =
| string // if string then it is the official rpc of the chain
| {
url: string;
tracking: "yes" | "limited" | "none";
trackingDetails: string;
};

export function getRpcUrls(rpcs: RpcType[]) {
console.log("rpcs here", rpcs);
console.log("rpcs type", typeof rpcs);
let urls: string[] = [];
rpcs.forEach((rpc) => {
if (typeof rpc == "string") {
urls.push(rpc);
} else {
urls.push(rpc.url);
}
});
return urls;
}

0 comments on commit 837fd86

Please sign in to comment.