Skip to content

Commit

Permalink
Multiple WS urls for PolkadotJS API (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs authored Jun 14, 2024
1 parent b303c1d commit d91de57
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/quick-parrots-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moonbeam-network/xcm-types': patch
'@moonbeam-network/xcm-utils': patch
---

Multiple WS urls for PolkadotJS API
2 changes: 1 addition & 1 deletion packages/types/src/chain/parachain/EvmParachain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class EvmParachain extends Parachain {
rpcUrls: {
default: {
http: [this.rpc],
webSocket: [this.ws],
webSocket: Array.isArray(this.ws) ? this.ws : [this.ws],
},
},
});
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/chain/parachain/Parachain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ParachainConstructorParams
ss58Format: number;
usesChainDecimals?: boolean;
weight?: number;
ws: string;
ws: string | string[];
}

export class Parachain extends Chain {
Expand All @@ -28,7 +28,7 @@ export class Parachain extends Chain {

readonly weight: number | undefined;

readonly ws: string;
readonly ws: string | string[];

constructor({
assetsData,
Expand Down
9 changes: 6 additions & 3 deletions packages/utils/src/polkadot/polkadot.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ const cache = new LRUCache<string, Promise<ApiPromise>>({
},
});

export async function getPolkadotApi(ws: string): Promise<ApiPromise> {
export async function getPolkadotApi(
ws: string | string[],
): Promise<ApiPromise> {
const key = Array.isArray(ws) ? ws.join(';') : ws;
const promise =
cache.get(ws) ||
cache.get(key) ||
ApiPromise.create({
noInitWarn: true,
provider: new WsProvider(ws),
Expand All @@ -39,7 +42,7 @@ export async function getPolkadotApi(ws: string): Promise<ApiPromise> {
typesBundle,
});

cache.set(ws, promise);
cache.set(key, promise);

const api = await promise;

Expand Down

0 comments on commit d91de57

Please sign in to comment.