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

Remove EthereumClientProvider #1209

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gnosis/eth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# flake8: noqa F401
from .ethereum_client import (
EthereumClient,
EthereumClientProvider,
EthereumTxSent,
FromAddressNotFound,
GasLimitExceeded,
Expand All @@ -15,13 +14,14 @@
TransactionQueueLimitReached,
TxSpeed,
UnknownAccount,
get_auto_ethereum_client,
)
from .ethereum_network import EthereumNetwork, EthereumNetworkNotSupported
from .exceptions import InvalidERC20Info, InvalidERC721Info

__all__ = [
"EthereumClient",
"EthereumClientProvider",
"get_auto_ethereum_client",
"EthereumTxSent",
"FromAddressNotFound",
"GasLimitExceeded",
Expand Down
3 changes: 1 addition & 2 deletions gnosis/eth/clients/ens_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
from dataclasses import dataclass
from functools import cache
from typing import Any, Dict, List, Optional, Union

import requests
from eth_typing import HexStr
from hexbytes import HexBytes

from gnosis.util import cache


class EnsClient:
"""
Expand Down
3 changes: 1 addition & 2 deletions gnosis/eth/clients/sourcify_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import os
from functools import cache
from typing import Any, Dict, List, Optional
from urllib.parse import urljoin

from gnosis.util import cache

from ...util.http import prepare_http_session
from .. import EthereumNetwork
from ..utils import fast_is_checksum_address
Expand Down
3 changes: 1 addition & 2 deletions gnosis/eth/contracts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
import json
import os
import sys
from functools import cache
from typing import Any, Callable, Dict, Optional

from eth_typing import ChecksumAddress
from hexbytes import HexBytes
from web3 import Web3
from web3.contract import Contract

from gnosis.util import cache

from .abis.multicall import multicall_v3_abi, multicall_v3_bytecode
from .contract_base import ContractBase

Expand Down
53 changes: 29 additions & 24 deletions gnosis/eth/ethereum_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from enum import Enum
from functools import cached_property, wraps
from functools import cache, cached_property, wraps
from logging import getLogger
from typing import (
Any,
Expand Down Expand Up @@ -59,7 +59,7 @@
mk_contract_address,
mk_contract_address_2,
)
from gnosis.util import cache, chunks
from gnosis.util import chunks

from ..util.http import prepare_http_session
from .constants import (
Expand Down Expand Up @@ -177,28 +177,33 @@ class TxSpeed(Enum):
FASTEST = 6


class EthereumClientProvider:
def __new__(cls):
if not hasattr(cls, "instance"):
try:
from django.conf import settings

ethereum_node_url = settings.ETHEREUM_NODE_URL
except ModuleNotFoundError:
ethereum_node_url = os.environ.get("ETHEREUM_NODE_URL")

cls.instance = EthereumClient(
ethereum_node_url,
provider_timeout=int(os.environ.get("ETHEREUM_RPC_TIMEOUT", 10)),
slow_provider_timeout=int(
os.environ.get("ETHEREUM_RPC_SLOW_TIMEOUT", 60)
),
retry_count=int(os.environ.get("ETHEREUM_RPC_RETRY_COUNT", 1)),
batch_request_max_size=int(
os.environ.get("ETHEREUM_RPC_BATCH_REQUEST_MAX_SIZE", 500)
),
)
return cls.instance
@cache
def get_auto_ethereum_client() -> "EthereumClient":
"""
Use environment variables to configure `EthereumClient` and build a singleton:
- `ETHEREUM_NODE_URL`: No default.
- `ETHEREUM_RPC_TIMEOUT`: `10` by default.
- `ETHEREUM_RPC_SLOW_TIMEOUT`: `60` by default.
- `ETHEREUM_RPC_RETRY_COUNT`: `60` by default.
- `ETHEREUM_RPC_BATCH_REQUEST_MAX_SIZE`: `500` by default.

:return: A configured singleton of EthereumClient
"""
try:
from django.conf import settings

ethereum_node_url = settings.ETHEREUM_NODE_URL
except ModuleNotFoundError:
ethereum_node_url = os.environ.get("ETHEREUM_NODE_URL")
return EthereumClient(
ethereum_node_url,
provider_timeout=int(os.environ.get("ETHEREUM_RPC_TIMEOUT", 10)),
slow_provider_timeout=int(os.environ.get("ETHEREUM_RPC_SLOW_TIMEOUT", 60)),
retry_count=int(os.environ.get("ETHEREUM_RPC_RETRY_COUNT", 1)),
batch_request_max_size=int(
os.environ.get("ETHEREUM_RPC_BATCH_REQUEST_MAX_SIZE", 500)
),
)


class EthereumClientManager:
Expand Down
4 changes: 2 additions & 2 deletions gnosis/eth/tests/ethereum_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from web3.contract import Contract
from web3.types import TxParams

from ..ethereum_client import EthereumClient, EthereumClientProvider
from ..ethereum_client import EthereumClient, get_auto_ethereum_client
from ..multicall import Multicall
from .utils import deploy_erc20, send_tx

Expand Down Expand Up @@ -35,7 +35,7 @@ def setUpClass(cls):
cls.ethereum_client = _cached_data["ethereum_client"]

if not cls.ethereum_client:
cls.ethereum_client = EthereumClientProvider()
cls.ethereum_client = get_auto_ethereum_client()
Multicall.deploy_contract(cls.ethereum_client, cls.ethereum_test_account)
_cached_data["ethereum_client"] = cls.ethereum_client

Expand Down
6 changes: 3 additions & 3 deletions gnosis/eth/tests/test_ethereum_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from ..contracts import get_erc20_contract
from ..ethereum_client import (
EthereumClient,
EthereumClientProvider,
EthereumNetwork,
FromAddressNotFound,
InsufficientFunds,
InvalidNonce,
SenderAccountNotFoundInNode,
TracingManager,
get_auto_ethereum_client,
)
from ..exceptions import BatchCallException, ChainIdIsRequired, InvalidERC20Info
from .ethereum_test_case import EthereumTestCaseMixin
Expand Down Expand Up @@ -922,8 +922,8 @@ def test_estimate_data_gas(self):
)

def test_provider_singleton(self):
ethereum_client1 = EthereumClientProvider()
ethereum_client2 = EthereumClientProvider()
ethereum_client1 = get_auto_ethereum_client()
ethereum_client2 = get_auto_ethereum_client()
self.assertEqual(ethereum_client1, ethereum_client2)

def test_send_eth_to(self):
Expand Down
2 changes: 1 addition & 1 deletion gnosis/safe/proxy_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import secrets
from abc import ABCMeta
from functools import cache
from typing import Callable, Optional

from eth_abi.packed import encode_packed
Expand Down Expand Up @@ -28,7 +29,6 @@
get_empty_tx_params,
mk_contract_address_2,
)
from gnosis.util import cache


class ProxyFactory(ContractBase, metaclass=ABCMeta):
Expand Down
4 changes: 2 additions & 2 deletions gnosis/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa F401
from .util import cache, chunks
from .util import chunks

__all__ = ["cache", "chunks"]
__all__ = ["chunks"]
7 changes: 0 additions & 7 deletions gnosis/util/util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
from typing import Any, Iterable, Sequence

try:
from functools import cache
except ImportError:
from functools import lru_cache

cache = lru_cache(maxsize=None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch 👍



def chunks(elements: Sequence[Any], n: int) -> Iterable[Any]:
"""
Expand Down
Loading