@oceanprotocol/lib 4.0.2 → 4.1.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.
@@ -12,8 +12,10 @@ Here are the steps:
12
12
  7. [Resolve published datasets and algorithms](#7-resolve-assets)
13
13
  8. [Send datatokens to consumer](#8-send-datatokens-to-consumer)
14
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)
15
+ 10. [Consumer starts a free compute job using a free C2D environment](#10-consumer-starts-a-compute-job)
16
16
  11. [Check compute status and get download compute results url](#11-check-compute-status-and-get-download-compute-results-url)
17
+ 12. [Consumer starts a paid compute job](#12-consumer-starts-a-paid-compute-job)
18
+ 13. [Check paid compute job status and get download compute results URL](#13-check-paid-compute-job-status-and-get-download-compute-results-url)
17
19
 
18
20
  Let's go through each step.
19
21
 
@@ -136,7 +138,6 @@ import {
136
138
  ComputeAsset,
137
139
  Config,
138
140
  Files,
139
- DDO,
140
141
  NftCreateData,
141
142
  DatatokenCreateParams,
142
143
  sendTx,
@@ -144,11 +145,15 @@ import {
144
145
  ConfigHelper,
145
146
  getEventFromTx,
146
147
  amountToUnits,
147
- isDefined
148
+ isDefined,
149
+ ComputeResourceRequest,
150
+ unitsToAmount
148
151
  } from '../../src/index.js'
149
- ```
150
152
  import crypto from 'crypto-js'
153
+ import { DDO } from '@oceanprotocol/ddo-js'
154
+ import { EscrowContract } from '../../src/contracts/Escrow.js'
151
155
  const { SHA256 } = crypto
156
+ ```
152
157
 
153
158
  ### 4.2. Constants and variables
154
159
 
@@ -183,7 +188,7 @@ Next, we define the metadata for the dataset and algorithm that will describe ou
183
188
  ```Typescript
184
189
  const DATASET_DDO: DDO = {
185
190
  '@context': ['https://w3id.org/did/v1'],
186
- id: 'id:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
191
+ id: 'did:op:efba17455c127a885ec7830d687a8f6e64f5ba559f8506f8723c1f10f05c049c',
187
192
  version: '4.1.0',
188
193
  chainId: 8996,
189
194
  nftAddress: '0x0',
@@ -201,16 +206,16 @@ const DATASET_DDO: DDO = {
201
206
  },
202
207
  services: [
203
208
  {
204
- id: 'notAnId',
209
+ id: '1155995dda741e93afe4b1c6ced2d01734a6ec69865cc0997daf1f4db7259a36',
205
210
  type: 'compute',
206
211
  files: '',
207
212
  datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3',
208
- serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com/',
213
+ serviceEndpoint: 'http://127.0.0.1:8001',
209
214
  timeout: 300,
210
215
  compute: {
211
- publisherTrustedAlgorithmPublishers: [],
212
- publisherTrustedAlgorithms: [],
213
- allowRawAlgorithm: true,
216
+ publisherTrustedAlgorithmPublishers: [] as any,
217
+ publisherTrustedAlgorithms: [] as any,
218
+ allowRawAlgorithm: false,
214
219
  allowNetworkAccess: true
215
220
  }
216
221
  }
@@ -248,11 +253,11 @@ const ALGORITHM_DDO: DDO = {
248
253
  },
249
254
  services: [
250
255
  {
251
- id: 'notAnId',
256
+ id: 'db164c1b981e4d2974e90e61bda121512e6909c1035c908d68933ae4cfaba6b0',
252
257
  type: 'access',
253
258
  files: '',
254
259
  datatokenAddress: '0xa15024b732A8f2146423D14209eFd074e61964F3',
255
- serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com',
260
+ serviceEndpoint: 'http://127.0.0.1:8001',
256
261
  timeout: 300
257
262
  }
258
263
  ]
@@ -342,7 +347,7 @@ async function createAssetHelper(
342
347
  assetUrl.nftAddress = nftAddress
343
348
  ddo.services[0].files = await ProviderInstance.encrypt(assetUrl, chain, providerUrl)
344
349
  ddo.services[0].datatokenAddress = datatokenAddressAsset
345
- ddo.services[0].serviceEndpoint = 'http://172.15.0.4:8030' // put back proviederUrl
350
+ ddo.services[0].serviceEndpoint = providerUrl
346
351
 
347
352
  ddo.nftAddress = nftAddress
348
353
  ddo.id = 'did:op:' + SHA256(ethers.utils.getAddress(nftAddress) + chain.toString(10))
@@ -353,7 +358,7 @@ async function createAssetHelper(
353
358
  nftAddress,
354
359
  await owner.getAddress(),
355
360
  0,
356
- 'http://172.15.0.4:8030', // put back proviederUrl
361
+ providerUrl,
357
362
  '',
358
363
  ethers.utils.hexlify(2),
359
364
  encryptedResponse,
@@ -428,9 +433,11 @@ We need to load the configuration. Add the following code into your `run(){ }` f
428
433
  const config = new ConfigHelper().getConfig(
429
434
  parseInt(String((await publisherAccount.provider.getNetwork()).chainId))
430
435
  )
431
- config.providerUri = process.env.PROVIDER_URL || config.providerUri
432
- aquariusInstance = new Aquarius(config?.metadataCacheUri)
433
- providerUrl = config?.providerUri
436
+ if (process.env.OCEAN_NODE_URL) {
437
+ config.oceanNodeUri = process.env.OCEAN_NODE_URL
438
+ }
439
+ aquariusInstance = new Aquarius(config?.oceanNodeUri)
440
+ providerUrl = config?.oceanNodeUri
434
441
  addresses = JSON.parse(
435
442
  // eslint-disable-next-line security/detect-non-literal-fs-filename
436
443
  fs.readFileSync(
@@ -443,7 +450,7 @@ We need to load the configuration. Add the following code into your `run(){ }` f
443
450
  ```
444
451
  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:
445
452
  ```Typescript
446
- console.log(`Aquarius URL: ${config.metadataCacheUri}`)
453
+ console.log(`Indexer URL: ${config.oceanNodeUri}`)
447
454
  console.log(`Provider URL: ${providerUrl}`)
448
455
  console.log(`Deployed contracts address: ${addresses}`)
449
456
  console.log(`Publisher account address: ${publisherAccount}`)
@@ -489,8 +496,10 @@ you need to mint oceans to mentioned accounts only if you are using barge to tes
489
496
  await publisherAccount.getAddress(),
490
497
  amountToUnits(null, null, '1000', 18)
491
498
  )
492
-
493
499
  ```
500
+ <!--
501
+ }).timeout(40000) ///
502
+ --->
494
503
 
495
504
  ### 5.2 Send some OCEAN to consumer account
496
505
  ```Typescript
@@ -520,8 +529,10 @@ you need to mint oceans to mentioned accounts only if you are using barge to tes
520
529
  Now, let's check that we successfully published a dataset (create NFT + Datatoken)
521
530
  ```Typescript
522
531
  console.log(`dataset id: ${datasetId}`)
523
-
524
532
  ```
533
+ <!--
534
+ }).timeout(40000)
535
+ -->
525
536
 
526
537
  ### 6.2 Publish an algorithm (create NFT + Datatoken) and set algorithm metadata
527
538
  ```Typescript
@@ -537,9 +548,10 @@ Now, let's check that we successfully published a dataset (create NFT + Datatoke
537
548
  Now, let's check that we successfully published a algorithm (create NFT + Datatoken)
538
549
  ```Typescript
539
550
  console.log(`algorithm id: ${algorithmId}`)
540
-
541
551
  ```
542
-
552
+ <!--
553
+ }).timeout(40000)
554
+ -->
543
555
  ## 7. Resolve assets
544
556
 
545
557
  ### 7.1 Resolve published datasets and algorithms
@@ -550,8 +562,8 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
550
562
  <!--
551
563
  assert(resolvedDatasetDdo, 'Cannot fetch DDO from Aquarius')
552
564
  assert(resolvedAlgorithmDdo, 'Cannot fetch DDO from Aquarius')
565
+ }).timeout(80000)
553
566
  -->
