Skip to content

Commit

Permalink
Add PairNotFound exception if looking up pairs by smart contract
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Nov 5, 2024
1 parent c387c19 commit a593895
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions tradingstrategy/pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@ def __init__(
quote_token: Optional[str]=None,
fee_tier: Optional[Percent] = None,
pair_id: Optional[int]=None,
exchange_slug: Optional[str] = None,
exchange_slug: Optional[str] = None,
address: Optional[str] = None,
exchange_id: Optional[int] = None,
description: Optional[HumanReadableTradingPairDescription] = None,
custom_message: Optional[str] = None,
):

if base_token:
Expand All @@ -192,7 +194,7 @@ def __init__(
message = f"No pair with base_token {base_token}, quote_token {quote_token}, fee tier {fee_tier}"
else:
assert exchange_slug or pair_id, "Either exchange_slug or pair_id must be specified if base_token and quote_token are not specified"
message = "No pair with "
message = custom_message or "No pair with "

if exchange_slug:
message = message + f" exchange_slug {exchange_slug}"
Expand All @@ -203,6 +205,9 @@ def __init__(
if pair_id:
message = message + f" pair_id {pair_id}"

if address:
message = message + f" address: {address}"

if description:
message = message + f" description: {description}"

Expand Down Expand Up @@ -961,6 +966,11 @@ def get_pair_by_smart_contract(self, address: str) -> Optional[DEXPair]:
address = address.lower()
assert self.smart_contract_map, "You need to build the index to use this function"
data = self.smart_contract_map.get(address)
if data is None:
raise PairNotFoundError(
address=address,
custom_message=f"smart_contract_map does not have entry for {address}, it has {len(self.smart_contract_map)} entries",
)
return self.get_pair_by_id(data["pair_id"])

def get_token(self, address: str, chain_id=None) -> Optional[Token]:
Expand Down

0 comments on commit a593895

Please sign in to comment.