Skip to content

Commit

Permalink
Fix chainId for not existing networks on our enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Mar 28, 2022
1 parent b585a43 commit eee59ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
15 changes: 11 additions & 4 deletions gnosis/eth/ethereum_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,13 +1339,20 @@ def current_block_number(self):
return self.w3.eth.block_number

@cache
def get_chain_id(self) -> int:
"""
:return: ChainId returned by the RPC `eth_chainId` method. It should never change, so it's cached.
"""
return int(self.w3.eth.chain_id)

def get_network(self) -> EthereumNetwork:
"""
Get network name based on the network version id. It should never change, so it's cached
Get network name based on the chainId
:return: The EthereumNetwork enum type
:return: EthereumNetwork based on the chainId. If network is not
on our list, `EthereumNetwork.UNKOWN` is returned
"""
return EthereumNetwork(int(self.w3.eth.chain_id))
return EthereumNetwork(self.get_chain_id())

@cache
def is_eip1559_supported(self) -> EthereumNetwork:
Expand Down Expand Up @@ -1602,7 +1609,7 @@ def set_eip1559_fees(
del tx["gasPrice"]

if "chainId" not in tx:
tx["chainId"] = self.get_network().value
tx["chainId"] = self.get_chain_id()

tx["maxPriorityFeePerGas"] = max_priority_fee_per_gas
tx["maxFeePerGas"] = base_fee_per_gas + max_priority_fee_per_gas
Expand Down
4 changes: 2 additions & 2 deletions gnosis/eth/ethereum_network.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from enum import Enum
from enum import Enum, unique


class EthereumNetworkNotSupported(Exception):
pass


@unique
class EthereumNetwork(Enum):
"""
Use https://chainlist.org/ as a reference
Expand Down Expand Up @@ -189,7 +190,6 @@ class EthereumNetwork(Enum):
GATHER_MAINNET = 192837465
EVMOS_TESTNET = 9000
EVMOS_MAINNET = 9001
default = UNKNOWN

@classmethod
def _missing_(cls, value):
Expand Down
13 changes: 4 additions & 9 deletions gnosis/eth/tests/test_ethereum_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,6 @@ def test_trace_filter(self):


class TestEthereumNetwork(EthereumTestCaseMixin, TestCase):
def test_default_ethereum_network_name(self):
self.assertEqual(
EthereumNetwork(EthereumNetwork.default), EthereumNetwork.UNKNOWN
)

def test_unknown_ethereum_network_name(self):
self.assertEqual(EthereumNetwork(2), EthereumNetwork.UNKNOWN)

Expand Down Expand Up @@ -906,27 +901,27 @@ def test_get_ethereum_network(self):
self.assertEqual(
self.ethereum_client.get_network(), EthereumNetwork.GANACHE
)
self.ethereum_client.get_network.cache_clear()
self.ethereum_client.get_chain_id.cache_clear()
self.assertEqual(
self.ethereum_client.get_network(), EthereumNetwork.MAINNET
)
self.ethereum_client.get_network.cache_clear()
self.ethereum_client.get_chain_id.cache_clear()

with mock.patch.object(
Eth, "chain_id", return_value=4, new_callable=mock.PropertyMock
):
self.assertEqual(
self.ethereum_client.get_network(), EthereumNetwork.RINKEBY
)
self.ethereum_client.get_network.cache_clear()
self.ethereum_client.get_chain_id.cache_clear()

with mock.patch.object(
Eth, "chain_id", return_value=4815162342, new_callable=mock.PropertyMock
):
self.assertEqual(
self.ethereum_client.get_network(), EthereumNetwork.UNKNOWN
)
self.ethereum_client.get_network.cache_clear()
self.ethereum_client.get_chain_id.cache_clear()

def test_get_nonce(self):
address = Account.create().address
Expand Down

0 comments on commit eee59ef

Please sign in to comment.