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

fixed asset.order method behavior

This commit is contained in:
Pedro Gutiérrez 2019-02-13 21:28:42 +01:00 committed by Pedro Gutiérrez
parent a6d2ce8fde
commit 42b7c0cced

View File

@ -4,18 +4,14 @@ import BrizoProvider from "../brizo/BrizoProvider"
import { Condition } from "../ddo/Condition" import { Condition } from "../ddo/Condition"
import { DDO } from "../ddo/DDO" import { DDO } from "../ddo/DDO"
import { MetaData } from "../ddo/MetaData" import { MetaData } from "../ddo/MetaData"
import { Service, ServiceAuthorization } from "../ddo/Service" import { Service } from "../ddo/Service"
import ContractEvent from "../keeper/Event"
import EventListener from "../keeper/EventListener"
import Keeper from "../keeper/Keeper" import Keeper from "../keeper/Keeper"
import SecretStoreProvider from "../secretstore/SecretStoreProvider" import SecretStoreProvider from "../secretstore/SecretStoreProvider"
import Account from "./Account" import Account from "./Account"
import DID from "./DID" import DID from "./DID"
import IdGenerator from "./IdGenerator"
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"
import ConfigProvider from "../ConfigProvider" import OceanAgreements from "./OceanAgreements"
/** /**
* Assets submodule of Ocean Protocol. * Assets submodule of Ocean Protocol.
@ -182,7 +178,8 @@ export default class OceanAssets {
} }
/** /**
* Purchases a service agreement by DID. * Start the purchase/order of an asset's service. Starts by signing the service agreement
* then sends the request to the publisher via the service endpoint (Brizo http service).
* @param {string} did Decentralized ID. * @param {string} did Decentralized ID.
* @param {string} serviceDefinitionId Service definition ID. * @param {string} serviceDefinitionId Service definition ID.
* @param {Account} consumer Consumer account. * @param {Account} consumer Consumer account.
@ -194,43 +191,12 @@ export default class OceanAssets {
consumer: Account, consumer: Account,
): Promise<string> { ): Promise<string> {
const d: DID = DID.parse(did as string) const oceanAreements = await OceanAgreements.getInstance()
const ddo = await AquariusProvider.getAquarius().retrieveDDO(d)
const serviceAgreementId: string = IdGenerator.generateId()
try { const {agreementId, signature} = await oceanAreements.prepare(did, serviceDefinitionId, consumer)
await ServiceAgreement.signServiceAgreement( await oceanAreements.send(did, agreementId, serviceDefinitionId, signature, consumer)
ddo, serviceDefinitionId, serviceAgreementId, consumer)
const accessService = ddo.findServiceByType("Access") return agreementId
const metadataService = ddo.findServiceByType("Metadata")
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}`)
}
const event: ContractEvent = EventListener.subscribe(
accessService.serviceAgreementContract.contractName,
accessService.serviceAgreementContract.events[0].name, {
serviceAgreementId,
})
event.listenOnce(async (data) => {
const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.agreementId)
await sa.payAsset(
d.getId(),
metadataService.metadata.base.price,
consumer,
)
})
return serviceAgreementId
} catch (err) {
throw new Error("Signing ServiceAgreement failed: " + err)
}
} }
/** /**