Skip to content

Commit

Permalink
Merge pull request #391 from tellor-io/fix-tests
Browse files Browse the repository at this point in the history
fixed tests to use sepolia / goerli
  • Loading branch information
oraclown authored Oct 2, 2023
2 parents 68f5859 + 0fde1d2 commit 9c5e33b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 57 deletions.
16 changes: 8 additions & 8 deletions src/telliot_core/apps/telliot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,25 @@ def override_test_config(cfg: TelliotConfig, write: bool = False) -> TelliotConf

# Override configuration for rinkeby testnet
override_main = False
if cfg.main.chain_id != 4:
cfg.main.chain_id = 4
if cfg.main.chain_id != 11155111:
cfg.main.chain_id = 11155111
override_main = True

rinkeby_endpoint = cfg.get_endpoint()
assert rinkeby_endpoint is not None
sepolia_endpoint = cfg.get_endpoint()
assert sepolia_endpoint is not None

override_endpoint = False
if os.getenv("NODE_URL", None):
rinkeby_endpoint.url = os.environ["NODE_URL"]
sepolia_endpoint.url = os.environ["NODE_URL"]
override_endpoint = True

rinkeby_accounts = find_accounts(chain_id=4)
if not rinkeby_accounts:
sepolia_accounts = find_accounts(chain_id=11155111)
if not sepolia_accounts:

# Add private key if detected on git
if os.getenv("PRIVATE_KEY", None):
# Create an account for use on git
ChainedAccount.add("git-rinkeby-key", chains=[4], key=os.environ["PRIVATE_KEY"], password="")
ChainedAccount.add("git-sepolia-key", chains=[11155111], key=os.environ["PRIVATE_KEY"], password="")

if write:
if override_endpoint:
Expand Down
6 changes: 3 additions & 3 deletions src/telliot_core/model/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RPCEndpoint(Base):
#: Chain ID
chain_id: Optional[int] = None

#: Network Name (e.g. 'mainnet', 'testnet', 'rinkeby')
#: Network Name (e.g. 'mainnet', 'testnet', 'sepolia')
network: str = ""

#: Provider Name (e.g. 'Infura')
Expand Down Expand Up @@ -61,8 +61,8 @@ def connect(self) -> bool:
else:
raise ValueError(f"Invalid endpoint url: {self.url}")

# Inject middleware if connecting to rinkeby (chain_id=4)
if self.chain_id == 4:
# Inject middleware if connecting to sepolia (chain_id=11155111)
if self.chain_id == 11155111:
self.web3.middleware_onion.inject(geth_poa_middleware, layer=0)

connected = False
Expand Down
60 changes: 47 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,59 @@


@pytest.fixture(scope="session", autouse=True)
def rinkeby_cfg():
"""Get rinkeby endpoint from config
def sepolia_cfg():
"""Get sepolia endpoint from config
If environment variables are defined, they will override the values in config files
"""
cfg = TelliotConfig()

# Override configuration for rinkeby testnet
cfg.main.chain_id = 4
# Override configuration for sepolia testnet
cfg.main.chain_id = 11155111

rinkeby_endpoint = cfg.get_endpoint()
# assert rinkeby_endpoint.network == "rinkeby"
sepolia_endpoint = cfg.get_endpoint()
# assert sepolia_endpoint.network == "sepolia"

if os.getenv("NODE_URL", None):
rinkeby_endpoint.url = os.environ["NODE_URL"]
sepolia_endpoint.url = os.environ["NODE_URL"]

rinkeby_accounts = find_accounts(chain_id=4)
if not rinkeby_accounts:
sepolia_accounts = find_accounts(chain_id=11155111)
if not sepolia_accounts:
# Create a test account using PRIVATE_KEY defined on github.
key = os.getenv("PRIVATE_KEY", None)
if key:
ChainedAccount.add("git-rinkeby-key", chains=4, key=os.environ["PRIVATE_KEY"], password="")
ChainedAccount.add("git-sepolia-key", chains=11155111, key=os.environ["PRIVATE_KEY"], password="")
else:
raise Exception("Need a rinkeby account")
raise Exception("Need a sepolia account")

return cfg


@pytest.fixture(scope="session", autouse=True)
def goerli_cfg():
"""Get goerli endpoint from config
If environment variables are defined, they will override the values in config files
"""
cfg = TelliotConfig()

# Override configuration for goerli testnet
cfg.main.chain_id = 5

goerli_endpoint = cfg.get_endpoint()
# assert goerli_endpoint.network == "goerli"

if os.getenv("NODE_URL", None):
goerli_endpoint.url = os.environ["NODE_URL"]

goerli_accounts = find_accounts(chain_id=5)
if not goerli_accounts:
# Create a test account using PRIVATE_KEY defined on github.
key = os.getenv("PRIVATE_KEY", None)
if key:
ChainedAccount.add("git-goerli-key", chains=5, key=os.environ["PRIVATE_KEY"], password="")
else:
raise Exception("Need a goerli account")

return cfg

Expand Down Expand Up @@ -117,5 +146,10 @@ def mumbai_test_cfg(scope="session", autouse=True):


@pytest.fixture
def rinkeby_test_cfg(scope="session", autouse=True):
return local_node_cfg(chain_id=4)
def sepolia_test_cfg(scope="session", autouse=True):
return local_node_cfg(chain_id=11155111)


@pytest.fixture
def goerli_test_cfg(scope="session", autouse=True):
return local_node_cfg(chain_id=5)
18 changes: 9 additions & 9 deletions tests/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@


@pytest.mark.asyncio
async def test_connect_to_tellor(rinkeby_test_cfg):
async def test_connect_to_tellor(sepolia_test_cfg):
"""Contract object should access Tellor functions"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
tellorx = core.get_tellorx_contracts()
assert len(tellorx.master.contract.all_functions()) > 0
async with TelliotCore(config=sepolia_test_cfg) as core:
tellor360 = core.get_tellor360_contracts()
assert len(tellor360.token.contract.all_functions()) > 0
assert isinstance(
tellorx.master.contract.all_functions()[0],
tellor360.token.contract.all_functions()[0],
web3.contract.ContractFunction,
)


