forked from Balmy-protocol/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalchemy-provider.ts
More file actions
39 lines (32 loc) · 1.33 KB
/
Copy pathalchemy-provider.ts
File metadata and controls
39 lines (32 loc) · 1.33 KB
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
import { ChainId } from '@types';
import { BaseHttpProvider, HttpProviderConfig } from './base/base-http-provider';
import { ALCHEMY_NETWORKS } from '@shared/alchemy';
export type AlchemySupportedChains = ChainId[];
export class AlchemyProviderSource extends BaseHttpProvider {
private readonly key: string;
private readonly supported: ChainId[];
constructor({ key, onChains, config }: { key: string; onChains?: AlchemySupportedChains; config?: HttpProviderConfig }) {
super(config);
this.key = key;
if (onChains === undefined) {
this.supported = alchemySupportedChains();
} else if (Array.isArray(onChains)) {
this.supported = onChains;
} else {
this.supported = alchemySupportedChains();
}
}
supportedChains(): ChainId[] {
return this.supported;
}
protected calculateUrl(chainId: ChainId): string {
return buildAlchemyRPCUrl({ chainId, apiKey: this.key, protocol: 'https' });
}
}
export function alchemySupportedChains(): ChainId[] {
return Object.entries(ALCHEMY_NETWORKS).map(([chainId]) => Number(chainId));
}
export function buildAlchemyRPCUrl({ chainId, apiKey, protocol }: { chainId: ChainId; apiKey: string; protocol: 'https' | 'wss' }) {
const { key: alchemyNetwork } = ALCHEMY_NETWORKS[chainId];
return `${protocol}://${alchemyNetwork}.g.alchemy.com/v2/${apiKey}`;
}