From 65b99456a63f3239005f4b1dddf6d207b3e45173 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 28 Jan 2020 13:01:39 +0100 Subject: [PATCH 01/22] add Compute component example --- src/Compute.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 3 +++ 2 files changed, 65 insertions(+) create mode 100644 src/Compute.js diff --git a/src/Compute.js b/src/Compute.js new file mode 100644 index 0000000..70a1a0a --- /dev/null +++ b/src/Compute.js @@ -0,0 +1,62 @@ +import React, { useState } from 'react' +import asset from './asset' + +export default function Compute({ ocean, web3 }) { + const [ddoAsset, setDdoAsset] = useState('') + const [ddoAlgorithm, setDdoAlgorithm] = useState('') + + // publish a dataset and an algorithm + async function publish() { + try { + const accounts = await ocean.accounts.list() + const ddoAssetNew = await ocean.assets.create(asset, accounts[0]) + const ddoAlgorithmNew = await ocean.assets.create(asset, accounts[0]) + + console.log('Asset successfully submitted.') + console.log(ddoAssetNew) + console.log(ddoAlgorithmNew) + // keep track of this registered asset for consumption later on + setDdoAsset(ddoAssetNew) + setDdoAlgorithm(ddoAlgorithmNew) + alert( + 'Asset successfully submitted. Look into your console to see the response DDO object.' + ) + } catch (error) { + console.error(error.message) + } + } + + // order and start the compute service + async function start() { + try { + const accounts = await ocean.accounts.list() + + // order the compute service + const agreementId = await ocean.compute.order(accounts[0], ddoAsset.id) + + // start a compute job + const status = await ocean.compute.start( + accounts[0], + agreementId, + ddoAlgorithm.id + ) + console.log(status) + } catch (error) { + console.error(error.message) + } + } + + // get results + + return ( + <> +

Compute

+ + + + ) +} diff --git a/src/index.js b/src/index.js index 9d8b74c..7f6395c 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom' import { Ocean } from '@oceanprotocol/squid' import Web3 from 'web3' import asset from './asset' +import Compute from './Compute' let web3 @@ -119,6 +120,8 @@ class App extends Component { +
+ ) } From dca0432b7c05f7e62a75660b88faa43b1759d0e5 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 28 Jan 2020 14:22:23 +0100 Subject: [PATCH 02/22] bump to squid-js 2.0.0-beta.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 106b814..a54c318 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "React + squid.js interacting in the most minimal way with Ocean Protocol.", "dependencies": { - "@oceanprotocol/squid": "^1.2.0", + "@oceanprotocol/squid": "^2.0.0-beta.0", "react": "^16.12.0", "react-dom": "^16.12.0", "react-scripts": "^3.3.0", From 0e9de2404b6018f4a7b8ab5d3a9f45f065f0d006 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 28 Jan 2020 15:20:04 +0100 Subject: [PATCH 03/22] update ocean.assets.order --- src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.js b/src/index.js index 7f6395c..f5c7656 100644 --- a/src/index.js +++ b/src/index.js @@ -81,7 +81,6 @@ class App extends Component { // order service agreement const agreement = await this.state.ocean.assets.order( consumeAsset.id, - service.index, accounts[0] ) // consume it From 0790ba6465b983ee3945b1d9d54dd35ca5d3d664 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 28 Jan 2020 15:43:16 +0100 Subject: [PATCH 04/22] bump squid-js --- package.json | 2 +- src/index.js | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index a54c318..33ce269 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "React + squid.js interacting in the most minimal way with Ocean Protocol.", "dependencies": { - "@oceanprotocol/squid": "^2.0.0-beta.0", + "@oceanprotocol/squid": "^2.0.0-beta.1", "react": "^16.12.0", "react-dom": "^16.12.0", "react-scripts": "^3.3.0", diff --git a/src/index.js b/src/index.js index f5c7656..edd0f58 100644 --- a/src/index.js +++ b/src/index.js @@ -76,8 +76,6 @@ class App extends Component { const accounts = await this.state.ocean.accounts.list() // get our registered asset const consumeAsset = this.state.ddo - // get service we want to execute - const service = consumeAsset.findServiceByType('access') // order service agreement const agreement = await this.state.ocean.assets.order( consumeAsset.id, @@ -87,7 +85,6 @@ class App extends Component { await this.state.ocean.assets.consume( agreement, consumeAsset.id, - service.index, accounts[0], '', 0 From 6bfd96d13dc235922432f57581cc7bfe3207e423 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 31 Jan 2020 15:02:01 +0100 Subject: [PATCH 05/22] bump to squid-js v2.0.0-beta.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 33ce269..b2552a4 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "React + squid.js interacting in the most minimal way with Ocean Protocol.", "dependencies": { - "@oceanprotocol/squid": "^2.0.0-beta.1", + "@oceanprotocol/squid": "^2.0.0-beta.2", "react": "^16.12.0", "react-dom": "^16.12.0", "react-scripts": "^3.3.0", From 5ca05440e4c9d39d4936b9fae2d0dae877fe784d Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Tue, 10 Mar 2020 13:32:34 +0200 Subject: [PATCH 06/22] fix compute tutorial --- scripts/keeper.sh | 31 ++++++++ src/Compute.js | 86 +++++++++++++++++---- src/asset.js | 185 ++++++++++++++++++++++++++++++---------------- src/index.js | 26 +++---- 4 files changed, 236 insertions(+), 92 deletions(-) create mode 100755 scripts/keeper.sh diff --git a/scripts/keeper.sh b/scripts/keeper.sh new file mode 100755 index 0000000..9d14fdf --- /dev/null +++ b/scripts/keeper.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Wait for contracts migration and extract Keeper artifacts + +RETRY_COUNT=0 +COMMAND_STATUS=1 + +printf '\n\e[33m◯ Waiting for contracts to be generated...\e[0m\n' + +mkdir -p artifacts + +until [ $COMMAND_STATUS -eq 0 ] || [ $RETRY_COUNT -eq 120 ]; do + keeper_contracts_docker_id=$(docker container ls | grep keeper-contracts | awk '{print $1}') + docker cp ${keeper_contracts_docker_id}:/keeper-contracts/artifacts/ready ./artifacts/ > /dev/null 2>&1 + COMMAND_STATUS=$? + sleep 5 + (( RETRY_COUNT=RETRY_COUNT+1 )) +done + +printf '\e[32m✔ Found new contract artifacts.\e[0m\n' + +rm -rf ./artifacts/ + +if [ $COMMAND_STATUS -ne 0 ]; then + echo "Waited for more than two minutes, but keeper contracts have not been migrated yet. Did you run an Ethereum RPC client and the migration script?" + exit 1 +fi + +docker cp "${keeper_contracts_docker_id}":/keeper-contracts/artifacts/. ./node_modules/@oceanprotocol/keeper-contracts/artifacts/ + +printf '\e[32m✔ Copied new contract artifacts.\e[0m\n' diff --git a/src/Compute.js b/src/Compute.js index 70a1a0a..a5f0947 100644 --- a/src/Compute.js +++ b/src/Compute.js @@ -1,26 +1,49 @@ import React, { useState } from 'react' -import asset from './asset' +import * as Assets from './asset' export default function Compute({ ocean, web3 }) { const [ddoAsset, setDdoAsset] = useState('') + const [jobStatus, setJobStatus] = useState('') + const [jobId, setJobId] = useState('') + const [agreementId, setAgreementId] = useState('') const [ddoAlgorithm, setDdoAlgorithm] = useState('') - + const computestate = { + inputdid: '', + algodid: '', + algooutput:'' + } // publish a dataset and an algorithm async function publish() { try { const accounts = await ocean.accounts.list() - const ddoAssetNew = await ocean.assets.create(asset, accounts[0]) - const ddoAlgorithmNew = await ocean.assets.create(asset, accounts[0]) - + console.log('Publishing asset.') + + + const service=await Assets.createComputeService(ocean,accounts[0],"0","2020-03-10T10:00:00Z") + console.log(service) + const ddoAssetNew = await ocean.assets.create(Assets.getAsset(), accounts[0],[service]) console.log('Asset successfully submitted.') console.log(ddoAssetNew) - console.log(ddoAlgorithmNew) // keep track of this registered asset for consumption later on setDdoAsset(ddoAssetNew) + alert( 'Asset successfully submitted.' ) + } catch (error) { + console.error(error.message) + } + } + + + async function publishalgo() { + try { + const accounts = await ocean.accounts.list() + console.log('Publishing algo.') + + const ddoAlgorithmNew = await ocean.assets.create(Assets.getAlgoAsset(), accounts[0]) + console.log(ddoAlgorithmNew) + console.log('Algo asset successfully submitted.') + // keep track of this registered asset for consumption later on setDdoAlgorithm(ddoAlgorithmNew) - alert( - 'Asset successfully submitted. Look into your console to see the response DDO object.' - ) + alert( 'Algorithm successfully submitted.' ) } catch (error) { console.error(error.message) } @@ -32,14 +55,33 @@ export default function Compute({ ocean, web3 }) { const accounts = await ocean.accounts.list() // order the compute service - const agreementId = await ocean.compute.order(accounts[0], ddoAsset.id) - + const agreement = await ocean.compute.order(accounts[0], ddoAsset.id) + setAgreementId(agreement) // start a compute job const status = await ocean.compute.start( accounts[0], - agreementId, + agreement, ddoAlgorithm.id ) + setJobId(status.jobId) + console.log(status) + alert( 'Job created. You can query for status now' ) + } catch (error) { + console.error(error.message) + } + } + + async function getStatus() { + try { + const accounts = await ocean.accounts.list() + // start a compute job + + const status = await ocean.compute.status( + accounts[0], + agreementId, + jobId + ) + setJobStatus(JSON.stringify(status, null, '\t')) console.log(status) } catch (error) { console.error(error.message) @@ -52,11 +94,27 @@ export default function Compute({ ocean, web3 }) { <>

Compute

+ + + +
+ Asset DID: +
+ Algo DID: +
+ Job ID: +
+ Compute status: + ) } diff --git a/src/asset.js b/src/asset.js index fda5ee5..fd06034 100644 --- a/src/asset.js +++ b/src/asset.js @@ -1,66 +1,121 @@ -const asset = { - main: { - name: '10 Monkey Species Small', - dateCreated: '2012-02-01T10:55:11Z', - author: 'Mario', - type: 'dataset', - license: 'CC0: Public Domain', - price: '0', - files: [ - { - index: 0, - contentType: 'application/zip', - checksum: '2bf9d229d110d1976cdf85e9f3256c7f', - checksumType: 'MD5', - contentLength: '12057507', - compression: 'zip', - encoding: 'UTF-8', - url: - 'https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/training.zip' - }, - { - index: 1, - contentType: 'text/txt', - checksum: '354d19c0733c47ef3a6cce5b633116b0', - checksumType: 'MD5', - contentLength: '928', - url: - 'https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/monkey_labels.txt' - } - ] - }, - additionalInformation: { - categories: ['Biology'], - tags: ['image data', 'classification', 'animals'], - description: 'EXAMPLE ONLY ', - copyrightHolder: 'Unknown', - workExample: 'image path, id, label', - links: [ - { - name: 'example model', - url: - 'https://drive.google.com/open?id=1uuz50RGiAW8YxRcWeQVgQglZpyAebgSM' - }, - { - name: 'example code', - type: 'example code', - url: 'https://github.com/slothkong/CNN_classification_10_monkey_species' - }, - { - url: - 'https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/links/discovery/n5151.jpg', - name: 'n5151.jpg', - type: 'discovery' - }, - { - url: - 'https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/links/sample/sample.zip', - name: 'sample.zip', - type: 'sample' - } - ], - inLanguage: 'en' - } -} -export default asset + + export function getAsset(){ + const asset = { + main: { + name: '10 Monkey Species Small', + dateCreated: '2012-02-01T10:55:11Z', + author: 'Mario', + type: 'dataset', + license: 'CC0: Public Domain', + price: '0', + files: [ + { + index: 0, + contentType: 'application/zip', + checksum: '2bf9d229d110d1976cdf85e9f3256c7f', + checksumType: 'MD5', + contentLength: '12057507', + compression: 'zip', + encoding: 'UTF-8', + url: + 'https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/training.zip' + }, + { + index: 1, + contentType: 'text/txt', + checksum: '354d19c0733c47ef3a6cce5b633116b0', + checksumType: 'MD5', + contentLength: '928', + url: + 'https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/monkey_labels.txt' + } + ] + }, + additionalInformation: { + categories: ['Biology'], + tags: ['image data', 'classification', 'animals'], + description: 'EXAMPLE ONLY ', + copyrightHolder: 'Unknown', + workExample: 'image path, id, label', + links: [ + { + name: 'example model', + url: + 'https://drive.google.com/open?id=1uuz50RGiAW8YxRcWeQVgQglZpyAebgSM' + }, + { + name: 'example code', + type: 'example code', + url: 'https://github.com/slothkong/CNN_classification_10_monkey_species' + } + ], + inLanguage: 'en' + } + } + return(asset) + } + + + export function getAlgoAsset(){ + const algoasset = { + main: { + name: 'My great algo', + dateCreated: '2012-02-01T10:55:11Z', + author: 'Alex', + type: "algorithm", + algorithm: { + format: "docker-image", + version: "0.1", + container: { + entrypoint: "node $ALGO", + image: "node", + tag: "10" + } + }, + license: 'CC0: Public Domain', + price: '0', + files: [ + { + index: 0, + contentType: 'application/text', + contentLength: '12057507', + compression: 'zip', + encoding: 'UTF-8', + url: 'https://raw.githubusercontent.com/oceanprotocol/test-algorithm/master/javascript/algo.js' + } + ] + }, + additionalInformation: { + description: 'My super algo ' + } + } + return(algoasset) + } + + + export async function createComputeService(ocean: Ocean,publisher: Account,price: string,datePublished: string){ + const { templates } = ocean.keeper + const serviceAgreementTemplate = await templates.escrowComputeExecutionTemplate.getServiceAgreementTemplate() + const name = 'dataAssetComputingServiceAgreement' + const service = { + type: 'compute', + serviceEndpoint: ocean.brizo.getComputeEndpoint(), + templateId: templates.escrowComputeExecutionTemplate.getId(), + attributes: { + main: { + creator: publisher.getId(), + datePublished, + price, + timeout: 3600, + name + }, + serviceAgreementTemplate + } + } + return(service) + } + + + +//export default Assets diff --git a/src/index.js b/src/index.js index edd0f58..7f909ae 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import React, { Component } from 'react' import ReactDOM from 'react-dom' import { Ocean } from '@oceanprotocol/squid' import Web3 from 'web3' -import asset from './asset' +import * as Assets from './asset' import Compute from './Compute' let web3 @@ -16,23 +16,23 @@ class App extends Component { state = { ocean: undefined, results: [], - ddo: undefined + ddo: undefined } async componentDidMount() { const ocean = await new Ocean.getInstance({ web3Provider: web3, - nodeUri: 'https://nile.dev-ocean.com', - aquariusUri: 'https://aquarius.marketplace.dev-ocean.com', - brizoUri: 'https://brizo.marketplace.dev-ocean.com', - brizoAddress: '0x4aaab179035dc57b35e2ce066919048686f82972', - secretStoreUri: 'https://secret-store.nile.dev-ocean.com', + //nodeUri: 'https://nile.dev-ocean.com', + //aquariusUri: 'https://aquarius.marketplace.dev-ocean.com', + //brizoUri: 'https://brizo.marketplace.dev-ocean.com', + //brizoAddress: '0x4aaab179035dc57b35e2ce066919048686f82972', + //secretStoreUri: 'https://secret-store.nile.dev-ocean.com', // local Spree connection - // nodeUri: 'http://localhost:8545', - // aquariusUri: 'http://aquarius:5000', - // brizoUri: 'http://localhost:8030', - // brizoAddress: '0x00bd138abd70e2f00903268f3db08f2d25677c9e', - // secretStoreUri: 'http://localhost:12001', + nodeUri: 'http://localhost:8545', + aquariusUri: 'http://aquarius:5000', + brizoUri: 'http://localhost:8030', + brizoAddress: '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0', + secretStoreUri: 'http://localhost:12001', verbose: true }) this.setState({ ocean }) @@ -42,7 +42,7 @@ class App extends Component { async registerAsset() { try { const accounts = await this.state.ocean.accounts.list() - const ddo = await this.state.ocean.assets.create(asset, accounts[0]) + const ddo = await this.state.ocean.assets.create(Assets.getAsset(), accounts[0]) console.log('Asset successfully submitted.') console.log(ddo) // keep track of this registered asset for consumption later on From 3fecbc4cbfe863081ba589d81434bdbcd438f9d6 Mon Sep 17 00:00:00 2001 From: Klaudiusz Dembler Date: Thu, 12 Mar 2020 09:16:09 +0100 Subject: [PATCH 07/22] cleanup and style fixes --- package.json | 2 +- src/Compute.js | 60 +++++++------ src/asset.js | 233 ++++++++++++++++++++++++------------------------- src/index.js | 7 +- 4 files changed, 154 insertions(+), 148 deletions(-) diff --git a/package.json b/package.json index b2552a4..ff93285 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "React + squid.js interacting in the most minimal way with Ocean Protocol.", "dependencies": { - "@oceanprotocol/squid": "^2.0.0-beta.2", + "@oceanprotocol/squid": "^2.0.0-beta.4", "react": "^16.12.0", "react-dom": "^16.12.0", "react-scripts": "^3.3.0", diff --git a/src/Compute.js b/src/Compute.js index a5f0947..4292ec9 100644 --- a/src/Compute.js +++ b/src/Compute.js @@ -2,48 +2,54 @@ import React, { useState } from 'react' import * as Assets from './asset' export default function Compute({ ocean, web3 }) { - const [ddoAsset, setDdoAsset] = useState('') + const [ddoAsset, setDdoAsset] = useState({ id: '' }) const [jobStatus, setJobStatus] = useState('') const [jobId, setJobId] = useState('') const [agreementId, setAgreementId] = useState('') - const [ddoAlgorithm, setDdoAlgorithm] = useState('') - const computestate = { - inputdid: '', - algodid: '', - algooutput:'' - } + const [ddoAlgorithm, setDdoAlgorithm] = useState({ id: '' }) + // publish a dataset and an algorithm async function publish() { try { const accounts = await ocean.accounts.list() console.log('Publishing asset.') - - - const service=await Assets.createComputeService(ocean,accounts[0],"0","2020-03-10T10:00:00Z") + + const service = await Assets.createComputeService( + ocean, + accounts[0], + '0', + '2020-03-10T10:00:00Z' + ) console.log(service) - const ddoAssetNew = await ocean.assets.create(Assets.getAsset(), accounts[0],[service]) + const ddoAssetNew = await ocean.assets.create( + Assets.getAsset(), + accounts[0], + [service] + ) console.log('Asset successfully submitted.') console.log(ddoAssetNew) // keep track of this registered asset for consumption later on setDdoAsset(ddoAssetNew) - alert( 'Asset successfully submitted.' ) + alert('Asset successfully submitted.') } catch (error) { console.error(error.message) } } - async function publishalgo() { try { const accounts = await ocean.accounts.list() console.log('Publishing algo.') - - const ddoAlgorithmNew = await ocean.assets.create(Assets.getAlgoAsset(), accounts[0]) + + const ddoAlgorithmNew = await ocean.assets.create( + Assets.getAlgoAsset(), + accounts[0] + ) console.log(ddoAlgorithmNew) console.log('Algo asset successfully submitted.') // keep track of this registered asset for consumption later on setDdoAlgorithm(ddoAlgorithmNew) - alert( 'Algorithm successfully submitted.' ) + alert('Algorithm successfully submitted.') } catch (error) { console.error(error.message) } @@ -65,7 +71,7 @@ export default function Compute({ ocean, web3 }) { ) setJobId(status.jobId) console.log(status) - alert( 'Job created. You can query for status now' ) + alert('Job created. You can query for status now') } catch (error) { console.error(error.message) } @@ -76,11 +82,7 @@ export default function Compute({ ocean, web3 }) { const accounts = await ocean.accounts.list() // start a compute job - const status = await ocean.compute.status( - accounts[0], - agreementId, - jobId - ) + const status = await ocean.compute.status(accounts[0], agreementId, jobId) setJobStatus(JSON.stringify(status, null, '\t')) console.log(status) } catch (error) { @@ -99,7 +101,6 @@ export default function Compute({ ocean, web3 }) { - @@ -107,14 +108,17 @@ export default function Compute({ ocean, web3 }) { 4)Get Job Status
- Asset DID: + Asset DID: +
- Algo DID: + Algo DID: +
- Job ID: + Job ID: +
- Compute status: - + Compute status: +