squid-js/src/keeper/Keeper.ts

246 lines
7.8 KiB
TypeScript
Raw Normal View History

import { lookup } from '@ethereum-navigator/navigator'
2019-06-20 00:20:09 +02:00
import { ContractBase } from './contracts/ContractBase'
2019-06-14 00:34:53 +02:00
2019-06-20 00:20:09 +02:00
import DIDRegistry from './contracts/DIDRegistry'
import Dispenser from './contracts/Dispenser'
import OceanToken from './contracts/Token'
import {
Condition,
LockRewardCondition,
EscrowReward,
2019-09-05 13:32:56 +02:00
AccessSecretStoreCondition,
ComputeExecutionCondition
2019-06-20 00:20:09 +02:00
} from './contracts/conditions'
import {
2019-09-05 13:33:49 +02:00
EscrowAccessSecretStoreTemplate,
EscrowComputeExecutionTemplate
2019-06-20 00:20:09 +02:00
} from './contracts/templates'
2019-11-15 00:00:10 +01:00
import {
TemplateStoreManager,
AgreementStoreManager,
ConditionStoreManager
} from './contracts/managers'
2018-12-17 17:37:36 +01:00
2019-06-20 00:20:09 +02:00
import { objectPromiseAll } from '../utils'
import { EventHandler } from './EventHandler'
2019-06-20 00:20:09 +02:00
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
2020-01-17 16:39:44 +01:00
import { AgreementTemplateBase } from './contracts/templates/AgreementTemplateBase'
/**
* Interface with Ocean Keeper contracts.
* Ocean Keeper implementation where we put the following modules together:
* - TCRs: users create challenges and resolve them through voting to maintain registries.
* - Ocean Tokens: the intrinsic tokens circulated inside Ocean network, which is used in the voting of TCRs.
* - Marketplace: the core marketplace where people can transact with each other with Ocean tokens.
*/
export class Keeper extends Instantiable {
/**
* Returns Keeper instance.
* @return {Promise<Keeper>}
*/
2019-09-09 12:18:54 +02:00
public static async getInstance(config: InstantiableConfig): Promise<Keeper> {
const keeper = new Keeper()
keeper.setInstanceConfig(config)
// Adding keeper inside Ocean to prevent `Keeper not defined yet` error
config.ocean.keeper = keeper
2019-06-14 00:34:53 +02:00
keeper.instances = {}
try {
2019-06-14 00:34:53 +02:00
keeper.instances = await objectPromiseAll({
// Main contracts
2019-06-25 11:12:32 +02:00
dispenser: undefined, // Optional
token: OceanToken.getInstance(config),
didRegistry: DIDRegistry.getInstance(config),
// Managers
templateStoreManager: TemplateStoreManager.getInstance(config),
2019-09-09 12:18:54 +02:00
agreementStoreManager: AgreementStoreManager.getInstance(config),
conditionStoreManager: ConditionStoreManager.getInstance(config),
// Conditions
lockRewardCondition: LockRewardCondition.getInstance(config),
escrowReward: EscrowReward.getInstance(config),
2019-11-15 00:00:10 +01:00
accessSecretStoreCondition: AccessSecretStoreCondition.getInstance(
config
),
2020-01-15 19:28:04 +01:00
computeExecutionCondition: ComputeExecutionCondition.getInstance(config)
})
keeper.connected = true
} catch {
keeper.connected = false
return
}
2019-06-25 11:12:32 +02:00
// Optionals
try {
keeper.instances.dispenser = await Dispenser.getInstance(config)
} catch {
keeper.logger.warn('Dispenser not available on this network.')
}
2019-04-10 12:28:51 +02:00
// Main contracts
2019-06-14 00:34:53 +02:00
keeper.dispenser = keeper.instances.dispenser
keeper.token = keeper.instances.token
keeper.didRegistry = keeper.instances.didRegistry
// Managers
2019-06-14 00:34:53 +02:00
keeper.templateStoreManager = keeper.instances.templateStoreManager
keeper.agreementStoreManager = keeper.instances.agreementStoreManager
keeper.conditionStoreManager = keeper.instances.conditionStoreManager
// Conditions
keeper.conditions = {
2019-06-14 00:34:53 +02:00
lockRewardCondition: keeper.instances.lockRewardCondition,
escrowReward: keeper.instances.escrowReward,
2019-09-09 12:18:54 +02:00
accessSecretStoreCondition: keeper.instances.accessSecretStoreCondition,
computeExecutionCondition: keeper.instances.computeExecutionCondition
2018-10-16 14:56:18 +02:00
}
2020-01-15 19:28:04 +01:00
// Templates
keeper.templates = Object()
keeper.templates.escrowAccessSecretStoreTemplate = new EscrowAccessSecretStoreTemplate(
2020-01-19 10:41:12 +01:00
keeper.templateStoreManager,
keeper.agreementStoreManager,
keeper.didRegistry,
keeper.conditions
2020-01-15 19:28:04 +01:00
)
keeper.templates.escrowComputeExecutionTemplate = new EscrowComputeExecutionTemplate(
2020-01-19 10:41:12 +01:00
keeper.templateStoreManager,
keeper.agreementStoreManager,
keeper.didRegistry,
keeper.conditions
2020-01-15 19:28:04 +01:00
)
// Utils
keeper.utils = {
2019-06-20 00:20:09 +02:00
eventHandler: new EventHandler(config)
}
return keeper
}
2018-10-16 14:56:18 +02:00
/**
* Is connected to the correct network or not.
* @type {boolean}
*/
public connected: boolean = false
/**
* Ocean Token smart contract instance.
* @type {OceanToken}
*/
2018-10-02 10:06:26 +02:00
public token: OceanToken
/**
* Ocean Market smart contract instance.
2019-01-28 15:41:19 +01:00
* @type {Dispenser}
*/
2019-01-28 15:41:19 +01:00
public dispenser: Dispenser
/**
* DID registry smart contract instance.
* @type {DIDRegistry}
*/
public didRegistry: DIDRegistry
/**
* Template store manager smart contract instance.
* @type {TemplateStoreManager}
*/
public templateStoreManager: TemplateStoreManager
/**
* Template store manager smart contract instance.
* @type {AgreementStoreManager}
*/
public agreementStoreManager: AgreementStoreManager
/**
* Template store manager smart contract instance.
* @type {ConditionStoreManager}
*/
public conditionStoreManager: ConditionStoreManager
/**
* Conditions instances.
*/
public conditions: {
2019-06-20 00:20:09 +02:00
lockRewardCondition: LockRewardCondition
escrowReward: EscrowReward
accessSecretStoreCondition: AccessSecretStoreCondition
2019-09-05 13:32:56 +02:00
computeExecutionCondition: ComputeExecutionCondition
}
/**
* Templates instances.
*/
public templates: {
2019-06-20 00:20:09 +02:00
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate
2019-09-05 13:33:49 +02:00
escrowComputeExecutionTemplate: EscrowComputeExecutionTemplate
}
/**
* Helpers for contracts.
*/
public utils: {
2019-06-20 00:20:09 +02:00
eventHandler: EventHandler
}
2019-06-20 00:20:09 +02:00
private instances: { [contractRef: string]: ContractBase & any }
2019-06-14 12:07:35 +02:00
/**
* Returns a condition by address.
* @param {string} address Address of deployed condition.
* @return {Condition} Condition instance.
*/
public getConditionByAddress(address: string): Condition {
2019-11-15 00:00:10 +01:00
return Object.values(this.conditions).find(
(condition) => condition.getAddress() === address
2019-11-15 00:00:10 +01:00
)
}
/**
* Returns a template by name.
* @param {string} name Template name.
* @return {AgreementTemplateBase} AgreementTemplateBase instance.
*/
public getTemplateByName(name: string): AgreementTemplateBase {
2019-11-15 00:00:10 +01:00
return Object.values(this.templates).find(
(template) => template.templateName === name
2019-11-15 00:00:10 +01:00
)
}
/**
* Returns a template by address.
* @param {string} templateId Template id (hex representation of bytes32).
* @return {AgreementTemplateBase} AgreementTemplateBase instance.
*/
public getTemplateById(templateId: string): AgreementTemplateBase {
2019-11-15 00:00:10 +01:00
return Object.values(this.templates).find(
(template) => template.getId() === templateId
2019-11-15 00:00:10 +01:00
)
}
/**
* Returns network id.
* @return {Promise<number>} Network ID.
*/
public getNetworkId(): Promise<number> {
return this.web3.eth.net.getId()
}
/**
* Returns the network by name.
* @return {Promise<string>} Network name.
*/
public async getNetworkName(): Promise<string> {
2019-11-11 12:27:18 +01:00
return this.web3.eth.net.getId().then((networkId: number) => {
const network = lookup(networkId)
return network && network.name ? network.name : 'Development'
2019-06-20 00:20:09 +02:00
})
}
2019-06-14 00:34:53 +02:00
2019-06-14 12:07:35 +02:00
public getAllInstances() {
2019-06-14 00:34:53 +02:00
return this.instances
}
}
export default Keeper