From c116cde4ca91411436c5a18da3910965b3e8a215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Tue, 23 Apr 2019 01:38:14 +0200 Subject: [PATCH] Allow create a Ocean instance in wrong network. #188 --- src/keeper/Keeper.ts | 67 ++++++++++++++++++++++-------------- src/utils/PromiseResolver.ts | 12 +++++++ src/utils/index.ts | 1 + 3 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 src/utils/PromiseResolver.ts diff --git a/src/keeper/Keeper.ts b/src/keeper/Keeper.ts index 429b115..e56a3fc 100644 --- a/src/keeper/Keeper.ts +++ b/src/keeper/Keeper.ts @@ -5,6 +5,7 @@ import { Condition, LockRewardCondition, EscrowReward, AccessSecretStoreConditio import { AgreementTemplate, EscrowAccessSecretStoreTemplate } from "./contracts/templates" import { TemplateStoreManager, AgreementStoreManager, ConditionStoreManager } from "./contracts/managers" +import { objectPromiseAll } from "../utils" import { EventHandler } from "./EventHandler" import { Instantiable, InstantiableConfig } from "../Instantiable.abstract" @@ -29,40 +30,48 @@ export class Keeper extends Instantiable { // Adding keeper inside Ocean to prevent `Keeper not defined yet` error config.ocean.keeper = keeper - const resolvedInstances = await Promise.all([ - // Main contracts - Dispenser.getInstance(config), - OceanToken.getInstance(config), - DIDRegistry.getInstance(config), - // Managers - TemplateStoreManager.getInstance(config), - AgreementStoreManager.getInstance(config), - ConditionStoreManager.getInstance(config), - // Conditions - LockRewardCondition.getInstance(config), - EscrowReward.getInstance(config), - AccessSecretStoreCondition.getInstance(config), - // Conditions - EscrowAccessSecretStoreTemplate.getInstance(config), - ]) + let instances = {} as any + try { + instances = await objectPromiseAll({ + // Main contracts + dispenser: Dispenser.getInstance(config), + token: OceanToken.getInstance(config), + didRegistry: DIDRegistry.getInstance(config), + // Managers + templateStoreManager: TemplateStoreManager.getInstance(config), + agreementStoreManager: AgreementStoreManager.getInstance(config), + conditionStoreManager: ConditionStoreManager.getInstance(config), + // Conditions + lockRewardCondition: LockRewardCondition.getInstance(config), + escrowReward: EscrowReward.getInstance(config), + accessSecretStoreCondition: AccessSecretStoreCondition.getInstance(config), + // Conditions + escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance(config), + }) + + keeper.connected = true + } catch { + keeper.connected = false + return + } // Main contracts - keeper.dispenser = resolvedInstances[0] - keeper.token = resolvedInstances[1] - keeper.didRegistry = resolvedInstances[2] + keeper.dispenser = instances.dispenser + keeper.token = instances.token + keeper.didRegistry = instances.didRegistry // Managers - keeper.templateStoreManager = resolvedInstances[3] - keeper.agreementStoreManager = resolvedInstances[4] - keeper.conditionStoreManager = resolvedInstances[5] + keeper.templateStoreManager = instances.templateStoreManager + keeper.agreementStoreManager = instances.agreementStoreManager + keeper.conditionStoreManager = instances.conditionStoreManager // Conditions keeper.conditions = { - lockRewardCondition: resolvedInstances[6], - escrowReward: resolvedInstances[7], - accessSecretStoreCondition: resolvedInstances[8], + lockRewardCondition: instances.lockRewardCondition, + escrowReward: instances.escrowReward, + accessSecretStoreCondition: instances.accessSecretStoreCondition, } // Conditions keeper.templates = { - escrowAccessSecretStoreTemplate: resolvedInstances[9], + escrowAccessSecretStoreTemplate: instances.escrowAccessSecretStoreTemplate, } // Utils @@ -73,6 +82,12 @@ export class Keeper extends Instantiable { return keeper } + /** + * Is connected to the correct network or not. + * @type {boolean} + */ + public connected: boolean = false + /** * Ocean Token smart contract instance. * @type {OceanToken} diff --git a/src/utils/PromiseResolver.ts b/src/utils/PromiseResolver.ts new file mode 100644 index 0000000..2d35148 --- /dev/null +++ b/src/utils/PromiseResolver.ts @@ -0,0 +1,12 @@ +const zipObject = (keys = [], values = []) => { + return keys.reduce((acc, key, index) => ({ + ...acc, + [key]: values[index], + }), {}) +} + +export const objectPromiseAll = async (obj: {[key: string]: Promise}) => { + const keys = Object.keys(obj) + const result = await Promise.all(Object.values(obj)) + return zipObject(keys, result) +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 9abf108..81fe379 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,4 @@ +export * from "./PromiseResolver" export * from "./Logger" export * from "./ConversionTypeHelpers" export * from "./GeneratorHelpers"