@pytest.mark.asyncio
async def test_mixed_gas_inputs(rinkeby_test_cfg):
async def test_mixed_gas_inputs(sepolia_test_cfg):
"""Contract.write() should refuse a combination of
legacy gas args and EIP-1559 gas args"""

with pytest.raises(ValueError):

async with TelliotCore(config=rinkeby_test_cfg) as core:
tellorx = core.get_tellorx_contracts()
async with TelliotCore(config=sepolia_test_cfg) as core:
tellor360 = core.get_tellor360_contracts()

tx_receipt, status = await tellorx.oracle.write(
tx_receipt, status = await tellor360.oracle.write(
func_name="transfer",
_to="0xF90cd1D6C1da49CE2cF5C39f82999D7145aa66aD",
_amount=1,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pytest
from requests.exceptions import SSLError

from telliot_core.gas.legacy_gas import fetch_gas_price
from telliot_core.gas.legacy_gas import legacy_gas_station
from src.telliot_core.gas.legacy_gas import fetch_gas_price
from src.telliot_core.gas.legacy_gas import legacy_gas_station


def raise_ssl_error(*args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_master_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def tellorx_master_mock_contract():


@pytest.mark.asyncio
async def test_get_staker_info(rinkeby_test_cfg, tellorx_master_mock_contract):
async def test_get_staker_info(goerli_test_cfg, tellorx_master_mock_contract):
"""Test the TellorXMaster contract"""

async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
tellorx = TellorxMasterContract(core.endpoint, account)
tellorx.address = tellorx_master_mock_contract.address
Expand Down
28 changes: 14 additions & 14 deletions tests/test_oracle_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def tellorx_oracle_mock_contract():


@pytest.mark.asyncio
async def test_getReportTimestampByIndex(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_getReportTimestampByIndex(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorxOracleContract getReportTimestampByIndex method"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand All @@ -32,9 +32,9 @@ async def test_getReportTimestampByIndex(rinkeby_test_cfg, tellorx_oracle_mock_c


@pytest.mark.asyncio
async def test_getReportingLock(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_getReportingLock(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorxOracleContract.getReportingLock() method"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand All @@ -47,9 +47,9 @@ async def test_getReportingLock(rinkeby_test_cfg, tellorx_oracle_mock_contract):


@pytest.mark.asyncio
async def test_gettimebasedreward(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_gettimebasedreward(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorxOracleContract.getTimeBasedReward() method"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand All @@ -61,9 +61,9 @@ async def test_gettimebasedreward(rinkeby_test_cfg, tellorx_oracle_mock_contract


@pytest.mark.asyncio
async def test_getCurrentReward(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_getCurrentReward(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorX oracle contract"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand All @@ -78,9 +78,9 @@ async def test_getCurrentReward(rinkeby_test_cfg, tellorx_oracle_mock_contract):


@pytest.mark.asyncio
async def test_getTimestampCountById(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_getTimestampCountById(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorX oracle contract"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand All @@ -94,9 +94,9 @@ async def test_getTimestampCountById(rinkeby_test_cfg, tellorx_oracle_mock_contr


@pytest.mark.asyncio
async def test_getTimeOfLastNewValue(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_getTimeOfLastNewValue(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorX oracle contract"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand All @@ -110,9 +110,9 @@ async def test_getTimeOfLastNewValue(rinkeby_test_cfg, tellorx_oracle_mock_contr


@pytest.mark.asyncio
async def test_getTipsById(rinkeby_test_cfg, tellorx_oracle_mock_contract):
async def test_getTipsById(goerli_test_cfg, tellorx_oracle_mock_contract):
"""Test the TellorX oracle contract"""
async with TelliotCore(config=rinkeby_test_cfg) as core:
async with TelliotCore(config=goerli_test_cfg) as core:
account = core.get_account()
oracle = TellorxOracleContract(core.endpoint, account)
oracle.address = tellorx_oracle_mock_contract.address # Override with locally-deployed mock contract address
Expand Down
12 changes: 6 additions & 6 deletions tests/test_rpc_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import pytest
from brownie import chain
from requests.exceptions import ConnectionError
from requests.exceptions import HTTPError

from telliot_core.model.endpoints import EndpointList
Expand Down Expand Up @@ -33,12 +34,14 @@ def test_very_bad_rpc_url():

def test_incomplete_rpc_url():
"""an incomplete url will raise an exception in RPCEndpoint"""
url = "https://eth-rinkeby.gateway.pokt.network/v1/lb/"
url = "https://eth-rpc.gateway.pokt.networ/"
endpt = RPCEndpoint(network=network, provider=provider, url=url)
try:
_ = endpt.connect()
except HTTPError:
pass # expected
except ConnectionError:
pass # expected
except ValueError as e:
assert "Invalid request path" in str(e)
else:
Expand All @@ -48,8 +51,5 @@ def test_incomplete_rpc_url():
def test_endpoint_list():
sl = EndpointList()
# print(json.dumps(sl.get_state(), indent=2))
ep4 = sl.find(chain_id=4)[0]
assert ep4.network == "rinkeby"

ep3 = sl.find(chain_id=3)[0]
assert ep3.network == "ropsten"
ep11155111 = sl.find(chain_id=11155111)[0]
assert ep11155111.network == "sepolia"

0 comments on commit 9c5e33b

Please sign in to comment.