1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-26 19:49:26 +01:00
docs/developers/ocean.js/configuration.md

180 lines
5.8 KiB
Markdown
Raw Normal View History

# Configuration
2023-07-14 16:29:02 +02:00
For obtaining the API keys for blockchain access and setting the correct environment variables, please consult [this section](http://127.0.0.1:5000/o/mTcjMqA4ylf55anucjH8/s/zQlpIJEeu8x5yl0OLuXn/) first and proceed with the next steps.
### Create a directory
2023-06-30 16:01:40 +02:00
Let's start with creating a working directory where we store the environment variable file, configuration files, and the scripts.
```bash
mkdir my-ocean-project
cd my-ocean-project
```
### Create a `.env` file
2023-06-30 16:01:40 +02:00
In the working directory create a `.env` file. The content of this file will store the values for the following variables:
2023-06-30 16:01:40 +02:00
<table><thead><tr><th width="235.47193347193348">Variable name</th><th width="421">Description</th><th>Required</th></tr></thead><tbody><tr><td><strong>OCEAN_NETWORK</strong></td><td>Name of the network where the Ocean Protocol's smart contracts are deployed.</td><td>Yes</td></tr><tr><td><strong>OCEAN_NETWORK_URL</strong></td><td>The URL of the Ethereum node (along with API key for non-local networks)**</td><td>Yes</td></tr><tr><td><strong>PRIVATE_KEY</strong></td><td>The private key of the account which you want to use. A private key is made up of 64 hex characters. Make sure you have sufficient balance to pay for the transaction fees.</td><td>Yes</td></tr><tr><td><strong>AQUARIUS_URL</strong></td><td>The URL of the Aquarius. This value is needed when reading an asset from off-chain store.</td><td>No</td></tr><tr><td><strong>PROVIDER_URL</strong></td><td>The URL of the Provider. This value is needed when publishing a new asset or update an existing asset.</td><td>No</td></tr></tbody></table>
{% hint style="info" %}
Treat this file as a secret and do not commit this file to git or share the content publicly. If you are using git, then include this file name in `.gitignore` file.
{% endhint %}
The below tabs show partially filled `.env` file content for some of the supported networks.
{% tabs %}
{% tab title="Mainnet" %}
{% code title=".env" %}
```bash
# Mandatory environment variables
OCEAN_NETWORK=mainnet
2022-07-11 11:16:46 +02:00
OCEAN_NETWORK_URL=<replace this>
PRIVATE_KEY=<secret>
# Optional environment variables
AQUARIUS_URL=https://v4.aquarius.oceanprotocol.com/
2023-05-26 13:50:44 +02:00
PROVIDER_URL=https://v4.provider.oceanprotocol.com
```
{% endcode %}
{% endtab %}
{% tab title="Polygon" %}
{% code title=".env" %}
```bash
# Mandatory environment variables
OCEAN_NETWORK=polygon
2022-07-11 11:16:46 +02:00
OCEAN_NETWORK_URL=<replace this>
PRIVATE_KEY=<secret>
# Optional environment variables
AQUARIUS_URL=https://v4.aquarius.oceanprotocol.com/
2023-05-26 13:50:44 +02:00
PROVIDER_URL=https://v4.provider.oceanprotocol.com
```
{% endcode %}
{% endtab %}
{% tab title="Local (using Barge)" %}
{% code title=".env" %}
```bash
# Mandatory environment variables
2022-07-11 11:32:21 +02:00
OCEAN_NETWORK=development
2022-07-11 11:16:46 +02:00
OCEAN_NETWORK_URL=http://172.15.0.3:8545/
AQUARIUS_URL=http://172.15.0.5:5000
PROVIDER_URL=http://172.15.0.4:8030
# Replace PRIVATE_KEY if needed
PRIVATE_KEY=0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58
```
{% endcode %}
{% endtab %}
{% endtabs %}
2023-07-16 09:19:03 +02:00
Replace `<replace this>` with the appropriate values. You can see all the networks configuration on Oceanjs' [config helper](https://github.com/oceanprotocol/ocean.js/blob/main/src/config/ConfigHelper.ts#L42).
### Setup dependencies
2022-07-11 11:32:21 +02:00
2023-06-30 16:01:40 +02:00
In this step, all required dependencies will be installed.
2023-05-26 13:50:44 +02:00
### Installation & Usage
2023-06-30 16:01:40 +02:00
Let's install Ocean.js library into your current project by running:
{% tabs %}
2023-05-26 13:50:44 +02:00
{% tab title="Terminal" %}
{% code overflow="wrap" %}
```bash
npm init
2023-06-09 15:10:40 +02:00
npm i @oceanprotocol/lib@latest dotenv crypto-js ethers@5.7.4 @truffle/hdwallet-provider
```
{% endcode %}
{% endtab %}
{% endtabs %}
### Create a configuration file
2023-05-26 13:50:44 +02:00
A configuration file will read the content of the `.env` file and initialize the required configuration objects which will be used in the further tutorials. The below scripts creates a Web3 wallet instance and an Ocean's configuration object.
Create the configuration file in the working directory i.e. at the same path where the `.env` is located.
{% tabs %}
2023-05-26 13:50:44 +02:00
{% tab title="config.js" %}
{% code title="config.js" %}
```javascript
require("dotenv").config();
const {
Aquarius,
ConfigHelper,
configHelperNetworks,
} = require("@oceanprotocol/lib");
const ethers = require("ethers");
import fs from "fs";
import { homedir } from "os";
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);
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;
oceanConfig = {
...oceanConfig,
oceanTokenAddress: addresses.Ocean,
fixedRateExchangeAddress: addresses.FixedPrice,
dispenserAddress: addresses.Dispenser,
nftFactoryAddress: addresses.ERC721Factory,
opfCommunityFeeCollector: addresses.OPFCommunityFeeCollector,
};
}
oceanConfig = {
...oceanConfig,
publisherAccount: publisherAccount,
consumerAccount: publisherAccount,
aquarius: aquarius,
};
return oceanConfig;
}
module.exports = {
oceanConfig,
};
```
{% endcode %}
{% endtab %}
{% endtabs %}
2023-05-26 13:50:44 +02:00
Now you have set up the necessary files and configurations to interact with Ocean Protocol's smart contracts using ocean.js. You can proceed with further tutorials or development using these configurations.