Skip to content
97 changes: 67 additions & 30 deletions uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def make_trade(
recipient: AddressLike = None,
fee: int = None,
slippage: float = None,
fee_on_transfer: bool = False,
) -> HexBytes:
"""Make a trade by defining the qty of the input token."""
if fee is None:
Expand All @@ -377,19 +378,19 @@ def make_trade(

if input_token == ETH_ADDRESS:
return self._eth_to_token_swap_input(
output_token, Wei(qty), recipient, fee, slippage
output_token, Wei(qty), recipient, fee, slippage, fee_on_transfer
)
else:
balance = self.get_token_balance(input_token)
if balance < qty:
raise InsufficientBalance(balance, qty)
if output_token == ETH_ADDRESS:
return self._token_to_eth_swap_input(
input_token, qty, recipient, fee, slippage
input_token, qty, recipient, fee, slippage, fee_on_transfer
)
else:
return self._token_to_token_swap_input(
input_token, output_token, qty, recipient, fee, slippage
input_token, output_token, qty, recipient, fee, slippage,fee_on_transfer
)

@check_approval
Expand Down Expand Up @@ -436,6 +437,7 @@ def _eth_to_token_swap_input(
recipient: Optional[AddressLike],
fee: int,
slippage: float,
fee_on_transfer: bool = False,
) -> HexBytes:
"""Convert ETH to tokens given an input amount."""
eth_balance = self.get_eth_balance()
Expand All @@ -459,15 +461,26 @@ def _eth_to_token_swap_input(
amount_out_min = int(
(1 - slippage) * self._get_eth_token_input_price(output_token, qty, fee)
)
return self._build_and_send_tx(
self.router.functions.swapExactETHForTokens(
amount_out_min,
[self.get_weth_address(), output_token],
recipient,
self._deadline(),
),
self._get_tx_params(qty),
)
if fee_on_transfer:
return self._build_and_send_tx(
self.router.functions.swapExactETHForTokensSupportingFeeOnTransferTokens(
amount_out_min,
[self.get_weth_address(), output_token],
recipient,
self._deadline(),
),
self._get_tx_params(qty),
)
else:
return self._build_and_send_tx(
self.router.functions.swapExactETHForTokens(
amount_out_min,
[self.get_weth_address(), output_token],
recipient,
self._deadline(),
),
self._get_tx_params(qty),
)
Comment thread
ErikBjare marked this conversation as resolved.
elif self.version == 3:
Comment thread
ErikBjare marked this conversation as resolved.
return self._token_to_token_swap_input(
self.get_weth_address(), output_token, qty, recipient, fee, slippage
Expand All @@ -482,6 +495,7 @@ def _token_to_eth_swap_input(
recipient: Optional[AddressLike],
fee: int,
slippage: float,
fee_on_transfer: bool = False,
) -> HexBytes:
"""Convert tokens to ETH given an input amount."""
# Balance check
Expand All @@ -504,15 +518,26 @@ def _token_to_eth_swap_input(
amount_out_min = int(
(1 - slippage) * self._get_token_eth_input_price(input_token, qty, fee)
)
return self._build_and_send_tx(
self.router.functions.swapExactTokensForETH(
qty,
amount_out_min,
[input_token, self.get_weth_address()],
recipient,
self._deadline(),
),
)
if fee_on_transfer:
return self._build_and_send_tx(
self.router.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(
qty,
amount_out_min,
[input_token, self.get_weth_address()],
recipient,
self._deadline(),
),
)
else:
return self._build_and_send_tx(
self.router.functions.swapExactTokensForETH(
qty,
amount_out_min,
[input_token, self.get_weth_address()],
recipient,
self._deadline(),
),
)
Comment thread
ErikBjare marked this conversation as resolved.
Outdated
elif self.version == 3:
Comment thread
ErikBjare marked this conversation as resolved.
return self._token_to_token_swap_input(
input_token, self.get_weth_address(), qty, recipient, fee, slippage
Expand All @@ -528,6 +553,7 @@ def _token_to_token_swap_input(
recipient: Optional[AddressLike],
fee: int,
slippage: float,
fee_on_transfer: bool = False,
) -> HexBytes:
"""Convert tokens to tokens given an input amount."""
if recipient is None:
Expand Down Expand Up @@ -558,15 +584,26 @@ def _token_to_token_swap_input(
input_token, output_token, qty, fee=fee
)
)
return self._build_and_send_tx(
self.router.functions.swapExactTokensForTokens(
qty,
min_tokens_bought,
[input_token, self.get_weth_address(), output_token],
recipient,
self._deadline(),
),
)
if fee_on_transfer:
return self._build_and_send_tx(
self.router.functions.swapExactTokensForTokensSupportingFeeOnTransferTokens(
qty,
min_tokens_bought,
[input_token, self.get_weth_address(), output_token],
recipient,
self._deadline(),
),
)
else:
return self._build_and_send_tx(
self.router.functions.swapExactTokensForTokens(
qty,
min_tokens_bought,
[input_token, self.get_weth_address(), output_token],
recipient,
self._deadline(),
),
)
Comment thread
ErikBjare marked this conversation as resolved.
Outdated
elif self.version == 3:
Comment thread
liquid-8 marked this conversation as resolved.
min_tokens_bought = int(
(1 - slippage)
Expand Down