1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/keeper/contracts/templates/AgreementTemplateBase.ts

126 lines
3.6 KiB
TypeScript

import { escrowAccessServiceAgreementTemplate } from './EscrowAccess.serviceAgreementTemplate'
import { TemplateStoreManager, AgreementStoreManager } from '../managers'
import DIDRegistry from '../DIDRegistry'
import { LockRewardCondition } from '../conditions/LockRewardCondition'
import { AccessSecretStoreCondition } from '../conditions/AccessSecretStoreCondition'
import { EscrowReward } from '../conditions/EscrowReward'
import { DDO } from '../../../ddo/DDO'
import { generateId, zeroX } from '../../../utils'
import { ComputeExecutionCondition } from '../conditions'
export interface Conditions {
lockRewardCondition: LockRewardCondition
accessSecretStoreCondition?: AccessSecretStoreCondition
computeExecutionCondition?: ComputeExecutionCondition
escrowReward: EscrowReward
}
export class AgreementTemplateBase {
public static templateName: string
public templateManager: TemplateStoreManager
public agreementStoreManager: AgreementStoreManager
public didRegistry: DIDRegistry
public conditions: Conditions
public constructor(
templateManager: TemplateStoreManager,
agreementStoreManager: AgreementStoreManager,
didRegistry: DIDRegistry,
conditions: Conditions
) {
this.templateManager = templateManager
this.agreementStoreManager = agreementStoreManager
this.didRegistry = didRegistry
this.conditions = conditions
}
public async getServiceAgreementTemplate() {
return escrowAccessServiceAgreementTemplate
}
public async createAgreementFromDDO(
agreementId: string,
ddo: DDO,
consumer: string,
from?: string
) {
return !!(await this.createFullAgreement(
ddo.shortId(),
ddo.findServiceByType('metadata').attributes.main.price,
consumer,
from,
agreementId
))
}
public async getConditionIdsFromDDO(
agreementId: string,
ddo: DDO,
consumer: string,
from?: string
) {
const conditionIds = await this.createFullAgreementData(
agreementId,
ddo.shortId(),
ddo.findServiceByType('metadata').attributes.main.price,
consumer
)
return conditionIds
}
public getId() {
return ''
}
/**
* Create a agreement using EscrowAccessSecretStoreTemplate using only the most important information.
* @param {string} did Asset DID.
* @param {number} amount Asset price.
* @param {string} from Consumer address.
*
* @return {Promise<string>} Agreement ID.
*/
public async createFullAgreement(
did: string,
amount: number | string,
consumer: string,
from?: string,
agreementId: string = generateId()
): Promise<string> {
const conditionIds = await this.createFullAgreementData(
agreementId,
did,
amount,
consumer
)
const publisher = await this.didRegistry.getDIDOwner(did)
await this.agreementStoreManager.createAgreement(
agreementId,
did,
this.getId(),
conditionIds,
[0, 0, 0],
[0, 0, 0],
[consumer, publisher],
from
)
return zeroX(agreementId)
}
protected async createFullAgreementData(
agreementId: string,
did: string,
amount: number | string,
consumer: string
): Promise<any> {
return null
}
}