Skip to content

Minor fixes #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _str_to_addr(s: str) -> AddressLike:
elif s.endswith(".eth"):
return ENS(s)
else:
raise Exception("Could't convert string {s} to AddressLike")
raise Exception(f"Couldn't convert string '{s}' to AddressLike")


def _addr_to_str(a: AddressLike) -> str:
Expand All @@ -110,8 +110,8 @@ def _addr_to_str(a: AddressLike) -> str:
elif a.startswith("0x"):
addr = Web3.toChecksumAddress(a)
return addr
else:
raise InvalidToken(a)

raise InvalidToken(a)


def _validate_address(a: AddressLike) -> None:
Expand Down Expand Up @@ -193,12 +193,12 @@ def __init__(
self.router_address: AddressLike = _str_to_addr(
"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
)
"""Documented here: https://uniswap.org/docs/v2/smart-contracts/router02/"""
# Documented here: https://uniswap.org/docs/v2/smart-contracts/router02/
self.router = self._load_contract(
abi_name="uniswap-v2/router02", address=self.router_address,
)
else:
raise Exception("Invalid version, only 1 or 2 supported")
raise Exception(f"Invalid version '{self.version}', only 1 or 2 supported")

logger.info(f"Using factory contract: {self.factory_contract}")

Expand Down Expand Up @@ -236,7 +236,8 @@ def exchange_address_from_token(self, token_addr: AddressLike) -> AddressLike:
ex_addr: AddressLike = self.factory_contract.functions.getExchange(
token_addr
).call()
# TODO: What happens if the token doesn't have an exchange/doesn't exist? Should probably raise an Exception (and test it)
# TODO: What happens if the token doesn't have an exchange/doesn't exist?
# Should probably raise an Exception (and test it)
return ex_addr

@supports([1])
Expand Down Expand Up @@ -294,7 +295,7 @@ def get_eth_token_input_price(self, token: AddressLike, qty: Wei) -> Wei:
if self.version == 1:
ex = self.exchange_contract(token)
price: Wei = ex.functions.getEthToTokenInputPrice(qty).call()
elif self.version == 2:
else:
price = self.router.functions.getAmountsOut(
qty, [self.get_weth_address(), token]
).call()[-1]
Expand Down Expand Up @@ -470,14 +471,13 @@ def make_trade_output(
if balance < need:
raise InsufficientBalance(balance, need)
return self._eth_to_token_swap_output(output_token, qty, recipient)
elif output_token == ETH_ADDRESS:
qty = Wei(qty)
return self._token_to_eth_swap_output(input_token, qty, recipient)
else:
if output_token == ETH_ADDRESS:
qty = Wei(qty)
return self._token_to_eth_swap_output(input_token, qty, recipient)
else:
return self._token_to_token_swap_output(
input_token, qty, output_token, recipient
)
return self._token_to_token_swap_output(
input_token, qty, output_token, recipient
)

def _eth_to_token_swap_input(
self, output_token: AddressLike, qty: Wei, recipient: Optional[AddressLike]
Expand Down