1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-01 15:55:34 +01:00
docs/developers/ocean.py/remote-setup.md

9.5 KiB

description
Remote setup for running & testing ocean.py

Remote Setup

This setup does not use barge and uses a remote chain to do the transactions. When the network URL is specified & configured, ocean.py will use components (such as Provider, Aquarius, C2D) according to the expected blockchain.

Here, we do setup for Mumbai, the testnet for Polygon. It's similar for other remote chains.

Here, we will:

  1. Configure Brownie networks
  2. Create two accounts - REMOTE_TEST_PRIVATE_KEY1 and 2
  3. Get test MATIC on Mumbai
  4. Get test OCEAN on Mumbai
  5. Set envvars
  6. Set up Alice and Bob wallets in Python

Let's go!

1. Configure Brownie Networks (One-Time)

1.1 Network config file

Brownie's network config file is network-config.yaml. It is located in the .brownie/ subfolder of your home folder.

  • For Linux & MacOS, it's: ~/.brownie/network-config.yaml
  • For Windows users, it's: C:\Users\<user_name>\.brownie\network-config.yaml

1.2 Generate network config file (if needed)

If you already see the config file, skip this section.

If you don't, you need to auto-generate by calling any brownie function from a Python console. Here's an example.

First, in a new or existing console, run Python:

python

In the Python console:

from ocean_lib.example_config import get_config_dict

It will generate the file in the target location. You can check the target location to confirm.

1.3 Contents of network config file

The network configuration file has settings for each network, e.g. development (ganache), Ethereum mainnet, Polygon, and Mumbai.

Each network gets specifications for:

  • host - the RPC URL, i.e. what URL do we pass through to talk to the chain
  • required_confs - the number of confirmations before a tx is done
  • id - e.g. polygon-main (Polygon), polygon-test (Mumbai)

development chains run locally; live chains run remotely.

The example network-config.yaml in Brownie's GitHub repo is here. It can serve as a comparison to your local copy.

Ocean.py follows the exact id name for the network's name from the default Brownie configuration file. Therefore, you need to ensure that your target network name matches the corresponding Brownie id.

1.4 Networks Supported

All Ocean-deployed chains (Eth mainnet, Polygon, etc) should be in Brownie's default network-config.yaml except Energy Web Chain (EWC).

For Windows users: it's possible that your network-config.yaml doesn't have all the network entries. In this case, just replace your local file's content with the network-config.yaml in Brownie's GitHub repo, here.

For all users: to use EWC, add the following to network-config.yaml:

- name: energyweb
  networks:
  - chainid: 246
    host: https://rpc.energyweb.org
    id: energyweb
    name: energyweb

1.5 RPCs and Infura

In order to obtain API keys for blockchain access, follow up this document for tips & tricks.

The config file's default RPCs point to Infura, which require you to have an Infura account with corresponding token WEB3_INFURA_PROJECT_ID.

If you do have an Infura account

  • Linux & MacOS users: in console: export WEB3_INFURA_PROJECT_ID=<your infura ID>
  • Windows: in console: set WEB3_INFURA_PROJECT_ID=<your infura ID>

If you do not have an Infura account

One option is to get an Infura account. A simpler option is to bypass the need for an Infura account: just change to RPCs that don't need Infura.

You can bypass manually: just edit your brownie network config file.

Or you can bypass via the command line. The following command replaces Infura RPCs with public ones in network-config.yaml:

  • Linux users: in the console:

{% code overflow="wrap" %}

sed -i 's#https://polygon-mainnet.infura.io/v3/$WEB3_INFURA_PROJECT_ID#https://polygon-rpc.com/#g; s#https://polygon-mumbai.infura.io/v3/$WEB3_INFURA_PROJECT_ID#https://rpc-mumbai.maticvigil.com#g' ~/.brownie/network-config.yaml

{% endcode %}

  • MacOS users: you can achieve the same thing with gnu-sed and the gsed command. (Or just manually edit the file.)
  • For Windows: you might need something similar to powershell. (Or just manually edit the file.)

1.6 Network config file wrapup

Congrats, you've now configured your Brownie network file! You rarely need to worry about it from now on.

2. Create EVM Accounts (One-Time)

An EVM account is singularly defined by its private key. Its address is a function of that key. Let's generate two accounts!

