Skip to content

Commit

Permalink
C2D_V2 ( Includes C2D V2 breaking changes + Free compute) (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean authored Mar 4, 2025
1 parent 9a542d0 commit 70d5a06
Show file tree
Hide file tree
Showing 17 changed files with 1,195 additions and 408 deletions.
208 changes: 118 additions & 90 deletions ComputeExamples.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ import {
configHelperNetworks,
ConfigHelper,
getEventFromTx,
amountToUnits
amountToUnits,
isDefined
} from '../../src/index.js'
```
import crypto from 'crypto-js'
Expand Down Expand Up @@ -276,6 +277,9 @@ let resolvedAlgorithmDdo: DDO

let computeJobId: string
let agreementId: string

let computeRoutePath: string
let hasFreeComputeSupport: boolean
```
### 4.3 Helper methods
Expand Down Expand Up @@ -595,119 +599,143 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
let's check the free compute environment
```Typescript
const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find(
(ce) => ce.priceMin === 0
(ce) => ce.priceMin === 0 || isDefined(ce.free)
)
console.log('Free compute environment = ', computeEnv)
```
<!--
assert(computeEnv, 'Cannot find the free compute env')
-->

Let's have 5 minute of compute access
```Typescript
const mytime = new Date()
const computeMinutes = 5
mytime.setMinutes(mytime.getMinutes() + computeMinutes)
const computeValidUntil = Math.floor(mytime.getTime() / 1000)

const assets: ComputeAsset[] = [
{
documentId: resolvedDatasetDdo.id,
serviceId: resolvedDatasetDdo.services[0].id
computeRoutePath = await ProviderInstance.getComputeStartRoutes(providerUrl, true)
if (isDefined(computeRoutePath)) {
hasFreeComputeSupport = true
Let's have 5 minute of compute access
```Typescript
const mytime = new Date()
const computeMinutes = 5
mytime.setMinutes(mytime.getMinutes() + computeMinutes)
const computeValidUntil = Math.floor(mytime.getTime() / 1000)

const assets: ComputeAsset[] = [
{
documentId: resolvedDatasetDdo.id,
serviceId: resolvedDatasetDdo.services[0].id
}
]
const dtAddressArray = [resolvedDatasetDdo.services[0].datatokenAddress]
const algo: ComputeAlgorithm = {
documentId: resolvedAlgorithmDdo.id,
serviceId: resolvedAlgorithmDdo.services[0].id
}
]
const dtAddressArray = [resolvedDatasetDdo.services[0].datatokenAddress]
const algo: ComputeAlgorithm = {
documentId: resolvedAlgorithmDdo.id,
serviceId: resolvedAlgorithmDdo.services[0].id
}

const providerInitializeComputeResults = await ProviderInstance.initializeCompute(
assets,
algo,
computeEnv.id,
computeValidUntil,
providerUrl,
await consumerAccount.getAddress()
)
```
<!--
assert(!('error' in providerInitializeComputeResults), 'Cannot order algorithm')
-->
```Typescript
algo.transferTxId = await handleOrder(
providerInitializeComputeResults.algorithm,
resolvedAlgorithmDdo.services[0].datatokenAddress,
consumerAccount,
computeEnv.consumerAddress,
0
)
for (let i = 0; i < providerInitializeComputeResults.datasets.length; i++) {
assets[i].transferTxId = await handleOrder(
providerInitializeComputeResults.datasets[i],
dtAddressArray[i],
const providerInitializeComputeResults = await ProviderInstance.initializeCompute(
assets,
algo,
computeEnv.id,
computeValidUntil,
providerUrl,
consumerAccount
)
```
<!--
assert(!('error' in providerInitializeComputeResults), 'Cannot order algorithm')
-->
```Typescript
algo.transferTxId = await handleOrder(
providerInitializeComputeResults.algorithm,
resolvedAlgorithmDdo.services[0].datatokenAddress,
consumerAccount,
computeEnv.consumerAddress,
0
)
}
for (let i = 0; i < providerInitializeComputeResults.datasets.length; i++) {
assets[i].transferTxId = await handleOrder(
providerInitializeComputeResults.datasets[i],
dtAddressArray[i],
consumerAccount,
computeEnv.consumerAddress,
0
)
}

const computeJobs = await ProviderInstance.computeStart(
providerUrl,
consumerAccount,
computeEnv.id,
assets[0],
algo
)
const computeJobs = await ProviderInstance.freeComputeStart(
providerUrl,
consumerAccount,
computeEnv.id,
assets,
algo
)

```
<!--
assert(computeJobs, 'Cannot start compute job')
-->
Let's save the compute job it, we re going to use later
```Typescript
computeJobId = computeJobs[0].jobId
// eslint-disable-next-line prefer-destructuring
agreementId = computeJobs[0].agreementId
```
<!--
assert(computeJobs, 'Cannot start compute job')
-->
Let's save the compute job it, we re going to use later
```Typescript
computeJobId = computeJobs[0].jobId
// eslint-disable-next-line prefer-destructuring
agreementId = computeJobs[0].agreementId
} else {
assert(
computeRoutePath === null,
'Route path for free compute is not defined (perhaps because provider does not support it yet?)'
)
hasFreeComputeSupport = false
}

