From e1b91977465835ced587d76ef7730d4b4813cda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Mon, 11 Feb 2019 14:56:48 +0100 Subject: [PATCH] added buy asset test --- integration/ocean/BuyAsset.test.ts | 112 +++++++++++++++++++++++++++++ src/keeper/ContractHandler.ts | 2 - src/ocean/Account.ts | 13 +--- src/ocean/OceanAssets.ts | 2 +- src/squid.ts | 27 ++++--- 5 files changed, 133 insertions(+), 23 deletions(-) create mode 100644 integration/ocean/BuyAsset.test.ts diff --git a/integration/ocean/BuyAsset.test.ts b/integration/ocean/BuyAsset.test.ts new file mode 100644 index 0000000..db5191d --- /dev/null +++ b/integration/ocean/BuyAsset.test.ts @@ -0,0 +1,112 @@ +import { assert } from 'chai' + +import { config } from "../config" + +import { Ocean, MetaData, DDO, DID, Account, ServiceAgreement } from '../../src' // @oceanprotocol/squid + +describe("Buy Asset", () => { + let ocean: Ocean + + let publisher: Account + let consumer: Account + + let metadata: Partial + + let ddo: DDO + let did: DID + let serviceAgreementSignatureResult: {serviceAgreementId: string, serviceAgreementSignature: string} + let serviceAgreement: ServiceAgreement + + before(async () => { + ocean = await Ocean.getInstance(config) + + // Accounts + publisher = (await ocean.getAccounts())[0] + publisher.setPassword(process.env.ACCOUNT_PASSWORD) + consumer = (await ocean.getAccounts())[1] + + // Data + metadata = { + base: { + name: "Office Humidity", + type: "dataset", + description: "Weather information of UK including temperature and humidity", + size: "3.1gb", + dateCreated: "2012-02-01T10:55:11+00:00", + author: "Met Office", + license: "CC-BY", + copyrightHolder: "Met Office", + encoding: "UTF-8", + compression: "zip", + contentType: "text/csv", + // tslint:disable-next-line + workExample: "stationId,latitude,longitude,datetime,temperature,humidity423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68", + files: [ + { + url: "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip", + checksum: "085340abffh21495345af97c6b0e761", + contentLength: "12324", + }, + { + url: "https://testocnfiles.blob.core.windows.net/testfiles/testzkp2.zip", + }, + ], + links: [ + {sample1: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-daily/"}, + {sample2: "http://data.ceda.ac.uk/badc/ukcp09/data/gridded-land-obs/gridded-land-obs-averages-25km/"}, + {fieldsDescription: "http://data.ceda.ac.uk/badc/ukcp09/"}, + ], + inLanguage: "en", + tags: "weather, uk, 2011, temperature, humidity", + price: 10, + }, + } + }) + + it("should regiester a asset", async () => { + ddo = await ocean.registerAsset(metadata as any, publisher) + did = DID.parse(ddo.id) + + assert.isDefined(ddo, "Register has not returned a DDO") + assert.match(ddo.id, /^did:op:[a-f0-9]{64}$/, "DDO id is not valid") + assert.isAtLeast(ddo.authentication.length, 1, "Default authentication not added") + assert.isDefined(ddo.findServiceByType("Access"), "DDO Access service doesn't exist") + }) + + it("should be able to request tokens for consumer", async () => { + const initialBalance = (await consumer.getBalance()).ocn; + await consumer.requestTokens(metadata.base.price) + + assert.equal((await consumer.getBalance()).ocn, initialBalance + metadata.base.price, "OCN Tokens not delivered") + }) + + it("should sign the service agreement", async () => { + const accessService = ddo.findServiceByType("Access") + + serviceAgreementSignatureResult = await ocean.signServiceAgreement(ddo.id, accessService.serviceDefinitionId, consumer) + + const {serviceAgreementId, serviceAgreementSignature} = serviceAgreementSignatureResult + assert.match(serviceAgreementId, /^[a-f0-9]{64}$/, "Service agreement ID seems not valid") + assert.match(serviceAgreementSignature, /^0x[a-f0-9]{130}$/, "Service agreement signature seems not valid") + }) + + it("should execute the service agreement", async () => { + const accessService = ddo.findServiceByType("Access") + + serviceAgreement = await ocean.executeServiceAgreement( + ddo.id, + accessService.serviceDefinitionId, + serviceAgreementSignatureResult.serviceAgreementId, + serviceAgreementSignatureResult.serviceAgreementSignature, + consumer, + publisher, + ) + + assert.match(serviceAgreement.getId(), /^0x[a-f0-9]{64}$/, "Service agreement ID seems not valid") + }) + + it("should pay asset trough the service agreement", async () => { + const paid = await serviceAgreement.payAsset(did.getId(), metadata.base.price, consumer) + assert.isTrue(paid, "The asset has not been paid correctly") + }) +}) diff --git a/src/keeper/ContractHandler.ts b/src/keeper/ContractHandler.ts index 2b5d9e1..d83f93a 100644 --- a/src/keeper/ContractHandler.ts +++ b/src/keeper/ContractHandler.ts @@ -41,8 +41,6 @@ export default class ContractHandler { const contract = new web3.eth.Contract(artifact.abi, artifact.address) if (ConfigProvider.getConfig().verbose) { Logger.log("Getting instance of", what, "from", where, "at address", artifact.address) - } else { - Logger.log("Loaded", what, "from", where) } ContractHandler.contracts.set(what, contract) return ContractHandler.contracts.get(what) diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index cfe7260..f9255d8 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -10,8 +10,6 @@ import OceanBase from "./OceanBase" * Account information. */ export default class Account extends OceanBase { - private balance: Balance - private password?: string /** @@ -59,15 +57,10 @@ export default class Account extends OceanBase { * @return {Promise} */ public async getBalance(): Promise { - - if (!this.balance) { - this.balance = { - eth: await this.getEtherBalance(), - ocn: await this.getOceanBalance(), - } as Balance + return { + eth: await this.getEtherBalance(), + ocn: await this.getOceanBalance(), } - - return this.balance } /** diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index b0e418f..40d5ec5 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -205,7 +205,7 @@ export default class OceanAssets { event.listenOnce(async (data) => { - const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.serviceAgreementId) + const sa: ServiceAgreement = new ServiceAgreement(data.returnValues.agreementId) await sa.payAsset( d.getId(), metadataService.metadata.base.price, diff --git a/src/squid.ts b/src/squid.ts index cef6c63..2a1b7b0 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -1,29 +1,36 @@ +import Access from "./ocean/ServiceAgreements/Templates/Access" import Account from "./ocean/Account" +import Config from "./models/Config" import DID from "./ocean/DID" +import FitchainCompute from "./ocean/ServiceAgreements/Templates/FitchainCompute" import IdGenerator from "./ocean/IdGenerator" +import Logger from "./utils/Logger" import Ocean from "./ocean/Ocean" +import SecretStoreProvider from "./secretstore/SecretStoreProvider" import ServiceAgreement from "./ocean/ServiceAgreements/ServiceAgreement" import ServiceAgreementTemplate from "./ocean/ServiceAgreements/ServiceAgreementTemplate" -import Access from "./ocean/ServiceAgreements/Templates/Access" -import FitchainCompute from "./ocean/ServiceAgreements/Templates/FitchainCompute" -import SecretStoreProvider from "./secretstore/SecretStoreProvider" -import Logger from "./utils/Logger" import WebServiceConnectorProvider from "./utils/WebServiceConnectorProvider" import EventListener from "./keeper/EventListener" +// Exports +export * from "./ddo/DDO" +export * from "./ddo/MetaData" + const Templates = {Access, FitchainCompute} export { Ocean, - ServiceAgreement, - ServiceAgreementTemplate, - Logger, - Templates, + Account, - IdGenerator, + Config, DID, EventListener, - WebServiceConnectorProvider, + IdGenerator, + Logger, SecretStoreProvider, + ServiceAgreement, + ServiceAgreementTemplate, + Templates, + WebServiceConnectorProvider, }