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

add event listener for lockPayment

This commit is contained in:
Sebastian Gerske 2018-11-23 16:01:42 +01:00
parent 07455e84a9
commit a904649c33
7 changed files with 70 additions and 25 deletions

7
src/ddo/Contract.ts Normal file
View File

@ -0,0 +1,7 @@
import Event from "./Event"
export default class Contract {
public contractName: string
public fulfillmentOperator: number
public events: Event[]
}

View File

@ -1,4 +1,5 @@
import Condition from "./Condition"
import Contract from "./Contract"
import MetaData from "./MetaData"
export default class Service {
@ -9,5 +10,6 @@ export default class Service {
public purchaseEndpoint?: string
public description?: string = "My public social inbox"
public metadata?: MetaData = {} as MetaData
public serviceAgreementContract?: Contract
public conditions?: Condition[] = []
}

View File

@ -54,6 +54,8 @@ import {Account, Logger, Ocean, ServiceAgreement} from "../squid"
const accessService = ddo.findServiceByType("Access")
await consumer.requestTokens(metaData.base.price)
const serviceAgreementSignatureResult: any = await ocean.signServiceAgreement(ddo.id,
accessService.serviceDefinitionId, consumer)
Logger.log("ServiceAgreement Id:", serviceAgreementSignatureResult.serviceAgreementId)

View File

@ -47,7 +47,7 @@ export default class ServiceAgreement extends ContractBase {
return this.send("executeAgreement", publisherAddress, [
serviceAgreementTemplateId, serviceAgreementSignatureHash, consumerAddress, valueHashes,
timeoutValues, "0x" + serviceAgreementId, "0x" + did.replace("did:op:", ""),
timeoutValues, serviceAgreementId, "0x" + did.replace("did:op:", ""),
])
}
}

View File

@ -4,9 +4,13 @@ import BrizoProvider from "../brizo/BrizoProvider"
import ConfigProvider from "../ConfigProvider"
import Authentication from "../ddo/Authentication"
import Condition from "../ddo/Condition"
import Contract from "../ddo/Contract"
import DDO from "../ddo/DDO"
import Event from "../ddo/Event"
import EventHandlers from "../ddo/EventHandlers"
import MetaData from "../ddo/MetaData"
import Service from "../ddo/Service"
import ContractEvent from "../keeper/Event"
import Keeper from "../keeper/Keeper"
import Web3Provider from "../keeper/Web3Provider"
import Config from "../models/Config"
@ -19,6 +23,8 @@ import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
import Access from "./ServiceAgreements/Templates/Access"
import EventListener from "../keeper/EventListener"
export default class Ocean {
public static async getInstance(config: Config) {
@ -105,6 +111,21 @@ export default class Ocean {
serviceDefinitionId: accessServiceDefinitionId,
// the id of the service agreement template
templateId: serviceAgreementTemplate.getId(),
serviceAgreementContract: {
contractName: "ServiceAgreement",
fulfillmentOperator: template.fulfillmentOperator,
events: [
{
name: "ExecuteAgreement",
actorType: ["consumer"],
handlers: {
moduleName: "payment",
functionName: "lockPayment",
version: "0.1",
} as EventHandlers,
} as Event,
],
} as Contract,
conditions,
} as Service,
{
@ -142,11 +163,35 @@ export default class Ocean {
const ddo = await AquariusProvider.getAquarius().retrieveDDO(did)
const id = did.replace("did:op:", "")
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
try {
const serviceAgreementSignature: string = await ServiceAgreement.signServiceAgreement(id,
ddo, serviceDefinitionId, serviceAgreementId, consumer)
const accessService: Service = ddo.findServiceByType("Access")
const metadataService: Service = ddo.findServiceByType("Metadata")
const event: ContractEvent = EventListener.subscribe(accessService.serviceAgreementContract.contractName,
accessService.serviceAgreementContract.events[0].name, {
serviceAgreementId,
})
const price = metadataService.metadata.base.price
const balance = await consumer.getOceanBalance()
if (balance < price) {
throw new Error(`Not enough ocean tokens! Should have ${price} but has ${balance}`)
}
event.listenOnce((data) => {
new ServiceAgreement(serviceAgreementId)
.buyAsset(id,
metadataService.metadata.base.price,
consumer,
)
})
return {
serviceAgreementId,
serviceAgreementSignature,

View File

@ -117,11 +117,6 @@ export default class ServiceAgreement extends OceanBase {
return new ServiceAgreement(
executeAgreementReceipt.events.ExecuteAgreement.returnValues.serviceAgreementId,
ddo,
publisher,
new Account(consumerAddress),
executeAgreementReceipt.events.ExecuteAgreement.returnValues.state,
executeAgreementReceipt.events.ExecuteAgreement.returnValues.status,
)
}
@ -186,7 +181,7 @@ export default class ServiceAgreement extends OceanBase {
contionValues.push({
type: parameter.type,
value: parameter.name === "serviceId" ? "0x" + serviceAgreementId : parameter.value,
value: parameter.name === "serviceId" ? serviceAgreementId : parameter.value,
} as ValuePair)
})
@ -196,12 +191,7 @@ export default class ServiceAgreement extends OceanBase {
return values
}
private constructor(serviceAgreementId: string,
ddo: DDO,
private publisher: Account,
consumer: Account,
state: boolean,
status: boolean) {
constructor(serviceAgreementId: string) {
super(serviceAgreementId)
}
@ -218,12 +208,11 @@ export default class ServiceAgreement extends OceanBase {
return lockPaymentReceipt.status
}
public async grantAccess(assetId: string, documentId: string): Promise<boolean> {
public async grantAccess(assetId: string, documentId: string, publisher: Account): Promise<boolean> {
const {accessConditions} = await Keeper.getInstance()
const grantAccessReceipt =
await accessConditions.grantAccess(this.getId(), assetId, documentId,
this.publisher.getId())
await accessConditions.grantAccess(this.getId(), assetId, documentId, publisher.getId())
return !!grantAccessReceipt.events.AccessGranted
}

View File

@ -60,7 +60,7 @@ describe("ServiceAgreement", () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
@ -79,7 +79,7 @@ describe("ServiceAgreement", () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
@ -103,7 +103,7 @@ describe("ServiceAgreement", () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
@ -127,7 +127,7 @@ describe("ServiceAgreement", () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService, metaDataService]})
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
@ -156,7 +156,7 @@ describe("ServiceAgreement", () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
@ -178,7 +178,7 @@ describe("ServiceAgreement", () => {
assert(paid)
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId)
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId, publisherAccount)
assert(accessGranted)
})
@ -186,7 +186,7 @@ describe("ServiceAgreement", () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
const serviceAgreementId: string = IdGenerator.generatePrefixedId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
@ -201,7 +201,7 @@ describe("ServiceAgreement", () => {
assert(serviceAgreement)
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId)
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId, publisherAccount)
assert(!accessGranted)
})
})