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

feat: add check for eth provider availability #402

Merged
merged 1 commit into from
Nov 18, 2020
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
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