From eee59effec11369839b914e753cc1568e177fdde Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria Date: Mon, 28 Mar 2022 11:45:14 +0200 Subject: [PATCH] Fix chainId for not existing networks on our enum --- gnosis/eth/ethereum_client.py | 15 +++++++++++---- gnosis/eth/ethereum_network.py | 4 ++-- gnosis/eth/tests/test_ethereum_client.py | 13 ++++--------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/gnosis/eth/ethereum_client.py b/gnosis/eth/ethereum_client.py index 41c0b308b..f1c0e8834 100644 --- a/gnosis/eth/ethereum_client.py +++ b/gnosis/eth/ethereum_client.py @@ -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: @@ -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 diff --git a/gnosis/eth/ethereum_network.py b/gnosis/eth/ethereum_network.py index fb0b20583..5ac919aff 100644 --- a/gnosis/eth/ethereum_network.py +++ b/gnosis/eth/ethereum_network.py @@ -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 @@ -189,7 +190,6 @@ class EthereumNetwork(Enum): GATHER_MAINNET = 192837465 EVMOS_TESTNET = 9000 EVMOS_MAINNET = 9001 - default = UNKNOWN @classmethod def _missing_(cls, value): diff --git a/gnosis/eth/tests/test_ethereum_client.py b/gnosis/eth/tests/test_ethereum_client.py index 91980263b..aef914b94 100644 --- a/gnosis/eth/tests/test_ethereum_client.py +++ b/gnosis/eth/tests/test_ethereum_client.py @@ -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) @@ -906,11 +901,11 @@ 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 @@ -918,7 +913,7 @@ def test_get_ethereum_network(self): 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 @@ -926,7 +921,7 @@ def test_get_ethereum_network(self): 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