Skip to content
46 changes: 40 additions & 6 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,25 @@ 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 +443,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,8 +467,14 @@ def _eth_to_token_swap_input(
amount_out_min = int(
(1 - slippage) * self._get_eth_token_input_price(output_token, qty, fee)
)
if fee_on_transfer:
func = (
self.router.functions.swapExactETHForTokensSupportingFeeOnTransferTokens
)
else:
func = self.router.functions.swapExactETHForTokens
return self._build_and_send_tx(
self.router.functions.swapExactETHForTokens(
func(
amount_out_min,
[self.get_weth_address(), output_token],
recipient,
Expand All @@ -469,6 +483,8 @@ def _eth_to_token_swap_input(
self._get_tx_params(qty),
)
elif self.version == 3:
Comment thread
ErikBjare marked this conversation as resolved.
if fee_on_transfer:
raise Exception("fee on transfer not supported by Uniswap v3")
return self._token_to_token_swap_input(
self.get_weth_address(), output_token, qty, recipient, fee, slippage
)
Expand All @@ -482,6 +498,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,8 +521,14 @@ def _token_to_eth_swap_input(
amount_out_min = int(
(1 - slippage) * self._get_token_eth_input_price(input_token, qty, fee)
)
if fee_on_transfer:
func = (
self.router.functions.swapExactTokensForETHSupportingFeeOnTransferTokens
)
else:
func = self.router.functions.swapExactTokensForETH
return self._build_and_send_tx(
self.router.functions.swapExactTokensForETH(
func(
qty,
amount_out_min,
[input_token, self.get_weth_address()],
Expand All @@ -514,6 +537,8 @@ def _token_to_eth_swap_input(
),
)
elif self.version == 3:
Comment thread
ErikBjare marked this conversation as resolved.
if fee_on_transfer:
raise Exception("fee on transfer not supported by Uniswap v3")
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,8 +584,14 @@ def _token_to_token_swap_input(
input_token, output_token, qty, fee=fee
)
)
if fee_on_transfer:
func = (
self.router.functions.swapExactTokensForTokensSupportingFeeOnTransferTokens
)
else:
func = self.router.functions.swapExactTokensForTokens
return self._build_and_send_tx(
self.router.functions.swapExactTokensForTokens(
func(
qty,
min_tokens_bought,
[input_token, self.get_weth_address(), output_token],
Expand All @@ -568,6 +600,8 @@ def _token_to_token_swap_input(
),
)
elif self.version == 3:
Comment thread
liquid-8 marked this conversation as resolved.
if fee_on_transfer:
raise Exception("fee on transfer not supported by Uniswap v3")
min_tokens_bought = int(
(1 - slippage)
* self._get_token_token_input_price(
Expand Down