@oceanprotocol/lib 2.5.1 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/CodeExamples.md +2 -2
- package/ComputeExamples.md +703 -0
- package/README.md +2 -2
- package/dist/lib.js +1 -1
- package/dist/lib.js.map +1 -1
- package/dist/lib.modern.js +1 -1
- package/dist/lib.modern.js.map +1 -1
- package/dist/lib.module.js +1 -1
- package/dist/lib.module.js.map +1 -1
- package/dist/lib.umd.js +1 -1
- package/dist/lib.umd.js.map +1 -1
- package/dist/src/@types/Asset.d.ts +22 -0
- package/dist/src/@types/File.d.ts +14 -0
- package/dist/src/contracts/Datatoken.d.ts +10 -0
- package/dist/src/services/Provider.d.ts +6 -4
- package/dist/test/integration/ComputeExamples.test.d.ts +1 -0
- package/package.json +10 -8
|
@@ -0,0 +1,703 @@
|
|
|
1
|
+
# Compute-to-Data (C2D) Code Examples
|
|
2
|
+
|
|
3
|
+
Here are the steps:
|
|
4
|
+
|
|
5
|
+
0. [Prerequisites](#-Prerequisites)
|
|
6
|
+
1. [Initialize services](#-initialize-services)
|
|
7
|
+
2. [Create a new node.js project](#-create-a-new-node.js-project)
|
|
8
|
+
3. [Install dependencies](#-install-dependencies)
|
|
9
|
+
4. [Import dependencies and add variables and constants](#-import-dependencies-and-add-variables-and-constants)
|
|
10
|
+
5. [Initialize accounts and deploy contracts](#-initialize-accounts-and-deploy-contracts)
|
|
11
|
+
6. [Publish a dataset and an algorithm](#-publish-a-dataset-data-nft-and-datatoken)
|
|
12
|
+
7. [Resolve published datasets and algorithms](#-resolve-published-datasets-and-algorithms)
|
|
13
|
+
8. [Send datatokens to consumer](#-send-datatokens-to-consumer)
|
|
14
|
+
9. [Consumer fetches compute environment](#-consumer-starts-a-compute-job-using-a-free-c2D-environment)
|
|
15
|
+
10. [Consumer starts a compute job using a free C2D environment](#-consumer-starts-a-compute-job-using-a-free-c2D-environment)
|
|
16
|
+
11. [Check compute status and get download compute results url](#-check-compute-status-and-get-download-compute-results-url)
|
|
17
|
+
|
|
18
|
+
Let's go through each step.
|
|
19
|
+
|
|
20
|
+
## 0. Prerequisites
|
|
21
|
+
Before we start it is important that you have all of the necessary prerequisites installed on your computer.
|
|
22
|
+
- **A Unix based operating system (Linux or Mac)**. If you are a Windows user you can try to run linux inside a virtual machine but this is outside of the scope of this article.
|
|
23
|
+
- **Git**. Instructions for installing Git can be found here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
|
|
24
|
+
- **Node.js** can be downloaded from here: https://nodejs.org/en/download/
|
|
25
|
+
- **Docker** can be installed from here: https://docs.docker.com/get-docker/. Please note that Docker must run as a non-root user, you can set this up by following these instructions: https://docs.docker.com/engine/install/linux-postinstall/
|
|
26
|
+
|
|
27
|
+
## 1. Initialize services
|
|
28
|
+
|
|
29
|
+
Ocean.js uses off-chain services for metadata (Aquarius) and consuming datasets (Provider).
|
|
30
|
+
|
|
31
|
+
We start by initializing the services. To do this, we clone the Barge repository and run it. This will run the current default versions of [Aquarius](https://github.com/oceanprotocol/aquarius), [Provider](https://github.com/oceanprotocol/provider), and [Ganache](https://github.com/trufflesuite/ganache) with [our contracts](https://github.com/oceanprotocol/contracts) deployed to it.
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
git clone https://github.com/oceanprotocol/barge.git
|
|
35
|
+
cd barge/
|
|
36
|
+
./start_ocean.sh --with-provider2 --no-dashboard --with-c2d
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 2. Create a new Node.js project with TypeScript
|
|
40
|
+
|
|
41
|
+
Start by creating a new Node.js project. Open a new terminal and enter the following commands:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
mkdir compute-quickstart
|
|
45
|
+
cd compute-quickstart
|
|
46
|
+
npm init
|
|
47
|
+
# Answer the questions in the command line prompt
|
|
48
|
+
touch compute.ts
|
|
49
|
+
# On linux press CTRL + D to save
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Next, we need to setup our TypeScript compiler options. Create a new file called `tsconfig.json` in the root of the `compute-quickstart` directory.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
touch tsconfig.json
|
|
56
|
+
# Copy the following json content into the file, On linux press CTRL + D to save
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"compilerOptions": {
|
|
62
|
+
"lib": ["es6", "es7"],
|
|
63
|
+
"module": "CommonJS",
|
|
64
|
+
"target": "ES5",
|
|
65
|
+
"esModuleInterop": true,
|
|
66
|
+
"allowSyntheticDefaultImports": true,
|
|
67
|
+
"outDir": "./dist/",
|
|
68
|
+
"declaration": true,
|
|
69
|
+
"declarationDir": "./dist/"
|
|
70
|
+
},
|
|
71
|
+
"include": [
|
|
72
|
+
"compute.ts"
|
|
73
|
+
],
|
|
74
|
+
"exclude": [ "node_modules", "dist" ]
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Now you can compile your TypeScript project.
|
|
79
|
+
If you have TypeScript installed use the following command:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
tsc
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
If you don't have TypeScript installed you can install it using the command below and then compile using the above command:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
npm install -g typescript
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Or if you don't want to install TypeScript use the following command to compile your file:
|
|
92
|
+
```bash
|
|
93
|
+
npx tsc compute.ts
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
To run your script as we go along, compile the script then you can use the following command:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
node dist/compute.js
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## 3. Install dependencies
|
|
103
|
+
|
|
104
|
+
Install dependencies running the following command in your terminal:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
npm install @oceanprotocol/lib crypto-js web3 web3-utils typescript @types/node ts-node
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## 4. Import dependencies and add variables, constants and helper methods
|
|
111
|
+
|
|
112
|
+
Now open the `compute.ts` file in your text editor.
|
|
113
|
+
|
|
114
|
+
### 4.1. Dependencies
|
|
115
|
+
|
|
116
|
+
Start by importing all of the necessary dependencies
|
|
117
|
+
|
|
118
|
+
```Typescript
|
|
119
|
+
import fs from 'fs'
|
|
120
|
+
import { homedir } from 'os'
|
|
121
|
+
|
|
122
|
+
import { SHA256 } from 'crypto-js'
|
|
123
|
+
import Web3 from 'web3'
|
|
124
|
+
import { AbiItem } from 'web3-utils'
|
|
125
|
+
import {
|
|
126
|
+
ProviderInstance,
|
|
127
|
+
Aquarius,
|
|
128
|
+
NftFactory,
|
|
129
|
+
Datatoken,
|
|
130
|
+
Nft,
|
|
131
|
+
ZERO_ADDRESS,
|
|
132
|
+
transfer,
|
|
133
|
+
sleep,
|
|
134
|
+
approveWei,
|
|
135
|
+
ProviderComputeInitialize,
|
|
136
|
+
ConsumeMarketFee,
|
|
137
|
+
ComputeAlgorithm,
|
|
138
|
+
ComputeAsset,
|
|
139
|
+
Config,
|
|
140
|
+
Files,
|
|
141
|
+
DDO,
|
|
142
|
+
NftCreateData,
|
|
143
|
+
DatatokenCreateParams,
|
|
144
|
+
calculateEstimatedGas,
|
|
145
|
+
sendTx,
|
|
146
|
+
configHelperNetworks,
|
|
147
|
+
ConfigHelper
|
|
148
|
+
} from '@oceanprotocol/lib'
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### 4.2. Constants and variables
|
|
153
|
+
|
|
154
|
+
We will need two files to publish, one as dataset and one as algorithm, so here we define the files that we intend to publish.
|
|
155
|
+
```Typescript
|
|
156
|
+
const DATASET_ASSET_URL: Files = {
|
|
157
|
+
datatokenAddress: '0x0',
|
|
158
|
+
nftAddress: '0x0',
|
|
159
|
+
files: [
|
|
160
|
+
{
|
|
161
|
+
type: 'url',
|
|
162
|
+
url: 'https://raw.githubusercontent.com/oceanprotocol/testdatasets/main/shs_dataset_test.txt',
|
|
163
|
+
method: 'GET'
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const ALGORITHM_ASSET_URL: Files = {
|
|
169
|
+
datatokenAddress: '0x0',
|
|
170
|
+
nftAddress: '0x0',
|
|
171
|
+
files: [
|
|
172
|
+
{
|
|
173
|
+
type: 'url',
|
|
174
|
+
url: 'https://raw.githubusercontent.com/oceanprotocol/testdatasets/main/shs_dataset_test.txt',
|
|
175
|
+
method: 'GET'
|
|
176
|
+
}
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Next, we define the metadata for the dataset and algorithm that will describe our data assets. This is what we call the DDOs
|
|
182
|
+
```Typescript
|
|
183
|
+
const DATASET_DDO: DDO = {
|
|
184
|
+
'@context': ['https://w3id.org/did/v1'],
|
|
185
|
+
id: 'id:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
|
|
186
|
+
version: '4.1.0',
|
|
187
|
+
chainId: 5,
|
|
188
|
+
nftAddress: '0x0',
|
|
189
|
+
metadata: {
|
|
190
|
+
created: '2021-12-20T14:35:20Z',
|
|
191
|
+
updated: '2021-12-20T14:35:20Z',
|
|
192
|
+
type: 'dataset',
|
|
193
|
+
name: 'dataset-name',
|
|
194
|
+
description: 'Ocean protocol test dataset description',
|
|
195
|
+
author: 'oceanprotocol-team',
|
|
196
|
+
license: 'https://market.oceanprotocol.com/terms',
|
|
197
|
+
additionalInformation: {
|
|
198
|
+
termsAndConditions: true
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
services: [
|
|
202
|
+
{
|
|
203
|
+
id: 'notAnId',
|
|
204
|
+
type: 'compute',
|
|
205
|
+
files: '',
|
|
206
|
+
datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3',
|
|
207
|
+
serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com/',
|
|
208
|
+
timeout: 300,
|
|
209
|
+
compute: {
|
|
210
|
+
publisherTrustedAlgorithmPublishers: [],
|
|
211
|
+
publisherTrustedAlgorithms: [],
|
|
212
|
+
allowRawAlgorithm: true,
|
|
213
|
+
allowNetworkAccess: true
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
]
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const ALGORITHM_DDO: DDO = {
|
|
220
|
+
'@context': ['https://w3id.org/did/v1'],
|
|
221
|
+
id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
|
|
222
|
+
version: '4.1.0',
|
|
223
|
+
chainId: 5,
|
|
224
|
+
nftAddress: '0x0',
|
|
225
|
+
metadata: {
|
|
226
|
+
created: '2021-12-20T14:35:20Z',
|
|
227
|
+
updated: '2021-12-20T14:35:20Z',
|
|
228
|
+
type: 'algorithm',
|
|
229
|
+
name: 'algorithm-name',
|
|
230
|
+
description: 'Ocean protocol test algorithm description',
|
|
231
|
+
author: 'oceanprotocol-team',
|
|
232
|
+
license: 'https://market.oceanprotocol.com/terms',
|
|
233
|
+
additionalInformation: {
|
|
234
|
+
termsAndConditions: true
|
|
235
|
+
},
|
|
236
|
+
algorithm: {
|
|
237
|
+
language: 'Node.js',
|
|
238
|
+
version: '1.0.0',
|
|
239
|
+
container: {
|
|
240
|
+
entrypoint: 'node $ALGO',
|
|
241
|
+
image: 'ubuntu',
|
|
242
|
+
tag: 'latest',
|
|
243
|
+
checksum:
|
|
244
|
+
'sha256:2d7ecc9c5e08953d586a6e50c29b91479a48f69ac1ba1f9dc0420d18a728dfc5'
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
services: [
|
|
249
|
+
{
|
|
250
|
+
id: 'notAnId',
|
|
251
|
+
type: 'access',
|
|
252
|
+
files: '',
|
|
253
|
+
datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3',
|
|
254
|
+
serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com',
|
|
255
|
+
timeout: 300
|
|
256
|
+
}
|
|
257
|
+
]
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Now we define the variables which we will need later
|
|
262
|
+
```Typescript
|
|
263
|
+
let web3: Web3
|
|
264
|
+
let config: Config
|
|
265
|
+
let aquarius: Aquarius
|
|
266
|
+
let datatoken: Datatoken
|
|
267
|
+
let providerUrl: string
|
|
268
|
+
let publisherAccount: string
|
|
269
|
+
let consumerAccount: string
|
|
270
|
+
let addresses
|
|
271
|
+
let computeEnvs
|
|
272
|
+
|
|
273
|
+
let datasetId: string
|
|
274
|
+
let algorithmId: string
|
|
275
|
+
let resolvedDatasetDdo: DDO
|
|
276
|
+
let resolvedAlgorithmDdo: DDO
|
|
277
|
+
|
|
278
|
+
let computeJobId: string
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### 4.3 Helper methods
|
|
282
|
+
|
|
283
|
+
Now we define the helper methods which we will use later to publish the dataset and algorithm, and also order them
|
|
284
|
+
|
|
285
|
+
Add a `createAsset()`function.
|
|
286
|
+
```Typescript
|
|
287
|
+
async function createAsset(
|
|
288
|
+
name: string,
|
|
289
|
+
symbol: string,
|
|
290
|
+
owner: string,
|
|
291
|
+
assetUrl: Files,
|
|
292
|
+
ddo: DDO,
|
|
293
|
+
providerUrl: string
|
|
294
|
+
) {
|
|
295
|
+
const nft = new Nft(web3)
|
|
296
|
+
const Factory = new NftFactory(addresses.ERC721Factory, web3)
|
|
297
|
+
|
|
298
|
+
// Now we update the DDO and set the right did
|
|
299
|
+
const chain = await web3.eth.getChainId()
|
|
300
|
+
ddo.chainId = parseInt(chain.toString(10))
|
|
301
|
+
const nftParamsAsset: NftCreateData = {
|
|
302
|
+
name,
|
|
303
|
+
symbol,
|
|
304
|
+
templateIndex: 1,
|
|
305
|
+
tokenURI: 'aaa',
|
|
306
|
+
transferable: true,
|
|
307
|
+
owner
|
|
308
|
+
}
|
|
309
|
+
const datatokenParams: DatatokenCreateParams = {
|
|
310
|
+
templateIndex: 1,
|
|
311
|
+
cap: '100000',
|
|
312
|
+
feeAmount: '0',
|
|
313
|
+
paymentCollector: ZERO_ADDRESS,
|
|
314
|
+
feeToken: ZERO_ADDRESS,
|
|
315
|
+
minter: owner,
|
|
316
|
+
mpFeeAddress: ZERO_ADDRESS
|
|
317
|
+
}
|
|
318
|
+
// Now we can make the contract call createNftWithDatatoken
|
|
319
|
+
const result = await Factory.createNftWithDatatoken(
|
|
320
|
+
owner,
|
|
321
|
+
nftParamsAsset,
|
|
322
|
+
datatokenParams
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
const nftAddress = result.events.NFTCreated.returnValues[0]
|
|
326
|
+
const datatokenAddressAsset = result.events.TokenCreated.returnValues[0]
|
|
327
|
+
ddo.nftAddress = web3.utils.toChecksumAddress(nftAddress)
|
|
328
|
+
|
|
329
|
+
// Next we encrypt the file or files using Ocean Provider. The provider is an off chain proxy built specifically for this task
|
|
330
|
+
assetUrl.datatokenAddress = datatokenAddressAsset
|
|
331
|
+
assetUrl.nftAddress = ddo.nftAddress
|
|
332
|
+
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
|
|
333
|
+
ddo.services[0].files = await providerResponse
|
|
334
|
+
ddo.services[0].datatokenAddress = datatokenAddressAsset
|
|
335
|
+
ddo.services[0].serviceEndpoint = providerUrl
|
|
336
|
+
|
|
337
|
+
// Next we update ddo and set the right did
|
|
338
|
+
ddo.nftAddress = web3.utils.toChecksumAddress(nftAddress)
|
|
339
|
+
ddo.id =
|
|
340
|
+
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
|
341
|
+
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl)
|
|
342
|
+
const encryptedResponse = await providerResponse
|
|
343
|
+
const validateResult = await aquarius.validate(ddo)
|
|
344
|
+
|
|
345
|
+
// Next you can check if if the ddo is valid by checking if validateResult.valid returned true
|
|
346
|
+
|
|
347
|
+
await nft.setMetadata(
|
|
348
|
+
nftAddress,
|
|
349
|
+
owner,
|
|
350
|
+
0,
|
|
351
|
+
providerUrl,
|
|
352
|
+
'',
|
|
353
|
+
'0x2',
|
|
354
|
+
encryptedResponse,
|
|
355
|
+
validateResult.hash
|
|
356
|
+
)
|
|
357
|
+
return ddo.id
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Add a `handleOrder()`function.
|
|
362
|
+
```Typescript
|
|
363
|
+
async function handleOrder(
|
|
364
|
+
order: ProviderComputeInitialize,
|
|
365
|
+
datatokenAddress: string,
|
|
366
|
+
payerAccount: string,
|
|
367
|
+
consumerAccount: string,
|
|
368
|
+
serviceIndex: number,
|
|
369
|
+
consumeMarkerFee?: ConsumeMarketFee
|
|
370
|
+
) {
|
|
371
|
+
/* We do have 3 possible situations:
|
|
372
|
+
- have validOrder and no providerFees -> then order is valid, providerFees are valid, just use it in startCompute
|
|
373
|
+
- have validOrder and providerFees -> then order is valid but providerFees are not valid, we need to call reuseOrder and pay only providerFees
|
|
374
|
+
- no validOrder -> we need to call startOrder, to pay 1 DT & providerFees
|
|
375
|
+
*/
|
|
376
|
+
if (order.providerFee && order.providerFee.providerFeeAmount) {
|
|
377
|
+
await approveWei(
|
|
378
|
+
web3,
|
|
379
|
+
config,
|
|
380
|
+
payerAccount,
|
|
381
|
+
order.providerFee.providerFeeToken,
|
|
382
|
+
datatokenAddress,
|
|
383
|
+
order.providerFee.providerFeeAmount
|
|
384
|
+
)
|
|
385
|
+
}
|
|
386
|
+
if (order.validOrder) {
|
|
387
|
+
if (!order.providerFee) return order.validOrder
|
|
388
|
+
const tx = await datatoken.reuseOrder(
|
|
389
|
+
datatokenAddress,
|
|
390
|
+
payerAccount,
|
|
391
|
+
order.validOrder,
|
|
392
|
+
order.providerFee
|
|
393
|
+
)
|
|
394
|
+
return tx.transactionHash
|
|
395
|
+
}
|
|
396
|
+
const tx = await datatoken.startOrder(
|
|
397
|
+
datatokenAddress,
|
|
398
|
+
payerAccount,
|
|
399
|
+
consumerAccount,
|
|
400
|
+
serviceIndex,
|
|
401
|
+
order.providerFee,
|
|
402
|
+
consumeMarkerFee
|
|
403
|
+
)
|
|
404
|
+
return tx.transactionHash
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
At the end of your compute.ts file define `async function run(){ }`. We will use this function to add and test the following chunks of code.
|
|
409
|
+
<!--
|
|
410
|
+
describe('Compute-to-data example tests
|
|
411
|
+
-->
|
|
412
|
+
|
|
413
|
+
We need to load the configuration. Add the following code into your `run(){ }` function
|
|
414
|
+
```Typescript
|
|
415
|
+
|
|
416
|
+
web3 = new Web3(process.env.NODE_URI || configHelperNetworks[1].nodeUri)
|
|
417
|
+
config = new ConfigHelper().getConfig(await web3.eth.getChainId())
|
|
418
|
+
config.providerUri = process.env.PROVIDER_URL || config.providerUri
|
|
419
|
+
addresses = JSON.parse(
|
|
420
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
|
421
|
+
fs.readFileSync(
|
|
422
|
+
process.env.ADDRESS_FILE ||
|
|
423
|
+
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
|
|
424
|
+
'utf8'
|
|
425
|
+
)
|
|
426
|
+
).development
|
|
427
|
+
aquarius = new Aquarius(config.metadataCacheUri)
|
|
428
|
+
providerUrl = config.providerUri
|
|
429
|
+
datatoken = new Datatoken(web3)
|
|
430
|
+
```
|
|
431
|
+
As we go along it's a good idea to console log the values so that you check they are right. At the end of your `run(){ ... }` function add the following logs:
|
|
432
|
+
```Typescript
|
|
433
|
+
console.log(`Aquarius URL: ${config.metadataCacheUri}`)
|
|
434
|
+
console.log(`Provider URL: ${providerUrl}`)
|
|
435
|
+
console.log(`Deployed contracts address: ${addresses}`)
|
|
436
|
+
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
Now at the end of your compute.ts file call you `run()` function. Next, let's compile the file with the `tsc` command in the console and run `node dist/compute.js`.
|
|
440
|
+
If everything is working you should see the logs in the console and no errors.
|
|
441
|
+
## 5. Initialize accounts
|
|
442
|
+
### 5.1 Initialize accounts
|
|
443
|
+
We will use all of the following code snippets in the same way. Add the code snippet and the logs to the end of your `run(){ ... }` function as well as the logs.
|
|
444
|
+
Then compile your file with the `tsc` command and run it with `node dist/compute.js`
|
|
445
|
+
```Typescript
|
|
446
|
+
const accounts = await web3.eth.getAccounts()
|
|
447
|
+
publisherAccount = accounts[0]
|
|
448
|
+
consumerAccount = accounts[1]
|
|
449
|
+
```
|
|
450
|
+
Again, lets console log the values so that we can check that they have been saved properly
|
|
451
|
+
```Typescript
|
|
452
|
+
console.log(`Publisher account address: ${publisherAccount}`)
|
|
453
|
+
console.log(`Consumer account address: ${consumerAccount}`)
|
|
454
|
+
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### 5.2 Mint OCEAN to publisher account
|
|
458
|
+
You can skip this step if you are running your script against a remote network,
|
|
459
|
+
you need to mint oceans to mentioned accounts only if you are using barge to test your script
|
|
460
|
+
|
|
461
|
+
```Typescript
|
|
462
|
+
const minAbi = [
|
|
463
|
+
{
|
|
464
|
+
constant: false,
|
|
465
|
+
inputs: [
|
|
466
|
+
{ name: 'to', type: 'address' },
|
|
467
|
+
{ name: 'value', type: 'uint256' }
|
|
468
|
+
],
|
|
469
|
+
name: 'mint',
|
|
470
|
+
outputs: [{ name: '', type: 'bool' }],
|
|
471
|
+
payable: false,
|
|
472
|
+
stateMutability: 'nonpayable',
|
|
473
|
+
type: 'function'
|
|
474
|
+
}
|
|
475
|
+
] as AbiItem[]
|
|
476
|
+
const tokenContract = new web3.eth.Contract(minAbi, addresses.Ocean)
|
|
477
|
+
const estGas = await calculateEstimatedGas(
|
|
478
|
+
publisherAccount,
|
|
479
|
+
tokenContract.methods.mint,
|
|
480
|
+
publisherAccount,
|
|
481
|
+
web3.utils.toWei('1000')
|
|
482
|
+
)
|
|
483
|
+
await sendTx(
|
|
484
|
+
publisherAccount,
|
|
485
|
+
estGas,
|
|
486
|
+
web3,
|
|
487
|
+
1,
|
|
488
|
+
tokenContract.methods.mint,
|
|
489
|
+
publisherAccount,
|
|
490
|
+
web3.utils.toWei('1000')
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
### 5.3 Send some OCEAN to consumer account
|
|
496
|
+
```Typescript
|
|
497
|
+
transfer(web3, config, publisherAccount, addresses.Ocean, consumerAccount, '100')
|
|
498
|
+
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
## 6. Publish assets dataset and algorithm
|
|
502
|
+
|
|
503
|
+
### 6.1 Publish a dataset (create NFT + Datatoken) and set dataset metadata
|
|
504
|
+
```Typescript
|
|
505
|
+
datasetId = await createAsset(
|
|
506
|
+
'D1Min',
|
|
507
|
+
'D1M',
|
|
508
|
+
publisherAccount,
|
|
509
|
+
DATASET_ASSET_URL,
|
|
510
|
+
DATASET_DDO,
|
|
511
|
+
providerUrl
|
|
512
|
+
)
|
|
513
|
+
```
|
|
514
|
+
Now, let's check that we successfully published a dataset (create NFT + Datatoken)
|
|
515
|
+
```Typescript
|
|
516
|
+
console.log(`dataset id: ${datasetId}`)
|
|
517
|
+
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
### 6.2 Publish an algorithm (create NFT + Datatoken) and set algorithm metadata
|
|
521
|
+
```Typescript
|
|
522
|
+
algorithmId = await createAsset(
|
|
523
|
+
'D1Min',
|
|
524
|
+
'D1M',
|
|
525
|
+
publisherAccount,
|
|
526
|
+
ALGORITHM_ASSET_URL,
|
|
527
|
+
ALGORITHM_DDO,
|
|
528
|
+
providerUrl
|
|
529
|
+
)
|
|
530
|
+
```
|
|
531
|
+
Now, let's check that we successfully published a algorithm (create NFT + Datatoken)
|
|
532
|
+
```Typescript
|
|
533
|
+
console.log(`algorithm id: ${algorithmId}`)
|
|
534
|
+
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
## 7. Resolve assets
|
|
538
|
+
|
|
539
|
+
### 7.1 Resolve published datasets and algorithms
|
|
540
|
+
```Typescript
|
|
541
|
+
resolvedDatasetDdo = await aquarius.waitForAqua(datasetId)
|
|
542
|
+
resolvedAlgorithmDdo = await aquarius.waitForAqua(algorithmId)
|
|
543
|
+
```
|
|
544
|
+
<!--
|
|
545
|
+
assert(resolvedDatasetDdo, 'Cannot fetch DDO from Aquarius')
|
|
546
|
+
assert(resolvedAlgorithmDdo, 'Cannot fetch DDO from Aquarius')
|
|
547
|
+
-->
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
## 8. Send datatokens to consumer
|
|
551
|
+
|
|
552
|
+
### 8.1 Mint dataset and algorithm datatokens to publisher
|
|
553
|
+
```Typescript
|
|
554
|
+
await datatoken.mint(
|
|
555
|
+
resolvedDatasetDdo.services[0].datatokenAddress,
|
|
556
|
+
publisherAccount,
|
|
557
|
+
'10',
|
|
558
|
+
consumerAccount
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
await datatoken.mint(
|
|
562
|
+
resolvedAlgorithmDdo.services[0].datatokenAddress,
|
|
563
|
+
publisherAccount,
|
|
564
|
+
'10',
|
|
565
|
+
consumerAccount
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
## 9. Get compute environments
|
|
571
|
+
|
|
572
|
+
### 9.1 Fetch compute environments from provider
|
|
573
|
+
```Typescript
|
|
574
|
+
computeEnvs = await ProviderInstance.getComputeEnvironments(providerUrl)
|
|
575
|
+
```
|
|
576
|
+
<!--
|
|
577
|
+
assert(computeEnvs, 'No Compute environments found')
|
|
578
|
+
-->
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
## 10. Consumer starts a compute job
|
|
582
|
+
|
|
583
|
+
### 10.1 Start a compute job using a free C2D environment
|
|
584
|
+
let's check the free compute environment
|
|
585
|
+
```Typescript
|
|
586
|
+
const computeEnv = computeEnvs.find((ce) => ce.priceMin === 0)
|
|
587
|
+
console.log('Free compute environment = ', computeEnv)
|
|
588
|
+
```
|
|
589
|
+
<!--
|
|
590
|
+
assert(computeEnv, 'Cannot find the free compute env')
|
|
591
|
+
-->
|
|
592
|
+
|
|
593
|
+
Let's have 5 minute of compute access
|
|
594
|
+
```Typescript
|
|
595
|
+
const mytime = new Date()
|
|
596
|
+
const computeMinutes = 5
|
|
597
|
+
mytime.setMinutes(mytime.getMinutes() + computeMinutes)
|
|
598
|
+
const computeValidUntil = Math.floor(mytime.getTime() / 1000)
|
|
599
|
+
|
|
600
|
+
const assets: ComputeAsset[] = [
|
|
601
|
+
{
|
|
602
|
+
documentId: resolvedDatasetDdo.id,
|
|
603
|
+
serviceId: resolvedDatasetDdo.services[0].id
|
|
604
|
+
}
|
|
605
|
+
]
|
|
606
|
+
const dtAddressArray = [resolvedDatasetDdo.services[0].datatokenAddress]
|
|
607
|
+
const algo: ComputeAlgorithm = {
|
|
608
|
+
documentId: resolvedAlgorithmDdo.id,
|
|
609
|
+
serviceId: resolvedAlgorithmDdo.services[0].id
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
const providerInitializeComputeResults = await ProviderInstance.initializeCompute(
|
|
613
|
+
assets,
|
|
614
|
+
algo,
|
|
615
|
+
computeEnv.id,
|
|
616
|
+
computeValidUntil,
|
|
617
|
+
providerUrl,
|
|
618
|
+
consumerAccount
|
|
619
|
+
)
|
|
620
|
+
```
|
|
621
|
+
<!--
|
|
622
|
+
assert(!('error' in providerInitializeComputeResults), 'Cannot order algorithm')
|
|
623
|
+
-->
|
|
624
|
+
```Typescript
|
|
625
|
+
algo.transferTxId = await handleOrder(
|
|
626
|
+
providerInitializeComputeResults.algorithm,
|
|
627
|
+
resolvedAlgorithmDdo.services[0].datatokenAddress,
|
|
628
|
+
consumerAccount,
|
|
629
|
+
computeEnv.consumerAddress,
|
|
630
|
+
0
|
|
631
|
+
)
|
|
632
|
+
for (let i = 0; i < providerInitializeComputeResults.datasets.length; i++) {
|
|
633
|
+
assets[i].transferTxId = await handleOrder(
|
|
634
|
+
providerInitializeComputeResults.datasets[i],
|
|
635
|
+
dtAddressArray[i],
|
|
636
|
+
consumerAccount,
|
|
637
|
+
computeEnv.consumerAddress,
|
|
638
|
+
0
|
|
639
|
+
)
|
|
640
|
+
}
|
|
641
|
+
const computeJobs = await ProviderInstance.computeStart(
|
|
642
|
+
providerUrl,
|
|
643
|
+
web3,
|
|
644
|
+
consumerAccount,
|
|
645
|
+
computeEnv.id,
|
|
646
|
+
assets[0],
|
|
647
|
+
algo
|
|
648
|
+
)
|
|
649
|
+
```
|
|
650
|
+
<!--
|
|
651
|
+
assert(computeJobs, 'Cannot start compute job')
|
|
652
|
+
-->
|
|
653
|
+
Let's save the compute job it, we re going to use later
|
|
654
|
+
```Typescript
|
|
655
|
+
computeJobId = computeJobs[0].jobId
|
|
656
|
+
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
## 11. Check compute status and get download compute results URL
|
|
660
|
+
### 11.1 Check compute status
|
|
661
|
+
You can also add various delays so you see the various states of the compute job
|
|
662
|
+
```Typescript
|
|
663
|
+
const jobStatus = await ProviderInstance.computeStatus(
|
|
664
|
+
providerUrl,
|
|
665
|
+
consumerAccount,
|
|
666
|
+
computeJobId,
|
|
667
|
+
DATASET_DDO.id
|
|
668
|
+
)
|
|
669
|
+
```
|
|
670
|
+
<!--
|
|
671
|
+
assert(jobStatus, 'Cannot retrieve compute status!')
|
|
672
|
+
-->
|
|
673
|
+
Now, let's see the current status of the previously started computer job
|
|
674
|
+
```Typescript
|
|
675
|
+
console.log('Current status of the compute job: ', jobStatus)
|
|
676
|
+
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
### 11.2 Get download compute results URL
|
|
680
|
+
```Typescript
|
|
681
|
+
await sleep(10000)
|
|
682
|
+
const downloadURL = await ProviderInstance.getComputeResultUrl(
|
|
683
|
+
providerUrl,
|
|
684
|
+
web3,
|
|
685
|
+
consumerAccount,
|
|
686
|
+
computeJobId,
|
|
687
|
+
0
|
|
688
|
+
)
|
|
689
|
+
```
|
|
690
|
+
<!--
|
|
691
|
+
assert(downloadURL, 'Provider getComputeResultUrl failed!')
|
|
692
|
+
-->
|
|
693
|
+
Let's check the compute results url for the specified index
|
|
694
|
+
```Typescript
|
|
695
|
+
console.log(`Compute results URL: ${downloadURL}`)
|
|
696
|
+
|
|
697
|
+
```
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
## Editing this file
|
|
701
|
+
Please note that ComputeExamples.md is an autogenerated file, you should not edit it directly.
|
|
702
|
+
Updates should be done in `test/integration/ComputeExamples.test.ts` and all markdown should have three forward slashes before it
|
|
703
|
+
e.g. `/// # H1 Title`
|
package/README.md
CHANGED
|
@@ -46,7 +46,7 @@ This is in alpha state. If you run into problems, please open up a [new issue](h
|
|
|
46
46
|
npm install @oceanprotocol/lib
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
- Checkout our [code examples](CodeExamples.md) to see how you can use ocean.js.
|
|
49
|
+
- Checkout our [code examples](CodeExamples.md) or [compute to data examples](C2DExamples.md) to see how you can use ocean.js.
|
|
50
50
|
- Refer to the [Ocean Protocol documentation](https://docs.oceanprotocol.com/) for more guides and tutorials.
|
|
51
51
|
- Visit the [Ocean Protocol website](https://docs.oceanprotocol.com/) for general information about Ocean Protocol.
|
|
52
52
|
- If you have any difficulties or if you have further questions about how to use ocean.js please reach out to us on [Discord](https://discord.gg/TnXjkR5).
|
|
@@ -85,7 +85,7 @@ Running all tests requires running Ocean Protocol components beforehand with [Ba
|
|
|
85
85
|
git clone https://github.com/oceanprotocol/barge
|
|
86
86
|
cd barge
|
|
87
87
|
|
|
88
|
-
./start_ocean.sh --with-provider2 --no-dashboard
|
|
88
|
+
./start_ocean.sh --with-provider2 --no-dashboard --with-c2d
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
You can then proceed to run in another terminal.
|