@oceanprotocol/lib 2.7.0 → 3.0.0-next.1

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/CodeExamples.md +273 -166
  3. package/ComputeExamples.md +106 -101
  4. package/dist/lib.js +1 -1
  5. package/dist/lib.js.map +1 -1
  6. package/dist/lib.modern.js +1 -1
  7. package/dist/lib.modern.js.map +1 -1
  8. package/dist/lib.module.js +1 -1
  9. package/dist/lib.module.js.map +1 -1
  10. package/dist/lib.umd.js +1 -1
  11. package/dist/lib.umd.js.map +1 -1
  12. package/dist/src/@types/Contracts.d.ts +26 -0
  13. package/dist/src/@types/DDO/Metadata.d.ts +2 -2
  14. package/dist/src/@types/File.d.ts +1 -1
  15. package/dist/src/@types/ReturnTypes.d.ts +3 -2
  16. package/dist/src/@types/index.d.ts +1 -0
  17. package/dist/src/config/Config.d.ts +1 -1
  18. package/dist/src/config/ConfigHelper.d.ts +6 -0
  19. package/dist/src/contracts/Datatoken.d.ts +90 -72
  20. package/dist/src/contracts/Dispenser.d.ts +27 -27
  21. package/dist/src/contracts/FixedRateExchange.d.ts +57 -60
  22. package/dist/src/contracts/NFT.d.ts +57 -34
  23. package/dist/src/contracts/NFTFactory.d.ts +78 -57
  24. package/dist/src/contracts/Router.d.ts +33 -18
  25. package/dist/src/contracts/SmartContract.d.ts +35 -12
  26. package/dist/src/contracts/SmartContractWithAddress.d.ts +7 -8
  27. package/dist/src/contracts/df/DfRewards.d.ts +6 -7
  28. package/dist/src/contracts/df/DfStrategyV1.d.ts +3 -5
  29. package/dist/src/contracts/ve/VeAllocate.d.ts +4 -7
  30. package/dist/src/contracts/ve/VeFeeDistributor.d.ts +6 -7
  31. package/dist/src/contracts/ve/VeFeeEstimate.d.ts +2 -2
  32. package/dist/src/contracts/ve/VeOcean.d.ts +12 -13
  33. package/dist/src/services/Aquarius.d.ts +3 -4
  34. package/dist/src/services/Provider.d.ts +124 -76
  35. package/dist/src/utils/ContractUtils.d.ts +36 -16
  36. package/dist/src/utils/DatatokenName.d.ts +5 -2
  37. package/dist/src/utils/DdoHelpers.d.ts +11 -0
  38. package/dist/src/utils/FetchHelper.d.ts +11 -0
  39. package/dist/src/utils/General.d.ts +1 -0
  40. package/dist/src/utils/ProviderErrors.d.ts +1 -0
  41. package/dist/src/utils/SignatureUtils.d.ts +8 -2
  42. package/dist/src/utils/TokenUtils.d.ts +43 -43
  43. package/dist/src/utils/index.d.ts +1 -0
  44. package/dist/src/utils/minAbi.d.ts +38 -2
  45. package/dist/test/config.d.ts +17 -3
  46. package/dist/test/integration/helpers.d.ts +7 -0
  47. package/package.json +10 -5
  48. package/dist/test/TestContractHandler.d.ts +0 -16
  49. /package/dist/test/integration/{SimplePublishConsumeFlow.test.d.ts → PublishEditConsume.test.d.ts} +0 -0
@@ -2,18 +2,18 @@
2
2
 
3
3
  Here are the steps:
