diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 19a76a5..5cff582 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,2 +1,4 @@ export * from './useConsume' export * from './useMetadata' +export * from './useCompute' +export * from './useSearch' diff --git a/src/hooks/useCompute/ComputeOptions.ts b/src/hooks/useCompute/ComputeOptions.ts new file mode 100644 index 0000000..40f5067 --- /dev/null +++ b/src/hooks/useCompute/ComputeOptions.ts @@ -0,0 +1,28 @@ +export interface ComputeValue { + entrypoint: string + image: string + tag: string +} +export interface ComputeOption { + name: string + value: ComputeValue +} + +export const computeOptions: ComputeOption[] = [ + { + name: 'nodejs', + value: { + entrypoint: 'node $ALGO', + image: 'node', + tag: '10' + } + }, + { + name: 'python3.7', + value: { + entrypoint: 'python $ALGO', + image: 'oceanprotocol/algo_dockers', + tag: 'python-panda' + } + } +] diff --git a/src/hooks/useCompute/README.md b/src/hooks/useCompute/README.md new file mode 100644 index 0000000..c188975 --- /dev/null +++ b/src/hooks/useCompute/README.md @@ -0,0 +1,76 @@ +# `useCompute` + +The `OceanProvider` maintains a connection to the Ocean Protocol network in multiple steps: + +1. On mount, connect to Aquarius instance right away so any asset metadata can be retrieved before, and independent of any Web3 connections. +2. Once Web3 becomes available, a connection to all Ocean Protocol network components is established. +3. Once Ocean becomes available, spits out some info about it. + +Also provides a `useOcean` helper hook to access its context values from any component. + +## Usage + +```tsx +import React from 'react' +import { + useWeb3, + useOcean, + useMetadata, + useCompute, + computeOptions, + ComputeValue, + readFileContent +} from '@oceanprotocol/react' + +const did = 'did:op:0x000000000' + +export default function MyComponent() { + // Get web3 from built-in Web3Provider context + const { web3 } = useWeb3() + + // Get Ocean instance from built-in OceanProvider context + const { ocean, account } = useOcean() + + // Get metadata for this asset + const { title, metadata } = useMetadata(did) + + + // compute asset + const { compute, computeStep } = useCompute() + // raw code text + const [algorithmRawCode, setAlgorithmRawCode] = useState('') + const [computeContainer, setComputeContainer] = useState() + async function handleCompute() { + await consume(did,algorithmRawCode,computeContainer) + } + + async function onChangeHandler(event){ + const fileText = await readFileContent(files[0]) + setAlgorithmRawCode(fileText) + } + async function handleSelectChange(event: any) { + const comType = event.target.value + setComputeContainer(comType) + } + + return ( +
+

{title}

+

Price: {web3.utils.fromWei(metadata.main.price)}

+ +

Your account: {account}

