Skip to content

Commit

Permalink
Polishing up
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Jul 13, 2023
1 parent 3298267 commit 1ddf873
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 38 deletions.
12 changes: 2 additions & 10 deletions eth_defi/enzyme/erc20.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions eth_defi/enzyme/uniswap_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,3 @@ def prepare_swap(enzyme: EnzymeDeployment, vault: Vault, uniswap_v2: UniswapV2De
)

return bound_call


13 changes: 5 additions & 8 deletions eth_defi/event_reader/lazy_timestamp_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__}"
Expand All @@ -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:
Expand Down
8 changes: 1 addition & 7 deletions eth_defi/revert_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions eth_defi/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion tests/enzyme/test_vault_controlled_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 4 additions & 6 deletions tests/test_lazy_timestamp_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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]

0 comments on commit 1ddf873

Please sign in to comment.