2019-11-11 12:27:18 +01:00
|
|
|
import Web3 from 'web3'
|
2019-06-20 00:20:09 +02:00
|
|
|
import Config from './models/Config'
|
|
|
|
import { Logger, LoggerInstance, LogLevel } from './utils'
|
|
|
|
import Web3Provider from './keeper/Web3Provider'
|
|
|
|
import { Ocean } from './ocean/Ocean'
|
2019-03-21 02:56:58 +01:00
|
|
|
|
|
|
|
export interface InstantiableConfig {
|
|
|
|
ocean: Ocean
|
|
|
|
config?: Config
|
|
|
|
web3?: Web3
|
|
|
|
logger?: Logger
|
|
|
|
}
|
|
|
|
|
2019-11-15 00:00:10 +01:00
|
|
|
export function generateIntantiableConfigFromConfig(
|
|
|
|
config: Config
|
|
|
|
): Partial<InstantiableConfig> {
|
2019-06-20 00:20:09 +02:00
|
|
|
const logLevel =
|
|
|
|
typeof config.verbose !== 'number'
|
|
|
|
? config.verbose
|
|
|
|
? LogLevel.Log
|
|
|
|
: LogLevel.None
|
|
|
|
: (config.verbose as LogLevel)
|
2019-03-21 02:56:58 +01:00
|
|
|
return {
|
|
|
|
config,
|
|
|
|
web3: Web3Provider.getWeb3(config),
|
2019-06-20 00:20:09 +02:00
|
|
|
logger: new Logger(logLevel)
|
2019-03-21 02:56:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class Instantiable {
|
|
|
|
protected get ocean() {
|
|
|
|
if (!this._ocean) {
|
2019-06-20 00:20:09 +02:00
|
|
|
this.logger.error('Ocean instance is not defined.')
|
2019-03-21 02:56:58 +01:00
|
|
|
}
|
|
|
|
return this._ocean
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get web3() {
|
|
|
|
if (!this._web3) {
|
2019-06-20 00:20:09 +02:00
|
|
|
this.logger.error('Web3 instance is not defined.')
|
|
|
|
this.logger.error('Using default instance.')
|
2019-03-21 02:56:58 +01:00
|
|
|
return Web3Provider.getWeb3()
|
|
|
|
}
|
|
|
|
return this._web3
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get config() {
|
|
|
|
if (!this._config) {
|
2019-06-20 00:20:09 +02:00
|
|
|
this.logger.error('Config instance is not defined.')
|
2019-03-21 02:56:58 +01:00
|
|
|
}
|
|
|
|
return this._config
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get logger() {
|
|
|
|
if (!this._logger) {
|
2019-06-20 00:20:09 +02:00
|
|
|
LoggerInstance.error('Logger instance is not defined.')
|
|
|
|
LoggerInstance.error('Using default instance.')
|
2019-03-21 02:56:58 +01:00
|
|
|
return LoggerInstance
|
|
|
|
}
|
|
|
|
return this._logger
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get instanceConfig(): InstantiableConfig {
|
2019-06-20 00:20:09 +02:00
|
|
|
const { ocean, web3, config, logger } = this
|
|
|
|
return { ocean, web3, config, logger }
|
2019-03-21 02:56:58 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
public static async getInstance(...args: any[]): Promise<any>
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
public static async getInstance(config: InstantiableConfig): Promise<any> {
|
2019-09-09 12:18:54 +02:00
|
|
|
LoggerInstance.warn('getInstance() methods has needs to be added to child class.')
|
2019-03-21 03:17:36 +01:00
|
|
|
}
|
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
protected static setInstanceConfig<T extends Instantiable>(
|
|
|
|
instance: T,
|
|
|
|
{ ocean, config, web3, logger }: InstantiableConfig
|
|
|
|
) {
|
2019-03-21 02:56:58 +01:00
|
|
|
instance._ocean = ocean
|
|
|
|
instance._config = config
|
|
|
|
instance._web3 = web3
|
|
|
|
instance._logger = logger
|
|
|
|
}
|
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
private _ocean: Ocean
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
private _web3: Web3
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
private _config: Config
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-03-21 03:17:36 +01:00
|
|
|
private _logger: Logger
|
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
protected setInstanceConfig(config: InstantiableConfig) {
|
|
|
|
Instantiable.setInstanceConfig(this, config)
|
|
|
|
}
|
|
|
|
}
|