From 1ddf8737245ad02c4b227f3bf8c69b223fcef17a Mon Sep 17 00:00:00 2001 From: Mikko Ohtamaa Date: Thu, 13 Jul 2023 14:14:53 +0200 Subject: [PATCH] Polishing up --- eth_defi/enzyme/erc20.py | 12 ++---------- eth_defi/enzyme/uniswap_v2.py | 2 -- eth_defi/event_reader/lazy_timestamp_reader.py | 13 +++++-------- eth_defi/revert_reason.py | 8 +------- eth_defi/tx.py | 5 +---- tests/enzyme/test_vault_controlled_wallet.py | 2 +- tests/test_lazy_timestamp_reader.py | 10 ++++------ 7 files changed, 14 insertions(+), 38 deletions(-) diff --git a/eth_defi/enzyme/erc20.py b/eth_defi/enzyme/erc20.py index 1fc9b9ab..3580d39d 100644 --- a/eth_defi/enzyme/erc20.py +++ b/eth_defi/enzyme/erc20.py @@ -9,13 +9,7 @@ from eth_defi.enzyme.vault import Vault -def prepare_transfer( - enzyme: EnzymeDeployment, - vault: Vault, - generic_adapter: Contract, - token: Contract, - receiver: HexAddress | str, - amount: int) -> ContractFunction: +def prepare_transfer(enzyme: EnzymeDeployment, vault: Vault, generic_adapter: Contract, token: Contract, receiver: HexAddress | str, amount: int) -> ContractFunction: """Prepare an ERC-20 transfer out from the Enzyme vault. - Tells the Enzyme vault to move away som etokes @@ -53,9 +47,7 @@ def prepare_transfer( bound_call = execute_calls_for_generic_adapter( comptroller=vault.comptroller, - external_calls=( - (token, encoded_transfer), - ), + external_calls=((token, encoded_transfer),), generic_adapter=generic_adapter, incoming_assets=incoming_assets, integration_manager=enzyme.contracts.integration_manager, diff --git a/eth_defi/enzyme/uniswap_v2.py b/eth_defi/enzyme/uniswap_v2.py index 9e17bc12..f6bf308c 100644 --- a/eth_defi/enzyme/uniswap_v2.py +++ b/eth_defi/enzyme/uniswap_v2.py @@ -99,5 +99,3 @@ def prepare_swap(enzyme: EnzymeDeployment, vault: Vault, uniswap_v2: UniswapV2De ) return bound_call - - diff --git a/eth_defi/event_reader/lazy_timestamp_reader.py b/eth_defi/event_reader/lazy_timestamp_reader.py index 788d6e52..e10d5eb6 100644 --- a/eth_defi/event_reader/lazy_timestamp_reader.py +++ b/eth_defi/event_reader/lazy_timestamp_reader.py @@ -41,7 +41,6 @@ def __init__(self, web3: Web3, start_block: int, end_block: int): self.cache_by_block_number = {} def update_block_hash(self, block_identifier: BlockIdentifier) -> int: - # Skip web3 stack of broken and slow result formatters if type(block_identifier) == int: assert block_identifier > 0 @@ -65,7 +64,6 @@ def update_block_hash(self, block_identifier: BlockIdentifier) -> int: return timestamp def __getitem__(self, block_hash: HexStr | HexBytes | str): - assert not type(block_hash) == int, f"Use block hashes, block numbers not supported, passed {block_hash}" assert type(block_hash) == str or isinstance(block_hash, HexBytes), f"Got: {block_hash} {block_hash.__class__}" @@ -85,15 +83,14 @@ def extract_timestamps_json_rpc_lazy( end_block: int, fetch_boundaries=True, ) -> LazyTimestampContainer: - """Get block timestamps from block headers. + """Create a cache container that instead of reading block timestamps upfront for the given range, only calls JSON-RPC API when requested - Use slow JSON-RPC block headers call to get this information. - - TODO: This is an old code path. This has been replaced by more robust - :py:class:`ReorganisationMonitor` implementation. + - Works on the cases where sparse event data is read over long block range + Use slow JSON-RPC block headers call to get this information. :return: - block hash -> UNIX timestamp mapping + Wrapper object for block hash based timestamp access. + """ container = LazyTimestampContainer(web3, start_block, end_block) if fetch_boundaries: diff --git a/eth_defi/revert_reason.py b/eth_defi/revert_reason.py index 2338d761..2389edf7 100644 --- a/eth_defi/revert_reason.py +++ b/eth_defi/revert_reason.py @@ -145,11 +145,5 @@ def fetch_transaction_revert_reason( current_block_number = web3.eth.block_number # TODO: Convert to logger record pretty_result = pprint.pformat(result) - logger.error(f"Transaction succeeded, when we tried to fetch its revert reason.\n" - f"Hash: {tx_hash.hex()}, tx block num: {tx['blockNumber']}, current block number: {current_block_number}\n" - f"Transaction result:\n" - f"{pretty_result}\n" - f"- Maybe the chain tip is unstable\n" - f"- Maybe transaction failed due to slippage\n" - f"- Maybe someone is frontrunning you and it does not happen with eth_call replay\n") + logger.error(f"Transaction succeeded, when we tried to fetch its revert reason.\n" f"Hash: {tx_hash.hex()}, tx block num: {tx['blockNumber']}, current block number: {current_block_number}\n" f"Transaction result:\n" f"{pretty_result}\n" f"- Maybe the chain tip is unstable\n" f"- Maybe transaction failed due to slippage\n" f"- Maybe someone is frontrunning you and it does not happen with eth_call replay\n") return unknown_error_message diff --git a/eth_defi/tx.py b/eth_defi/tx.py index f08a2fd8..a153494f 100644 --- a/eth_defi/tx.py +++ b/eth_defi/tx.py @@ -112,10 +112,7 @@ def __mul__(self, other: float | int) -> "AssetDelta": assert d2.raw_amount == int(10**6 * 0.99) """ assert isinstance(other, (float, int)) - return AssetDelta( - self.asset, - int(self.raw_amount * other) - ) + return AssetDelta(self.asset, int(self.raw_amount * other)) def is_incoming(self) -> bool: """This delta describes incoming assets.""" diff --git a/tests/enzyme/test_vault_controlled_wallet.py b/tests/enzyme/test_vault_controlled_wallet.py index 028e0df6..e7d67c7a 100644 --- a/tests/enzyme/test_vault_controlled_wallet.py +++ b/tests/enzyme/test_vault_controlled_wallet.py @@ -95,7 +95,7 @@ def deployment( def test_asset_delta_mul(usdc: Contract): """Check that the asset delta multiplier works.""" - d = AssetDelta(usdc.address, 1*10**6) + d = AssetDelta(usdc.address, 1 * 10**6) d2 = d * 0.99 assert d2.raw_amount == int(10**6 * 0.99) diff --git a/tests/test_lazy_timestamp_reader.py b/tests/test_lazy_timestamp_reader.py index f657b19f..7b3e639a 100644 --- a/tests/test_lazy_timestamp_reader.py +++ b/tests/test_lazy_timestamp_reader.py @@ -33,7 +33,7 @@ def web3(anvil: AnvilLaunch) -> Web3: provider.middlewares = ( # attrdict_middleware, # default_transaction_fields_middleware, - #ethereum_tester_middleware, + # ethereum_tester_middleware, ) web3 = Web3(provider) @@ -43,19 +43,18 @@ def web3(anvil: AnvilLaunch) -> Web3: return web3 - def test_lazy_timestamp_reader_block_range(web3: Web3): """Read timestamps lazily.""" # Create some blocks - for i in range(1, 5+1): + for i in range(1, 5 + 1): mine(web3) assert web3.eth.block_number == 5 timestamps = extract_timestamps_json_rpc_lazy(web3, 1, 5) assert isinstance(timestamps, LazyTimestampContainer) - for i in range(1, 5+1): + for i in range(1, 5 + 1): block_hash = web3.eth.get_block(i)["hash"] assert timestamps[block_hash] > 0 @@ -64,7 +63,7 @@ def test_lazy_timestamp_reader_out_of_block_range(web3: Web3): """Read timestamps lazily, but peek out of allowed range.""" # Create some blocks - for i in range(1, 5+1): + for i in range(1, 5 + 1): mine(web3) assert web3.eth.block_number == 5 @@ -74,4 +73,3 @@ def test_lazy_timestamp_reader_out_of_block_range(web3: Web3): with pytest.raises(OutOfSpecifiedRangeRead): block_hash = web3.eth.get_block(5)["hash"] timestamps[block_hash] -