From 64f7a713037d9157ca786a0949117d3a72a9596e Mon Sep 17 00:00:00 2001 From: fewensa Date: Thu, 1 Aug 2024 07:13:22 +0000 Subject: [PATCH] Pick rpc --- template/ts/files/package.json | 2 +- template/ts/files/src/helixconf.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/template/ts/files/package.json b/template/ts/files/package.json index 5d44ca0..99b54ea 100644 --- a/template/ts/files/package.json +++ b/template/ts/files/package.json @@ -1,6 +1,6 @@ { "name": "@helixbridge/helixconf", - "version": "1.1.5", + "version": "1.1.6", "description": "Helix conf", "main": "dist/src/index.js", "publishConfig": { diff --git a/template/ts/files/src/helixconf.ts b/template/ts/files/src/helixconf.ts index 42b9248..345f6e9 100644 --- a/template/ts/files/src/helixconf.ts +++ b/template/ts/files/src/helixconf.ts @@ -53,6 +53,17 @@ export interface CoupleFilter { symbol?: string } +export interface PickRPCOptions { + strategy: PickRPCStrategy, + picker?: (rpcs: string[]) => Promise, +} + +export enum PickRPCStrategy { + Custom, + First, + Best, +} + export interface HelixChainConfType { _network: _NetworkType id: bigint @@ -116,6 +127,22 @@ export class HelixChainConf { // this._data[key] = value; // } + async rpc(options?: PickRPCOptions): Promise { + const strategy = options?.strategy ?? PickRPCStrategy.First; + switch (strategy) { + case PickRPCStrategy.Custom: { + if (!(options?.picker)) { + return this.rpcs[0]; + } + return await options.picker(this.rpcs); + } + case PickRPCStrategy.First: + case PickRPCStrategy.Best: + default: + return this.rpcs[0]; + } + } + keys(): Array { return Object.keys(this._data) as Array; }