mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
fix listener, add eventing, moved generation of conditions to template structure
This commit is contained in:
parent
9503419b0e
commit
0c3c21b4c5
@ -1,14 +1,15 @@
|
|||||||
|
import Dependency from "./Dependency"
|
||||||
import Event from "./Event"
|
import Event from "./Event"
|
||||||
import Parameter from "./Parameter"
|
import Parameter from "./Parameter"
|
||||||
|
|
||||||
export default class Condition {
|
export default class Condition {
|
||||||
|
public name: string
|
||||||
public contractName: string = "AccessCondition"
|
public contractName: string = "AccessCondition"
|
||||||
public methodName: string = "lockPayment"
|
public functionName: string = "lockPayment"
|
||||||
public timeout: number = 0
|
public timeout: number = 0
|
||||||
public conditionKey: string = "0x12122434"
|
public conditionKey: string = "0x12122434"
|
||||||
public parameters: Parameter[]
|
public parameters: Parameter[]
|
||||||
public events: Event[]
|
public events: Event[]
|
||||||
public dependencies: string[] = []
|
public dependencies: Dependency[] = []
|
||||||
public dependencyTimeoutFlags: number[] = []
|
|
||||||
public isTerminalCondition: boolean = false
|
public isTerminalCondition: boolean = false
|
||||||
}
|
}
|
||||||
|
4
src/ddo/Dependency.ts
Normal file
4
src/ddo/Dependency.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export default class Dependency {
|
||||||
|
public name: string = "lockPayment"
|
||||||
|
public timeout: number = 0
|
||||||
|
}
|
28
src/examples/Listener.ts
Normal file
28
src/examples/Listener.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import {Logger, Ocean} from "../squid"
|
||||||
|
// tslint:disable-next-line
|
||||||
|
import EventListener from "../keeper/EventListener"
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
Ocean.getInstance({
|
||||||
|
nodeUri: "http://localhost:8545",
|
||||||
|
aquariusUri: "http://localhost:5000",
|
||||||
|
brizoUri: "http://localhost:8030",
|
||||||
|
parityUri: "http://localhost:9545",
|
||||||
|
secretStoreUri: "http://localhost:12001",
|
||||||
|
threshold: 0,
|
||||||
|
password: "unittest",
|
||||||
|
address: "0xed243adfb84a6626eba46178ccb567481c6e655d",
|
||||||
|
})
|
||||||
|
})()
|
||||||
|
|
||||||
|
const event = EventListener.subscribe("OceanToken", "Transfer", {})
|
||||||
|
|
||||||
|
Logger.log(`Listening to Transfer event of OceanToken`)
|
||||||
|
|
||||||
|
event.listen((data: any[]) => {
|
||||||
|
|
||||||
|
Logger.log(Date.now())
|
||||||
|
Logger.log("From", data[0].returnValues.from)
|
||||||
|
Logger.log("To", data[0].returnValues.to)
|
||||||
|
Logger.log("Value", data[0].returnValues.value)
|
||||||
|
})
|
@ -1,5 +1,6 @@
|
|||||||
import GenericContract from "./contracts/GenericContract"
|
import GenericContract from "./contracts/GenericContract"
|
||||||
import EventListener from "./EventListener"
|
import EventListener from "./EventListener"
|
||||||
|
import Web3Provider from "./Web3Provider"
|
||||||
|
|
||||||
export default class Event {
|
export default class Event {
|
||||||
|
|
||||||
@ -17,8 +18,10 @@ export default class Event {
|
|||||||
clearTimeout(this.poller)
|
clearTimeout(this.poller)
|
||||||
}
|
}
|
||||||
|
|
||||||
public listen(callback: any) {
|
public async listen(callback: any) {
|
||||||
this.poller = setTimeout(
|
this.lastBlock = await Web3Provider.getWeb3().eth.getBlockNumber() + 1
|
||||||
|
|
||||||
|
this.poller = setInterval(
|
||||||
() => this.handler(callback),
|
() => this.handler(callback),
|
||||||
this.interval)
|
this.interval)
|
||||||
}
|
}
|
||||||
@ -31,16 +34,17 @@ export default class Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async handler(callback: any) {
|
private async handler(callback: any) {
|
||||||
const contract = await
|
const contract = await GenericContract.getInstance(this.contractName)
|
||||||
GenericContract.getInstance(this.contractName)
|
|
||||||
const events = await
|
const events = await contract.getEventData(this.eventName, {
|
||||||
contract.getEventData(this.eventName, {
|
|
||||||
filter: this.filter,
|
filter: this.filter,
|
||||||
fromBlock: this.lastBlock,
|
fromBlock: this.lastBlock,
|
||||||
toBlock: "latest",
|
toBlock: "latest",
|
||||||
})
|
})
|
||||||
|
|
||||||
this.lastBlock = events[events.length - 1].blockNumber
|
if (events.length > 0) {
|
||||||
|
this.lastBlock = events[events.length - 1].blockNumber + 1
|
||||||
callback(events)
|
callback(events)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -3,12 +3,9 @@ import SearchQuery from "../aquarius/query/SearchQuery"
|
|||||||
import BrizoProvider from "../brizo/BrizoProvider"
|
import BrizoProvider from "../brizo/BrizoProvider"
|
||||||
import ConfigProvider from "../ConfigProvider"
|
import ConfigProvider from "../ConfigProvider"
|
||||||
import Authentication from "../ddo/Authentication"
|
import Authentication from "../ddo/Authentication"
|
||||||
import DDOCondition from "../ddo/Condition"
|
import Condition from "../ddo/Condition"
|
||||||
import DDO from "../ddo/DDO"
|
import DDO from "../ddo/DDO"
|
||||||
import Event from "../ddo/Event"
|
|
||||||
import EventHandlers from "../ddo/EventHandlers"
|
|
||||||
import MetaData from "../ddo/MetaData"
|
import MetaData from "../ddo/MetaData"
|
||||||
import Parameter from "../ddo/Parameter"
|
|
||||||
import Service from "../ddo/Service"
|
import Service from "../ddo/Service"
|
||||||
import Keeper from "../keeper/Keeper"
|
import Keeper from "../keeper/Keeper"
|
||||||
import Web3Provider from "../keeper/Web3Provider"
|
import Web3Provider from "../keeper/Web3Provider"
|
||||||
@ -18,7 +15,6 @@ import SecretStoreProvider from "../secretstore/SecretStoreProvider"
|
|||||||
import Logger from "../utils/Logger"
|
import Logger from "../utils/Logger"
|
||||||
import Account from "./Account"
|
import Account from "./Account"
|
||||||
import IdGenerator from "./IdGenerator"
|
import IdGenerator from "./IdGenerator"
|
||||||
import Condition from "./ServiceAgreements/Condition"
|
|
||||||
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
||||||
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
|
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
|
||||||
import Access from "./ServiceAgreements/Templates/Access"
|
import Access from "./ServiceAgreements/Templates/Access"
|
||||||
@ -74,64 +70,7 @@ export default class Ocean {
|
|||||||
const template = new Access()
|
const template = new Access()
|
||||||
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
|
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
|
||||||
|
|
||||||
// get condition keys from template
|
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, assetId)
|
||||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
|
|
||||||
|
|
||||||
// create ddo conditions out of the keys
|
|
||||||
const ddoConditions: DDOCondition[] = conditions
|
|
||||||
.map((condition: Condition, index: number): DDOCondition => {
|
|
||||||
const events: Event[] = [
|
|
||||||
{
|
|
||||||
name: "PaymentReleased",
|
|
||||||
actorType: [
|
|
||||||
"consumer",
|
|
||||||
],
|
|
||||||
handlers: {
|
|
||||||
moduleName: "serviceAgreement",
|
|
||||||
functionName: "fulfillAgreement",
|
|
||||||
version: "0.1",
|
|
||||||
} as EventHandlers,
|
|
||||||
} as Event,
|
|
||||||
]
|
|
||||||
|
|
||||||
const mapParameterValueToName = (name) => {
|
|
||||||
|
|
||||||
switch (name) {
|
|
||||||
case "price":
|
|
||||||
return metadata.base.price
|
|
||||||
case "assetId":
|
|
||||||
return "0x" + assetId
|
|
||||||
case "documentKeyId":
|
|
||||||
return "0x" + assetId
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const parameters: Parameter[] = condition.parameters.map((parameter: Parameter) => {
|
|
||||||
return {
|
|
||||||
name: parameter.name,
|
|
||||||
type: parameter.type,
|
|
||||||
value: mapParameterValueToName(parameter.name),
|
|
||||||
} as Parameter
|
|
||||||
})
|
|
||||||
|
|
||||||
// Logger.log(`${condition.methodReflection.contractName}.${condition.methodReflection.methodName}`,
|
|
||||||
// JSON.stringify(parameters, null, 2))
|
|
||||||
|
|
||||||
return {
|
|
||||||
contractName: condition.methodReflection.contractName,
|
|
||||||
methodName: condition.methodReflection.methodName,
|
|
||||||
timeout: condition.timeout,
|
|
||||||
index,
|
|
||||||
conditionKey: condition.condtionKey,
|
|
||||||
parameters,
|
|
||||||
events,
|
|
||||||
dependencies: condition.dependencies,
|
|
||||||
dependencyTimeoutFlags: condition.dependencyTimeoutFlags,
|
|
||||||
isTerminalCondition: condition.isTerminalCondition,
|
|
||||||
} as DDOCondition
|
|
||||||
})
|
|
||||||
|
|
||||||
const serviceEndpoint = aquarius.getServiceEndpoint(did)
|
const serviceEndpoint = aquarius.getServiceEndpoint(did)
|
||||||
|
|
||||||
@ -166,7 +105,7 @@ export default class Ocean {
|
|||||||
serviceDefinitionId: accessServiceDefinitionId,
|
serviceDefinitionId: accessServiceDefinitionId,
|
||||||
// the id of the service agreement template
|
// the id of the service agreement template
|
||||||
templateId: serviceAgreementTemplate.getId(),
|
templateId: serviceAgreementTemplate.getId(),
|
||||||
conditions: ddoConditions,
|
conditions,
|
||||||
} as Service,
|
} as Service,
|
||||||
{
|
{
|
||||||
type: "Compute",
|
type: "Compute",
|
||||||
@ -185,6 +124,8 @@ export default class Ocean {
|
|||||||
|
|
||||||
const storedDdo = await aquarius.storeDDO(ddo)
|
const storedDdo = await aquarius.storeDDO(ddo)
|
||||||
|
|
||||||
|
Logger.log(JSON.stringify(storedDdo, null, 2))
|
||||||
|
|
||||||
await didRegistry.registerAttribute(
|
await didRegistry.registerAttribute(
|
||||||
assetId,
|
assetId,
|
||||||
ValueType.DID,
|
ValueType.DID,
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
import DDOCondition from "../../ddo/Condition"
|
||||||
|
import Dependency from "../../ddo/Dependency"
|
||||||
|
import Event from "../../ddo/Event"
|
||||||
|
import EventHandlers from "../../ddo/EventHandlers"
|
||||||
|
import MetaData from "../../ddo/MetaData"
|
||||||
|
import Parameter from "../../ddo/Parameter"
|
||||||
import ContractReflector from "../../keeper/ContractReflector"
|
import ContractReflector from "../../keeper/ContractReflector"
|
||||||
import Keeper from "../../keeper/Keeper"
|
import Keeper from "../../keeper/Keeper"
|
||||||
import Web3Provider from "../../keeper/Web3Provider"
|
import Web3Provider from "../../keeper/Web3Provider"
|
||||||
@ -96,7 +102,71 @@ export default class ServiceAgreementTemplate extends OceanBase {
|
|||||||
return new Account(await serviceAgreement.getTemplateOwner(this.id))
|
return new Account(await serviceAgreement.getTemplateOwner(this.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getConditions(): Promise<Condition[]> {
|
public async getConditions(metadata: MetaData, assetId: string): Promise<DDOCondition[]> {
|
||||||
|
const conditions = await this.blendConditions()
|
||||||
|
return conditions.map((condition: Condition, index: number): DDOCondition => {
|
||||||
|
const events: Event[] = [
|
||||||
|
{
|
||||||
|
name: "PaymentReleased",
|
||||||
|
actorType: [
|
||||||
|
"consumer",
|
||||||
|
],
|
||||||
|
handlers: {
|
||||||
|
moduleName: "serviceAgreement",
|
||||||
|
functionName: "fulfillAgreement",
|
||||||
|
version: "0.1",
|
||||||
|
} as EventHandlers,
|
||||||
|
} as Event,
|
||||||
|
]
|
||||||
|
|
||||||
|
const mapParameterValueToName = (name) => {
|
||||||
|
|
||||||
|
switch (name) {
|
||||||
|
case "price":
|
||||||
|
return metadata.base.price
|
||||||
|
case "assetId":
|
||||||
|
return "0x" + assetId
|
||||||
|
case "documentKeyId":
|
||||||
|
return "0x" + assetId
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const parameters: Parameter[] = condition.parameters.map((parameter: Parameter) => {
|
||||||
|
return {
|
||||||
|
name: parameter.name,
|
||||||
|
type: parameter.type,
|
||||||
|
value: mapParameterValueToName(parameter.name),
|
||||||
|
} as Parameter
|
||||||
|
})
|
||||||
|
|
||||||
|
// Logger.log(`${condition.methodReflection.contractName}.${condition.methodReflection.methodName}`,
|
||||||
|
// JSON.stringify(parameters, null, 2))
|
||||||
|
|
||||||
|
const dependencies: Dependency[] = condition.dependencies.map((dep, i) => {
|
||||||
|
return {
|
||||||
|
name: dep,
|
||||||
|
timeout: condition.dependencyTimeoutFlags[i],
|
||||||
|
} as Dependency
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: condition.methodReflection.methodName,
|
||||||
|
dependencies,
|
||||||
|
timeout: condition.timeout,
|
||||||
|
isTerminalCondition: condition.isTerminalCondition,
|
||||||
|
conditionKey: condition.condtionKey,
|
||||||
|
contractName: condition.methodReflection.contractName,
|
||||||
|
functionName: condition.methodReflection.methodName,
|
||||||
|
index,
|
||||||
|
parameters,
|
||||||
|
events,
|
||||||
|
} as DDOCondition
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private async blendConditions(): Promise<Condition[]> {
|
||||||
const methodReflections = await this.getMethodReflections()
|
const methodReflections = await this.getMethodReflections()
|
||||||
|
|
||||||
const conditions: Condition[] = methodReflections.map((methodReflection, i) => {
|
const conditions: Condition[] = methodReflections.map((methodReflection, i) => {
|
||||||
|
@ -1,16 +1,12 @@
|
|||||||
import {assert} from "chai"
|
import {assert} from "chai"
|
||||||
import ConfigProvider from "../../src/ConfigProvider"
|
import ConfigProvider from "../../src/ConfigProvider"
|
||||||
import DDOCondition from "../../src/ddo/Condition"
|
import Condition from "../../src/ddo/Condition"
|
||||||
import DDO from "../../src/ddo/DDO"
|
import DDO from "../../src/ddo/DDO"
|
||||||
import Event from "../../src/ddo/Event"
|
|
||||||
import EventHandlers from "../../src/ddo/EventHandlers"
|
|
||||||
import MetaData from "../../src/ddo/MetaData"
|
import MetaData from "../../src/ddo/MetaData"
|
||||||
import Parameter from "../../src/ddo/Parameter"
|
|
||||||
import Service from "../../src/ddo/Service"
|
import Service from "../../src/ddo/Service"
|
||||||
import Account from "../../src/ocean/Account"
|
import Account from "../../src/ocean/Account"
|
||||||
import IdGenerator from "../../src/ocean/IdGenerator"
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||||
import Ocean from "../../src/ocean/Ocean"
|
import Ocean from "../../src/ocean/Ocean"
|
||||||
import Condition from "../../src/ocean/ServiceAgreements/Condition"
|
|
||||||
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
|
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
|
||||||
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
|
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
|
||||||
import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
|
import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
|
||||||
@ -44,70 +40,13 @@ describe("ServiceAgreement", () => {
|
|||||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||||
new ServiceAgreementTemplate(new Access())
|
new ServiceAgreementTemplate(new Access())
|
||||||
|
|
||||||
// get condition keys from template
|
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, assetId)
|
||||||
const conditions: Condition[] = await serviceAgreementTemplate.getConditions()
|
|
||||||
|
|
||||||
// create ddo conditions out of the keys
|
|
||||||
const ddoConditions: DDOCondition[] = conditions
|
|
||||||
.map((condition: Condition, index): DDOCondition => {
|
|
||||||
|
|
||||||
const events: Event[] = [
|
|
||||||
{
|
|
||||||
name: "PaymentReleased",
|
|
||||||
actorType: [
|
|
||||||
"consumer",
|
|
||||||
],
|
|
||||||
handlers: {
|
|
||||||
moduleName: "serviceAgreement",
|
|
||||||
functionName: "fulfillAgreement",
|
|
||||||
version: "0.1",
|
|
||||||
} as EventHandlers,
|
|
||||||
} as Event,
|
|
||||||
]
|
|
||||||
|
|
||||||
const mapParameterValueToName = (name) => {
|
|
||||||
|
|
||||||
switch (name) {
|
|
||||||
case "price":
|
|
||||||
return metadata.base.price
|
|
||||||
case "assetId":
|
|
||||||
return "0x" + assetId
|
|
||||||
case "documentKeyId":
|
|
||||||
return "0x" + assetId
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const parameters: Parameter[] = condition.parameters
|
|
||||||
.map((parameter: Parameter) => {
|
|
||||||
return {
|
|
||||||
name: parameter.name,
|
|
||||||
type: parameter.type,
|
|
||||||
value: mapParameterValueToName(parameter.name),
|
|
||||||
} as Parameter
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
contractName: condition.methodReflection.contractName,
|
|
||||||
methodName: condition.methodReflection.methodName,
|
|
||||||
timeout: condition.timeout,
|
|
||||||
index,
|
|
||||||
conditionKey: condition.condtionKey,
|
|
||||||
parameters,
|
|
||||||
events,
|
|
||||||
dependencies: condition.dependencies,
|
|
||||||
dependencyTimeoutFlags: condition.dependencyTimeoutFlags,
|
|
||||||
isTerminalCondition: condition.isTerminalCondition,
|
|
||||||
} as DDOCondition
|
|
||||||
})
|
|
||||||
|
|
||||||
accessService = {
|
accessService = {
|
||||||
type: "Access",
|
type: "Access",
|
||||||
serviceDefinitionId: IdGenerator.generateId(),
|
serviceDefinitionId: IdGenerator.generateId(),
|
||||||
templateId: serviceAgreementTemplate.getId(),
|
templateId: serviceAgreementTemplate.getId(),
|
||||||
conditions: ddoConditions,
|
conditions,
|
||||||
} as Service
|
} as Service
|
||||||
|
|
||||||
metaDataService = {
|
metaDataService = {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import {assert} from "chai"
|
import {assert} from "chai"
|
||||||
import ConfigProvider from "../../src/ConfigProvider"
|
import ConfigProvider from "../../src/ConfigProvider"
|
||||||
|
import MetaData from "../../src/ddo/MetaData"
|
||||||
import Account from "../../src/ocean/Account"
|
import Account from "../../src/ocean/Account"
|
||||||
import IdGenerator from "../../src/ocean/IdGenerator"
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||||
import Ocean from "../../src/ocean/Ocean"
|
import Ocean from "../../src/ocean/Ocean"
|
||||||
@ -48,7 +49,7 @@ describe("ServiceAgreementTemplate", () => {
|
|||||||
new ServiceAgreementTemplate(access)
|
new ServiceAgreementTemplate(access)
|
||||||
assert(serviceAgreementTemplate)
|
assert(serviceAgreementTemplate)
|
||||||
|
|
||||||
const conds = await serviceAgreementTemplate.getConditions()
|
const conds = await serviceAgreementTemplate.getConditions(new MetaData(), IdGenerator.generateId())
|
||||||
assert(conds)
|
assert(conds)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user