Skip to content

Commit

Permalink
Support trading on custom routes in v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpineo committed Mar 22, 2024
1 parent 59fba76 commit ed2abce
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ def make_trade(
fee: Optional[int] = None,
slippage: Optional[float] = None,
fee_on_transfer: bool = False,
route: Optional[List[AddressLike]] = None,
) -> HexBytes:
"""Make a trade by defining the qty of the input token."""
if not isinstance(qty, int):
Expand All @@ -461,6 +462,9 @@ def make_trade(
if input_token == output_token:
raise ValueError

if route and (input_token == ETH_ADDRESS or output_token == ETH_ADDRESS):
raise ValueError

if input_token == ETH_ADDRESS:
return self._eth_to_token_swap_input(
output_token, Wei(qty), recipient, fee, slippage, fee_on_transfer
Expand All @@ -478,6 +482,7 @@ def make_trade(
fee,
slippage,
fee_on_transfer,
route,
)

@check_approval
Expand All @@ -489,6 +494,7 @@ def make_trade_output(
recipient: Optional[AddressLike] = None,
fee: Optional[int] = None,
slippage: Optional[float] = None,
route: Optional[List[AddressLike]] = None,
) -> HexBytes:
"""Make a trade by defining the qty of the output token."""
fee = validate_fee_tier(fee=fee, version=self.version)
Expand All @@ -499,6 +505,9 @@ def make_trade_output(
if input_token == output_token:
raise ValueError

if route and (input_token == ETH_ADDRESS or output_token == ETH_ADDRESS):
raise ValueError

if input_token == ETH_ADDRESS:
balance = self.get_eth_balance()
need = self._get_eth_token_output_price(output_token, qty, fee)
Expand All @@ -513,7 +522,7 @@ def make_trade_output(
)
else:
return self._token_to_token_swap_output(
input_token, output_token, qty, recipient, fee, slippage
input_token, output_token, qty, recipient, fee, slippage, route
)

def _eth_to_token_swap_input(
Expand Down Expand Up @@ -695,6 +704,7 @@ def _token_to_token_swap_input(
fee: int,
slippage: float,
fee_on_transfer: bool = False,
route: Optional[List[AddressLike]] = None,
) -> HexBytes:
"""Convert tokens to tokens given an input amount."""
# Balance check
Expand Down Expand Up @@ -730,10 +740,12 @@ def _token_to_token_swap_input(
function = token_funcs.tokenToTokenTransferInput(*func_params)
return self._build_and_send_tx(function)
elif self.version == 2:
if not route:
route = [input_token, self.get_weth_address(), output_token]
min_tokens_bought = int(
(1 - slippage)
* self._get_token_token_input_price(
input_token, output_token, qty, fee=fee
input_token, output_token, qty, fee=fee, route=route,
)
)
if fee_on_transfer:
Expand All @@ -746,7 +758,7 @@ def _token_to_token_swap_input(
func(
qty,
min_tokens_bought,
[input_token, self.get_weth_address(), output_token],
route,
recipient,
self._deadline(),
),
Expand Down Expand Up @@ -959,6 +971,7 @@ def _token_to_token_swap_output(
recipient: Optional[AddressLike],
fee: int,
slippage: float,
route: Optional[List[AddressLike]] = None,
) -> HexBytes:
"""Convert tokens to tokens given an output amount.
Expand All @@ -971,7 +984,7 @@ def _token_to_token_swap_output(

# Balance check
input_balance = self.get_token_balance(input_token)
cost = self._get_token_token_output_price(input_token, output_token, qty, fee)
cost = self._get_token_token_output_price(input_token, output_token, qty, fee, route)
amount_in_max = int((1 + slippage) * cost)
if (
amount_in_max > input_balance
Expand Down Expand Up @@ -1000,15 +1013,17 @@ def _token_to_token_swap_output(
elif self.version == 2:
if recipient is None:
recipient = self.address
if not route:
route = [input_token, self.get_weth_address(), output_token]
cost = self._get_token_token_output_price(
input_token, output_token, qty, fee=fee
input_token, output_token, qty, fee=fee, route=route
)
amount_in_max = int((1 + slippage) * cost)
return self._build_and_send_tx(
self.router.functions.swapTokensForExactTokens(
qty,
amount_in_max,
[input_token, self.get_weth_address(), output_token],
route,
recipient,
self._deadline(),
),
Expand Down

0 comments on commit ed2abce

Please sign in to comment.