Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: add check for eth provider availability (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak authored Nov 18, 2020
1 parent 0c3b92c commit 6f8f16f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import { ReorgEmitterService, NewBlockEmitterService, ConfirmatorService } from

const logger = loggingFactory('blockchain')

export function ethFactory (): Eth {
export async function ethFactory (): Promise<Eth> {
const provider = Eth.givenProvider || config.get('blockchain.provider')
logger.info(`Connecting to provider ${provider}`)

return new Eth(provider)
const eth = new Eth(provider)
try {
await eth.getProtocolVersion()
} catch (e) {
throw new Error(`Can't connect to the node on address ${provider}`)
}
return eth
}

export async function web3eventsFactory (eth: Eth, sequelize: Sequelize): Promise<Web3Events> {
Expand Down Expand Up @@ -61,7 +66,7 @@ function subscribeAndEmitNewBlocks (app: Application, web3Events: Web3Events): v

export default async function (app: Application): Promise<void> {
await waitForReadyApp(app)
const eth = ethFactory()
const eth = await ethFactory()
app.set('eth', eth)

const sequelize = app.get('sequelize') as Sequelize
Expand Down
2 changes: 1 addition & 1 deletion src/cli/precache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ${formattedServices}`
// Init Store
await initStore(sequelize)

const eth = ethFactory()
const eth = await ethFactory()
const web3events = await web3eventsFactory(eth, sequelize)

const tasksDefinition = servicesToPreCache.map(
Expand Down
2 changes: 1 addition & 1 deletion test/integration/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class TestingApp {
}

private async initBlockchainProvider (): Promise<void> {
this.eth = ethFactory()
this.eth = await ethFactory()
const [owner, provider, consumer, ...accounts] = await this.eth.getAccounts()
this.contractOwner = owner
this.providerAddress = provider
Expand Down

0 comments on commit 6f8f16f

Please sign in to comment.