| 35 | export type GetFetchFunc = () => FetchFunc; |
| 36 | |
| 37 | export class ApiPromiseConnection |
| 38 | implements |
| 39 | IApiConnectionSpecific< |
| 40 | ApiPromise, |
| 41 | ApiAt, |
| 42 | IBlock<BlockContent>[] | IBlock<LightBlockContent>[] |
| 43 | > |
| 44 | { |
| 45 | readonly networkMeta: NetworkMetadataPayload; |
| 46 | |
| 47 | private constructor( |
| 48 | public unsafeApi: ApiPromise, |
| 49 | private fetchBlocksBatches: GetFetchFunc, |
| 50 | ) { |
| 51 | this.networkMeta = { |
| 52 | chain: unsafeApi.runtimeChain.toString(), |
| 53 | specName: unsafeApi.runtimeVersion.specName.toString(), |
| 54 | genesisHash: unsafeApi.genesisHash.toString(), |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | static async create( |
| 59 | endpoint: string, |
| 60 | fetchBlocksBatches: GetFetchFunc, |
| 61 | args: { chainTypes?: RegisteredTypes }, |
| 62 | config: IEndpointConfig, |
| 63 | ): Promise<ApiPromiseConnection> { |
| 64 | let provider: ProviderInterface; |
| 65 | let throwOnConnect = false; |
| 66 | |
| 67 | const headers = { |
| 68 | 'User-Agent': `SubQuery-Node ${packageVersion}`, |
| 69 | ...config.headers, |
| 70 | }; |
| 71 | |
| 72 | if (endpoint.startsWith('ws')) { |
| 73 | provider = createCachedProvider( |
| 74 | new WsProvider(endpoint, RETRY_DELAY, headers), |
| 75 | ); |
| 76 | } else if (endpoint.startsWith('http')) { |
| 77 | provider = createCachedProvider(new HttpProvider(endpoint, headers)); |
| 78 | throwOnConnect = true; |
| 79 | } else { |
| 80 | throw new Error(`Invalid endpoint: ${endpoint}`); |
| 81 | } |
| 82 | |
| 83 | const apiOption = { |
| 84 | provider, |
| 85 | throwOnConnect, |
| 86 | noInitWarn: true, |
| 87 | ...args.chainTypes, |
| 88 | }; |
| 89 | const api = await ApiPromise.create(apiOption); |
| 90 | return new ApiPromiseConnection(api, fetchBlocksBatches); |
| 91 | } |
| 92 | |
| 93 | safeApi(height: number): ApiAt { |
| 94 | throw new Error(`Not Implemented`); |
nothing calls this directly
no outgoing calls
no test coverage detected