Skip to content

Commit

Permalink
All routes work
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Dec 24, 2024
1 parent e6b6d19 commit 35b2705
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions eth_defi/vault/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#:
Expand All @@ -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()})



Expand Down
17 changes: 8 additions & 9 deletions eth_defi/vault/lower_case_dict.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand All @@ -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)
3 changes: 2 additions & 1 deletion eth_defi/vault/mass_buyer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions eth_defi/vault/valuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 35b2705

Please sign in to comment.