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

Fix consume flow.

This commit is contained in:
Pedro Gutiérrez 2019-03-14 16:58:07 +01:00 committed by Pedro Gutiérrez
parent 7debfd8fb5
commit 885157a119
3 changed files with 65 additions and 52 deletions

View File

@ -21,9 +21,8 @@ before_script:
- ganache-cli --port 18545 > ganache-cli.log & - ganache-cli --port 18545 > ganache-cli.log &
- git clone https://github.com/oceanprotocol/barge - git clone https://github.com/oceanprotocol/barge
- cd barge - cd barge
- export KEEPER_VERSION=v0.8.1
- export KEEPER_OWNER_ROLE_ADDRESS="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260" - export KEEPER_OWNER_ROLE_ADDRESS="0xe2DD09d719Da89e5a3D0F2549c7E24566e947260"
- bash -x start_ocean.sh --latest --no-brizo --no-pleuston --local-spree-node 2>&1 > start_ocean.log & - bash -x start_ocean.sh --latest --no-pleuston --local-spree-node 2>&1 > start_ocean.log &
- cd .. - cd ..
- ./scripts/unlock-spree-accounts.sh 2>&1 > /dev/null & - ./scripts/unlock-spree-accounts.sh 2>&1 > /dev/null &

View File

@ -7,5 +7,5 @@
"threshold": 0, "threshold": 0,
"password": "secret", "password": "secret",
"address": "0xa99d43d86a0758d5632313b8fa3972b6088a21bb", "address": "0xa99d43d86a0758d5632313b8fa3972b6088a21bb",
"verbose": "error" "verbose": 0
} }

View File

@ -1,47 +1,32 @@
import { assert } from "chai" import { assert } from 'chai'
import * as Web3 from "web3"
import * as fs from "fs";
import { config } from "../config" import { config } from "../config"
import { Ocean, MetaData, Account, DDO } from "../../src" // @oceanprotocol/squid import { Ocean, MetaData, DDO, DID, Account/*, ServiceAgreement*/ } from '../../src' // @oceanprotocol/squid
xdescribe("Consume Asset", () => { describe("Consume Asset", () => {
let ocean: Ocean let ocean: Ocean
let publisher: Account let publisher: Account
let consumer: Account let consumer: Account
let ddo: DDO
let agreementId: string
const testHash = Math.random().toString(36).substr(2)
let metadata: Partial<MetaData> let metadata: Partial<MetaData>
let metadataGenerator = (name: string) => ({
...metadata, let ddo: DDO
base: { let serviceAgreementSignatureResult: {agreementId: string, signature: string}
...metadata.base,
name: `${name}${testHash}`,
},
})
before(async () => { before(async () => {
ocean = await Ocean.getInstance({ ocean = await Ocean.getInstance(config)
...config,
verbose: true,
web3Provider: new Web3.providers.HttpProvider("http://localhost:8545", 0, "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "node0"),
})
// Accounts // Accounts
publisher = new Account("0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e") publisher = (await ocean.accounts.list())[0]
publisher.setPassword("node0") publisher.setPassword(process.env.ACCOUNT_PASSWORD)
consumer = new Account("0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0") consumer = (await ocean.accounts.list())[1]
consumer.setPassword("secret")
// Data // Data
metadata = { metadata = {
base: { base: {
name: undefined, name: "Office Humidity",
type: "dataset", type: "dataset",
description: "Weather information of UK including temperature and humidity", description: "Weather information of UK including temperature and humidity",
size: "3.1gb", size: "3.1gb",
@ -56,12 +41,12 @@ xdescribe("Consume Asset", () => {
workExample: "stationId,latitude,longitude,datetime,temperature,humidity423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68", workExample: "stationId,latitude,longitude,datetime,temperature,humidity423432fsd,51.509865,-0.118092,2011-01-01T10:55:11+00:00,7.2,68",
files: [ files: [
{ {
url: "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.pdf", url: "https://testocnfiles.blob.core.windows.net/testfiles/testzkp.zip",
checksum: "085340abffh21495345af97c6b0e761", checksum: "085340abffh21495345af97c6b0e761",
contentLength: "12324", contentLength: "12324",
}, },
{ {
url: "https://raw.githubusercontent.com/oceanprotocol/squid-js/develop/README.md", url: "https://testocnfiles.blob.core.windows.net/testfiles/testzkp2.zip",
}, },
], ],
links: [ links: [
@ -74,41 +59,70 @@ xdescribe("Consume Asset", () => {
price: 10, price: 10,
}, },
} }
}) })
it("should regiester an asset", async () => { it("should regiester a asset", async () => {
ddo = await ocean.assets.create(metadataGenerator("ToBeConsumed") as any, publisher); ddo = await ocean.assets.create(metadata as any, publisher)
assert.instanceOf(ddo, DDO) 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 order the asset", async () => { 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") const accessService = ddo.findServiceByType("Access")
try { serviceAgreementSignatureResult = await ocean.agreements.prepare(ddo.id, accessService.serviceDefinitionId, consumer)
agreementId = await ocean.assets.order(ddo.id, accessService.serviceDefinitionId, consumer) const {agreementId, signature} = serviceAgreementSignatureResult
} catch(e) { assert.match(agreementId, /^[a-f0-9]{64}$/, "Service agreement ID seems not valid")
console.warn(e) assert.match(signature, /^0x[a-f0-9]{130}$/, "Service agreement signature seems not valid")
}
assert.isDefined(agreementId)
}) })
xit("should consume and store the assets", async () => { it("should execute the service agreement", async () => {
const accessService = ddo.findServiceByType("Access") const accessService = ddo.findServiceByType("Access")
const folder = "/tmp/ocean/squid-js" const success = await ocean.agreements.create(
const path = await ocean.assets.consume(agreementId, ddo.id, accessService.serviceDefinitionId, consumer, folder) ddo.id,
serviceAgreementSignatureResult.agreementId,
accessService.serviceDefinitionId,
serviceAgreementSignatureResult.signature,
consumer,
publisher,
)
assert.include(path, folder, "The storage path is not correct.") assert.isTrue(success)
})
const files = await new Promise(resolve => { xit("should lock the payment", async () => {
fs.readdir(path, (err, files) => { const paid = await ocean.agreements.conditions
resolve(files) .lockReward(
}); serviceAgreementSignatureResult.agreementId,
}) ddo.findServiceByType("Metadata").metadata.base.price,
publisher,
)
assert.deepEqual(files, ["README.md", "testzkp.pdf"], "Stored files are not correct.") assert.isTrue(paid, "The asset has not been paid correctly")
})
xit("should lock the payment", async () => {
const granted = await ocean.agreements.conditions
.grantAccess(serviceAgreementSignatureResult.agreementId, ddo.id, consumer.getId(), publisher)
assert.isTrue(granted, "The asset has not been granted correctly")
})
xit("should consume the assets", async () => {
const accessService = ddo.findServiceByType("Access")
await ocean.assets.consume(serviceAgreementSignatureResult.agreementId, ddo.id, accessService.serviceDefinitionId, consumer)
}) })
}) })