mirror of
https://github.com/oceanprotocol/react-tutorial
synced 2024-12-22 09:13:14 +01:00
fix compute tutorial
This commit is contained in:
parent
6bfd96d13d
commit
5ca05440e4
31
scripts/keeper.sh
Executable file
31
scripts/keeper.sh
Executable file
@ -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'
|
@ -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 }) {
|
||||
<>
|
||||
<h3>Compute</h3>
|
||||
<button onClick={() => publish()} disabled={!web3}>
|
||||
Publish dataset and algorithm
|
||||
1) Publish dataset with compute service
|
||||
</button>
|
||||
<button onClick={() => publishalgo()} disabled={!web3}>
|
||||
2) Publish algorithm
|
||||
</button>
|
||||
|
||||
<button onClick={() => start()} disabled={!web3}>
|
||||
Order and start compute service
|
||||
3)Order and start compute service
|
||||
</button>
|
||||
<button onClick={() => getStatus()} disabled={!web3}>
|
||||
4)Get Job Status
|
||||
</button>
|
||||
<hr />
|
||||
Asset DID:<input type="text" value={ddoAsset.id}></input>
|
||||
<hr />
|
||||
Algo DID:<input type="text" value={ddoAlgorithm.id}></input>
|
||||
<hr />
|
||||
Job ID:<input type="text" value={jobId}></input>
|
||||
<hr />
|
||||
Compute status:<textarea rows="10" cols="180" value={jobStatus}></textarea>
|
||||
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
185
src/asset.js
185
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
|
||||
|
26
src/index.js
26
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
|
||||
|
Loading…
Reference in New Issue
Block a user