Skip to content

Commit

Permalink
support http endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Oct 28, 2023
1 parent 5e1f68c commit 482a8b4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
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
2 changes: 1 addition & 1 deletion packages/e2e/src/dev-dry-run-ump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'
import { setupApi, ws } from './helper'

setupApi({
endpoint: 'wss://rpc.polkadot.io',
endpoint: 'https://rpc.polkadot.io',
blockHash: '0xb012d04c56b65cfa1f47cb1f884d920f95d0097b1ed42f5da18d5e2a436c2f3e',
})

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
2 changes: 1 addition & 1 deletion packages/e2e/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SetupOption, setupContext } from '@acala-network/chopsticks-testing'
dotenvConfig

const endpoints = {
polkadot: 'wss://rpc.polkadot.io',
polkadot: 'https://rpc.polkadot.io',
acala: 'wss://acala-rpc-1.aca-api.network',
}

Expand Down

0 comments on commit 482a8b4

Please sign in to comment.