From 35b2705f5f4b2d5252d995da09212685e23b90b4 Mon Sep 17 00:00:00 2001 From: Mikko Ohtamaa Date: Tue, 24 Dec 2024 13:20:34 +0100 Subject: [PATCH] All routes work --- eth_defi/vault/base.py | 6 +++--- eth_defi/vault/lower_case_dict.py | 17 ++++++++--------- eth_defi/vault/mass_buyer.py | 3 ++- eth_defi/vault/valuation.py | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/eth_defi/vault/base.py b/eth_defi/vault/base.py index aabf38ff..c73403b2 100644 --- a/eth_defi/vault/base.py +++ b/eth_defi/vault/base.py @@ -84,7 +84,7 @@ class VaultPortfolio: #: #: Addresses not checksummed #: - spot_erc20: dict[HexAddress, Decimal] + spot_erc20: LowercaseDict #: For route finding, which DEX tokens should use. #: @@ -111,10 +111,10 @@ def is_spot_only(self) -> bool: def get_position_count(self): return len(self.spot_erc20) - def get_raw_spot_balances(self, web3: Web3) -> dict[HexAddress, int]: + def get_raw_spot_balances(self, web3: Web3) -> LowercaseDict: """Convert spot balances to raw token balances""" chain_id = web3.eth.chain_id - return {addr: fetch_erc20_details(web3, addr, chain_id=chain_id).convert_to_raw(value) for addr, value in self.spot_erc20.items()} + return LowercaseDict(**{addr: fetch_erc20_details(web3, addr, chain_id=chain_id).convert_to_raw(value) for addr, value in self.spot_erc20.items()}) diff --git a/eth_defi/vault/lower_case_dict.py b/eth_defi/vault/lower_case_dict.py index 6fb36358..6624cf0b 100644 --- a/eth_defi/vault/lower_case_dict.py +++ b/eth_defi/vault/lower_case_dict.py @@ -1,9 +1,12 @@ +"""Ethereum address headache tools.""" + class LowercaseDict(dict): """A dictionary subclass that automatically converts all string keys to lowercase. + - Because of legacy, Ethrereum services mix loewrcased and checksum-case addresses + - Ethereum checksum addresse where a f**king bad idea and everyone needs to suffer from this shitty idea for the eternity - """ def __init__(self, *args, **kwargs): @@ -18,20 +21,17 @@ def __init__(self, *args, **kwargs): def __setitem__(self, key, value): """Override setitem to convert string keys to lowercase.""" - if isinstance(key, str): - key = key.lower() + key = key.lower() super().__setitem__(key, value) def __getitem__(self, key): """Override getitem to convert string keys to lowercase.""" - if isinstance(key, str): - key = key.lower() + key = key.lower() return super().__getitem__(key) def get(self, key, default=None): """Override get method to convert string keys to lowercase.""" - if isinstance(key, str): - key = key.lower() + key = key.lower() return super().get(key, default) def update(self, other=None, **kwargs): @@ -44,6 +44,5 @@ def update(self, other=None, **kwargs): def setdefault(self, key, default=None): """Override setdefault to convert string keys to lowercase.""" - if isinstance(key, str): - key = key.lower() + key = key.lower() return super().setdefault(key, default) \ No newline at end of file diff --git a/eth_defi/vault/mass_buyer.py b/eth_defi/vault/mass_buyer.py index 5c15d6ee..866cd441 100644 --- a/eth_defi/vault/mass_buyer.py +++ b/eth_defi/vault/mass_buyer.py @@ -14,6 +14,7 @@ from eth_defi.uniswap_v3.swap import swap_with_slippage_protection as swap_with_slippage_protection_uni_v3 from eth_defi.uniswap_v3.deployment import UniswapV3Deployment from eth_defi.vault.base import VaultPortfolio +from eth_defi.vault.lower_case_dict import LowercaseDict from eth_defi.vault.valuation import NetAssetValueCalculator, Route, ValuationQuoter logger = logging.getLogger(__name__) @@ -102,7 +103,7 @@ def create_buy_portfolio( ) -> VaultPortfolio: """Create a portfolio of tokens to buy based on given Python.""" buy_portfolio = VaultPortfolio( - spot_erc20={t[2]: amount_denomination_token for t in tokens}, + spot_erc20=LowercaseDict(**{t[2]: amount_denomination_token for t in tokens}), dex_hints={t[2]: t[0] for t in tokens}, ) return buy_portfolio diff --git a/eth_defi/vault/valuation.py b/eth_defi/vault/valuation.py index 60508c7a..f53905fa 100644 --- a/eth_defi/vault/valuation.py +++ b/eth_defi/vault/valuation.py @@ -746,7 +746,7 @@ def resolve_best_valuations( # Validate all tokens got at least one path for token_address in input_tokens: - if token_address == self.denomination_token.address: + if token_address == self.denomination_token.address_lower: # Cannot route reserve currency to itself continue @@ -848,7 +848,7 @@ def try_swap_paths( denomination_token = self.denomination_token tokens: dict[HexAddress, TokenDetails] = {address: fetch_erc20_details(web3, address, chain_id) for address in portfolio.tokens} - raw_balances = {address: denomination_token.convert_to_raw(portfolio.spot_erc20[address]) for address, token in tokens.items()} + raw_balances = LowercaseDict(**{address: denomination_token.convert_to_raw(portfolio.spot_erc20[address]) for address, token in tokens.items()}) logger.info( "try_swap_paths(), %d routes, %d quoters, multicall is %s",