```

## 11. Check compute status and get download compute results URL
### 11.1 Check compute status
You can also add various delays so you see the various states of the compute job
```Typescript
const jobStatus = await ProviderInstance.computeStatus(
providerUrl,
await consumerAccount.getAddress(),
computeJobId,
agreementId
)
```
<!--
assert(jobStatus, 'Cannot retrieve compute status!')
-->
Now, let's see the current status of the previously started computer job
```Typescript
console.log('Current status of the compute job: ', jobStatus)
if (!hasFreeComputeSupport) {
assert(
computeRoutePath === null,
'Compute route path for free compute is not defined (perhaps because provider does not support it yet?)'
)
} else {
You can also add various delays so you see the various states of the compute job
```Typescript
const jobStatus = await ProviderInstance.computeStatus(
providerUrl,
await consumerAccount.getAddress(),
computeJobId,
agreementId
)
```
<!--
assert(jobStatus, 'Cannot retrieve compute status!')
-->
Now, let's see the current status of the previously started computer job
```Typescript
console.log('Current status of the compute job: ', jobStatus)
}

```

### 11.2 Get download compute results URL
```Typescript
await sleep(10000)
const downloadURL = await ProviderInstance.getComputeResultUrl(
providerUrl,
consumerAccount,
computeJobId,
0
)
```
<!--
assert(downloadURL, 'Provider getComputeResultUrl failed!')
-->
Let's check the compute results url for the specified index
```Typescript
console.log(`Compute results URL: ${downloadURL}`)
if (!hasFreeComputeSupport) {
assert(
computeRoutePath === null,
'Compute route path for free compute is not defined (perhaps because provider does not support it yet?)'
)
} else {
```Typescript
await sleep(10000)
const downloadURL = await ProviderInstance.getComputeResultUrl(
providerUrl,
consumerAccount,
computeJobId,
0
)
```
<!--
assert(downloadURL, 'Provider getComputeResultUrl failed!')
-->
Let's check the compute results url for the specified index
```Typescript
console.log(`Compute results URL: ${downloadURL}`)
}

