diff --git a/packages/chopsticks/src/server.ts b/packages/chopsticks/src/server.ts index 1e0e7a66..901ce44e 100644 --- a/packages/chopsticks/src/server.ts +++ b/packages/chopsticks/src/server.ts @@ -1,6 +1,6 @@ +import { AddressInfo, WebSocket, WebSocketServer } from 'ws' import { ResponseError, SubscriptionManager } from '@acala-network/chopsticks-core' import { z } from 'zod' -import WebSocket, { AddressInfo, WebSocketServer } from 'ws' import { defaultLogger, truncate } from './logger' diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index 6b558930..17760b78 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -62,10 +62,12 @@ export class Api { this.#ready = this.#provider['isReady'] } else { this.#ready = new Promise((resolve): void => { - this.#provider.on('connected', (): void => { + if (this.#provider.hasSubscriptions) { + this.#provider.on('connected', resolve) + this.#provider.connect() + } else { resolve() - }) - this.#provider.connect() + } }) } } diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index 591874cf..dae2ed2d 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -1,8 +1,8 @@ import '@polkadot/types-codec' import { HexString } from '@polkadot/util/types' +import { HttpProvider, WsProvider } from '@polkadot/rpc-provider' import { ProviderInterface } from '@polkadot/rpc-provider/types' import { RegisteredTypes } from '@polkadot/types/types' -import { WsProvider } from '@polkadot/rpc-provider' import { Api } from './api' import { Blockchain } from './blockchain' @@ -37,6 +37,8 @@ export const setup = async (options: SetupOptions) => { let provider: ProviderInterface if (options.genesis) { provider = options.genesis + } else if (/^(https|http):\/\//.test(options.endpoint || '')) { + provider = new HttpProvider(options.endpoint) } else { provider = new WsProvider(options.endpoint) } diff --git a/packages/e2e/src/batch-request.test.ts b/packages/e2e/src/batch-request.test.ts index e5e3c54e..d968cbae 100644 --- a/packages/e2e/src/batch-request.test.ts +++ b/packages/e2e/src/batch-request.test.ts @@ -1,5 +1,5 @@ +import { WebSocket } from 'ws' import { afterAll, beforeAll, describe, expect, it } from 'vitest' -import WebSocket from 'ws' import networks from './networks' diff --git a/packages/e2e/src/build-block.test.ts b/packages/e2e/src/build-block.test.ts index 576e73af..c6bc5175 100644 --- a/packages/e2e/src/build-block.test.ts +++ b/packages/e2e/src/build-block.test.ts @@ -13,7 +13,7 @@ const KUSAMA_STORAGE = { } describe.runIf(process.env.CI).each([ - { chain: 'Polkadot', endpoint: 'wss://rpc.polkadot.io' }, + { chain: 'Polkadot', endpoint: 'https://rpc.polkadot.io' }, { chain: 'Statemint', endpoint: 'wss://statemint-rpc.polkadot.io' }, { chain: 'Polkadot Collectives', endpoint: 'wss://polkadot-collectives-rpc.polkadot.io' }, { chain: 'Acala', endpoint: 'wss://acala-rpc-1.aca-api.network' }, diff --git a/packages/e2e/src/helper.ts b/packages/e2e/src/helper.ts index fc077377..ad301612 100644 --- a/packages/e2e/src/helper.ts +++ b/packages/e2e/src/helper.ts @@ -1,6 +1,7 @@ -import { ApiPromise, WsProvider } from '@polkadot/api' +import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api' import { Codec, RegisteredTypes } from '@polkadot/types/types' import { HexString } from '@polkadot/util/types' +import { ProviderInterface } from '@polkadot/rpc-provider/types' import { beforeAll, beforeEach, expect, vi } from 'vitest' import { Api } from '@acala-network/chopsticks' @@ -58,7 +59,15 @@ export const setupAll = async ({ registeredTypes = {}, runtimeLogLevel, }: SetupOption) => { - const api = new Api(genesis ? await genesisFromUrl(genesis) : new WsProvider(endpoint), { + let provider: ProviderInterface + if (genesis) { + provider = await genesisFromUrl(genesis) + } else if (/^(https|http):\/\//.test(endpoint || '')) { + provider = new HttpProvider(endpoint) + } else { + provider = new WsProvider(endpoint) + } + const api = new Api(provider, { SetEvmOrigin: { payload: {}, extrinsic: {} }, })