Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Arbitrum, Optimism and Polygon support #46

Merged
merged 10 commits into from
Sep 9, 2022
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ venv/
ENV/
env.bak/
venv.bak/
.idea/

# mkdocs documentation
/site
Expand Down
34 changes: 25 additions & 9 deletions ape_infura/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@

from .providers import Infura

NETWORKS = [
"mainnet",
"ropsten",
"rinkeby",
"kovan",
"goerli",
]
NETWORKS = {
"ethereum": [
"mainnet",
"ropsten",
"rinkeby",
"kovan",
"goerli",
],
"arbitrum": [
"mainnet",
"testnet",
],
"optimism": [
"mainnet",
"kovan",
"goerli",
],
"polygon": [
"mainnet",
"mumbai",
],
}


@plugins.register(plugins.ProviderPlugin)
def providers():
for network_name in NETWORKS:
yield "ethereum", network_name, Infura
for ecosystem_name in NETWORKS:
for network_name in NETWORKS[ecosystem_name]:
yield ecosystem_name, network_name, Infura
20 changes: 14 additions & 6 deletions ape_infura/providers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Dict
from typing import Dict, Tuple

from ape.api import UpstreamProvider, Web3Provider
from ape.exceptions import ContractLogicError, ProviderError, VirtualMachineError
Expand All @@ -24,13 +24,14 @@ def __init__(self):


class Infura(Web3Provider, UpstreamProvider):
network_uris: Dict[str, str] = {}
network_uris: Dict[Tuple[str, str], str] = {}

@property
def uri(self) -> str:
ecosystem_name = self.network.ecosystem.name
network_name = self.network.name
if network_name in self.network_uris:
return self.network_uris[network_name]
if (ecosystem_name, network_name) in self.network_uris:
return self.network_uris[(ecosystem_name, network_name)]

key = None
for env_var_name in _ENVIRONMENT_VARIABLE_NAMES:
Expand All @@ -42,8 +43,15 @@ def uri(self) -> str:
if not key:
raise MissingProjectKeyError()

network_uri = f"https://{self.network.name}.infura.io/v3/{key}"
self.network_uris[network_name] = network_uri
prefix = f"{ecosystem_name}-" if ecosystem_name != "ethereum" else ""

# currently only "testnet" is supported on arbitrum (rinkeby in ape-arbitrum)
# need uri to contain "rinkeby" and self.network_uris to have key ("arbitrum", "testnet")
network_name_in_uri = network_name
if ecosystem_name == "arbitrum" and network_name == "testnet":
network_name_in_uri = "rinkeby"
network_uri = f"https://{prefix}{network_name_in_uri}.infura.io/v3/{key}"
self.network_uris[(ecosystem_name, network_name)] = network_uri
return network_uri

@property
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"pytest-xdist", # multi-process runner
"pytest-cov", # Coverage analyzer plugin
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
"ape-arbitrum>=0.4.0a,<0.5",
"ape-optimism>=0.4.0a,<0.5",
"ape-polygon>=0.4.0a,<0.5",
],
"lint": [
"black>=22.6.0", # auto-formatter and linter
Expand Down
21 changes: 21 additions & 0 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,24 @@ def test_provider_works():
with networks.ethereum.mainnet.use_provider("infura") as provider:
assert isinstance(provider, Infura)
assert provider.get_balance("0x0000000000000000000000000000000000000000") > 0
assert provider.uri.startswith("https://mainnet.infura.io/v3/")

with networks.arbitrum.mainnet.use_provider("infura") as provider:
assert isinstance(provider, Infura)
assert provider.get_balance("0x0000000000000000000000000000000000000000") > 0
assert provider.uri.startswith("https://arbitrum-mainnet.infura.io/v3/")

with networks.arbitrum.testnet.use_provider("infura") as provider:
assert isinstance(provider, Infura)
assert provider.get_balance("0x0000000000000000000000000000000000000000") > 0
assert provider.uri.startswith("https://arbitrum-rinkeby.infura.io/v3/")

with networks.optimism.mainnet.use_provider("infura") as provider:
assert isinstance(provider, Infura)
assert provider.get_balance("0x0000000000000000000000000000000000000000") > 0
assert provider.uri.startswith("https://optimism-mainnet.infura.io/v3/")

with networks.polygon.mumbai.use_provider("infura") as provider:
assert isinstance(provider, Infura)
assert provider.get_balance("0x0000000000000000000000000000000000000000") > 0
assert provider.uri.startswith("https://polygon-mumbai.infura.io/v3/")