2022-07-09 13:00:07 +02:00
# Configuration
2022-07-23 16:42:46 +02:00
### Obtaining API key for Ethereum node provider
2022-07-09 13:00:07 +02:00
2022-08-03 12:59:05 +02:00
Ocean Protocol's smart contracts are deployed on EVM-compatible networks. Using an API key provided by a third-party Ethereum node provider allows you to interact with the Ocean Protocol's smart contracts on the supported networks without requiring you to host a local node.
Choose any API provider of your choice. Some of the commonly used are:
* [Infura ](https://infura.io/ )
* [Alchemy ](https://www.alchemy.com/ )
* [Moralis ](https://moralis.io/ )
2023-05-24 18:02:40 +02:00
The supported networks are listed [here ](../../discover/networks/ ).
2022-07-09 13:00:07 +02:00
2022-08-02 11:59:29 +02:00
### Create a directory
Let's start with creating a working directory where we store the environment variable file, configuration files and the scripts.
2022-07-09 13:00:07 +02:00
```
mkdir my-ocean-project
cd my-ocean-project
```
2022-07-09 11:34:57 +02:00
### Create a `.env` file
2022-08-02 14:52:51 +02:00
In the working directory create a `.env` file. The content of this file will store the values for following variables:
2022-08-02 11:59:29 +02:00
| Variable name | Description | Required |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| **OCEAN\_NETWORK** | Name of the network where the Ocean Protocol's smart contracts are deployed. | Yes |
2023-05-26 13:50:44 +02:00
| **OCEAN\_NETWORK\_URL** | The URL of the Ethereum node (along with API key for non-local networks)\*\* | Yes |
2022-08-02 11:59:29 +02:00
| **PRIVATE\_KEY** | 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. | Yes |
| **AQUARIUS\_URL** | The URL of the Aquarius. This value is needed when reading an asset from off-chain store. | No |
| **PROVIDER\_URL** | The URL of the Provider. This value is needed when publishing a new asset or update an existing asset. | No |
{% hint style="info" %}
2022-08-03 12:50:04 +02:00
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.
2022-08-02 11:59:29 +02:00
{% endhint %}
2022-08-03 12:50:04 +02:00
The below tabs show partially filled `.env` file content for some of the supported networks.
2022-08-02 11:59:29 +02:00
2022-07-09 13:00:07 +02:00
{% tabs %}
{% tab title="Mainnet" %}
{% code title=".env" %}
```
# Mandatory environment variables
OCEAN_NETWORK=mainnet
2022-07-11 11:16:46 +02:00
OCEAN_NETWORK_URL=< replace this >
2022-07-09 13:00:07 +02:00
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
2022-07-09 11:34:57 +02:00
```
2022-07-09 13:00:07 +02:00
{% endcode %}
{% endtab %}
{% tab title="Polygon" %}
{% code title=".env" %}
```
# Mandatory environment variables
OCEAN_NETWORK=polygon
2022-07-11 11:16:46 +02:00
OCEAN_NETWORK_URL=< replace this >
2022-07-09 13:00:07 +02:00
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
2022-07-09 13:00:07 +02:00
```
{% endcode %}
{% endtab %}
{% tab title="Local (using Barge)" %}
{% code title=".env" %}
```
# 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/
2022-07-09 11:34:57 +02:00
AQUARIUS_URL=http://172.15.0.5:5000
PROVIDER_URL=http://172.15.0.4:8030
2022-07-09 13:00:07 +02:00
# Replace PRIVATE_KEY if needed
PRIVATE_KEY=0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58
2022-07-09 11:34:57 +02:00
```
2022-07-09 13:00:07 +02:00
{% endcode %}
{% endtab %}
{% endtabs %}
2022-07-09 11:34:57 +02:00
2023-05-26 13:50:44 +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 ).
2022-08-02 11:59:29 +02:00
### Setup dependencies
2022-07-11 11:32:21 +02:00
2023-05-26 13:50:44 +02:00
In this step all required dependencies will be installed.
### Installation & Usage
Let's install Oceanjs library into your current project by running:
2022-07-09 13:00:07 +02:00
{% tabs %}
2023-05-26 13:50:44 +02:00
{% tab title="Terminal" %}
2022-07-09 13:00:07 +02:00
```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
2022-07-09 13:00:07 +02:00
```
{% endtab %}
{% endtabs %}
2022-08-02 11:59:29 +02:00
### 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.
2022-08-02 11:59:29 +02:00
2022-08-03 12:50:04 +02:00
Create the configuration file in the working directory i.e. at the same path where the `.env` is located.
2022-07-09 13:00:07 +02:00
{% tabs %}
2023-05-26 13:50:44 +02:00
{% tab title="config.js" %}
2022-07-09 13:00:07 +02:00
{% code title="config.js" %}
```javascript
require('dotenv').config();
2023-05-26 13:50:44 +02:00
const { Aquarius, ZERO_ADDRESS, ConfigHelper, configHelperNetworks } = require('@oceanprotocol/lib');
const { providers } = require('ethers')
const ethers = require('ethers');
async function oceanConfig(){
// Get configuration for the given network
const provider = new providers.JsonRpcProvider(
process.env.OCEAN_NETWORK_URL || configHelperNetworks[1].nodeUri
)
const ethersProvider = new ethers.Wallet(
process.env.PRIVATE_KEY,
provider
2022-07-09 13:00:07 +02:00
);
2023-06-09 15:10:40 +02:00
const publisherAccount = wallet.connect(provider);
2023-05-26 13:50:44 +02:00
let oceanConfig = new ConfigHelper().getConfig(
2023-06-09 15:10:40 +02:00
parseInt(String((await publisherAccount.provider.getNetwork()).chainId))
2023-05-26 13:50:44 +02:00
)
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,
poolTemplateAddress: addresses.poolTemplate,
fixedRateExchangeAddress: addresses.FixedPrice,
dispenserAddress: addresses.Dispenser,
nftFactoryAddress: addresses.ERC721Factory,
sideStakingAddress: addresses.Staking,
opfCommunityFeeCollector: addresses.OPFCommunityFeeCollector
};
}
2022-07-09 13:00:07 +02:00
oceanConfig = {
...oceanConfig,
2023-05-26 13:50:44 +02:00
publisherAccount: publisherAccount,
consumerAccount: consumerAccount,
stakerAccount: stakerAccount,
2023-06-09 15:10:40 +02:00
aquarius: aquarius
2022-07-09 13:00:07 +02:00
};
2023-05-26 13:50:44 +02:00
return oceanConfig
2022-07-09 13:00:07 +02:00
};
module.exports = {
oceanConfig
2023-05-26 13:50:44 +02:00
}
2022-07-09 13:00:07 +02:00
```
{% endcode %}
{% endtab %}
2022-08-02 11:59:29 +02:00
{% 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.