diff --git a/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts b/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts index fb11115..08c6b83 100644 --- a/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts +++ b/integration/ocean/RegisterEscrowAccessSecretStoreTemplate.test.ts @@ -2,7 +2,7 @@ import { assert } from 'chai' import { config } from "../config" -import { Ocean, templates, conditions, generateId, Keeper, Account } from '../../src' // @oceanprotocol/squid +import { Ocean, templates, conditions, utils, Keeper, Account } from '../../src' // @oceanprotocol/squid const { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } = conditions const { EscrowAccessSecretStoreTemplate } = templates @@ -57,8 +57,8 @@ describe("Register Escrow Access Secret Store Template", () => { }) describe("Full flow", () => { - const agreementId = `0x${generateId()}` - const did = `0x${generateId()}` + const agreementId = `0x${utils.generateId()}` + const did = `0x${utils.generateId()}` let conditionIdAccess: string let conditionIdLock: string @@ -164,7 +164,7 @@ describe("Register Escrow Access Secret Store Template", () => { }) describe("Short flow", () => { - const did = `0x${generateId()}` + const did = `0x${utils.generateId()}` let agreementId diff --git a/src/squid.ts b/src/squid.ts index ca791d2..1de9300 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -9,11 +9,11 @@ import EventListener from "./keeper/EventListener" import * as templates from "./keeper/contracts/templates" import * as conditions from "./keeper/contracts/conditions" +import * as utils from "./utils" // Exports export * from "./ddo/DDO" export * from "./ddo/MetaData" -export { generateId } from "./utils/GeneratorHelpers" export { AgreementTemplate } from "./keeper/contracts/templates" export { Condition } from "./keeper/contracts/conditions" @@ -31,4 +31,5 @@ export { conditions, templates, + utils, } diff --git a/src/utils/DDOHelpers.ts b/src/utils/DDOHelpers.ts new file mode 100644 index 0000000..8410860 --- /dev/null +++ b/src/utils/DDOHelpers.ts @@ -0,0 +1,38 @@ +import { DDO } from "../ddo/DDO" +import { ServiceAgreementTemplateCondition, ServiceAgreementTemplateParameter } from "../ddo/ServiceAgreementTemplate" + +function fillParameterWithDDO(parameter: ServiceAgreementTemplateParameter, ddo: DDO): ServiceAgreementTemplateParameter { + const getValue = name => { + switch (name) { + case "amount": + case "price": + return ddo.findServiceByType("Metadata").metadata.base.price.toString() + case "assetId": + case "documentId": + case "documentKeyId": + return ddo.shortId() + } + + return null + } + const value = getValue(parameter.name.replace(/^_/, "")) + + return {...parameter, value} +} + +/** + * Fill some static parameters that depends on the metadata. + * @param {ServiceAgreementTemplateCondition[]} conditions Conditions to fill. + * @param {DDO} ddo DDO related to this conditions. + * @return {ServiceAgreementTemplateCondition[]} Filled conditions. + */ +export function fillConditionsWithDDO(conditions: ServiceAgreementTemplateCondition[], ddo: DDO): ServiceAgreementTemplateCondition[] { + return conditions + .map(condition => ({ + ...condition, + parameters: condition.parameters + .map(parameter => ({ + ...fillParameterWithDDO(parameter, ddo) + })) + })) +} diff --git a/src/utils/index.ts b/src/utils/index.ts index b00f783..6328e8e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,3 +2,4 @@ export { Logger, LogLevel } from './Logger' export * from './SignatureHelpers' export * from './ConversionTypeHelpers' export * from './GeneratorHelpers' +export * from './DDOHelpers'