squid-js/src/keeper/Keeper.ts

153 lines
4.8 KiB
TypeScript
Raw Normal View History

import DIDRegistry from "./contracts/DIDRegistry"
2019-01-28 15:41:19 +01:00
import Dispenser from "./contracts/Dispenser"
import OceanToken from "./contracts/Token"
import { Condition, LockRewardCondition, EscrowReward, AccessSecretStoreCondition } from "./contracts/conditions"
import { EscrowAccessSecretStoreTemplate } from "./contracts/templates"
import { TemplateStoreManager } from "./contracts/managers"
2018-12-17 17:37:36 +01:00
2018-10-16 14:56:18 +02:00
import Web3Provider from "./Web3Provider"
/**
* 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 default class Keeper {
/**
* Returns Keeper instance.
* @return {Promise<Keeper>}
*/
public static async getInstance(): Promise<Keeper> {
2018-10-16 14:56:18 +02:00
if (Keeper.instance === null) {
Keeper.instance = new Keeper()
// Main contracts
2019-01-28 15:41:19 +01:00
Keeper.instance.dispenser = await Dispenser.getInstance()
2018-10-16 14:56:18 +02:00
Keeper.instance.token = await OceanToken.getInstance()
Keeper.instance.didRegistry = await DIDRegistry.getInstance()
// Managers
Keeper.instance.templateStoreManager = await TemplateStoreManager.getInstance()
// Conditions
Keeper.instance.conditions = {
lockRewardCondition: await LockRewardCondition.getInstance(),
escrowReward: await EscrowReward.getInstance(),
accessSecretStoreCondition: await AccessSecretStoreCondition.getInstance(),
}
// Conditions
Keeper.instance.templates = {
escrowAccessSecretStoreTemplate: await EscrowAccessSecretStoreTemplate.getInstance(),
}
2018-10-16 14:56:18 +02:00
}
2018-10-16 14:56:18 +02:00
return Keeper.instance
}
/**
* Keeper instance.
* @type {Keeper}
*/
2018-10-16 14:56:18 +02:00
private static instance: Keeper = null
/**
* 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
/**
* Conditions instances.
*/
public conditions: {
lockRewardCondition: LockRewardCondition,
escrowReward: EscrowReward,
accessSecretStoreCondition: AccessSecretStoreCondition,
}
/**
* Templates instances.
*/
public templates: {
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate,
}
/**
* Returns a condition by address.
* @param {string} address Address of deployed condition.
* @return {Condition} Condition instance.
*/
public getConditionByAddress(address: string): Condition {
return Object.values(this.conditions)
.find(condition => condition.getAddress() === address)
}
/**
* Returns the network by name.
* @return {Promise<string>} Network name.
*/
2018-10-16 14:56:18 +02:00
public async getNetworkName(): Promise<string> {
return Web3Provider.getWeb3().eth.net.getId()
.then((networkId) => {
2018-11-14 09:36:37 +01:00
let network: string = "Unknown"
2018-10-16 14:56:18 +02:00
switch (networkId) {
case 1:
network = "Main"
break
case 2:
network = "Morden"
break
case 3:
network = "Ropsten"
break
case 4:
network = "Rinkeby"
break
case 77:
network = "POA_Sokol"
break
case 99:
network = "POA_Core"
break
2018-10-16 14:56:18 +02:00
case 42:
network = "Kovan"
break
2018-11-23 09:57:37 +01:00
case 8996:
2018-12-07 15:19:16 +01:00
network = "Spree"
// network = "ocean_poa_net_local"
2018-11-23 09:57:37 +01:00
break
2018-11-14 09:36:37 +01:00
case 8995:
2018-12-07 15:19:16 +01:00
network = "Nile"
2018-11-14 09:36:37 +01:00
break
2018-10-16 14:56:18 +02:00
default:
// Logger.log(`NetworkId ${networkId} not found defaulting`)
2018-11-14 09:36:37 +01:00
network = "Development"
2018-10-16 14:56:18 +02:00
}
return network
})
}
}