Skip to content

Commit

Permalink
fix provider events
Browse files Browse the repository at this point in the history
  • Loading branch information
0xslipk committed Nov 17, 2022
1 parent 13533e6 commit e8d0894
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@polkadot/api-augment'
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'
import { Module } from '@nestjs/common'
import { GraphQLModule } from '@nestjs/graphql'
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/contracts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ContractsService {
}

async getContractQueries(contractAddress: string): Promise<Contract> {
const api = await connect(this.env.WS_PROVIDER)
const api = await connect(this.env.WS_PROVIDER, this.logger)
const contract = await this.findOne(contractAddress)
const { address, metadata } = contract
if (!address || !metadata) {
Expand Down
3 changes: 1 addition & 2 deletions src/subscriptions/subscriptions.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { Injectable, OnModuleInit } from '@nestjs/common'
import '@polkadot/api-augment'
import { ApiPromise } from '@polkadot/api'
import { BlockHash, Header } from '@polkadot/types/interfaces'
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino'
Expand Down Expand Up @@ -35,7 +34,7 @@ export class SubscriptionsService implements OnModuleInit {
}

async syncBlocks() {
const api = await connect(this.env.WS_PROVIDER)
const api = await connect(this.env.WS_PROVIDER, this.logger)
const lastDBBlockNumber = (await this.blocksService.getLastBlock())?.number || 0
const lastBlockNumber = (await api.rpc.chain.getHeader()).number.toNumber()
const loadFromBlockNumber = this.env.LOAD_ALL_BLOCKS ? this.env.FIRST_BLOCK_TO_LOAD : lastDBBlockNumber
Expand Down
3 changes: 3 additions & 0 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ApiPromise } from '@polkadot/api'
import { apiMock } from '../mocks/api-mock'
import { connect } from './utils'

jest.mock('@polkadot/api')

describe('Utils', () => {
let api: ApiPromise

Expand All @@ -13,6 +15,7 @@ describe('Utils', () => {

it('should return an ApiPromise instance', async () => {
const result = await connect('ws://localhost:9944')

expect(result).toEqual(api)
})
})
25 changes: 23 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { PinoLogger } from 'nestjs-pino'

export const connect = async (provider: string | string[] | undefined) => {
return ApiPromise.create({ provider: new WsProvider(provider) })
export const connect = async (endpoint: string, logger: PinoLogger | Console = console) => {
const provider = new WsProvider(endpoint, false)

logger.info(`Connecting to ${endpoint}...`)

await provider.connect()

provider.on('connected', () => {
logger.info(`Connected to ${endpoint}`)
})

provider.on('disconnected', async () => {
logger.warn(`Disconnected to ${endpoint}, retying...`)

await provider.connect()
})

provider.on('error', async (err) => {
logger.error({ err }, `Error connecting to ${endpoint}`)
})

return ApiPromise.create({ provider })
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"noImplicitThis": true,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"skipLibCheck": true
}
}

0 comments on commit e8d0894

Please sign in to comment.