+ + + + +
+ ) +} +``` diff --git a/src/hooks/useCompute/index.ts b/src/hooks/useCompute/index.ts new file mode 100644 index 0000000..227cf84 --- /dev/null +++ b/src/hooks/useCompute/index.ts @@ -0,0 +1,2 @@ +export * from './useCompute' +export * from './ComputeOptions' diff --git a/src/hooks/useCompute/useCompute.ts b/src/hooks/useCompute/useCompute.ts new file mode 100644 index 0000000..1e392be --- /dev/null +++ b/src/hooks/useCompute/useCompute.ts @@ -0,0 +1,87 @@ +import { useState } from 'react' +import { DID, MetaDataAlgorithm } from '@oceanprotocol/squid' +import { useOcean } from '../../providers' +import { ComputeValue } from './ComputeOptions' + +interface UseCompute { + compute: ( + did: DID, + algorithmRawCode: string, + computeContainer: ComputeValue + ) => Promise + computeStep?: number + computeError?: string +} + +// TODO: customize for compute +export const computeFeedback: { [key in number]: string } = { + 99: 'Decrypting file URL...', + 0: '1/3 Asking for agreement signature...', + 1: '1/3 Agreement initialized.', + 2: '2/3 Asking for two payment confirmations...', + 3: '2/3 Payment confirmed. Requesting access...', + 4: '3/3 Access granted. Consuming file...' +} +const rawAlgorithmMeta: MetaDataAlgorithm = { + rawcode: `console.log('Hello world'!)`, + format: 'docker-image', + version: '0.1', + container: { + entrypoint: '', + image: '', + tag: '' + } +} + +function useCompute(): UseCompute { + const { ocean, account, accountId, config } = useOcean() + const [computeStep, setComputeStep] = useState() + const [computeError, setComputeError] = useState() + + async function compute( + did: DID | string, + algorithmRawCode: string, + computeContainer: ComputeValue + ): Promise { + if (!ocean || !account) return + + setComputeError(undefined) + + try { + const computeOutput = { + publishAlgorithmLog: false, + publishOutput: false, + brizoAddress: config.brizoAddress, + brizoUri: config.brizoUri, + metadataUri: config.aquariusUri, + nodeUri: config.nodeUri, + owner: accountId, + secretStoreUri: config.secretStoreUri + } + + const agreement = await ocean.compute + .order(account, did as string) + .next((step: number) => setComputeStep(step)) + + rawAlgorithmMeta.container = computeContainer + rawAlgorithmMeta.rawcode = algorithmRawCode + + await ocean.compute.start( + account, + agreement, + undefined, + rawAlgorithmMeta, + computeOutput + ) + } catch (error) { + setComputeError(error.message) + } finally { + setComputeStep(undefined) + } + } + + return { compute, computeStep, computeError } +} + +export { useCompute, UseCompute } +export default UseCompute diff --git a/src/hooks/useSearch/index.ts b/src/hooks/useSearch/index.ts new file mode 100644 index 0000000..25e9f76 --- /dev/null +++ b/src/hooks/useSearch/index.ts @@ -0,0 +1 @@ +export * from './useSearch' diff --git a/src/hooks/useSearch/useSearch.ts b/src/hooks/useSearch/useSearch.ts new file mode 100644 index 0000000..11ad469 --- /dev/null +++ b/src/hooks/useSearch/useSearch.ts @@ -0,0 +1,69 @@ +import { useState } from 'react' +import { Logger } from '@oceanprotocol/squid' +import { useOcean } from '../../providers' +import { + SearchQuery, + Aquarius, + QueryResult +} from '@oceanprotocol/squid/dist/node/aquarius/Aquarius' + +// TODO searchText +interface UseSearch { + searchQuery: (query: SearchQuery) => Promise + getPublishedList: ( + account: string, + page: number, + offset: number + ) => Promise + searchError?: string +} + +function useSearch(): UseSearch { + const { ocean, account, config } = useOcean() + const [searchError, setSearchError] = useState() + + async function searchQuery(query: SearchQuery): Promise { + if (!ocean || !account) return + + setSearchError(undefined) + + try { + const aquarius = new Aquarius(config.aquariusUri as string, Logger) + return await aquarius.queryMetadata(query) + } catch (error) { + setSearchError(error.message) + } + } + + async function getPublishedList( + account: string, + page: number, + offset: number + ): Promise { + if (!ocean || !account) return + + setSearchError(undefined) + + try { + const query = { + page, + offset, + query: { + 'publicKey.owner': [account] + }, + sort: { + created: -1 + } + } as SearchQuery + + return await searchQuery(query) + } catch (error) { + setSearchError(error.message) + } + } + + return { searchQuery, getPublishedList, searchError } +} + +export { useSearch, UseSearch } +export default useSearch diff --git a/src/index.ts b/src/index.ts index 441c499..779a417 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,5 @@ import './@types/globals' export * from './providers' export * from './hooks' // export * from './components' + +export * from './utils' diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..144ae1e --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,13 @@ +export function readFileContent(file: File): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader() + reader.onerror = () => { + reader.abort() + reject(new DOMException('Problem parsing input file.')) + } + reader.onload = () => { + resolve(reader.result as string) + } + reader.readAsText(file) + }) +}