Skip to content

Commit

Permalink
Change config to use RPC URL directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
calina-c committed Oct 25, 2023
1 parent 9419ffe commit f73ed78
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 94 deletions.
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: 1 addition & 19 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 @@ -98,10 +88,6 @@ As usual, Linux/MacOS needs "`export`" and Windows needs "`set`". In the console
# 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>

# network rpc url, e.g.
export MUMBAI_RPC_URL=https://rpc-mumbai.maticvigil.com
export POLYGON_RPC_URL=https://polygon-rpc.com
```


Expand All @@ -110,10 +96,6 @@ export POLYGON_RPC_URL=https://polygon-rpc.com
# 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>

# network rpc url, e.g.
set MUMBAI_RPC_URL=https://rpc-mumbai.maticvigil.com
set POLYGON_RPC_URL=https://polygon-rpc.com
```

Optionally, chainlist.org has other RPCs for [Mumbai](https://chainlist.org/chain/80001) and [Polygon](https://chainlist.org/chain/137).
Expand All @@ -131,7 +113,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("http://rpc-mumbai.maticvigil.com") # or another RPC url...
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
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
6 changes: 4 additions & 2 deletions tests/integration/remote/test_mumbai_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

from . import util

MUMBAI_RPC_URL = "https://rpc-mumbai.maticvigil.com"


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(MUMBAI_RPC_URL)
ocean = Ocean(config)
(alice_wallet, bob_wallet) = util.get_wallets()

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

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

(alice_wallet, _) = util.get_wallets()
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/remote/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

from . import util

POLYGON_RPC_URL = "https://polygon-rpc.com"


@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(POLYGON_RPC_URL)
ocean = Ocean(config)

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

0 comments on commit f73ed78

Please sign in to comment.