112 lines
3.3 KiB
TypeScript
112 lines
3.3 KiB
TypeScript
import ContractBase from '../ContractBase'
|
|
import { zeroX } from '../../../utils'
|
|
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
|
|
|
export enum TemplateState {
|
|
Uninitialized = 0,
|
|
Proposed = 1,
|
|
Approved = 2,
|
|
Revoked = 3
|
|
}
|
|
|
|
export interface TemplateMetadata {
|
|
state: TemplateState
|
|
owner: string
|
|
lastUpdatedBy: string
|
|
blockNumberUpdated: number
|
|
conditionTypes: string[]
|
|
actorTypeIds: string[]
|
|
}
|
|
|
|
export class TemplateStoreManager extends ContractBase {
|
|
public static async getInstance(
|
|
config: InstantiableConfig
|
|
): Promise<TemplateStoreManager> {
|
|
const templateStoreManeger: TemplateStoreManager = new TemplateStoreManager(
|
|
'TemplateStoreManager'
|
|
)
|
|
await templateStoreManeger.init(config)
|
|
return templateStoreManeger
|
|
}
|
|
|
|
public generateId(templateName: string) {
|
|
const args: any = [{ type: 'string', value: templateName }]
|
|
return this.web3.utils.soliditySha3(...args)
|
|
}
|
|
|
|
public getOwner(): Promise<string> {
|
|
return this.call('owner', [])
|
|
}
|
|
|
|
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) {
|
|
throw new Error('Template already exist.')
|
|
}
|
|
} else {
|
|
return this.sendFrom('proposeTemplate', [zeroX(templateId)], from)
|
|
}
|
|
}
|
|
|
|
public async approveTemplate(
|
|
templateId: string,
|
|
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)
|
|
}
|
|
|
|
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)
|
|
)
|
|
}
|
|
|
|
public async getActorTypeValue(actorTypeId: string) {
|
|
const typeValue = await this.call('getTemplateActorTypeValue', [actorTypeId])
|
|
return typeValue
|
|
}
|
|
|
|
public async getTemplate(templateId: string) {
|
|
const {
|
|
state,
|
|
owner,
|
|
lastUpdatedBy,
|
|
blockNumberUpdated,
|
|
conditionTypes,
|
|
actorTypeIds
|
|
} = await this.call('getTemplate', [zeroX(templateId)])
|
|
return {
|
|
state: +state,
|
|
owner,
|
|
lastUpdatedBy,
|
|
blockNumberUpdated: +blockNumberUpdated,
|
|
conditionTypes,
|
|
actorTypeIds
|
|
} as TemplateMetadata
|
|
}
|
|
}
|