squid-js/src/keeper/Keeper.ts

253 lines
8.1 KiB
TypeScript
Raw Normal View History

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 {
AgreementTemplate,
2019-09-05 13:33:49 +02:00
EscrowAccessSecretStoreTemplate,
EscrowComputeExecutionTemplate
2019-06-20 00:20:09 +02:00
} from './contracts/templates'
2019-09-09 12:18:54 +02: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'
/**
* 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-09-09 12:18:54 +02:00
accessSecretStoreCondition: AccessSecretStoreCondition.getInstance(config),
computeExecutionCondition: ComputeExecutionCondition.getInstance(config),
2019-06-14 00:34:53 +02:00
// Templates
2019-09-09 12:18:54 +02:00
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance(config),
escrowComputeExecutionTemplate: EscrowComputeExecutionTemplate.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
}
// Conditions
keeper.templates = {
2019-09-09 12:18:54 +02:00
escrowAccessSecretStoreTemplate: keeper.instances.escrowAccessSecretStoreTemplate,
escrowComputeExecutionTemplate: keeper.instances.escrowComputeExecutionTemplate
}
// 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-09-09 12:18:54 +02:00
return Object.values(this.conditions).find(condition => condition.getAddress() === address)
}
/**
* Returns a template by name.
* @param {string} name Template name.
* @return {AgreementTemplate} Agreement template instance.
*/
public getTemplateByName(name: string): AgreementTemplate {
2019-09-09 12:18:54 +02:00
return Object.values(this.templates).find(template => template.contractName === name)
}
/**
* Returns a template by address.
* @param {string} address Template address.
* @return {AgreementTemplate} Agreement template instance.
*/
public getTemplateByAddress(address: string): AgreementTemplate {
2019-09-09 12:18:54 +02:00
return Object.values(this.templates).find(template => template.getAddress() === address)
}
/**
* 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 getNetworkName(): Promise<string> {
2019-11-11 12:27:18 +01:00
return this.web3.eth.net.getId().then((networkId: number) => {
2019-06-20 00:20:09 +02:00
switch (networkId) {
case 1:
return 'Main'
case 2:
return 'Morden'
case 3:
return 'Ropsten'
case 4:
return 'Rinkeby'
case 77:
return 'POA_Sokol'
case 99:
return 'POA_Core'
case 42:
return 'Kovan'
2019-11-22 09:53:37 +01:00
case 100:
return 'xDai'
2019-06-20 00:20:09 +02:00
case 2199:
return 'Duero'
case 8996:
return 'Spree'
case 8995:
return 'Nile'
2019-06-24 13:48:19 +02:00
case 0xcea11:
return 'Pacific'
2019-06-20 00:20:09 +02:00
default:
return 'Development'
}
})
}
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