diff --git a/developers/ocean.js/configuration.md b/developers/ocean.js/configuration.md index ea98009e..3f94dbe8 100644 --- a/developers/ocean.js/configuration.md +++ b/developers/ocean.js/configuration.md @@ -26,6 +26,7 @@ The below tabs show partially filled `.env` file content for some of the support {% tabs %} {% tab title="Mainnet" %} {% code title=".env" %} + ```bash # Mandatory environment variables @@ -38,11 +39,13 @@ PRIVATE_KEY= AQUARIUS_URL=https://v4.aquarius.oceanprotocol.com/ PROVIDER_URL=https://v4.provider.oceanprotocol.com ``` + {% endcode %} {% endtab %} {% tab title="Polygon" %} {% code title=".env" %} + ```bash # Mandatory environment variables @@ -55,11 +58,13 @@ PRIVATE_KEY= AQUARIUS_URL=https://v4.aquarius.oceanprotocol.com/ PROVIDER_URL=https://v4.provider.oceanprotocol.com ``` + {% endcode %} {% endtab %} {% tab title="Local (using Barge)" %} {% code title=".env" %} + ```bash # Mandatory environment variables OCEAN_NETWORK=development @@ -70,6 +75,7 @@ PROVIDER_URL=http://172.15.0.4:8030 # Replace PRIVATE_KEY if needed PRIVATE_KEY=0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58 ``` + {% endcode %} {% endtab %} {% endtabs %} @@ -87,10 +93,12 @@ Let's install Ocean.js library into your current project by running: {% tabs %} {% tab title="Terminal" %} {% code overflow="wrap" %} + ```bash npm init npm i @oceanprotocol/lib@latest dotenv crypto-js ethers@5.7.4 @truffle/hdwallet-provider ``` + {% endcode %} {% endtab %} {% endtabs %} @@ -104,69 +112,66 @@ Create the configuration file in the working directory i.e. at the same path whe {% tabs %} {% tab title="config.js" %} {% code title="config.js" %} + ```javascript -require('dotenv').config(); -const { Aquarius, ZERO_ADDRESS, ConfigHelper, configHelperNetworks } = require('@oceanprotocol/lib'); -const { providers } = require('ethers') -const ethers = require('ethers'); -async function oceanConfig(){ +require("dotenv").config(); +const { + Aquarius, + ConfigHelper, + configHelperNetworks, +} = require("@oceanprotocol/lib"); +const ethers = require("ethers"); +import fs from "fs"; +import { homedir } from "os"; - // Get configuration for the given network - const provider = new providers.JsonRpcProvider( - process.env.OCEAN_NETWORK_URL || configHelperNetworks[1].nodeUri - ) +async function oceanConfig() { + const provider = new ethers.providers.JsonRpcProvider( + process.env.OCEAN_NETWORK_URL || configHelperNetworks[1].nodeUri + ); + const publisherAccount = new ethers.Wallet(process.env.PRIVATE_KEY, provider); - const ethersProvider = new ethers.Wallet( - process.env.PRIVATE_KEY, - provider - ); - - const publisherAccount = wallet.connect(provider); + let oceanConfig = new ConfigHelper().getConfig( + parseInt(String((await publisherAccount.provider.getNetwork()).chainId)) + ); + const aquarius = new Aquarius(oceanConfig?.metadataCacheUri); - let oceanConfig = new ConfigHelper().getConfig( - parseInt(String((await publisherAccount.provider.getNetwork()).chainId)) - ) - const aquarius = new Aquarius(oceanConfig?.metadataCacheUri) + // If using local development environment, read the addresses from local file. + // The local deployment address file can be generated using barge. + if (process.env.OCEAN_NETWORK === "development") { + const addresses = JSON.parse( + // eslint-disable-next-line security/detect-non-literal-fs-filename + fs.readFileSync( + process.env.ADDRESS_FILE || + `${homedir}/.ocean/ocean-contracts/artifacts/address.json`, + "utf8" + ) + ).development; - // If using local development environment, read the addresses from local file. - // The local deployment address file can be generated using barge. - if (process.env.OCEAN_NETWORK === 'development') { - const addresses = JSON.parse( - // eslint-disable-next-line security/detect-non-literal-fs-filename - fs.readFileSync( - process.env.ADDRESS_FILE || - `${homedir}/.ocean/ocean-contracts/artifacts/address.json`, - 'utf8' - ) - ).development + oceanConfig = { + ...oceanConfig, + oceanTokenAddress: addresses.Ocean, + fixedRateExchangeAddress: addresses.FixedPrice, + dispenserAddress: addresses.Dispenser, + nftFactoryAddress: addresses.ERC721Factory, + opfCommunityFeeCollector: addresses.OPFCommunityFeeCollector, + }; + } - oceanConfig = { - ...oceanConfig, - oceanTokenAddress: addresses.Ocean, - poolTemplateAddress: addresses.poolTemplate, - fixedRateExchangeAddress: addresses.FixedPrice, - dispenserAddress: addresses.Dispenser, - nftFactoryAddress: addresses.ERC721Factory, - sideStakingAddress: addresses.Staking, - opfCommunityFeeCollector: addresses.OPFCommunityFeeCollector - }; - } + oceanConfig = { + ...oceanConfig, + publisherAccount: publisherAccount, + consumerAccount: publisherAccount, + aquarius: aquarius, + }; - oceanConfig = { - ...oceanConfig, - publisherAccount: publisherAccount, - consumerAccount: consumerAccount, - stakerAccount: stakerAccount, - aquarius: aquarius - }; - - return oceanConfig -}; + return oceanConfig; +} module.exports = { - oceanConfig -} + oceanConfig, +}; ``` + {% endcode %} {% endtab %} {% endtabs %} diff --git a/developers/ocean.js/consume-asset.md b/developers/ocean.js/consume-asset.md index 85fe56eb..bb017188 100644 --- a/developers/ocean.js/consume-asset.md +++ b/developers/ocean.js/consume-asset.md @@ -18,13 +18,13 @@ Later on, when working with the ocean.js library, you can use this order transac #### Prerequisites -* [Obtain an API key](../get-api-keys-for-blockchain-access.md) -* [Set up the .env file](configuration.md#create-a-env-file) -* [Install the dependencies](configuration.md#setup-dependencies) -* [Create a configuration file](configuration.md#create-a-configuration-file) +- [Obtain an API key](../get-api-keys-for-blockchain-access.md) +- [Set up the .env file](configuration.md#create-a-env-file) +- [Install the dependencies](configuration.md#setup-dependencies) +- [Create a configuration file](configuration.md#create-a-configuration-file) {% hint style="info" %} -The variables **AQUARIUS\_URL** and **PROVIDER\_URL** should be set correctly in `.env` file +The variables **AQUARIUS_URL** and **PROVIDER_URL** should be set correctly in `.env` file {% endhint %} #### Create a script to consume an asset @@ -32,76 +32,90 @@ The variables **AQUARIUS\_URL** and **PROVIDER\_URL** should be set correctly in Create a new file in the same working directory where the configuration file (`config.js`) and `.env` files are present, and copy the code as listed below.
// Note: Make sure .env file and config.js are created and setup correctly
-const { oceanConfig } = require('./config.js');
-const { ZERO_ADDRESS, NftFactory, getHash, Nft } = require ('@oceanprotocol/lib');
+const { oceanConfig } = require("./config.js");
+const {
+	ZERO_ADDRESS,
+	NftFactory,
+	getHash,
+	ProviderFees,
+	Datatoken,
+	ProviderInstance,
+	Nft,
+	FixedRateExchange,
+  approve
+} = require("@oceanprotocol/lib");
 
 // replace the did here
 const did = "did:op:a419f07306d71f3357f8df74807d5d12bddd6bcd738eb0b461470c64859d6f0f";
 
 // This function takes did as a parameter and updates the data NFT information
 const consumeAsset = async (did) => {
-  
-  const consumer = await oceanConfig.consumerAccount.getAddress();
-  
-   // Fetch ddo from Aquarius
-  const asset = await await oceanConfig.aquarius.resolve(did);
+	const consumer = await oceanConfig.consumerAccount.getAddress();
 
-  const nft = new Nft(oceanConfig.consumerAccount);
-  
-  await approve(
-    consumerAccount,
-    config,
-    await consumerAccount.getAddress(),
-    addresses.Ocean,
-    freAddress,
-    '1'
-  )
-    
- const fixedRate = new FixedRateExchange(fixedRateExchangeAddress, consumerAccount)
- const tx = await fixedRate.buyDatatokens(fixedRateId, '1', '2')
- 
- const initializeData = await ProviderInstance.initialize(
-    resolvedDDO.id,
-    resolvedDDO.services[0].id,
-    0,
-    await consumerAccount.getAddress(),
-    providerUrl
-  )
+	// Fetch ddo from Aquarius
+	const asset = await await oceanConfig.aquarius.resolve(did);
 
-  const providerFees: ProviderFees = {
-    providerFeeAddress: initializeData.providerFee.providerFeeAddress,
-    providerFeeToken: initializeData.providerFee.providerFeeToken,
-    providerFeeAmount: initializeData.providerFee.providerFeeAmount,
-    v: initializeData.providerFee.v,
-    r: initializeData.providerFee.r,
-    s: initializeData.providerFee.s,
-    providerData: initializeData.providerFee.providerData,
-    validUntil: initializeData.providerFee.validUntil
-  }
+	const nft = new Nft(consumer);
 
- datatoken = new Datatoken(consumerAccount)
-    
- const tx = await datatoken.startOrder(
-    freDatatokenAddress,
-    await consumerAccount.getAddress(),
-    0,
-    providerFees
- )
-    
- const orderTx = await tx.wait()
- const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted')
+	await approve(
+		Error,
+		oceanConfig,
+		await consumer.getAddress(),
+		oceanConfig.Ocean,
+		oceanConfig.fixedRateExchangeAddress,
+		"1"
+	);
 
- const downloadURL = await ProviderInstance.getDownloadUrl(
-   fixedDDO.id,
-   fixedDDO.services[0].id,
-   0,
-   orderStartedTx.transactionHash,
-   providerUrl,
-   consumerAccount
- )
-  console.log(`Resolved asset did [${updatedAsset.id}]from aquarius.`);
-  console.log(`Updated asset state: [${updatedAsset.nft.state}].`);
+	const fixedRate = new FixedRateExchange(
+		oceanConfig.fixedRateExchangeAddress,
+		consumer
+	);
 
+	const txBuyDt = await fixedRate.buyDatatokens(
+		oceanConfig.fixedRateId,
+		"1",
+		"2"
+	);
+
+	const initializeData = await ProviderInstance.initialize(
+		asset.id,
+		asset.services[0].id,
+		0,
+		await consumer.getAddress(),
+		oceanConfig.providerUri
+	);
+
+	const providerFees: ProviderFees = {
+		providerFeeAddress: initializeData.providerFee.providerFeeAddress,
+		providerFeeToken: initializeData.providerFee.providerFeeToken,
+		providerFeeAmount: initializeData.providerFee.providerFeeAmount,
+		v: initializeData.providerFee.v,
+		r: initializeData.providerFee.r,
+		s: initializeData.providerFee.s,
+		providerData: initializeData.providerFee.providerData,
+		validUntil: initializeData.providerFee.validUntil,
+	};
+
+	const datatoken = new Datatoken(consumer);
+
+	const tx = await datatoken.startOrder(
+		oceanConfig.fixedRateExchangeAddress,
+		await consumer.getAddress(),
+		0,
+		providerFees
+	);
+
+	const orderTx = await tx.wait();
+	const orderStartedTx = getEventFromTx(orderTx, "OrderStarted");
+
+	const downloadURL = await ProviderInstance.getDownloadUrl(
+		asset.id,
+		asset.services[0].id,
+		0,
+		orderTx.transactionHash,
+		oceanConfig.providerUri,
+		consumer
+	);
 };
 
 // Call setMetadata(...) function defined above