From 3f1c1a2f63386d69f56aedc1feab8983d8b9170a Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria <6909403+Uxio0@users.noreply.github.com> Date: Wed, 11 Sep 2024 16:01:51 +0200 Subject: [PATCH] Fix ethereum_client warnings --- safe_eth/eth/ethereum_client.py | 33 +++++++++++++++++++++------------ safe_eth/eth/multicall.py | 2 ++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/safe_eth/eth/ethereum_client.py b/safe_eth/eth/ethereum_client.py index 3f63bb8e..b80471bb 100644 --- a/safe_eth/eth/ethereum_client.py +++ b/safe_eth/eth/ethereum_client.py @@ -11,6 +11,7 @@ Optional, Sequence, Tuple, + Type, Union, cast, ) @@ -109,7 +110,7 @@ def tx_with_exception_handling(func): :param func: :return: """ - error_with_exception: Dict[str, Exception] = { + error_with_exception: Dict[str, Type[Exception]] = { "EIP-155": ChainIdIsRequired, "Transaction with the same hash was already imported": TransactionAlreadyImported, "replacement transaction underpriced": ReplacementTransactionUnderpriced, @@ -466,8 +467,8 @@ def _decode_transfer_log( elif topics_len == 3: # ERC20 Transfer(address indexed from, address indexed to, uint256 value) # 3 topics (transfer topic + from + to) + value_data = HexBytes(data) try: - value_data = HexBytes(data) value = eth_abi.decode(["uint256"], value_data)[0] except DecodingError: logger.warning( @@ -475,8 +476,8 @@ def _decode_transfer_log( value_data.hex(), ) return None + from_to_data = b"".join(topics[1:]) try: - from_to_data = b"".join(topics[1:]) _from, to = ( fast_to_checksum_address(address) for address in eth_abi.decode( @@ -493,8 +494,8 @@ def _decode_transfer_log( elif topics_len == 4: # ERC712 Transfer(address indexed from, address indexed to, uint256 indexed tokenId) # 4 topics (transfer topic + from + to + tokenId) + from_to_token_id_data = b"".join(topics[1:]) try: - from_to_token_id_data = b"".join(topics[1:]) _from, to, token_id = eth_abi.decode( ["address", "address", "uint256"], from_to_token_id_data ) @@ -1473,7 +1474,7 @@ def deploy_and_initialize_contract( initializer_data: Optional[Union[bytes, HexStr]] = None, check_receipt: bool = True, deterministic: bool = True, - ) -> EthereumTxSent: + ) -> Optional[EthereumTxSent]: """ :param deployer_account: @@ -1484,11 +1485,16 @@ def deploy_and_initialize_contract( :return: """ contract_address: Optional[ChecksumAddress] = None + assert ( + constructor_data and initializer_data + ), "At least constructor_data or initializer_data must be provided" + tx_hash: Optional[HexBytes] = None + tx: Optional[TxParams] = None for data in (constructor_data, initializer_data): # Because initializer_data is not mandatory if data: data = HexBytes(data) - tx: TxParams = { + tx = { "from": deployer_account.address, "data": data, "gasPrice": self.w3.eth.gas_price, @@ -1528,7 +1534,9 @@ def deploy_and_initialize_contract( assert tx_receipt assert tx_receipt["status"] - return EthereumTxSent(tx_hash, tx, contract_address) + if tx_hash and tx: + return EthereumTxSent(tx_hash, tx, contract_address) + return None def get_nonce_for_account( self, @@ -1633,6 +1641,8 @@ def estimate_fee_eip1559( percentile = 90 elif tx_speed == TxSpeed.FASTEST: percentile = 100 + else: + percentile = 50 result = self.w3.eth.fee_history(1, "latest", reward_percentiles=[percentile]) # Get next block `base_fee_per_gas` @@ -1755,11 +1765,10 @@ def get_block( def _parse_block_identifier(self, block_identifier: BlockIdentifier) -> str: if isinstance(block_identifier, int): - return hex(block_identifier) + return HexStr(hex(block_identifier)) elif isinstance(block_identifier, bytes): - return HexBytes(block_identifier).hex() - else: - return block_identifier + return HexStr(HexBytes(block_identifier).hex()) + return str(block_identifier) def get_blocks( self, @@ -1820,7 +1829,7 @@ def build_tx_params( :param gas_price: :param nonce: :param chain_id: - :param tx_params: An existing TxParams dictionary will be replaced by the providen values + :param tx_params: An existing TxParams dictionary will be replaced by the provided values :return: """ diff --git a/safe_eth/eth/multicall.py b/safe_eth/eth/multicall.py index b6f29e5c..13798e36 100644 --- a/safe_eth/eth/multicall.py +++ b/safe_eth/eth/multicall.py @@ -115,6 +115,8 @@ def deploy_contract( ethereum_tx_sent = ethereum_client.deploy_and_initialize_contract( deployer_account, constructor_data ) + if not ethereum_tx_sent: + return None assert ethereum_tx_sent.contract_address is not None contract_address = ethereum_tx_sent.contract_address