mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Add EscrowComputeExecutionTemplate.
This commit is contained in:
parent
1f6e57fb60
commit
8761d2e087
@ -12,7 +12,8 @@ import {
|
|||||||
} from './contracts/conditions'
|
} from './contracts/conditions'
|
||||||
import {
|
import {
|
||||||
AgreementTemplate,
|
AgreementTemplate,
|
||||||
EscrowAccessSecretStoreTemplate
|
EscrowAccessSecretStoreTemplate,
|
||||||
|
EscrowComputeExecutionTemplate
|
||||||
} from './contracts/templates'
|
} from './contracts/templates'
|
||||||
import {
|
import {
|
||||||
TemplateStoreManager,
|
TemplateStoreManager,
|
||||||
@ -73,6 +74,9 @@ export class Keeper extends Instantiable {
|
|||||||
// Templates
|
// Templates
|
||||||
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance(
|
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate.getInstance(
|
||||||
config
|
config
|
||||||
|
),
|
||||||
|
escrowComputeExecutionTemplate: EscrowComputeExecutionTemplate.getInstance(
|
||||||
|
config
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -109,9 +113,10 @@ export class Keeper extends Instantiable {
|
|||||||
// Conditions
|
// Conditions
|
||||||
keeper.templates = {
|
keeper.templates = {
|
||||||
escrowAccessSecretStoreTemplate:
|
escrowAccessSecretStoreTemplate:
|
||||||
keeper.instances.escrowAccessSecretStoreTemplate
|
keeper.instances.escrowAccessSecretStoreTemplate,
|
||||||
|
escrowComputeExecutionTemplate:
|
||||||
|
keeper.instances.escrowComputeExecutionTemplate
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
keeper.utils = {
|
keeper.utils = {
|
||||||
eventHandler: new EventHandler(config)
|
eventHandler: new EventHandler(config)
|
||||||
@ -177,6 +182,7 @@ export class Keeper extends Instantiable {
|
|||||||
*/
|
*/
|
||||||
public templates: {
|
public templates: {
|
||||||
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate
|
escrowAccessSecretStoreTemplate: EscrowAccessSecretStoreTemplate
|
||||||
|
escrowComputeExecutionTemplate: EscrowComputeExecutionTemplate
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
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,11 +1,12 @@
|
|||||||
import { AgreementTemplate } from './AgreementTemplate.abstract'
|
import { AgreementTemplate } from './AgreementTemplate.abstract'
|
||||||
|
import { BaseEscrowTemplate } from './BaseEscrowTemplate.abstract'
|
||||||
import { DDO } from '../../../ddo/DDO'
|
import { DDO } from '../../../ddo/DDO'
|
||||||
import { generateId, zeroX } from '../../../utils'
|
import { generateId, zeroX } from '../../../utils'
|
||||||
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
import { InstantiableConfig } from '../../../Instantiable.abstract'
|
||||||
|
|
||||||
import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from './EscrowAccessSecretStoreTemplate.serviceAgreementTemplate'
|
import { escrowAccessSecretStoreTemplateServiceAgreementTemplate } from './EscrowAccessSecretStoreTemplate.serviceAgreementTemplate'
|
||||||
|
|
||||||
export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
|
export class EscrowAccessSecretStoreTemplate extends BaseEscrowTemplate {
|
||||||
public static async getInstance(
|
public static async getInstance(
|
||||||
config: InstantiableConfig
|
config: InstantiableConfig
|
||||||
): Promise<EscrowAccessSecretStoreTemplate> {
|
): Promise<EscrowAccessSecretStoreTemplate> {
|
||||||
@ -20,37 +21,6 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
|
|||||||
return escrowAccessSecretStoreTemplateServiceAgreementTemplate
|
return escrowAccessSecretStoreTemplateServiceAgreementTemplate
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a agreement using EscrowAccessSecretStoreTemplate.
|
|
||||||
* @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 createAgreementFromDDO(
|
public async createAgreementFromDDO(
|
||||||
agreementId: string,
|
agreementId: string,
|
||||||
ddo: DDO,
|
ddo: DDO,
|
||||||
@ -172,8 +142,4 @@ export class EscrowAccessSecretStoreTemplate extends AgreementTemplate {
|
|||||||
escrowRewardId
|
escrowRewardId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAgreementData(agreementId: string) {
|
|
||||||
return this.call<any>('getAgreementData', [zeroX(agreementId)])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,142 @@
|
|||||||
|
import { ServiceAgreementTemplate } from '../../../ddo/ServiceAgreementTemplate'
|
||||||
|
|
||||||
|
export const escrowComputeExecutionTemplateServiceAgreementTemplate: ServiceAgreementTemplate = {
|
||||||
|
contractName: 'EscrowComputeExecutionTemplate',
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
name: 'AgreementCreated',
|
||||||
|
actorType: 'consumer',
|
||||||
|
handler: {
|
||||||
|
moduleName: 'serviceExecutionTemplate',
|
||||||
|
functionName: 'fulfillLockRewardCondition',
|
||||||
|
version: '0.1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
fulfillmentOrder: [
|
||||||
|
'lockReward.fulfill',
|
||||||
|
'serviceExecution.fulfill',
|
||||||
|
'escrowReward.fulfill'
|
||||||
|
],
|
||||||
|
conditionDependency: {
|
||||||
|
lockReward: [],
|
||||||
|
serviceExecution: [],
|
||||||
|
escrowReward: ['lockReward', 'serviceExecution']
|
||||||
|
},
|
||||||
|
conditions: [
|
||||||
|
{
|
||||||
|
name: 'lockReward',
|
||||||
|
timelock: 0,
|
||||||
|
timeout: 0,
|
||||||
|
contractName: 'LockRewardCondition',
|
||||||
|
functionName: 'fulfill',
|
||||||
|
parameters: [
|
||||||
|
{
|
||||||
|
name: '_rewardAddress',
|
||||||
|
type: 'address',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '_amount',
|
||||||
|
type: 'uint256',
|
||||||
|
value: ''
|
||||||
|
}
|
||||||
|
],
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
name: 'Fulfilled',
|
||||||
|
actorType: 'publisher',
|
||||||
|
handler: {
|
||||||
|
moduleName: 'lockRewardCondition',
|
||||||
|
functionName: 'fulfillServiceExecutionCondition',
|
||||||
|
version: '0.1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'serviceExecution',
|
||||||
|
timelock: 0,
|
||||||
|
timeout: 0,
|
||||||
|
contractName: 'ComputeExecutionCondition',
|
||||||
|
functionName: 'fulfill',
|
||||||
|
parameters: [
|
||||||
|
{
|
||||||
|
name: '_documentId',
|
||||||
|
type: 'bytes32',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '_grantee',
|
||||||
|
type: 'address',
|
||||||
|
value: ''
|
||||||
|
}
|
||||||
|
],
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
name: 'Fulfilled',
|
||||||
|
actorType: 'publisher',
|
||||||
|
handler: {
|
||||||
|
moduleName: 'serviceExecution',
|
||||||
|
functionName: 'fulfillServiceExecutionCondition',
|
||||||
|
version: '0.1'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'TimedOut',
|
||||||
|
actorType: 'consumer',
|
||||||
|
handler: {
|
||||||
|
moduleName: 'serviceExec',
|
||||||
|
functionName: 'fulfillServiceExecutionCondition',
|
||||||
|
version: '0.1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'escrowReward',
|
||||||
|
timelock: 0,
|
||||||
|
timeout: 0,
|
||||||
|
contractName: 'EscrowReward',
|
||||||
|
functionName: 'fulfill',
|
||||||
|
parameters: [
|
||||||
|
{
|
||||||
|
name: '_amount',
|
||||||
|
type: 'uint256',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '_receiver',
|
||||||
|
type: 'address',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '_sender',
|
||||||
|
type: 'address',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '_lockCondition',
|
||||||
|
type: 'bytes32',
|
||||||
|
value: ''
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: '_releaseCondition',
|
||||||
|
type: 'bytes32',
|
||||||
|
value: ''
|
||||||
|
}
|
||||||
|
],
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
name: 'Fulfilled',
|
||||||
|
actorType: 'publisher',
|
||||||
|
handler: {
|
||||||
|
moduleName: 'escrowRewardCondition',
|
||||||
|
functionName: 'verifyRewardTokens',
|
||||||
|
version: '0.1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
145
src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts
Normal file
145
src/keeper/contracts/templates/EscrowComputeExecutionTemplate.ts
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
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 { escrowComputeExecutionTemplateServiceAgreementTemplate } from './EscrowComputeExecutionTemplate.serviceAgreementTemplate'
|
||||||
|
|
||||||
|
export class EscrowComputeExecutionTemplate extends BaseEscrowTemplate {
|
||||||
|
public static async getInstance(
|
||||||
|
config: InstantiableConfig
|
||||||
|
): Promise<EscrowComputeExecutionTemplate> {
|
||||||
|
return AgreementTemplate.getInstance(
|
||||||
|
config,
|
||||||
|
'EscrowComputeExecutionTemplate',
|
||||||
|
EscrowComputeExecutionTemplate
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getServiceAgreementTemplate() {
|
||||||
|
return escrowComputeExecutionTemplateServiceAgreementTemplate
|
||||||
|
}
|
||||||
|
|
||||||
|
public async createAgreementFromDDO(
|
||||||
|
agreementId: string,
|
||||||
|
ddo: DDO,
|
||||||
|
consumer: string,
|
||||||
|
from?: string
|
||||||
|
) {
|
||||||
|
return !!(await this.createFullAgreement(
|
||||||
|
ddo.shortId(),
|
||||||
|
ddo.findServiceByType('Metadata').metadata.base.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').metadata.base.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(
|
||||||
|
agreementId: string,
|
||||||
|
did: string,
|
||||||
|
amount: number | string,
|
||||||
|
consumer: string
|
||||||
|
) {
|
||||||
|
const { didRegistry, conditions } = this.ocean.keeper
|
||||||
|
|
||||||
|
const {
|
||||||
|
computeExecutionCondition,
|
||||||
|
lockRewardCondition,
|
||||||
|
escrowReward
|
||||||
|
} = conditions
|
||||||
|
|
||||||
|
const publisher = await didRegistry.getDIDOwner(did)
|
||||||
|
|
||||||
|
const lockRewardConditionId = await lockRewardCondition.generateIdHash(
|
||||||
|
agreementId,
|
||||||
|
await escrowReward.getAddress(),
|
||||||
|
amount
|
||||||
|
)
|
||||||
|
const computeExecutionConditionId = await computeExecutionCondition.generateIdHash(
|
||||||
|
agreementId,
|
||||||
|
did,
|
||||||
|
consumer
|
||||||
|
)
|
||||||
|
const escrowRewardId = await escrowReward.generateIdHash(
|
||||||
|
agreementId,
|
||||||
|
String(amount),
|
||||||
|
publisher,
|
||||||
|
consumer,
|
||||||
|
lockRewardConditionId,
|
||||||
|
computeExecutionConditionId
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
lockRewardConditionId,
|
||||||
|
computeExecutionConditionId,
|
||||||
|
escrowRewardId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,8 @@
|
|||||||
export * from './AgreementTemplate.abstract'
|
export * from './AgreementTemplate.abstract'
|
||||||
|
export { BaseEscrowTemplate } from './BaseEscrowTemplate.abstract'
|
||||||
export {
|
export {
|
||||||
EscrowAccessSecretStoreTemplate
|
EscrowAccessSecretStoreTemplate
|
||||||
} from './EscrowAccessSecretStoreTemplate'
|
} from './EscrowAccessSecretStoreTemplate'
|
||||||
|
export {
|
||||||
|
EscrowComputeExecutionTemplate
|
||||||
|
} from './EscrowComputeExecutionTemplate'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user