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:
parent
e7b9f990b6
commit
44a611248e
@ -2,7 +2,7 @@ import { assert } from 'chai'
|
|||||||
|
|
||||||
import { config } from "../config"
|
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 { LockRewardCondition, EscrowReward, AccessSecretStoreCondition } = conditions
|
||||||
const { EscrowAccessSecretStoreTemplate } = templates
|
const { EscrowAccessSecretStoreTemplate } = templates
|
||||||
@ -57,8 +57,8 @@ describe("Register Escrow Access Secret Store Template", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("Full flow", () => {
|
describe("Full flow", () => {
|
||||||
const agreementId = `0x${generateId()}`
|
const agreementId = `0x${utils.generateId()}`
|
||||||
const did = `0x${generateId()}`
|
const did = `0x${utils.generateId()}`
|
||||||
|
|
||||||
let conditionIdAccess: string
|
let conditionIdAccess: string
|
||||||
let conditionIdLock: string
|
let conditionIdLock: string
|
||||||
@ -164,7 +164,7 @@ describe("Register Escrow Access Secret Store Template", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("Short flow", () => {
|
describe("Short flow", () => {
|
||||||
const did = `0x${generateId()}`
|
const did = `0x${utils.generateId()}`
|
||||||
|
|
||||||
let agreementId
|
let agreementId
|
||||||
|
|
||||||
|
@ -9,11 +9,11 @@ import EventListener from "./keeper/EventListener"
|
|||||||
|
|
||||||
import * as templates from "./keeper/contracts/templates"
|
import * as templates from "./keeper/contracts/templates"
|
||||||
import * as conditions from "./keeper/contracts/conditions"
|
import * as conditions from "./keeper/contracts/conditions"
|
||||||
|
import * as utils from "./utils"
|
||||||
|
|
||||||
// Exports
|
// Exports
|
||||||
export * from "./ddo/DDO"
|
export * from "./ddo/DDO"
|
||||||
export * from "./ddo/MetaData"
|
export * from "./ddo/MetaData"
|
||||||
export { generateId } from "./utils/GeneratorHelpers"
|
|
||||||
|
|
||||||
export { AgreementTemplate } from "./keeper/contracts/templates"
|
export { AgreementTemplate } from "./keeper/contracts/templates"
|
||||||
export { Condition } from "./keeper/contracts/conditions"
|
export { Condition } from "./keeper/contracts/conditions"
|
||||||
@ -31,4 +31,5 @@ export {
|
|||||||
|
|
||||||
conditions,
|
conditions,
|
||||||
templates,
|
templates,
|
||||||
|
utils,
|
||||||
}
|
}
|
||||||
|
38
src/utils/DDOHelpers.ts
Normal file
38
src/utils/DDOHelpers.ts
Normal 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)
|
||||||
|
}))
|
||||||
|
}))
|
||||||
|
}
|
@ -2,3 +2,4 @@ export { Logger, LogLevel } from './Logger'
|
|||||||
export * from './SignatureHelpers'
|
export * from './SignatureHelpers'
|
||||||
export * from './ConversionTypeHelpers'
|
export * from './ConversionTypeHelpers'
|
||||||
export * from './GeneratorHelpers'
|
export * from './GeneratorHelpers'
|
||||||
|
export * from './DDOHelpers'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user