1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-02 00:05:35 +01:00
docs/developers/using-ocean-libraries/configuration.md

212 lines
7.3 KiB
Markdown
Raw Normal View History

# Configuration
### Obtaining API key for Ethereum node provider
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/)
The supported networks are listed [here](../../core-concepts/networks.md).
### Create a directory
Let's start with creating a working directory where we store the environment variable file, configuration files and the scripts.
```
mkdir my-ocean-project
cd my-ocean-project
```
### Create a `.env` file
In the working directory create a `.env` file. The content of this file will store the values for following variables:
| Variable name | Description | Required |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| **OCEAN\_NETWORK** | Name of the network where the Ocean Protocol's smart contracts are deployed. | Yes |
| **OCEAN\_NETWORK\_URL** | The URL of the Ethereum node (along with API key for non-local networks) | Yes |
| **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" %}
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" %}
```
# 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/
PROVIDER_URL=https://v4.provider.mainnet.oceanprotocol.com
```
{% 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>
PRIVATE_KEY=<secret>
# Optional environment variables
AQUARIUS_URL=https://v4.aquarius.oceanprotocol.com/
PROVIDER_URL=https://v4.provider.polygon.oceanprotocol.com
```
{% 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/
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 %}
_NOTE: If using ocean.py, additionally specify **ADDRESS\_FILE** variable in the `.env` file. Copy the content of this_ [_link_](https://github.com/oceanprotocol/contracts/blob/v4main/addresses/address.json) _locally and set the **ADDRESS\_FILE** so that its value is a correct file path._
### Setup dependencies
2022-07-11 11:32:21 +02:00
In this step the required dependencies will be installed.
{% tabs %}
{% tab title="ocean.js" %}
```bash
npm init
npm install @oceanprotocol/lib@latest dotenv web3 @truffle/hdwallet-provider
```
{% endtab %}
{% tab title="ocean.py" %}
```bash
python3 -m venv venv
source venv/bin/activate
2023-03-09 15:20:18 +01:00
pip install wheel
# Install Ocean library. Allow pre-releases to get the latest v4 version.
2023-03-09 15:20:18 +01:00
pip install ocean-lib
```
{% endtab %}
{% endtabs %}
### Create a configuration file
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 a Ocean's configuration object.
Create the configuration file in the working directory i.e. at the same path where the `.env` is located.
{% tabs %}
{% tab title="ocean.js" %}
{% code title="config.js" %}
```javascript
// Import dependencies
require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const { homedir } = require('os');
const { ConfigHelper } = require('@oceanprotocol/lib');
// Get configuration for the given network
let oceanConfig = new ConfigHelper().getConfig(process.env.OCEAN_NETWORK);
// 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 addressData = JSON.parse(
fs.readFileSync(
process.env.ADDRESS_FILE
|| `${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
)
);
const addresses = addressData[process.env.OCEAN_NETWORK];
oceanConfig = {
...oceanConfig,
oceanTokenAddress: addresses.Ocean,
poolTemplateAddress: addresses.poolTemplate,
fixedRateExchangeAddress: addresses.FixedPrice,
dispenserAddress: addresses.Dispenser,
erc721FactoryAddress: addresses.ERC721Factory,
sideStakingAddress: addresses.Staking,
opfCommunityFeeCollector: addresses.OPFCommunityFeeCollector
};
}
oceanConfig = {
...oceanConfig,
2022-07-11 11:16:46 +02:00
nodeUri: process.env.OCEAN_NETWORK_URL,
// Set optional properties - Provider URL and Aquarius URL
metadataCacheUri: process.env.AQUARIUS_URL || oceanConfig.metadataCacheUri,
providerUri: process.env.PROVIDER_URL || oceanConfig.providerUri
};
const web3Provider = new HDWalletProvider(
process.env.PRIVATE_KEY,
oceanConfig.nodeUri
);
module.exports = {
web3Provider,
oceanConfig
};
```
{% endcode %}
{% endtab %}
{% tab title="ocean.py" %}
{% code title="config.py" %}
```python
2022-07-11 11:16:46 +02:00
import os
from dotenv import load_dotenv
2023-03-09 15:20:18 +01:00
from ocean_lib.example_config import get_config_dict
from ocean_lib.web3_internal.utils import connect_to_network
2022-07-11 11:16:46 +02:00
from ocean_lib.ocean.ocean import Ocean
load_dotenv()
2023-03-09 15:20:18 +01:00
# Create Ocean instance
network_name = os.getenv("OCEAN_NETWORK")
connect_to_network(network_name)
config = get_config_dict(network_name)
2022-07-11 11:16:46 +02:00
ocean = Ocean(config)
2023-03-09 15:20:18 +01:00
from brownie.network import accounts
accounts.clear()
2022-07-11 11:16:46 +02:00
user_private_key = os.getenv('PRIVATE_KEY')
2023-03-09 15:20:18 +01:00
wallet = accounts.add(user_private_key)
```
{% endcode %}
{% endtab %}
{% endtabs %}
Now, all the dependencies are ready and you can proceed with interacting with Ocean infrastructure using Ocean libraries.