mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
template refactoring
This commit is contained in:
parent
d242fe3e33
commit
3d978f792d
@ -62,14 +62,7 @@ export class Keeper extends Instantiable {
|
||||
accessSecretStoreCondition: AccessSecretStoreCondition.getInstance(
|
||||
config
|
||||
),
|
||||
computeExecutionCondition: ComputeExecutionCondition.getInstance(config),
|
||||
// Templates
|
||||
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance(
|
||||
config
|
||||
),
|
||||
escrowComputeExecutionTemplate: EscrowComputeExecutionTemplate.getInstance(
|
||||
config
|
||||
)
|
||||
computeExecutionCondition: ComputeExecutionCondition.getInstance(config)
|
||||
})
|
||||
|
||||
keeper.connected = true
|
||||
@ -107,6 +100,19 @@ export class Keeper extends Instantiable {
|
||||
escrowComputeExecutionTemplate:
|
||||
keeper.instances.escrowComputeExecutionTemplate
|
||||
}
|
||||
// Templates
|
||||
keeper.instances.escrowAccessSecretStoreTemplate = new EscrowAccessSecretStoreTemplate(
|
||||
keeper.instances.templateStoreManager,
|
||||
keeper.instances.agreementStoreManager,
|
||||
keeper.instances.didRegistry,
|
||||
keeper.instances.conditions
|
||||
)
|
||||
keeper.instances.escrowComputeExecutionTemplate = new EscrowComputeExecutionTemplate(
|
||||
keeper.instances.templateStoreManager,
|
||||
keeper.instances.agreementStoreManager,
|
||||
keeper.instances.didRegistry,
|
||||
keeper.instances.conditions
|
||||
)
|
||||
// Utils
|
||||
keeper.utils = {
|
||||
eventHandler: new EventHandler(config)
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Condition } from './Condition.abstract'
|
||||
import { zeroX, didZeroX, didPrefixed } from '../../../utils'
|
||||
import { zeroX, didZeroX } from '../../../utils'
|
||||
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
||||
|
||||
export class ComputeExecutionCondition extends Condition {
|
||||
|
@ -15,11 +15,11 @@ export class AgreementStoreManager extends ContractBase {
|
||||
public static async getInstance(
|
||||
config: InstantiableConfig
|
||||
): Promise<AgreementStoreManager> {
|
||||
const templateStoreManeger: AgreementStoreManager = new AgreementStoreManager(
|
||||
const templateStoreManager: AgreementStoreManager = new AgreementStoreManager(
|
||||
'AgreementStoreManager'
|
||||
)
|
||||
await templateStoreManeger.init(config)
|
||||
return templateStoreManeger
|
||||
await templateStoreManager.init(config)
|
||||
return templateStoreManager
|
||||
}
|
||||
|
||||
public getOwner(): Promise<string> {
|
||||
@ -44,4 +44,42 @@ export class AgreementStoreManager extends ContractBase {
|
||||
blockNumberUpdated: +blockNumberUpdated
|
||||
} as AgreementData
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a agreement using EscrowComputeExecutionTemplate.
|
||||
* @param {string} agreementId Generated agreement ID.
|
||||
* @param {string} did Asset DID.
|
||||
* @param {string} templateId Template ID.
|
||||
* @param {string[]} conditionIds List of conditions IDs.
|
||||
* @param {number[]} timeLocks Timelocks.
|
||||
* @param {number[]} timeOuts Timeouts.
|
||||
* @param {string[]} actors ETH account addresses of provider, consumer, etc.
|
||||
* @param {string} from Action sender.
|
||||
*
|
||||
* @return {any} Transaction receipt.
|
||||
*/
|
||||
public async createAgreement(
|
||||
agreementId: string,
|
||||
did: string,
|
||||
templateId: string,
|
||||
conditionIds: string[],
|
||||
timeLocks: number[],
|
||||
timeOuts: number[],
|
||||
actors: string[],
|
||||
from?: string
|
||||
): Promise<any> {
|
||||
this.sendFrom(
|
||||
'createAgreement',
|
||||
[
|
||||
zeroX(agreementId),
|
||||
zeroX(did),
|
||||
zeroX(templateId),
|
||||
conditionIds.map(zeroX),
|
||||
timeLocks,
|
||||
timeOuts,
|
||||
actors
|
||||
],
|
||||
from
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -34,38 +34,6 @@ export abstract class AgreementTemplate extends ContractBase {
|
||||
super(contractName)
|
||||
}
|
||||
|
||||
public createAgreement(
|
||||
agreementId: string,
|
||||
did: string,
|
||||
conditionIds: string[],
|
||||
timeLocks: number[],
|
||||
timeOuts: number[],
|
||||
...args: any[]
|
||||
)
|
||||
|
||||
public createAgreement(
|
||||
agreementId: string,
|
||||
did: string,
|
||||
conditionIds: string[],
|
||||
timeLocks: number[],
|
||||
timeOuts: number[],
|
||||
extraArgs: any[],
|
||||
from?: string
|
||||
) {
|
||||
return this.sendFrom(
|
||||
'createAgreement',
|
||||
[
|
||||
zeroX(agreementId),
|
||||
zeroX(did),
|
||||
conditionIds.map(zeroX),
|
||||
timeLocks,
|
||||
timeOuts,
|
||||
...extraArgs
|
||||
],
|
||||
from
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditions address list.
|
||||
* @return {Promise<string[]>} Conditions address.
|
||||
@ -91,7 +59,7 @@ export abstract class AgreementTemplate extends ContractBase {
|
||||
* @param {string} from Consumer address.
|
||||
* @return {Promise<string[]>} Condition IDs.
|
||||
*/
|
||||
public abstract getAgreementIdsFromDDO(
|
||||
public abstract getConditionIdsFromDDO(
|
||||
agreementId: string,
|
||||
ddo: DDO,
|
||||
consumer: string,
|
||||
|
125
src/keeper/contracts/templates/AgreementTemplateBase.ts
Normal file
125
src/keeper/contracts/templates/AgreementTemplateBase.ts
Normal file
@ -0,0 +1,125 @@
|
||||
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
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
import { AgreementTemplate } from './AgreementTemplate.abstract'
|
||||
import { DDO } from '../../../ddo/DDO'
|
||||
import { generateId, zeroX } from '../../../utils'
|
||||
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
||||
|
||||
// import { EscrowComputeExecutionTemplateServiceAgreementTemplate } from './EscrowComputeExecutionTemplate.serviceAgreementTemplate'
|
||||
|
||||
export abstract class BaseEscrowTemplate extends AgreementTemplate {
|
||||
/**
|
||||
* Create a agreement using EscrowComputeExecutionTemplate.
|
||||
* @param {string} agreementId Generated agreement ID.
|
||||
* @param {string} did Asset DID.
|
||||
* @param {string[]} conditionIds List of conditions IDs.
|
||||
* @param {number[]} timeLocks Timelocks.
|
||||
* @param {number[]} timeOuts Timeouts.
|
||||
* @param {string} accessConsumer Consumer address.
|
||||
* @param {string} from Action sender.
|
||||
* @param {any} Transaction receipt.
|
||||
*/
|
||||
public createAgreement(
|
||||
agreementId: string,
|
||||
did: string,
|
||||
conditionIds: string[],
|
||||
timeLocks: number[],
|
||||
timeOuts: number[],
|
||||
accessConsumer: string,
|
||||
from?: string
|
||||
) {
|
||||
return super.createAgreement(
|
||||
agreementId,
|
||||
did,
|
||||
conditionIds,
|
||||
timeLocks,
|
||||
timeOuts,
|
||||
[accessConsumer],
|
||||
from
|
||||
)
|
||||
}
|
||||
|
||||
public async getAgreementData(agreementId: string) {
|
||||
return this.call<any>('getAgreementData', [zeroX(agreementId)])
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import { ServiceAgreementTemplate } from '../../../ddo/ServiceAgreementTemplate'
|
||||
|
||||
export const escrowAccessSecretStoreTemplateServiceAgreementTemplate: ServiceAgreementTemplate = {
|
||||
export const escrowAccessServiceAgreementTemplate: ServiceAgreementTemplate = {
|
||||
contractName: 'EscrowAccessSecretStoreTemplate',
|
||||
events: [
|
||||
{
|
@ -1,108 +1,26 @@
|
||||
import { AgreementTemplate } from './AgreementTemplate.abstract'
|
||||
import { BaseEscrowTemplate } from './BaseEscrowTemplate.abstract'
|
||||
import { DDO } from '../../../ddo/DDO'
|
||||
import { generateId, zeroX } from '../../../utils'
|
||||
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
||||
import { AgreementTemplateBase } from './AgreementTemplateBase'
|
||||
import { escrowAccessServiceAgreementTemplate } from './EscrowAccess.serviceAgreementTemplate'
|
||||
|
||||
import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from './EscrowAccessSecretStoreTemplate.serviceAgreementTemplate'
|
||||
|
||||
export class EscrowAccessSecretStoreTemplate extends BaseEscrowTemplate {
|
||||
public static async getInstance(
|
||||
config: InstantiableConfig
|
||||
): Promise<EscrowAccessSecretStoreTemplate> {
|
||||
return AgreementTemplate.getInstance(
|
||||
config,
|
||||
'EscrowAccessSecretStoreTemplate',
|
||||
EscrowAccessSecretStoreTemplate
|
||||
)
|
||||
}
|
||||
export class EscrowAccessSecretStoreTemplate extends AgreementTemplateBase {
|
||||
public static templateName: 'EscrowAccessSecretStoreTemplate'
|
||||
|
||||
public async getServiceAgreementTemplate() {
|
||||
return escrowAccessSecretStoreTemplateServiceAgreementTemplate
|
||||
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 getAgreementIdsFromDDO(
|
||||
agreementId: string,
|
||||
ddo: DDO,
|
||||
consumer: string,
|
||||
from?: string
|
||||
) {
|
||||
const {
|
||||
accessSecretStoreConditionId,
|
||||
lockRewardConditionId,
|
||||
escrowRewardId
|
||||
} = await this.createFullAgreementData(
|
||||
agreementId,
|
||||
ddo.shortId(),
|
||||
ddo.findServiceByType('metadata').attributes.main.price,
|
||||
consumer
|
||||
)
|
||||
return [accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId]
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
accessSecretStoreConditionId,
|
||||
lockRewardConditionId,
|
||||
escrowRewardId
|
||||
} = await this.createFullAgreementData(agreementId, did, amount, consumer)
|
||||
|
||||
await this.createAgreement(
|
||||
agreementId,
|
||||
did,
|
||||
[accessSecretStoreConditionId, lockRewardConditionId, escrowRewardId],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
consumer,
|
||||
from
|
||||
)
|
||||
|
||||
return zeroX(agreementId)
|
||||
}
|
||||
|
||||
private async createFullAgreementData(
|
||||
protected async createFullAgreementData(
|
||||
agreementId: string,
|
||||
did: string,
|
||||
amount: number | string,
|
||||
consumer: string
|
||||
) {
|
||||
const { didRegistry, conditions } = this.ocean.keeper
|
||||
|
||||
const {
|
||||
accessSecretStoreCondition,
|
||||
lockRewardCondition,
|
||||
accessSecretStoreCondition,
|
||||
escrowReward
|
||||
} = conditions
|
||||
} = this.conditions
|
||||
|
||||
const publisher = await didRegistry.getDIDOwner(did)
|
||||
const publisher = await this.didRegistry.getDIDOwner(did)
|
||||
|
||||
const lockRewardConditionId = await lockRewardCondition.generateIdHash(
|
||||
agreementId,
|
||||
@ -123,10 +41,6 @@ export class EscrowAccessSecretStoreTemplate extends BaseEscrowTemplate {
|
||||
accessSecretStoreConditionId
|
||||
)
|
||||
|
||||
return {
|
||||
lockRewardConditionId,
|
||||
accessSecretStoreConditionId,
|
||||
escrowRewardId
|
||||
}
|
||||
return [lockRewardConditionId, accessSecretStoreConditionId, escrowRewardId]
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { ServiceAgreementTemplate } from '../../../ddo/ServiceAgreementTemplate'
|
||||
|
||||
export const escrowComputeExecutionTemplateServiceAgreementTemplate: ServiceAgreementTemplate = {
|
||||
export const escrowComputeServiceAgreementTemplate: ServiceAgreementTemplate = {
|
||||
contractName: 'EscrowComputeExecutionTemplate',
|
||||
events: [
|
||||
{
|
@ -1,108 +1,26 @@
|
||||
import { AgreementTemplate } from './AgreementTemplate.abstract'
|
||||
import { BaseEscrowTemplate } from './BaseEscrowTemplate.abstract'
|
||||
import { DDO } from '../../../ddo/DDO'
|
||||
import { generateId, zeroX } from '../../../utils'
|
||||
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
||||
import { AgreementTemplateBase } from './AgreementTemplateBase'
|
||||
import { escrowComputeServiceAgreementTemplate } from './EscrowCompute.serviceAgreementTemplate'
|
||||
|
||||
import { escrowComputeExecutionTemplateServiceAgreementTemplate } from './EscrowComputeExecutionTemplate.serviceAgreementTemplate'
|
||||
|
||||
export class EscrowComputeExecutionTemplate extends BaseEscrowTemplate {
|
||||
public static async getInstance(
|
||||
config: InstantiableConfig
|
||||
): Promise<EscrowComputeExecutionTemplate> {
|
||||
return AgreementTemplate.getInstance(
|
||||
config,
|
||||
'EscrowComputeExecutionTemplate',
|
||||
EscrowComputeExecutionTemplate
|
||||
)
|
||||
}
|
||||
export class EscrowComputeExecutionTemplate extends AgreementTemplateBase {
|
||||
public static templateName: 'EscrowComputeExecutionTemplate'
|
||||
|
||||
public async getServiceAgreementTemplate() {
|
||||
return escrowComputeExecutionTemplateServiceAgreementTemplate
|
||||
return escrowComputeServiceAgreementTemplate
|
||||
}
|
||||
|
||||
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 getAgreementIdsFromDDO(
|
||||
agreementId: string,
|
||||
ddo: DDO,
|
||||
consumer: string,
|
||||
from?: string
|
||||
) {
|
||||
const {
|
||||
computeExecutionConditionId,
|
||||
lockRewardConditionId,
|
||||
escrowRewardId
|
||||
} = await this.createFullAgreementData(
|
||||
agreementId,
|
||||
ddo.shortId(),
|
||||
ddo.findServiceByType('metadata').attributes.main.price,
|
||||
consumer
|
||||
)
|
||||
return [computeExecutionConditionId, lockRewardConditionId, escrowRewardId]
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a agreement using EscrowAccess____SecretStoreTemplate 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 {
|
||||
computeExecutionConditionId,
|
||||
lockRewardConditionId,
|
||||
escrowRewardId
|
||||
} = await this.createFullAgreementData(agreementId, did, amount, consumer)
|
||||
|
||||
await this.createAgreement(
|
||||
agreementId,
|
||||
did,
|
||||
[computeExecutionConditionId, lockRewardConditionId, escrowRewardId],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
consumer,
|
||||
from
|
||||
)
|
||||
|
||||
return zeroX(agreementId)
|
||||
}
|
||||
|
||||
private async createFullAgreementData(
|
||||
protected async createFullAgreementData(
|
||||
agreementId: string,
|
||||
did: string,
|
||||
amount: number | string,
|
||||
consumer: string
|
||||
) {
|
||||
const { didRegistry, conditions } = this.ocean.keeper
|
||||
|
||||
const {
|
||||
computeExecutionCondition,
|
||||
lockRewardCondition,
|
||||
computeExecutionCondition,
|
||||
escrowReward
|
||||
} = conditions
|
||||
} = this.conditions
|
||||
|
||||
const publisher = await didRegistry.getDIDOwner(did)
|
||||
const publisher = await this.didRegistry.getDIDOwner(did)
|
||||
|
||||
const lockRewardConditionId = await lockRewardCondition.generateIdHash(
|
||||
agreementId,
|
||||
|
@ -57,7 +57,7 @@ export class OceanAgreements extends Instantiable {
|
||||
.serviceAgreementTemplate.contractName
|
||||
const agreementConditionsIds = await this.ocean.keeper
|
||||
.getTemplateByName(templateName)
|
||||
.getAgreementIdsFromDDO(agreementId, ddo, consumer.getId(), consumer.getId())
|
||||
.getConditionIdsFromDDO(agreementId, ddo, consumer.getId(), consumer.getId())
|
||||
|
||||
const signature = await this.ocean.utils.agreements.signServiceAgreement(
|
||||
ddo,
|
||||
|
Loading…
Reference in New Issue
Block a user