554
-
555
567
 
556
568
  ## 8. Send datatokens to consumer
557
569
 
@@ -574,8 +586,10 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
574
586
  '10',
575
587
  await consumerAccount.getAddress()
576
588
  )
577
-
578
589
  ```
590
+ <!--
591
+ }).timeout(40000)
592
+ -->
579
593
 
580
594
  ## 9. Get compute environments
581
595
 
@@ -585,38 +599,202 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
585
599
  ```
586
600
  <!--
587
601
  assert(computeEnvs, 'No Compute environments found')
602
+ }).timeout(40000)
588
603
  -->
589
-
590
604
 
591
- ## 10. Consumer starts a compute job
605
+ ## 10. Consumer starts a free compute job
592
606
 
593
607
  ### 10.1 Start a compute job using a free C2D environment
608
+ <!--
594
609
  datatoken = new Datatoken(
595
610
  consumerAccount,
596
611
  (await consumerAccount.provider.getNetwork()).chainId
597
612
  )
613
+ -->
598
614
 
599
615
  let's check the free compute environment
600
616
  ```Typescript
601
- const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find(
602
- (ce) => ce.priceMin === 0 || isDefined(ce.free)
603
- )
617
+ const computeEnv = computeEnvs.find((ce) => isDefined(ce.free))
604
618
  console.log('Free compute environment = ', computeEnv)
605
619
  ```
