Skip to content

Commit

Permalink
Change config to use RPC URL directly. (#1488)
Browse files Browse the repository at this point in the history
* Change config to use RPC URL directly.

* Fix incomplete test.

* Add infura rpcs for testing.
  • Loading branch information
calina-c authored Nov 1, 2023
1 parent 0c0eb5b commit 8e07749
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 92 deletions.
1 change: 1 addition & 0 deletions .github/workflows/libcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
OCEAN_NETWORK_URL: "http://127.0.0.1:8545"
OCEAN_CONFIG_FILE: "config.ini"
FACTORY_DEPLOYER_PRIVATE_KEY: "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58"
MUMBAI_RPC_URL: "https://polygon-mumbai.infura.io/v3/${{ secrets.WEB3_INFURA_PROJECT_ID }}"
run: |
mkcodes --github --output tests/generated-readmes/test_{name}.{ext} READMEs
pytest tests/readmes/test_readmes.py
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
env:
REMOTE_TEST_PRIVATE_KEY1: ${{secrets.REMOTE_TEST_PRIVATE_KEY1}}
REMOTE_TEST_PRIVATE_KEY2: ${{secrets.REMOTE_TEST_PRIVATE_KEY2}}
MUMBAI_RPC_URL: "https://polygon-mumbai.infura.io/v3/${{ secrets.WEB3_INFURA_PROJECT_ID }}"
- name: docker logs
run: docker logs ocean_aquarius_1 && docker logs ocean_provider_1
if: ${{ failure() }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ ocean_lib.egg-info/
.tox/
consume-downloads/
coverage.xml
setup-local.sh
2 changes: 1 addition & 1 deletion READMEs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Here are the urls for the local services, for use in the config dict.
Remember, here's how the config dict is set.
```python
from ocean_lib.example_config import get_config_dict
config = get_config_dict("mumbai") # returns a dict
config = get_config_dict(<RPC_URL>) # returns a dict
# (then, here you can update the config dict as you wish)
ocean = Ocean(config)
```
Expand Down
2 changes: 1 addition & 1 deletion READMEs/setup-local.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ In the Python console:
```python
# Create Ocean instance
from ocean_lib.example_config import get_config_dict
config = get_config_dict("development")
config = get_config_dict("http://localhost:8545")

from ocean_lib.ocean.ocean import Ocean
ocean = Ocean(config)
Expand Down
20 changes: 3 additions & 17 deletions READMEs/setup-remote.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ Let's go!

All [Ocean chain deployments](https://docs.oceanprotocol.com/discover/networks) (Eth mainnet, Polygon, etc) are supported.

Export env vars of the format `NETWORKNAME_RPC_URL` e.g. `export POLYGON_RPC_URL=https://polygon-rpc.com`

In case you have an Infura project, you need to also export the `WEB3_INFURA_PROJECT_ID` variable *alongside* the base rpc urls.

#### 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>`


## 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!
Expand Down Expand Up @@ -99,9 +89,7 @@ As usual, Linux/MacOS needs "`export`" and Windows needs "`set`". In the console
export REMOTE_TEST_PRIVATE_KEY1=<your REMOTE_TEST_PRIVATE_KEY1>
export REMOTE_TEST_PRIVATE_KEY2=<your REMOTE_TEST_PRIVATE_KEY2>

# network rpc url, e.g.
export MUMBAI_RPC_URL=https://rpc-mumbai.maticvigil.com
export POLYGON_RPC_URL=https://polygon-rpc.com
export MUMBAI_RPC_URL=<your RPC_URL> # exported used for convenience/security, you can also use the direct URL string later
```


Expand All @@ -111,9 +99,7 @@ export POLYGON_RPC_URL=https://polygon-rpc.com
set REMOTE_TEST_PRIVATE_KEY1=<your REMOTE_TEST_PRIVATE_KEY1>
set REMOTE_TEST_PRIVATE_KEY2=<your REMOTE_TEST_PRIVATE_KEY2>

# network rpc url, e.g.
set MUMBAI_RPC_URL=https://rpc-mumbai.maticvigil.com
set POLYGON_RPC_URL=https://polygon-rpc.com
set MUMBAI_RPC_URL=<your RPC_URL> # exported used for convenience/security, you can also use the direct URL string later
```

Optionally, chainlist.org has other RPCs for [Mumbai](https://chainlist.org/chain/80001) and [Polygon](https://chainlist.org/chain/137).
Expand All @@ -131,7 +117,7 @@ In the Python console:
import os
from ocean_lib.example_config import get_config_dict
from ocean_lib.ocean.ocean import Ocean
config = get_config_dict("mumbai")
config = get_config_dict(os.getenv("MUMBAI_RPC_URL")) # you can also input the string directly
ocean = Ocean(config)

# Create OCEAN object. ocean_lib knows where OCEAN is on all remote networks
Expand Down
79 changes: 35 additions & 44 deletions ocean_lib/example_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import os
from pathlib import Path
from typing import Optional

import addresses
from enforce_typing import enforce_types
Expand All @@ -22,73 +23,63 @@
DEFAULT_PROVIDER_URL = "http://172.15.0.4:8030"

config_defaults = {
"NETWORK_NAME": "development",
"METADATA_CACHE_URI": "http://172.15.0.5:5000",
"PROVIDER_URL": "http://172.15.0.4:8030",
"DOWNLOADS_PATH": "consume-downloads",
}

PROVIDER_PER_NETWORK = {
"mainnet": "https://v4.provider.mainnet.oceanprotocol.com",
"goerli": "https://v4.provider.goerli.oceanprotocol.com",
"bsc": "https://v4.provider.bsc.oceanprotocol.com",
"polygon": "https://v4.provider.polygon.oceanprotocol.com",
"energyweb": "https://v4.provider.energyweb.oceanprotocol.com",
"moonriver": "https://v4.provider.moonriver.oceanprotocol.com",
"moonbase": "https://v4.provider.moonbase.oceanprotocol.com",
"mumbai": "https://v4.provider.mumbai.oceanprotocol.com",
"sepolia": "https://v4.provider.oceanprotocol.com",
"development": DEFAULT_PROVIDER_URL,
1: "https://v4.provider.mainnet.oceanprotocol.com",
5: "https://v4.provider.goerli.oceanprotocol.com",
56: "https://v4.provider.bsc.oceanprotocol.com",
137: "https://v4.provider.polygon.oceanprotocol.com",
246: "https://v4.provider.energyweb.oceanprotocol.com",
1285: "https://v4.provider.moonriver.oceanprotocol.com",
1287: "https://v4.provider.moonbase.oceanprotocol.com",
80001: "https://v4.provider.mumbai.oceanprotocol.com",
58008: "https://v4.provider.oceanprotocol.com",
8996: DEFAULT_PROVIDER_URL,
}


def get_rpc_url(network_name: str) -> str:
"""Return the RPC URL for a given network."""
if network_name == "development":
if os.getenv("DEVELOPMENT_RPC_URL"):
return os.getenv("DEVELOPMENT_RPC_URL")

return "http://localhost:8545"

base_url = None

if os.getenv(f"{network_name.upper()}_RPC_URL"):
base_url = os.getenv(f"{network_name.upper()}_RPC_URL")

if os.getenv("WEB3_INFURA_PROJECT_ID"):
base_url = f"{base_url}{os.getenv('WEB3_INFURA_PROJECT_ID')}"

if base_url:
return base_url

raise Exception(f"Need to set {network_name.upper()}_RPC_URL env variable.")
NAME_PER_NETWORK = {
1: "mainnet",
5: "goerli",
56: "bsc",
137: "polygon",
246: "energyweb",
1285: "moonriver",
1287: "moonbase",
80001: "mumbai",
58008: "sepolia",
8996: "development",
}


def get_config_dict(network_name=None) -> dict:
def get_config_dict(network_url: Optional[str] = None) -> dict:
"""Return config dict containing default values for a given network.
Chain ID is determined by querying the RPC specified by network_url.
"""
if not network_name or network_name in ["ganache", "development"]:
network_name = "development"

if network_name not in PROVIDER_PER_NETWORK:
raise ValueError("The chain id for the specific RPC could not be fetched!")

network_url = get_rpc_url(network_name)
if not network_url:
network_url = "http://localhost:8545"

config_dict = copy.deepcopy(config_defaults)
config_dict["PROVIDER_URL"] = PROVIDER_PER_NETWORK[network_name]
config_dict["NETWORK_NAME"] = network_name
config_dict["web3_instance"] = get_web3(network_url)
config_dict["CHAIN_ID"] = config_dict["web3_instance"].eth.chain_id

if network_name != "development":
chain_id = config_dict["CHAIN_ID"]
if chain_id not in PROVIDER_PER_NETWORK:
raise ValueError("The chain id for the specific RPC could not be fetched!")

config_dict["PROVIDER_URL"] = PROVIDER_PER_NETWORK[chain_id]
config_dict["NETWORK_NAME"] = NAME_PER_NETWORK[chain_id]

if chain_id != 8996:
config_dict["METADATA_CACHE_URI"] = METADATA_CACHE_URI

if os.getenv("ADDRESS_FILE"):
base_file = os.getenv("ADDRESS_FILE")
address_file = os.path.expanduser(base_file)
elif network_name == "development":
elif chain_id == 8996:
# this is auto-created when barge is run
base_file = "~/.ocean/ocean-contracts/artifacts/address.json"
address_file = os.path.expanduser(base_file)
Expand Down
3 changes: 3 additions & 0 deletions ocean_lib/ocean/ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def __init__(self, config_dict: Dict, data_provider: Optional[Type] = None) -> N
if "web3_instance" not in config_dict:
config_errors["web3_instance"] = "required"

if "NETWORK_NAME" not in config_dict:
config_errors["NETWORK_NAME"] = "required"

if config_errors:
raise Exception(json.dumps(config_errors))

Expand Down
26 changes: 2 additions & 24 deletions ocean_lib/test/test_example_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,15 @@ def test_ganache_example_config():
@pytest.mark.unit
def test_polygon_example_config():
"""Tests the config structure of Polygon network."""
config = get_config_dict("polygon")
config = get_config_dict("https://polygon-rpc.com")

assert config["METADATA_CACHE_URI"] == METADATA_CACHE_URI
assert config["PROVIDER_URL"] == "https://v4.provider.polygon.oceanprotocol.com"


@pytest.mark.unit
def test_bsc_example_config(monkeypatch):
"""Tests the config structure of BSC network."""
monkeypatch.setenv("BSC_RPC_URL", "http://localhost:8545")

config = get_config_dict("bsc")

assert config["METADATA_CACHE_URI"] == METADATA_CACHE_URI
assert config["PROVIDER_URL"] == "https://v4.provider.bsc.oceanprotocol.com"


@pytest.mark.unit
def test_moonbeam_alpha_example_config(monkeypatch):
"""Tests the config structure of Moonbeam Alpha network."""
monkeypatch.setenv("MOONBASE_RPC_URL", "http://localhost:8545")

config = get_config_dict("moonbase")

assert config["METADATA_CACHE_URI"] == METADATA_CACHE_URI
assert config["PROVIDER_URL"] == "https://v4.provider.moonbase.oceanprotocol.com"


@pytest.mark.unit
def test_get_address_of_type():
config = get_config_dict("polygon")
config = get_config_dict("https://polygon-rpc.com")

data_nft_factory = get_address_of_type(config, DataNFTFactoryContract.CONTRACT_NAME)
addresses = get_contracts_addresses(config)
Expand Down
2 changes: 0 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ env =
D:TEST_PRIVATE_KEY8=0x1263dc73bef43a9da06149c7e598f52025bf4027f1d6c13896b71e81bb9233fb
D:FACTORY_DEPLOYER_PRIVATE_KEY=0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58
D:PROVIDER_PRIVATE_KEY=0xfd5c1ccea015b6d663618850824154a3b3fb2882c46cefb05b9a93fea8c3d215
D:MUMBAI_RPC_URL=https://rpc-mumbai.maticvigil.com
D:POLYGON_RPC_URL=https://polygon-rpc.com
D:ADDRESS_FILE=~/.ocean/ocean-contracts/artifacts/address.json
14 changes: 12 additions & 2 deletions tests/integration/remote/test_mumbai_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
# Copyright 2023 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
import os

from ocean_lib.example_config import get_config_dict
from ocean_lib.ocean.ocean import Ocean

from . import util


def _get_mumbai_rpc():
infura_id = os.getenv("WEB3_INFURA_PROJECT_ID")

if not infura_id:
return "https://rpc-mumbai.maticvigil.com"

return f"https://polygon-mumbai.infura.io/v3/{infura_id}"


def test_nonocean_tx(tmp_path, monkeypatch):
"""Do a simple non-Ocean tx on Mumbai. Only use Ocean config"""
monkeypatch.delenv("ADDRESS_FILE")
# setup

config = get_config_dict("mumbai")
config = get_config_dict(_get_mumbai_rpc())
ocean = Ocean(config)
(alice_wallet, bob_wallet) = util.get_wallets()

Expand All @@ -27,7 +37,7 @@ def test_ocean_tx__create(tmp_path, monkeypatch):
monkeypatch.delenv("ADDRESS_FILE")
# setup

config = get_config_dict("mumbai")
config = get_config_dict(_get_mumbai_rpc())
ocean = Ocean(config)

(alice_wallet, _) = util.get_wallets()
Expand Down
13 changes: 12 additions & 1 deletion tests/integration/remote/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright 2023 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
import os

import pytest

from ocean_lib.example_config import get_config_dict
Expand All @@ -10,13 +12,22 @@
from . import util


def _get_polygon_rpc():
infura_id = os.getenv("WEB3_INFURA_PROJECT_ID")

if not infura_id:
return "https://polygon-rpc.com"

return f"https://polygon-mainnet.infura.io/v3/{infura_id}"


@pytest.mark.integration
def test_ocean_tx__create(tmp_path, monkeypatch):
"""On Polygon, do a simple Ocean tx: create"""
monkeypatch.delenv("ADDRESS_FILE")

# setup
config = get_config_dict("polygon")
config = get_config_dict(_get_polygon_rpc())
ocean = Ocean(config)

(alice_wallet, _) = util.get_wallets()
Expand Down

0 comments on commit 8e07749

Please sign in to comment.