In a new or existing console, run Python.

python

In the Python console:

from eth_account.account import Account
account1 = Account.create()
account2 = Account.create()

print(f"""
REMOTE_TEST_PRIVATE_KEY1={account1.key.hex()}, ADDRESS1={account1.address}
REMOTE_TEST_PRIVATE_KEY2={account2.key.hex()}, ADDRESS2={account2.address}
""")

Then, hit Ctrl-C to exit the Python console.

Now, you have two EVM accounts (address & private key). Save them somewhere safe, like a local file or a password manager.

These accounts will work on any EVM-based chain: production chains like Eth mainnet and Polygon, and testnets like Goerli and Mumbai. Here, we'll use them for Mumbai.

3. Get (test) MATIC on Mumbai

We need the a network's native token to pay for transactions on the network. ETH is the native token for Ethereum mainnet; MATIC is the native token for Polygon, and (test) MATIC is the native token for Mumbai.

To get free (test) MATIC on Mumbai:

  1. Go to the faucet https://faucet.polygon.technology/. Ensure you've selected "Mumbai" network and "MATIC" token.
  2. Request funds for ADDRESS1
  3. Request funds for ADDRESS2

You can confirm receiving funds by going to the following url, and seeing your reported MATIC balance: https://mumbai.polygonscan.com/address/<ADDRESS1 or ADDRESS2>

4. Get (test) OCEAN on Mumbai

OCEAN can be used as a data payment token, and locked into veOCEAN for Data Farming / curation. The READMEs show how to use OCEAN in both cases.

To get free (test) OCEAN on Mumbai:

  1. Go to the faucet https://faucet.mumbai.oceanprotocol.com/
  2. Request funds for ADDRESS1
  3. Request funds for ADDRESS2

You can confirm receiving funds by going to the following url, and seeing your reported OCEAN balance: https://mumbai.polygonscan.com/token/0xd8992Ed72C445c35Cb4A2be468568Ed1079357c8?a=<ADDRESS1 or ADDRESS2>

5. Set envvars

As usual, Linux/MacOS needs "export" and Windows needs "set". In the console:

Linux & MacOS users:

# For accounts: set private keys
export REMOTE_TEST_PRIVATE_KEY1=<your REMOTE_TEST_PRIVATE_KEY1>
export REMOTE_TEST_PRIVATE_KEY2=<your REMOTE_TEST_PRIVATE_KEY2>

Windows users:

# For accounts: set private keys
set REMOTE_TEST_PRIVATE_KEY1=<your REMOTE_TEST_PRIVATE_KEY1>
set REMOTE_TEST_PRIVATE_KEY2=<your REMOTE_TEST_PRIVATE_KEY2>

6. Setup in Python

In your working console, run Python:

python

In the Python console:

# Create Ocean instance
from ocean_lib.web3_internal.utils import connect_to_network
connect_to_network("polygon-test") # mumbai is "polygon-test"

import os
from ocean_lib.example_config import get_config_dict
from ocean_lib.ocean.ocean import Ocean
config = get_config_dict("polygon-test")
ocean = Ocean(config)

# Create OCEAN object. ocean_lib knows where OCEAN is on all remote networks
OCEAN = ocean.OCEAN_token

# Create Alice's wallet
from brownie.network import accounts
accounts.clear()

alice_private_key = os.getenv('REMOTE_TEST_PRIVATE_KEY1')
alice = accounts.add(alice_private_key)
assert alice.balance() > 0, "Alice needs MATIC"
assert OCEAN.balanceOf(alice) > 0, "Alice needs OCEAN"

# Create Bob's wallet. While some flows just use Alice wallet, it's simpler to do all here.
bob_private_key = os.getenv('REMOTE_TEST_PRIVATE_KEY2')
bob = accounts.add(bob_private_key)
assert bob.balance() > 0, "Bob needs MATIC"
assert OCEAN.balanceOf(bob) > 0, "Bob needs OCEAN"

# Compact wei <> eth conversion
from ocean_lib.ocean.util import to_wei, from_wei

If you get a gas-related error like transaction underpriced, you'll need to change the priority_fee or max_fee. See details in brownie docs or you can check the dedicated README which customize your gas strategy.