606
620
  <!--
607
621
  assert(computeEnv, 'Cannot find the free compute env')
608
622
  -->
609
623
 
624
+ <!--
610
625
  computeRoutePath = await ProviderInstance.getComputeStartRoutes(providerUrl, true)
611
626
  if (isDefined(computeRoutePath)) {
612
627
  hasFreeComputeSupport = true
628
+ -->
629
+
613
630
  Let's have 5 minute of compute access
614
631
  ```Typescript
615
632
  const mytime = new Date()
616
633
  const computeMinutes = 5
617
634
  mytime.setMinutes(mytime.getMinutes() + computeMinutes)
635
+
636
+ ```
637
+ Let's prepare the dataset and algorithm assets to be used in the compute job
638
+ ```Typescript
639
+ const assets: ComputeAsset[] = [
640
+ {
641
+ documentId: resolvedDatasetDdo.id,
642
+ serviceId: resolvedDatasetDdo.services[0].id
643
+ }
644
+ ]
645
+
646
+ const algo: ComputeAlgorithm = {
647
+ documentId: resolvedAlgorithmDdo.id,
648
+ serviceId: resolvedAlgorithmDdo.services[0].id,
649
+ meta: resolvedAlgorithmDdo.metadata.algorithm
650
+ }
651
+ ```
652
+
653
+ Let's start the free compute job
654
+ ```Typescript
655
+ const computeJobs = await ProviderInstance.freeComputeStart(
656
+ providerUrl,
657
+ consumerAccount,
658
+ computeEnv.id,
659
+ assets,
660
+ algo
661
+ )
662
+ ```
663
+
664
+ <!--
665
+ assert(computeJobs, 'Cannot start compute job')
666
+ -->
667
+
668
+ Let's save the compute job it, we re going to use later
669
+ ```Typescript
670
+ computeJobId = computeJobs[0].jobId
671
+ // eslint-disable-next-line prefer-destructuring
672
+ agreementId = computeJobs[0].agreementId
673
+ ```
674
+ <!--
675
+ } else {
676
+ assert(
677
+ computeRoutePath === null,
678
+ 'Route path for free compute is not defined (perhaps because provider does not support it yet?)'
679
+ )
680
+ hasFreeComputeSupport = false
681
+ }
682
+ }).timeout(40000)
683
+ -->
684
+
685
+ ## 11. Check compute status and get download compute results URL
686
+ ### 11.1 Check compute status
687
+ <!--
688
+ if (!hasFreeComputeSupport) {
689
+ assert(
690
+ computeRoutePath === null,
691
+ 'Compute route path for free compute is not defined (perhaps because provider does not support it yet?)'
692
+ )
693
+ } else {
694
+ -->
695
+ You can also add various delays so you see the various states of the compute job
696
+ ```Typescript
697
+ const jobStatus = await ProviderInstance.computeStatus(
698
+ providerUrl,
699
+ await consumerAccount.getAddress(),
700
+ computeJobId,
701
+ agreementId
702
+ )
703
+ ```
704
+ <!--
705
+ assert(jobStatus, 'Cannot retrieve compute status!')
706
+ -->
707
+ Now, let's see the current status of the previously started computer job
708
+ ```Typescript
709
+ console.log('Current status of the compute job: ', jobStatus)
710
+ ```
711
+ <!--
712
+ }
713
+ }).timeout(40000)
714
+ -->
715
+
716
+ ### 11.2 Get download compute results URL
717
+ <!--
718
+ if (!hasFreeComputeSupport) {
719
+ assert(
720
+ computeRoutePath === null,
721
+ 'Compute route path for free compute is not defined (perhaps because provider does not support it yet?)'
722
+ )
723
+ } else {
724
+ -->
725
+
726
+ ```Typescript
727
+ await sleep(10000)
728
+ const downloadURL = await ProviderInstance.getComputeResultUrl(
729
+ providerUrl,
730
+ consumerAccount,
731
+ computeJobId,
732
+ 0
733
+ )
734
+ ```
735
+ <!--
736
+ assert(downloadURL, 'Provider getComputeResultUrl failed!')
737
+ -->
738
+ Let's check the compute results url for the specified index
739
+ ```Typescript
740
+ console.log(`Compute results URL: ${downloadURL}`)
741
+ ```
742
+ <!--
743
+ }
744
+ }).timeout(40000)
745
+ -->
746
+
747
+ ## 12. Consumer starts a paid compute job
748
+
749
+ ### 12.1 Start a compute job using a paid C2D resources
750
+ <!--
751
+ datatoken = new Datatoken(
752
+ consumerAccount,
753
+ (await consumerAccount.provider.getNetwork()).chainId
754
+ )
755
+ -->
756
+
757
+ let's select compute environment which have free and paid resources
758
+ ```Typescript
759
+ const computeEnv = computeEnvs[0]
760
+ console.log('Compute environment = ', computeEnv)
761
+ ```
762
+ <!--
763
+ assert(computeEnv, 'Cannot find the compute env')
764
+ -->
765
+
766
+ <!--
767
+ const paymentToken = addresses.Ocean
768
+ computeRoutePath = await ProviderInstance.getComputeStartRoutes(providerUrl, false)
769
+ if (isDefined(computeRoutePath)) {
770
+ -->
771
+
772
+ Let's have 5 minute of compute access
773
+ ```Typescript
774
+
775
+ const mytime = new Date()
776
+ const computeMinutes = 5
777
+ mytime.setMinutes(mytime.getMinutes() + computeMinutes)
618
778
  const computeValidUntil = Math.floor(mytime.getTime() / 1000)
619
779
 
780
+ ```
781
+
782
+ Let's prepare the dataset and algorithm assets to be used in the compute job
783
+ ```Typescript
784
+ const resources: ComputeResourceRequest[] = [
785
+ {
786
+ id: 'cpu',
787
+ amount: 2
788
+ },
789
+ {
790
+ id: 'ram',
791
+ amount: 1000000000
792
+ },
793
+ {
794
+ id: 'disk',
795
+ amount: 0
796
+ }
797
+ ]
620
798
  const assets: ComputeAsset[] = [
621
799
  {
622
800
  documentId: resolvedDatasetDdo.id,
@@ -626,22 +804,77 @@ let's check the free compute environment
626
804
  const dtAddressArray = [resolvedDatasetDdo.services[0].datatokenAddress]
627
805
  const algo: ComputeAlgorithm = {
628
806
  documentId: resolvedAlgorithmDdo.id,
629
- serviceId: resolvedAlgorithmDdo.services[0].id
807
+ serviceId: resolvedAlgorithmDdo.services[0].id,
808
+ meta: resolvedAlgorithmDdo.metadata.algorithm
630
809
  }
810
+ ```
631
811
 
812
+ Triggering initialize compute to see payment options
813
+ ```Typescript
632
814
  const providerInitializeComputeResults = await ProviderInstance.initializeCompute(
633
815
  assets,
634
816
  algo,
635
817
  computeEnv.id,
818
+ paymentToken,
636
819
  computeValidUntil,
637
820
  providerUrl,
638
- consumerAccount
821
+ consumerAccount,
822
+ resources
639
823
  )
824
+
825
+ console.log(
826
+ 'providerInitializeComputeResults = ',
827
+ JSON.stringify(providerInitializeComputeResults)
828
+ )
829
+
640
830
  ```
831
+
641
832
  <!--
642
833
  assert(!('error' in providerInitializeComputeResults), 'Cannot order algorithm')
643
834
  -->
835
+
836
+ Let's check funds for escrow payment
644
837
  ```Typescript
838
+ const escrow = new EscrowContract(
839
+ ethers.utils.getAddress(providerInitializeComputeResults.payment.escrowAddress),
840
+ consumerAccount
841
+ )
842
+ const paymentTokenPublisher = new Datatoken(publisherAccount)
843
+ const balancePublisherPaymentToken = await paymentTokenPublisher.balance(
844
+ paymentToken,
845
+ await publisherAccount.getAddress()
846
+ )
847
+ assert(
848
+ ethers.utils.parseEther(balancePublisherPaymentToken) > ethers.BigNumber.from(0),
849
+ 'Balance should be higher than 0'
850
+ )
851
+ const tx = await publisherAccount.sendTransaction({
852
+ to: computeEnv.consumerAddress,
853
+ value: ethers.utils.parseEther('1.5')
854
+ })
855
+ await tx.wait()
856
+
857
+ await paymentTokenPublisher.transfer(
858
+ paymentToken,
859
+ ethers.utils.getAddress(computeEnv.consumerAddress),
860
+ (Number(balancePublisherPaymentToken) / 2).toString()
861
+ )
862
+ const amountToDeposit = (
863
+ providerInitializeComputeResults.payment.amount * 2
864
+ ).toString()
865
+ await escrow.verifyFundsForEscrowPayment(
866
+ paymentToken,
867
+ computeEnv.consumerAddress,
868
+ await unitsToAmount(consumerAccount, paymentToken, amountToDeposit),
869
+ providerInitializeComputeResults.payment.amount.toString(),
870
+ providerInitializeComputeResults.payment.minLockSeconds.toString(),
871
+ '10'
872
+ )
873
+ ```
874
+
875
+ Let's order assets
876
+ ```Typescript
877
+
645
878
  algo.transferTxId = await handleOrder(
646
879
  providerInitializeComputeResults.algorithm,
647
880
  resolvedAlgorithmDdo.services[0].datatokenAddress,
@@ -658,24 +891,34 @@ let's check the free compute environment
658
891
  0
659
892
  )
660
893
  }
894
+ ```
661
895
 
662
- const computeJobs = await ProviderInstance.freeComputeStart(
896
+ Let's start compute job
897
+ ```Typescript
898
+ const computeJobs = await ProviderInstance.computeStart(
663
899
  providerUrl,
664
900
  consumerAccount,
665
901
  computeEnv.id,
666
902
  assets,
667
- algo
903
+ algo,
904
+ computeValidUntil,
905
+ paymentToken,
906
+ resources,
907
+ (
908
+ await consumerAccount.provider.getNetwork()
909
+ ).chainId
668
910
  )
669
-
670
911
  ```
912
+
671
913
  <!--
672
914
  assert(computeJobs, 'Cannot start compute job')
673
915
  -->
916
+
674
917
  Let's save the compute job it, we re going to use later
675
918
  ```Typescript
676
919
  computeJobId = computeJobs[0].jobId
677
- // eslint-disable-next-line prefer-destructuring
678
- agreementId = computeJobs[0].agreementId
920
+ ```
921
+ <!--
679
922
  } else {
680
923
  assert(
681
924
  computeRoutePath === null,
@@ -683,24 +926,25 @@ let's check the free compute environment
683
926
  )
684
927
  hasFreeComputeSupport = false
685
928
  }
686
-
687
- ```
929
+ }).timeout(40000)
930
+ -->
688
931
 
689
- ## 11. Check compute status and get download compute results URL
690
- ### 11.1 Check compute status
932
+ ## 13. Check paid compute job status and get download compute results URL
933
+ ### 13.1 Check compute status for paid compute job
934
+ <!--
691
935
  if (!hasFreeComputeSupport) {
692
936
  assert(
693
937
  computeRoutePath === null,
694
938
  'Compute route path for free compute is not defined (perhaps because provider does not support it yet?)'
695
939
  )
696
940
  } else {
941
+ -->
697
942
  You can also add various delays so you see the various states of the compute job
698
943
  ```Typescript
699
944
  const jobStatus = await ProviderInstance.computeStatus(
700
945
  providerUrl,
701
946
  await consumerAccount.getAddress(),
702
- computeJobId,
703
- agreementId
947
+ computeJobId
704
948
  )
705
949
  ```
706
950
  <!--
@@ -709,17 +953,22 @@ let's check the free compute environment
709
953
  Now, let's see the current status of the previously started computer job
710
954
  ```Typescript
711
955
  console.log('Current status of the compute job: ', jobStatus)
956
+ ```
957
+ <!--
712
958
  }
713
-
714
- ```
959
+ }).timeout(40000)
960
+ -->
715
961
 
716
- ### 11.2 Get download compute results URL
962
+ ### 13.2 Get download compute results URL
963
+ <!--
717
964
  if (!hasFreeComputeSupport) {
718
965
  assert(
719
966
  computeRoutePath === null,
720
- 'Compute route path for free compute is not defined (perhaps because provider does not support it yet?)'
967
+ 'Compute route path for paid compute is not defined (perhaps because provider does not support it yet?)'
721
968
  )
722
969
  } else {
970
+ -->
971
+
723
972
  ```Typescript
724
973
  await sleep(10000)
725
974
  const downloadURL = await ProviderInstance.getComputeResultUrl(
@@ -735,10 +984,12 @@ let's check the free compute environment
735
984
  Let's check the compute results url for the specified index
736
985
  ```Typescript
737
986
  console.log(`Compute results URL: ${downloadURL}`)
987
+ ```
988
+ <!--
738
989
  }
739
-
740
- ```
741
-
990
+ }).timeout(40000)
991
+ })
992
+ -->
742
993
 
743
994
  ## Editing this file
744
995
  Please note that ComputeExamples.md is an autogenerated file, you should not edit it directly.