4
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)
5
+ 0. [Prerequisites](#0-prerequisites)
6
+ 1. [Initialize services](#1-initialize-services)
7
+ 2. [Create a new node.js project](#2-create-a-new-nodejs-project-with-typescript)
8
+ 3. [Install dependencies](#3-install-dependencies)
9
+ 4. [Import dependencies and add variables and constants](#4-import-dependencies-and-add-variables-constants-and-helper-methods)
10
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)
11
+ 6. [Publish a dataset and an algorithm](#6-publish-assets-dataset-and-algorithm)
12
+ 7. [Resolve published datasets and algorithms](#7-resolve-assets)
13
+ 8. [Send datatokens to consumer](#8-send-datatokens-to-consumer)
14
+ 9. [Consumer fetches compute environment](#9-get-compute-environments)
15
+ 10. [Consumer starts a compute job using a free C2D environment](#10-consumer-starts-a-compute-job)
16
+ 11. [Check compute status and get download compute results url](#11-check-compute-status-and-get-download-compute-results-url)
17
17
 
18
18
  Let's go through each step.
19
19
 
@@ -104,7 +104,7 @@ node dist/compute.js
104
104
  Install dependencies running the following command in your terminal:
105
105
 
106
106
  ```bash
107
- npm install @oceanprotocol/lib crypto-js web3 web3-utils typescript @types/node ts-node
107
+ npm install @oceanprotocol/lib crypto-js ethers typescript @types/node ts-node
108
108
  ```
109
109
 
110
110
  ## 4. Import dependencies and add variables, constants and helper methods
@@ -120,8 +120,7 @@ import fs from 'fs'
120
120
  import { homedir } from 'os'
121
121
 
122
122
  import { SHA256 } from 'crypto-js'
123
- import Web3 from 'web3'
124
- import { AbiItem } from 'web3-utils'
123
+ import { ethers, providers, Signer } from 'ethers'
125
124
  import {
126
125
  ProviderInstance,
127
126
  Aquarius,
@@ -141,12 +140,12 @@ import {
141
140
  DDO,
142
141
  NftCreateData,
143
142
  DatatokenCreateParams,
144
- calculateEstimatedGas,
145
143
  sendTx,
146
144
  configHelperNetworks,
147
- ConfigHelper
145
+ ConfigHelper,
146
+ getEventFromTx,
147
+ amountToUnits
148
148
  } from '@oceanprotocol/lib'
149
-
150
149
  ```
151
150
 
152
151
  ### 4.2. Constants and variables
@@ -260,13 +259,12 @@ const ALGORITHM_DDO: DDO = {
260
259
 
261
260
  Now we define the variables which we will need later
262
261
  ```Typescript
263
- let web3: Web3
264
262
  let config: Config
265
- let aquarius: Aquarius
263
+ let aquariusInstance: Aquarius
266
264
  let datatoken: Datatoken
267
265
  let providerUrl: string
268
- let publisherAccount: string
269
- let consumerAccount: string
266
+ let publisherAccount: Signer
267
+ let consumerAccount: Signer
270
268
  let addresses
271
269
  let computeEnvs
272
270
 
@@ -287,16 +285,17 @@ Add a `createAsset()`function.
287
285
  async function createAsset(
288
286
  name: string,
289
287
  symbol: string,
290
- owner: string,
288
+ owner: Signer,
291
289
  assetUrl: Files,
292
290
  ddo: DDO,
293
291
  providerUrl: string
294
292
  ) {
295
- const nft = new Nft(web3)
296
- const Factory = new NftFactory(addresses.ERC721Factory, web3)
293
+ const nft = new Nft(owner, (await owner.provider.getNetwork()).chainId)
294
+
295
+ const nftFactory = new NftFactory(addresses.ERC721Factory, owner)
296
+
297
+ const chain = (await owner.provider.getNetwork()).chainId
297
298
 
298
- // Now we update the DDO and set the right did
299
- const chain = await web3.eth.getChainId()
300
299
  ddo.chainId = parseInt(chain.toString(10))
301
300
  const nftParamsAsset: NftCreateData = {
302
301
  name,
@@ -304,7 +303,7 @@ async function createAsset(
304
303
  templateIndex: 1,
305
304
  tokenURI: 'aaa',
306
305
  transferable: true,
307
- owner
306
+ owner: await owner.getAddress()
308
307
  }
309
308
  const datatokenParams: DatatokenCreateParams = {
310
309
  templateIndex: 1,
@@ -312,45 +311,41 @@ async function createAsset(
312
311
  feeAmount: '0',
313
312
  paymentCollector: ZERO_ADDRESS,
314
313
  feeToken: ZERO_ADDRESS,
315
- minter: owner,
314
+ minter: await owner.getAddress(),
316
315
  mpFeeAddress: ZERO_ADDRESS
317
316
  }
318
- // Now we can make the contract call createNftWithDatatoken
319
- const result = await Factory.createNftWithDatatoken(
320
- owner,
317
+
318
+ const bundleNFT = await nftFactory.createNftWithDatatoken(
321
319
  nftParamsAsset,
322
320
  datatokenParams
323
321
  )
324
322
 
325
- const nftAddress = result.events.NFTCreated.returnValues[0]
326
- const datatokenAddressAsset = result.events.TokenCreated.returnValues[0]
327
- ddo.nftAddress = web3.utils.toChecksumAddress(nftAddress)
323
+ const trxReceipt = await bundleNFT.wait()
324
+ // events have been emitted
325
+ const nftCreatedEvent = getEventFromTx(trxReceipt, 'NFTCreated')
326
+ const tokenCreatedEvent = getEventFromTx(trxReceipt, 'TokenCreated')
328
327
 
329
- // Next we encrypt the file or files using Ocean Provider. The provider is an off chain proxy built specifically for this task
328
+ const nftAddress = nftCreatedEvent.args.newTokenAddress
329
+ const datatokenAddressAsset = tokenCreatedEvent.args.newTokenAddress
330
+ // create the files encrypted string
330
331
  assetUrl.datatokenAddress = datatokenAddressAsset
331
- assetUrl.nftAddress = ddo.nftAddress
332
- let providerResponse = await ProviderInstance.encrypt(assetUrl, chain, providerUrl)
333
- ddo.services[0].files = await providerResponse
332
+ assetUrl.nftAddress = nftAddress
333
+ ddo.services[0].files = await ProviderInstance.encrypt(assetUrl, chain, providerUrl)
334
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, chain, providerUrl)
342
- const encryptedResponse = await providerResponse
343
- const validateResult = await aquarius.validate(ddo)
335
+ ddo.services[0].serviceEndpoint = 'http://172.15.0.4:8030' // put back proviederUrl
344
336
 
345
- // Next you can check if if the ddo is valid by checking if validateResult.valid returned true
337
+ ddo.nftAddress = nftAddress
338
+ ddo.id = 'did:op:' + SHA256(ethers.utils.getAddress(nftAddress) + chain.toString(10))
346
339
 
340
+ const encryptedResponse = await ProviderInstance.encrypt(ddo, chain, providerUrl)
341
+ const validateResult = await aquariusInstance.validate(ddo)
347
342
  await nft.setMetadata(
348
343
  nftAddress,
349
- owner,
344
+ await owner.getAddress(),
350
345
  0,
351
- providerUrl,
346
+ 'http://172.15.0.4:8030', // put back proviederUrl
352
347
  '',
353
- '0x2',
348
+ ethers.utils.hexlify(2),
354
349
  encryptedResponse,
355
350
  validateResult.hash
356
351
  )
@@ -363,7 +358,7 @@ Add a `handleOrder()`function.
363
358
  async function handleOrder(
364
359
  order: ProviderComputeInitialize,
365
360
  datatokenAddress: string,
366
- payerAccount: string,
361
+ payerAccount: Signer,
367
362
  consumerAccount: string,
368
363
  serviceIndex: number,
369
364
  consumeMarkerFee?: ConsumeMarketFee
@@ -375,9 +370,9 @@ async function handleOrder(
375
370
  */
376
371
  if (order.providerFee && order.providerFee.providerFeeAmount) {
377
372
  await approveWei(
378
- web3,
379
- config,
380
373
  payerAccount,
374
+ config,
375
+ await payerAccount.getAddress(),
381
376
  order.providerFee.providerFeeToken,
382
377
  datatokenAddress,
383
378
  order.providerFee.providerFeeAmount
@@ -387,21 +382,23 @@ async function handleOrder(
387
382
  if (!order.providerFee) return order.validOrder
388
383
  const tx = await datatoken.reuseOrder(
389
384
  datatokenAddress,
390
- payerAccount,
391
385
  order.validOrder,
392
386
  order.providerFee
393
387
  )
394
- return tx.transactionHash
388
+ const reusedTx = await tx.wait()
389
+ const orderReusedTx = getEventFromTx(reusedTx, 'OrderReused')
390
+ return orderReusedTx.transactionHash
395
391
  }
396
392
  const tx = await datatoken.startOrder(
397
393
  datatokenAddress,
398
- payerAccount,
399
394
  consumerAccount,
400
395
  serviceIndex,
401
396
  order.providerFee,
402
397
  consumeMarkerFee
403
398
  )
404
- return tx.transactionHash
399
+ const orderTx = await tx.wait()
400
+ const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted')
401
+ return orderStartedTx.transactionHash
405
402
  }
406
403
  ```
407
404
 
@@ -413,9 +410,17 @@ describe('Compute-to-data example tests
413
410
  We need to load the configuration. Add the following code into your `run(){ }` function
414
411
  ```Typescript
415
412
 
416
- web3 = new Web3(process.env.NODE_URI || configHelperNetworks[1].nodeUri)
417
- config = new ConfigHelper().getConfig(await web3.eth.getChainId())
413
+ const provider = new providers.JsonRpcProvider(
414
+ process.env.NODE_URI || configHelperNetworks[1].nodeUri
415
+ )
416
+ publisherAccount = (await provider.getSigner(0)) as Signer
417
+ consumerAccount = (await provider.getSigner(1)) as Signer
418
+ const config = new ConfigHelper().getConfig(
419
+ parseInt(String((await publisherAccount.provider.getNetwork()).chainId))
420
+ )
418
421
  config.providerUri = process.env.PROVIDER_URL || config.providerUri
422
+ aquariusInstance = new Aquarius(config?.metadataCacheUri)
423
+ providerUrl = config?.providerUri
419
424
  addresses = JSON.parse(
420
425
  // eslint-disable-next-line security/detect-non-literal-fs-filename
421
426
  fs.readFileSync(
@@ -424,37 +429,24 @@ We need to load the configuration. Add the following code into your `run(){ }` f
424
429
  'utf8'
425
430
  )
426
431
  ).development
427
- aquarius = new Aquarius(config.metadataCacheUri)
428
- providerUrl = config.providerUri
429
- datatoken = new Datatoken(web3)
432
+
430
433
  ```
431
434
  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
435
  ```Typescript
433
436
  console.log(`Aquarius URL: ${config.metadataCacheUri}`)
434
437
  console.log(`Provider URL: ${providerUrl}`)
435
438
  console.log(`Deployed contracts address: ${addresses}`)
439
+ console.log(`Publisher account address: ${publisherAccount}`)
440
+ console.log(`Consumer account address: ${consumerAccount}`)
436
441
 
437
442
  ```
438
443
 
439
444
  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
445
  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
446
  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
447
  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
448
 
457
- ### 5.2 Mint OCEAN to publisher account
449
+ ### 5.1 Mint OCEAN to publisher account
458
450
  You can skip this step if you are running your script against a remote network,
459
451
  you need to mint oceans to mentioned accounts only if you are using barge to test your script
460
452
 
@@ -472,29 +464,33 @@ you need to mint oceans to mentioned accounts only if you are using barge to tes
472
464
  stateMutability: 'nonpayable',
473
465
  type: 'function'
474
466
  }
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')
467
+ ]
468
+
469
+ const tokenContract = new ethers.Contract(addresses.Ocean, minAbi, publisherAccount)
470
+ const estGasPublisher = await tokenContract.estimateGas.mint(
471
+ await publisherAccount.getAddress(),
472
+ amountToUnits(null, null, '1000', 18)
482
473
  )
483
474
  await sendTx(
475
+ estGasPublisher,
484
476
  publisherAccount,
485
- estGas,
486
- web3,
487
477
  1,
488
- tokenContract.methods.mint,
489
- publisherAccount,
490
- web3.utils.toWei('1000')
478
+ tokenContract.mint,
479
+ await publisherAccount.getAddress(),
480
+ amountToUnits(null, null, '1000', 18)
491
481
  )
492
482
 
493
483
  ```
494
484
 
495
- ### 5.3 Send some OCEAN to consumer account
485
+ ### 5.2 Send some OCEAN to consumer account
496
486
  ```Typescript
497
- transfer(web3, config, publisherAccount, addresses.Ocean, consumerAccount, '100')
487
+ transfer(
488
+ publisherAccount,
489
+ config,
490
+ addresses.Ocean,
491
+ await consumerAccount.getAddress(),
492
+ '100'
493
+ )
498
494
 
499
495
  ```
500
496
 
@@ -538,8 +534,8 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
538
534
 
539
535
  ### 7.1 Resolve published datasets and algorithms
540
536
  ```Typescript
541
- resolvedDatasetDdo = await aquarius.waitForAqua(datasetId)
542
- resolvedAlgorithmDdo = await aquarius.waitForAqua(algorithmId)
537
+ resolvedDatasetDdo = await aquariusInstance.waitForAqua(datasetId)
538
+ resolvedAlgorithmDdo = await aquariusInstance.waitForAqua(algorithmId)
543
539
  ```
544
540
  <!--
545
541
  assert(resolvedDatasetDdo, 'Cannot fetch DDO from Aquarius')
@@ -551,18 +547,22 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
551
547
 
552
548
  ### 8.1 Mint dataset and algorithm datatokens to publisher
553
549
  ```Typescript
550
+ const datatoken = new Datatoken(
551
+ publisherAccount,
552
+ (await publisherAccount.provider.getNetwork()).chainId
553
+ )
554
554
  await datatoken.mint(
555
555
  resolvedDatasetDdo.services[0].datatokenAddress,
556
- publisherAccount,
556
+ await publisherAccount.getAddress(),
557
557
  '10',
558
- consumerAccount
558
+ await consumerAccount.getAddress()
559
559
  )
560
560
 
561
561
  await datatoken.mint(
562
562
  resolvedAlgorithmDdo.services[0].datatokenAddress,
563
- publisherAccount,
563
+ await publisherAccount.getAddress(),
564
564
  '10',
565
- consumerAccount
565
+ await consumerAccount.getAddress()
566
566
  )
567
567
 
568
568
  ```
@@ -581,6 +581,11 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
581
581
  ## 10. Consumer starts a compute job
582
582
 
583
583
  ### 10.1 Start a compute job using a free C2D environment
584
+ datatoken = new Datatoken(
585
+ consumerAccount,
586
+ (await consumerAccount.provider.getNetwork()).chainId
587
+ )
588
+
584
589
  let's check the free compute environment
585
590
  ```Typescript
586
591
  const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find(
@@ -617,7 +622,7 @@ Let's have 5 minute of compute access
617
622
  computeEnv.id,
618
623
  computeValidUntil,
619
624
  providerUrl,
620
- consumerAccount
625
+ await consumerAccount.getAddress()
621
626
  )
622
627
  ```
623
628
  <!--
@@ -640,14 +645,15 @@ Let's have 5 minute of compute access
640
645
  0
641
646
  )
642
647
  }
648
+
643
649
  const computeJobs = await ProviderInstance.computeStart(
644
650
  providerUrl,
645
- web3,
646
651
  consumerAccount,
647
652
  computeEnv.id,
648
653
  assets[0],
649
654
  algo
650
655
  )
656
+
651
657
  ```
652
658
  <!--
653
659
  assert(computeJobs, 'Cannot start compute job')
@@ -664,7 +670,7 @@ You can also add various delays so you see the various states of the compute job
664
670
  ```Typescript
665
671
  const jobStatus = await ProviderInstance.computeStatus(
666
672
  providerUrl,
667
- consumerAccount,
673
+ await consumerAccount.getAddress(),
668
674
  computeJobId,
669
675
  DATASET_DDO.id
670
676
  )
@@ -683,7 +689,6 @@ Now, let's see the current status of the previously started computer job
683
689
  await sleep(10000)
684
690
  const downloadURL = await ProviderInstance.getComputeResultUrl(
685
691
  providerUrl,
686
- web3,
687
692
  consumerAccount,
688
693
  computeJobId,
689
694
  0