Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support http endpoint #480

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/chopsticks/src/server.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/setup.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/batch-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WebSocket } from 'ws'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import WebSocket from 'ws'

import networks from './networks'

Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/build-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
13 changes: 11 additions & 2 deletions packages/e2e/src/helper.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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: {} },
})

Expand Down