|
1 | 1 | import networksAsJSON from "./networks.json"
|
2 | 2 |
|
3 |
| -export interface Contracts { |
4 |
| - [name: string]: string |
| 3 | +interface ContractsJSON { |
| 4 | + readonly [name: string]: string |
| 5 | +} |
| 6 | +export class Contracts implements ContractsJSON { |
| 7 | + [name: string]: string |
| 8 | +} |
| 9 | + |
| 10 | +export enum RPCProtocol { |
| 11 | + HTTP, |
| 12 | + WEBSOCKET |
5 | 13 | }
|
6 | 14 |
|
7 |
| -export interface Chain { |
8 |
| - id: number |
9 |
| - rpcHttpUrl: string |
10 |
| - rpcWsUrl: string |
11 |
| - contracts: Contracts |
| 15 | +interface RPCEndpointJSON { |
| 16 | + readonly url: string |
12 | 17 | }
|
13 | 18 |
|
14 |
| -export interface Chains { |
15 |
| - [name: string]: Chain |
| 19 | +export class RPCEndpoint implements RPCEndpointJSON { |
| 20 | + constructor( |
| 21 | + readonly url: string, |
| 22 | + //readonly readTimeoutSecond: int, |
| 23 | + //readonly writeTimeoutSecond: int, |
| 24 | + ) {} |
| 25 | +} |
| 26 | + |
| 27 | +interface ChainJSON { |
| 28 | + readonly id: number |
| 29 | + readonly rpcEndpoints: RPCEndpointJSON[] |
| 30 | + readonly contracts: ContractsJSON |
| 31 | +} |
| 32 | + |
| 33 | +export class Chain implements ChainJSON { |
| 34 | + constructor( |
| 35 | + public readonly id: number, |
| 36 | + public rpcEndpoints: RPCEndpoint[], |
| 37 | + public contracts: Contracts, |
| 38 | + ) { |
| 39 | + this.id = id |
| 40 | + this.rpcEndpoints = new Array<RPCEndpoint>() |
| 41 | + for (const rpcEndpoint of rpcEndpoints) { |
| 42 | + this.rpcEndpoints.push(new RPCEndpoint(rpcEndpoint.url)) |
| 43 | + } |
| 44 | + this.contracts = new Contracts() |
| 45 | + for (const key of Object.keys(contracts)) { |
| 46 | + this.contracts[key] = contracts[key] |
| 47 | + } |
| 48 | + } |
| 49 | + getRPCEndpointsByProtocol(protocol: RPCProtocol): RPCEndpoint[] { |
| 50 | + const endpoints = new Array<RPCEndpoint>() |
| 51 | + for (const rpcEndpoint of this.rpcEndpoints) { |
| 52 | + if (protocol === RPCProtocol.HTTP) { |
| 53 | + if (rpcEndpoint.url.startsWith("https://") || rpcEndpoint.url.startsWith("http://")) { |
| 54 | + endpoints.push(new RPCEndpoint(rpcEndpoint.url)) |
| 55 | + } |
| 56 | + } else if (protocol === RPCProtocol.WEBSOCKET) { |
| 57 | + if (rpcEndpoint.url.startsWith("wss://") || rpcEndpoint.url.startsWith("ws://")) { |
| 58 | + endpoints.push(new RPCEndpoint(rpcEndpoint.url)) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return endpoints |
| 63 | + } |
16 | 64 | }
|
17 | 65 |
|
18 | 66 | export type Environment = "development" | "production"
|
19 | 67 |
|
20 |
| -export type Networks = { |
21 |
| - [env in Environment]: Chains |
| 68 | +type NetworksJSON = { |
| 69 | + readonly [env in Environment]: ChainsJSON |
| 70 | +} |
| 71 | + |
| 72 | +interface ChainsJSON { |
| 73 | + readonly [name: string]: ChainJSON |
| 74 | +} |
| 75 | + |
| 76 | +export class Chains implements ChainsJSON { |
| 77 | + [name: string]: Chain |
| 78 | + public static load(env: Environment): Chains { |
| 79 | + const networks: NetworksJSON = networksAsJSON |
| 80 | + const chainsJson: ChainsJSON = networks[env] |
| 81 | + const chains: Chains = ChainsFactory.create(chainsJson) |
| 82 | + return chains |
| 83 | + } |
22 | 84 | }
|
23 | 85 |
|
24 |
| -export const loadConfig = (env: Environment): Chains => { |
25 |
| - const networks: Networks = networksAsJSON |
26 |
| - const chain: Chains = networks[env] |
27 |
| - return chain |
| 86 | +class ChainsFactory { |
| 87 | + private constructor() {} |
| 88 | + static create(chainsJson: ChainsJSON): Chains { |
| 89 | + const chains = new Chains() |
| 90 | + for (const key in chainsJson) { |
| 91 | + const chainJson: ChainJSON = chainsJson[key] |
| 92 | + const rpcEndpoints = new Array<RPCEndpoint>() |
| 93 | + for (const rpcEndpoint of chainJson.rpcEndpoints) { |
| 94 | + rpcEndpoints.push(new RPCEndpoint(rpcEndpoint.url)) |
| 95 | + } |
| 96 | + const contracts = new Contracts() |
| 97 | + for (const key of Object.keys(chainJson.contracts)) { |
| 98 | + contracts[key] = chainJson.contracts[key] |
| 99 | + } |
| 100 | + chains[key] = new Chain(chainJson.id, rpcEndpoints, contracts) |
| 101 | + } |
| 102 | + return chains |
| 103 | + } |
28 | 104 | }
|
0 commit comments