Compute using a published algorithm on a Data Set

Compute using a published algorithm on a Data Set

Requirements

This is a continuation of the React App Setup tutorial, so make sure you have done all the steps described in there.

  1. React App Setup

Open src/Compute.js from your marketplace/ folder.

Define Compute Output

First, let’s define some options for our upcoming job:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
        <p>
          <Label htmlFor="publishOutputState">
            <input
              type="checkbox"
              id="publishOutputState"
              checked={publishOutputState}
              onChange={handlePublishOutputState}
            />
            Publish Output into the Marketplace
          </Label>
          <Label htmlFor="publishLogState">
            <input
              type="checkbox"
              id="publishLogState"
              checked={publishLogState}
              onChange={handlePublishLogState}
            />
            Publish Algorithm Logs into the Marketplace
          </Label>
        </p>

and use them

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
      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
      }

Order the dataset

Next, we have to order the dataset that we are going to compute upon. We are going to use the ddoAssetId, which was set during publishing of the asset.

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
      const agreement = await ocean.compute.order(accounts[0], ddoAssetId)

Start the compute job

And finally, start the job:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
      const status = await ocean.compute.start(
        accounts[0],
        agreement,
        algorithmId,
        algorithmMeta,
        computeOutput
      )

Final Result

Now that we have all the requirements, we need a function to handle it.

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
  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)
    }
  }
  async function startWithPublishedAlgo() {
    return startCompute(ddoAlgorithmId)
  }

The last thing we need is a button to start our compute job:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
        <button
          onClick={startWithPublishedAlgo}
          disabled={!ddoAssetId || !ddoAlgorithmId}
        >
          Order and start compute service with published algorithm
        </button>

** Notice that the button will be disabled if there were no previous published Datasets and Algorithms.

Move on to Get Status of a Compute Job.