From 8e07749d92a067ad79b4571620130a04d5c9b299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83lina=20Cenan?= Date: Wed, 1 Nov 2023 08:04:39 +0200 Subject: [PATCH] Change config to use RPC URL directly. (#1488) * Change config to use RPC URL directly. * Fix incomplete test. * Add infura rpcs for testing. --- .github/workflows/libcheck.yml | 1 + .github/workflows/pytest.yml | 1 + .gitignore | 1 + READMEs/services.md | 2 +- READMEs/setup-local.md | 2 +- READMEs/setup-remote.md | 20 +---- ocean_lib/example_config.py | 79 +++++++++----------- ocean_lib/ocean/ocean.py | 3 + ocean_lib/test/test_example_config.py | 26 +------ pytest.ini | 2 - tests/integration/remote/test_mumbai_main.py | 14 +++- tests/integration/remote/test_polygon.py | 13 +++- 12 files changed, 72 insertions(+), 92 deletions(-) diff --git a/.github/workflows/libcheck.yml b/.github/workflows/libcheck.yml index f73510a88..f326687d7 100644 --- a/.github/workflows/libcheck.yml +++ b/.github/workflows/libcheck.yml @@ -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 diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 0e7e13af4..fc22bc456 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -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() }} diff --git a/.gitignore b/.gitignore index 56e2905f5..75ac5e405 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ ocean_lib.egg-info/ .tox/ consume-downloads/ coverage.xml +setup-local.sh diff --git a/READMEs/services.md b/READMEs/services.md index 7b365c5a5..48e877bc9 100644 --- a/READMEs/services.md +++ b/READMEs/services.md @@ -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() # returns a dict # (then, here you can update the config dict as you wish) ocean = Ocean(config) ``` diff --git a/READMEs/setup-local.md b/READMEs/setup-local.md index 2f7bf77ba..4bdb8a7be 100644 --- a/READMEs/setup-local.md +++ b/READMEs/setup-local.md @@ -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) diff --git a/READMEs/setup-remote.md b/READMEs/setup-remote.md index 0a0ca2fb3..342f56e57 100644 --- a/READMEs/setup-remote.md +++ b/READMEs/setup-remote.md @@ -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=` -- Windows: in console: `set WEB3_INFURA_PROJECT_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! @@ -99,9 +89,7 @@ As usual, Linux/MacOS needs "`export`" and Windows needs "`set`". In the console export REMOTE_TEST_PRIVATE_KEY1= export 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= # exported used for convenience/security, you can also use the direct URL string later ``` @@ -111,9 +99,7 @@ export POLYGON_RPC_URL=https://polygon-rpc.com set REMOTE_TEST_PRIVATE_KEY1= set 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= # 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). @@ -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 diff --git a/ocean_lib/example_config.py b/ocean_lib/example_config.py index bd2f42fea..e1c2c8362 100644 --- a/ocean_lib/example_config.py +++ b/ocean_lib/example_config.py @@ -7,6 +7,7 @@ import logging import os from pathlib import Path +from typing import Optional import addresses from enforce_typing import enforce_types @@ -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) diff --git a/ocean_lib/ocean/ocean.py b/ocean_lib/ocean/ocean.py index 7efa0adbc..f06389ad3 100644 --- a/ocean_lib/ocean/ocean.py +++ b/ocean_lib/ocean/ocean.py @@ -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)) diff --git a/ocean_lib/test/test_example_config.py b/ocean_lib/test/test_example_config.py index 542a7679f..6ea24451f 100644 --- a/ocean_lib/test/test_example_config.py +++ b/ocean_lib/test/test_example_config.py @@ -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) diff --git a/pytest.ini b/pytest.ini index 75a9b0f74..f23de599f 100644 --- a/pytest.ini +++ b/pytest.ini @@ -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 diff --git a/tests/integration/remote/test_mumbai_main.py b/tests/integration/remote/test_mumbai_main.py index 425060b00..07b38e372 100644 --- a/tests/integration/remote/test_mumbai_main.py +++ b/tests/integration/remote/test_mumbai_main.py @@ -2,6 +2,7 @@ # 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 @@ -9,12 +10,21 @@ 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() @@ -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() diff --git a/tests/integration/remote/test_polygon.py b/tests/integration/remote/test_polygon.py index 77d82138d..10c5637b9 100644 --- a/tests/integration/remote/test_polygon.py +++ b/tests/integration/remote/test_polygon.py @@ -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 @@ -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()