1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/utils/DDOHelpers.ts
2019-03-18 13:37:49 +01:00

39 lines
1.5 KiB
TypeScript

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 ""
}
const value = getValue(parameter.name.replace(/^_/, ""))
return {...parameter, value: parameter.type.includes("int") ? Number(value) : 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),
})),
}))
}