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

Create helper to fill some common attributes on service agreement template on DDO.

This commit is contained in:
Pedro Gutiérrez 2019-03-11 22:48:17 +01:00 committed by Pedro Gutiérrez
parent e7b9f990b6
commit 44a611248e
4 changed files with 45 additions and 5 deletions

View File

@ -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

View File

@ -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,
}

38
src/utils/DDOHelpers.ts Normal file
View File

@ -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)
}))
}))
}

View File

@ -2,3 +2,4 @@ export { Logger, LogLevel } from './Logger'
export * from './SignatureHelpers'
export * from './ConversionTypeHelpers'
export * from './GeneratorHelpers'
export * from './DDOHelpers'