```

Expand Down
44 changes: 38 additions & 6 deletions docs/classes/Provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ___

### computeStart

**computeStart**(`providerUri`, `consumer`, `computeEnv`, `dataset`, `algorithm`, `signal?`, `additionalDatasets?`, `output?`): `Promise`<[`ComputeJob`](../interfaces/ComputeJob.md) \| [`ComputeJob`](../interfaces/ComputeJob.md)[]\>
**computeStart**(`providerUri`, `signer`, `computeEnv`, `datasets`, `algorithm`, `resources`, `chainId`, `output?`, `freeEnvironment`, `signal?`): `Promise`<[`ComputeJob`](../interfaces/ComputeJob.md) \| [`ComputeJob`](../interfaces/ComputeJob.md)[]\>

Instruct the provider to start a compute job

Expand All @@ -106,13 +106,15 @@ Instruct the provider to start a compute job
| Name | Type | Description |
| :------ | :------ | :------ |
| `providerUri` | `string` | The provider URI. |
| `consumer` | `Signer` | - |
| `signer` | `Signer` | - | The consumer signer/account
| `computeEnv` | `string` | The compute environment. |
| `dataset` | [`ComputeAsset`](../interfaces/ComputeAsset.md) | The dataset to start compute on |
| `datasets` | [`ComputeAsset`](../interfaces/ComputeAsset.md) | The dataset to start compute on |
| `algorithm` | [`ComputeAlgorithm`](../interfaces/ComputeAlgorithm.md) | The algorithm to start compute with. |
| `signal?` | `AbortSignal` | abort signal |
| `additionalDatasets?` | [`ComputeAsset`](../interfaces/ComputeAsset.md)[] | The additional datasets if that is the case. |
| `resources` | [`ComputeResourceRequest`](../interfaces/ComputeResourcesRequest.md) | The resources to start compute with. |
| `chainId?` | [`number`] | The network for the payments |
| `output?` | [`ComputeOutput`](../interfaces/ComputeOutput.md) | The compute job output settings. |
| `signal?` | `AbortSignal` | abort signal |


#### Returns

Expand Down Expand Up @@ -486,7 +488,7 @@ Initializes the provider for a compute request.
| `computeEnv` | `string` | The compute environment. |
| `validUntil` | `number` | The job expiration date. |
| `providerUri` | `string` | The provider URI. |
| `accountId` | `string` | caller address |
| `signer` | `Signer` | caller account |
| `signal?` | `AbortSignal` | abort signal |

#### Returns
Expand All @@ -501,6 +503,36 @@ ProviderComputeInitialize data

___


### computeStreamableLogs

**computeStreamableLogs**(`providerUri`, `signer`, `jobId`, `signal?`): `Promise`<`any`\>

Gets the streamable compute logs.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `providerUri` | `string` | The provider URI. |
| `signer` | `Signer` | The signer. |
| `jobId` | `string` | The Job Id. |
| `signal?` | `AbortSignal` | The abort signal. |


#### Returns

`Promise`<`any`\>

The compute logs.

#### Defined in

[services/Provider.ts:908](https://github.com/oceanprotocol/ocean.js/blob/c99bc5c6/src/services/Provider.ts#L908)

___


### inputMatch

`Private` **inputMatch**(`input`, `regexp`, `conversorName`): `Object`
Expand Down
30 changes: 30 additions & 0 deletions docs/interfaces/ComputeEnvFees.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[@oceanprotocol/lib](../README.md) / [Exports](../modules.md) / ComputeEnvFees

# Interface: ComputeEnvFees

## Table of contents

### Properties

- [feeToken](ComputeEnvFees.md#feeToken)
- [prices](ComputeEnvFees.md#prices)

## Properties

### feeToken

**feeToken**: `string`

#### Defined in

[@types/Compute.ts:42](https://github.com/oceanprotocol/ocean.js/blob/c99bc5c6/src/@types/Compute.ts#L42)

___

### prices

**prices**: `ComputeResourcesPricingInfo`[]

#### Defined in

[@types/Compute.ts:43](https://github.com/oceanprotocol/ocean.js/blob/c99bc5c6/src/@types/Compute.ts#L43)
20 changes: 20 additions & 0 deletions docs/interfaces/ComputeEnvFeesStructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[@oceanprotocol/lib](../README.md) / [Exports](../modules.md) / ComputeEnvFeesStructure

# Interface: ComputeEnvFeesStructure

## Table of contents

### Properties

- [feeToken](ComputeEnvFeesStructure.md#chainId)

## Properties

### chainId

**chainId**: `ComputeEnvFees`

#### Defined in

[@types/Compute.ts:42](https://github.com/oceanprotocol/ocean.js/blob/c99bc5c6/src/@types/Compute.ts#L46)

Loading

0 comments on commit 70d5a06

Please sign in to comment.