diff --git a/package.json b/package.json index 106b814..1e9cae0 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,12 @@ "version": "1.0.0", "description": "React + squid.js interacting in the most minimal way with Ocean Protocol.", "dependencies": { - "@oceanprotocol/squid": "^1.2.0", - "react": "^16.12.0", - "react-dom": "^16.12.0", - "react-scripts": "^3.3.0", - "web3": "^1.2.5" + "@oceanprotocol/squid": "^2.0.0", + "prettier": "^2.0.2", + "react": "^16.13.1", + "react-dom": "^16.13.1", + "react-scripts": "^3.4.1", + "web3": "^1.2.6" }, "scripts": { "start": "react-scripts start", diff --git a/src/Compute.js b/src/Compute.js new file mode 100644 index 0000000..d5ed20c --- /dev/null +++ b/src/Compute.js @@ -0,0 +1,255 @@ +import React, { useState } from 'react' + +import asset from './asset' +import { assetAlgo, rawAlgoMeta } from './asset-compute' + +export default function Compute({ ocean, web3 }) { + const [ddoAssetId, setDdoAssetId] = useState('') + const [jobStatus, setJobStatus] = useState('') + const [jobId, setJobId] = useState('') + const [agreementId, setAgreementId] = useState('') + const [ddoAlgorithmId, setDdoAlgorithmId] = useState('') + const [isAlgoInputVisible, setIsAlgoInputVisible] = useState('') + const [textRawAlgo, setTextRawAlgo] = useState('') + const [publishLogState, setPublishLogState] = useState(false) + const [publishOutputState, setPublishOutputState] = useState(false) + + // publish a dataset and an algorithm + async function publish() { + try { + const accounts = await ocean.accounts.list() + console.log('Publishing asset.') + + const service = await ocean.compute.createComputeServiceAttributes( + accounts[0], + '0', + '2020-03-10T10:00:00Z' + ) + console.log(service) + const ddoAssetNew = await ocean.assets.create(asset, accounts[0], [ + service + ]) + console.log('Asset successfully submitted.') + console.log(ddoAssetNew) + // keep track of this registered asset for consumption later on + setDdoAssetId(ddoAssetNew.id) + 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(assetAlgo, accounts[0]) + console.log(ddoAlgorithmNew) + console.log('Algo asset successfully submitted.') + // keep track of this registered asset for consumption later on + setDdoAlgorithmId(ddoAlgorithmNew.id) + alert('Algorithm successfully submitted.') + } catch (error) { + console.error(error.message) + } + } + + async function startCompute(algorithmId, algorithmMeta) { + try { + const accounts = await ocean.accounts.list() + const computeOutput = { + publishAlgorithmLog: publishLogState, + publishOutput: publishOutputState, + brizoAddress: ocean.config.brizoAddress, + brizoUri: ocean.config.brizoUri, + metadataUri: ocean.config.aquariusUri, + nodeUri: ocean.config.nodeUri, + owner: accounts[0].getId(), + secretStoreUri: ocean.config.secretStoreUri + } + console.log(computeOutput) + // order the compute service + const agreement = await ocean.compute.order(accounts[0], ddoAssetId) + setAgreementId(agreement) + // start a compute job + const status = await ocean.compute.start( + accounts[0], + agreement, + algorithmId, + algorithmMeta, + computeOutput + ) + setJobId(status.jobId) + console.log(status) + alert('Compute job created. You can query for its status now.') + } catch (error) { + console.error(error.message) + } + } + + // order and start the compute service with an algorithm published as an asset + async function startWithPublishedAlgo() { + return startCompute(ddoAlgorithmId) + } + + // order and start the compute service with a passed raw algorithm + async function startWithRawAlgo() { + rawAlgoMeta.rawcode = textRawAlgo + return startCompute(undefined, rawAlgoMeta) + } + + async function getStatus() { + try { + const accounts = await ocean.accounts.list() + + 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) + } + } + + async function showDivAlgo() { + setIsAlgoInputVisible(isAlgoInputVisible ? false : true) + } + + async function updateRawAlgoCode(event) { + setTextRawAlgo(event.target.value) + } + + async function updateDdoAssetId(event) { + setDdoAssetId(event.target.value) + } + + async function handlePublishOutputState(event) { + setPublishOutputState(event.target.checked) + } + + async function handlePublishLogState(event) { + setPublishLogState(event.target.checked) + } + + if (!web3) { + return null + } + + return ( + <> +

Compute

+ +

1. Publish Dataset

+ + +

+ + {ddoAssetId} +

+
+ + +

2. Publish Algorithm

+ +

+ + {ddoAlgorithmId} +

+
+ + +

3. Start Compute Job

+ +

+ + +

+ +
+ +

+ +