1
0
mirror of https://github.com/oceanprotocol/react.git synced 2024-11-23 02:00:27 +01:00
This commit is contained in:
mihaisc 2020-07-17 16:09:01 +03:00
parent 927d0acf05
commit 29f5b2d16c
6 changed files with 104 additions and 27 deletions

View File

@ -1,5 +1,11 @@
import React from 'react'
import { useOcean, usePublish, useConsume } from '@oceanprotocol/react'
import {
useOcean,
usePublish,
useConsume,
useCompute,
computeOptions
} from '@oceanprotocol/react'
import { Metadata, DDO } from '@oceanprotocol/lib'
import { useState } from 'react'
import { useEffect } from 'react'
@ -7,6 +13,7 @@ import { useEffect } from 'react'
export function ConsumeDdo() {
const { accountId, ocean } = useOcean()
const { consumeStepText, consume, consumeError } = useConsume()
const { compute, computeStepText } = useCompute()
const [did, setDid] = useState<string | undefined>()
useEffect(() => {
async function init() {}
@ -20,6 +27,25 @@ export function ConsumeDdo() {
await consume(did, ddo.dataToken, 'access')
}
const computeDid = async () => {
if (did === undefined) return
const ddo = await ocean.assets.resolve(did)
console.log(ddo)
console.log('ocean dt', ocean.datatokens)
const computeService = ddo.findServiceByType('compute')
console.log('ddo compute service', computeService)
const serv = ddo.findServiceById(computeService.index)
console.log('ddo compute service resolved', serv)
await compute(
did,
computeService,
ddo.dataToken,
"console.log('test')",
computeOptions[0].value
)
}
const handleChange = (e: any) => {
setDid(e.target.value)
}
@ -31,8 +57,12 @@ export function ConsumeDdo() {
</div>
<div>
<button onClick={consumeDid}>Consume did</button>
<button onClick={computeDid}>Compute</button>
</div>
<div>
{consumeStepText}
{computeStepText}
</div>
<div>{consumeStepText}</div>
<div>{consumeError}</div>
</>
)

View File

@ -33,7 +33,8 @@ export function Publish() {
const publishAsset = async () => {
const ddo = await publish(asset as Metadata, '4', marketAddress, [
{ serviceType: 'access', cost: '1' }
{ serviceType: 'access', cost: '1' },
{ serviceType: 'compute', cost: '1' }
])
console.log(ddo)
setDdo(ddo)

13
package-lock.json generated
View File

@ -376,9 +376,9 @@
"integrity": "sha512-5Oohzno3tnISMYG1jhtHxKMMkcIypTZAPlIoFjE5vDlAW51tyuxwYxBXHYQ2/FLRVFkmUmy5yASKg9zbnKKwQw=="
},
"@oceanprotocol/lib": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.4.tgz",
"integrity": "sha512-H+soD+W+fOdKXgFxM8pkhAs9JoJDMWxudvTJxT696Ch+z1CvbcEh4bFNlUpjWhVgwjMTVCigYum3BG+V1mqN1Q==",
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.1.6.tgz",
"integrity": "sha512-eFncaLtetef6REKzd2u68fFCVdCfBqwGnsDU9oirMfBrdcGu0VquXtAsEfrnHAkIsCaf97wd1GAZ4Yp1JaXP1g==",
"requires": {
"@ethereum-navigator/navigator": "^0.5.0",
"@oceanprotocol/contracts": "^0.2.3",
@ -390,13 +390,6 @@
"web3": "^1.2.9",
"web3-eth-contract": "^1.2.9",
"whatwg-url": "^8.0.0"
},
"dependencies": {
"@oceanprotocol/contracts": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-0.2.3.tgz",
"integrity": "sha512-5Oohzno3tnISMYG1jhtHxKMMkcIypTZAPlIoFjE5vDlAW51tyuxwYxBXHYQ2/FLRVFkmUmy5yASKg9zbnKKwQw=="
}
}
},
"@octokit/auth-token": {

View File

@ -22,7 +22,7 @@
],
"dependencies": {
"@oceanprotocol/contracts": "^0.2.3",
"@oceanprotocol/lib": "^0.1.4",
"@oceanprotocol/lib": "^0.1.6",
"@walletconnect/web3-provider": "^1.0.15",
"axios": "^0.19.2",
"react": "^16.9.41",

View File

@ -4,15 +4,16 @@ import { ComputeValue } from './ComputeOptions'
import { feedback } from './../../utils'
import { DID, Logger } from '@oceanprotocol/lib'
import { MetadataAlgorithm } from '@oceanprotocol/lib/dist/node/ddo/interfaces/MetadataAlgorithm'
import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/ComputeJob'
interface UseCompute {
compute: (
did: string,
computeServiceIndex: number,
computeService: any,
dataTokenAddress: string,
algorithmRawCode: string,
computeContainer: ComputeValue
) => Promise<void>
) => Promise<ComputeJob>
computeStep?: number
computeStepText?: string
computeError?: string
@ -21,8 +22,9 @@ interface UseCompute {
// TODO: customize for compute
export const computeFeedback: { [key in number]: string } = {
...feedback,
4: '3/3 Access granted. Starting job...'
0: '1/3 Ordering asset...',
1: '2/3 Transfering data token.',
2: '3/3 Access granted. Starting job...'
}
const rawAlgorithmMeta: MetadataAlgorithm = {
rawcode: `console.log('Hello world'!)`,
@ -53,7 +55,7 @@ function useCompute(): UseCompute {
dataTokenAddress: string,
algorithmRawCode: string,
computeContainer: ComputeValue
): Promise<void> {
): Promise<ComputeJob> {
if (!ocean || !account) return
setComputeError(undefined)
@ -65,6 +67,13 @@ function useCompute(): UseCompute {
rawAlgorithmMeta.rawcode = algorithmRawCode
const output = {}
Logger.log(
'compute order',
accountId,
did,
computeService,
rawAlgorithmMeta
)
const order = await ocean.compute.order(
accountId,
did,
@ -73,13 +82,16 @@ function useCompute(): UseCompute {
rawAlgorithmMeta
)
setStep(1)
const computeOrder = JSON.parse(order)
const tokenTransfer = await ocean.datatokens.transfer(
Logger.log('compute order', computeOrder)
const tokenTransfer = await ocean.datatokens.transferWei(
computeOrder.dataToken,
computeOrder.to,
String(computeOrder.numTokens),
computeOrder.from
)
setStep(2)
const response = await ocean.compute.start(
did,
(tokenTransfer as any).transactionHash,
@ -91,11 +103,12 @@ function useCompute(): UseCompute {
computeService.index,
computeService.type
)
return response
} catch (error) {
Logger.error(error)
setComputeError(error.message)
} finally {
setComputeStep(undefined)
setStep(undefined)
setIsLoading(false)
}
}

View File

@ -2,7 +2,10 @@ import { useEffect, useState } from 'react'
import { DDO, Metadata, DataTokens, Logger } from '@oceanprotocol/lib'
import { useOcean } from '../../providers'
import ProviderStatus from '../../providers/OceanProvider/ProviderStatus'
import { Service } from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
import {
Service,
ServiceComputePrivacy
} from '@oceanprotocol/lib/dist/node/ddo/interfaces/Service'
import { ServiceConfig } from './ServiceConfig'
import { publishFeedback } from '../../utils'
@ -72,7 +75,7 @@ function usePublish(): UsePublish {
new Date(Date.now()).toISOString().split('.')[0] + 'Z'
const timeout = 0
const services: Service[] = []
setStep(3)
serviceConfigs.forEach(async (serviceConfig) => {
const price = ocean.datatokens.toWei(serviceConfig.cost)
switch (serviceConfig.serviceType) {
@ -88,11 +91,47 @@ function usePublish(): UsePublish {
break
}
case 'compute': {
const computeService = await ocean.assets.createAccessServiceAttributes(
const cluster = ocean.compute.createClusterAttributes(
'Kubernetes',
'http://10.0.0.17/xxx'
)
const servers = [
ocean.compute.createServerAttributes(
'1',
'xlsize',
'50',
'16',
'0',
'128gb',
'160gb',
timeout
)
]
const containers = [
ocean.compute.createContainerAttributes(
'tensorflow/tensorflow',
'latest',
'sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc'
)
]
const provider = ocean.compute.createProviderAttributes(
'Azure',
'Compute service with 16gb ram for each node.',
cluster,
containers,
servers
)
const origComputePrivacy = {
allowRawAlgorithm: true,
allowNetworkAccess: false,
trustedAlgorithms: []
}
const computeService = ocean.compute.createComputeService(
account,
price,
publishedDate,
0
provider,
origComputePrivacy as ServiceComputePrivacy
)
services.push(computeService)
break
@ -100,14 +139,15 @@ function usePublish(): UsePublish {
}
})
Logger.log('services created', services)
setStep(4)
setStep(3)
const ddo = await ocean.assets.create(
asset,
account,
services,
tokenAddress
)
setStep(5)
Logger.log('ddo created', ddo)
setStep(4)
return ddo
} catch (error) {