Skip to content

Commit

Permalink
quick rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa committed Aug 1, 2024
1 parent 63af2cf commit 1f6344f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion template/ts/files/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helixbridge/helixconf",
"version": "1.1.6",
"version": "1.1.7",
"description": "Helix conf",
"main": "dist/src/index.js",
"publishConfig": {
Expand Down
35 changes: 28 additions & 7 deletions template/ts/files/src/helixconf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ export interface PickRPCOptions {
picker?: (rpcs: string[]) => Promise<string>,
}

export interface PickRPCOptionsSync {
strategy: PickRPCStrategy,
picker?: (rpcs: string[]) => string,
}

export enum PickRPCStrategy {
Custom,
First,
Best,
Random,
}

export interface HelixChainConfType {
Expand Down Expand Up @@ -119,6 +125,10 @@ export class HelixChainConf {
return this._data.couples;
}

get rpc(): string {
return this.pickRpcSync();
}

get<K extends keyof HelixChainConfType>(key: K): HelixChainConfType[K] {
return this._data[key];
}
Expand All @@ -127,26 +137,37 @@ export class HelixChainConf {
// this._data[key] = value;
// }

rpc(): string {
return this.rpcs[0];
}

async pickRpc(options?: PickRPCOptions): Promise<string> {
pickRpcSync(options?: PickRPCOptionsSync): string {
const strategy = options?.strategy ?? PickRPCStrategy.First;
switch (strategy) {
case PickRPCStrategy.Custom: {
if (!(options?.picker)) {
return this.rpcs[0];
}
return await options.picker(this.rpcs);
return options.picker(this.rpcs);
}
case PickRPCStrategy.Random: {
const len = this.rpcs.length;
return this.rpcs[Math.floor(Math.random() * len)];
}
case PickRPCStrategy.Best: // todo: pick best rpc url, maybe check latency
case PickRPCStrategy.First:
case PickRPCStrategy.Best:
default:
return this.rpcs[0];
}
}

async pickRpc(options?: PickRPCOptions): Promise<string> {
const strategy = options?.strategy ?? PickRPCStrategy.First;
if (strategy === PickRPCStrategy.Custom) {
if (!(options?.picker)) {
return this.rpcs[0];
}
return await options.picker(this.rpcs);
}
return this.pickRpcSync({strategy: strategy});
}

keys(): Array<keyof HelixChainConfType> {
return Object.keys(this._data) as Array<keyof HelixChainConfType>;
}
Expand Down

0 comments on commit 1f6344f

Please sign in to comment.