generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.ts
110 lines (94 loc) · 3.6 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { JsonRpcProvider } from "@ethersproject/providers";
import { networkCurrencies, networkExplorers, networkRpcs } from "./constants";
import { CHAINS_IDS, EXTRA_RPCS } from "../dynamic";
import { LogInterface, PrettyLogs, PrettyLogsWithOk } from "./logs";
export type BlockExplorer = {
name: string;
url: string;
standard?: string;
icon?: string;
};
export type ValidBlockData = {
jsonrpc: string;
id: number;
result: {
number: string;
timestamp: string;
hash: string;
};
};
export type Token = {
decimals: number;
address: string;
symbol: string;
};
export type NativeToken = {
name: string;
symbol: string;
decimals: number;
};
export type HandlerInterface = {
getProvider(): JsonRpcProvider | null;
clearInstance(): void;
getFastestRpcProvider(): Promise<JsonRpcProvider | null>;
testRpcPerformance(): Promise<JsonRpcProvider | null>;
};
// This is log message prefix which can be used to identify the logs from this module
type ModuleName = "[RPCHandler Provider Proxy] - ";
type ProxySettings = {
retryCount: number; // how many times we'll loop the list of RPCs retrying the request before failing
retryDelay: number; // how long we'll wait before moving to the next RPC
// eslint-disable-next-line @typescript-eslint/ban-types
logTier: (PrettyLogsWithOk & {}) | null; // set to "none" for no logs, null will default to "error", "verbose" will log all
logger: PrettyLogs | LogInterface | null; // null will default to PrettyLogs, otherwise pass in your own logger
strictLogs: boolean; // true is default, only the specified logTier will be logged. false will log all logs.
moduleName?: ModuleName | string; // this is the prefix for the logs
disabled?: boolean;
};
export type HandlerConstructorConfig = {
networkId: NetworkId;
networkName: NetworkName | null;
tracking?: Tracking; // "yes" | "limited" | "none", default is "yes". This is the data tracking status of the RPC provider
networkRpcs: Rpc[] | null; // e.g "https://mainnet.infura.io/..."
autoStorage: boolean | null; // browser only, will store in localStorage
cacheRefreshCycles: number | null; // bad RPCs are excluded if they fail, this is how many cycles before they're re-tested
runtimeRpcs: string[] | null; // e.g "<networkId>__https://mainnet.infura.io/..." > "1__https://mainnet.infura.io/..."
rpcTimeout: number | null; // when the RPCs are tested they are raced, this is the max time to allow for a response
proxySettings: ProxySettings; // settings for the proxy
};
export type NetworkRPCs = typeof networkRpcs;
export type NetworkCurrencies = typeof networkCurrencies;
export type NetworkExplorers = typeof networkExplorers;
/**
* Without this NetworkName builds as `any` because `keyof typeof EXTRA_RPCS`
* extends symbol which cannot be used to index an object
*/
type NetworkIds<T extends PropertyKey = keyof typeof EXTRA_RPCS> = {
[K in T]: K extends string ? K : never;
}[T];
// filtered NetworkId union
export type NetworkId = NetworkIds | "31337" | "1337";
// unfiltered Record<NetworkId, NetworkName>
type ChainsUnfiltered = {
-readonly [K in keyof typeof CHAINS_IDS]: (typeof CHAINS_IDS)[K];
};
// filtered NetworkName union
export type NetworkName = ChainsUnfiltered[NetworkId] | "anvil" | "hardhat";
export type Tracking = "yes" | "limited" | "none";
export type Rpc = {
url: string;
tracking?: Tracking;
trackingDetails?: string;
isOpenSource?: boolean;
};
export function getRpcUrls(rpcs: Rpc[]) {
const urls: string[] = [];
rpcs.forEach((rpc) => {
if (typeof rpc == "string") {
urls.push(rpc);
} else {
urls.push(rpc.url);
}
});
return urls;
}