squid-js/src/keeper/contracts/managers/TemplateStoreManager.ts

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import ContractBase from '../ContractBase'
import { zeroX } from '../../../utils'
import { InstantiableConfig } from '../../../Instantiable.abstract'
export enum TemplateState {
Uninitialized = 0,
Proposed = 1,
Approved = 2,
2019-06-20 00:20:09 +02:00
Revoked = 3
}
export interface TemplateMetadata {
2019-06-20 00:20:09 +02:00
state: TemplateState
owner: string
lastUpdatedBy: string
blockNumberUpdated: number
conditionTypes: string[]
actorTypeIds: string[]
}
export class TemplateStoreManager extends ContractBase {
2019-11-15 00:00:10 +01:00
public static async getInstance(
config: InstantiableConfig
): Promise<TemplateStoreManager> {
const templateStoreManeger: TemplateStoreManager = new TemplateStoreManager(
'TemplateStoreManager'
)
await templateStoreManeger.init(config)
return templateStoreManeger
}
public generateId(templateName: string) {
2020-01-17 16:39:44 +01:00
const args: any = [{ type: 'string', value: templateName }]
2020-01-16 15:47:29 +01:00
return this.web3.utils.soliditySha3(...args)
}
2019-03-14 21:28:51 +01:00
public getOwner(): Promise<string> {
2019-06-20 00:20:09 +02:00
return this.call('owner', [])
}
2020-01-17 16:39:44 +01:00
public async proposeTemplate(
templateId: string,
from?: string,
ignoreExists?: boolean
) {
const template = await this.getTemplate(templateId)
if (template.blockNumberUpdated !== 0) {
this.logger.warn(`Template "${templateId}" already exist.`)
if (!ignoreExists) {
2019-06-20 00:20:09 +02:00
throw new Error('Template already exist.')
}
} else {
return this.sendFrom('proposeTemplate', [zeroX(templateId)], from)
}
}
2019-11-15 00:00:10 +01:00
public async approveTemplate(
templateId: string,
2019-11-15 00:00:10 +01:00
from?: string,
ignoreApproved?: boolean
) {
const template = await this.getTemplate(templateId)
if (template.state !== TemplateState.Proposed) {
this.logger.warn(`Template "${templateId}" is not in "proposed" state.`)
if (!ignoreApproved) {
throw new Error(`Template not in "proposed" state.`)
}
} else {
return this.sendFrom('approveTemplate', [zeroX(templateId)], from)
}
}
public revokeTemplate(templateId: string, from?: string) {
return this.sendFrom('revokeTemplate', [zeroX(templateId)], from)
}
2020-01-17 16:39:44 +01:00
public async getConditionTypes(templateId: string): Promise<string[]> {
const { conditionTypes } = await this.getTemplate(templateId)
return conditionTypes
}
public getConditions(conditionTypes: string[]) {
return conditionTypes.map((address) =>
this.ocean.keeper.getConditionByAddress(address)
)
}
2020-01-16 15:47:29 +01:00
public async getActorTypeValue(actorTypeId: string) {
2020-01-17 16:39:44 +01:00
const typeValue = await this.call('getTemplateActorTypeValue', [actorTypeId])
2020-01-16 15:47:29 +01:00
return typeValue
}
public async getTemplate(templateId: string) {
2019-11-15 00:00:10 +01:00
const {
state,
owner,
lastUpdatedBy,
blockNumberUpdated,
conditionTypes,
actorTypeIds
} = await this.call('getTemplate', [zeroX(templateId)])
2019-06-20 00:20:09 +02:00
return {
state: +state,
owner,
lastUpdatedBy,
blockNumberUpdated: +blockNumberUpdated,
conditionTypes,
actorTypeIds
2019-06-20 00:20:09 +02:00
} as TemplateMetadata
}
}