Skip to content

Commit c2cc36c

Browse files
liquid-8ErikBjare
andauthored
feat: added swapExact...SupportingFeeOnTransferTokens() support (#132)
Co-authored-by: Erik Bjäreholt <erik.bjareholt@gmail.com>
1 parent 7914357 commit c2cc36c

1 file changed

Lines changed: 40 additions & 6 deletions

File tree

‎uniswap/uniswap.py‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def make_trade(
365365
recipient: AddressLike = None,
366366
fee: int = None,
367367
slippage: float = None,
368+
fee_on_transfer: bool = False,
368369
) -> HexBytes:
369370
"""Make a trade by defining the qty of the input token."""
370371
if fee is None:
@@ -377,19 +378,25 @@ def make_trade(
377378

378379
if input_token == ETH_ADDRESS:
379380
return self._eth_to_token_swap_input(
380-
output_token, Wei(qty), recipient, fee, slippage
381+
output_token, Wei(qty), recipient, fee, slippage, fee_on_transfer
381382
)
382383
else:
383384
balance = self.get_token_balance(input_token)
384385
if balance < qty:
385386
raise InsufficientBalance(balance, qty)
386387
if output_token == ETH_ADDRESS:
387388
return self._token_to_eth_swap_input(
388-
input_token, qty, recipient, fee, slippage
389+
input_token, qty, recipient, fee, slippage, fee_on_transfer
389390
)
390391
else:
391392
return self._token_to_token_swap_input(
392-
input_token, output_token, qty, recipient, fee, slippage
393+
input_token,
394+
output_token,
395+
qty,
396+
recipient,
397+
fee,
398+
slippage,
399+
fee_on_transfer,
393400
)
394401

395402
@check_approval
@@ -436,6 +443,7 @@ def _eth_to_token_swap_input(
436443
recipient: Optional[AddressLike],
437444
fee: int,
438445
slippage: float,
446+
fee_on_transfer: bool = False,
439447
) -> HexBytes:
440448
"""Convert ETH to tokens given an input amount."""
441449
eth_balance = self.get_eth_balance()
@@ -459,8 +467,14 @@ def _eth_to_token_swap_input(
459467
amount_out_min = int(
460468
(1 - slippage) * self._get_eth_token_input_price(output_token, qty, fee)
461469
)
470+
if fee_on_transfer:
471+
func = (
472+
self.router.functions.swapExactETHForTokensSupportingFeeOnTransferTokens
473+
)
474+
else:
475+
func = self.router.functions.swapExactETHForTokens
462476
return self._build_and_send_tx(
463-
self.router.functions.swapExactETHForTokens(
477+
func(
464478
amount_out_min,
465479
[self.get_weth_address(), output_token],
466480
recipient,
@@ -469,6 +483,8 @@ def _eth_to_token_swap_input(
469483
self._get_tx_params(qty),
470484
)
471485
elif self.version == 3:
486+
if fee_on_transfer:
487+
raise Exception("fee on transfer not supported by Uniswap v3")
472488
return self._token_to_token_swap_input(
473489
self.get_weth_address(), output_token, qty, recipient, fee, slippage
474490
)
@@ -482,6 +498,7 @@ def _token_to_eth_swap_input(
482498
recipient: Optional[AddressLike],
483499
fee: int,
484500
slippage: float,
501+
fee_on_transfer: bool = False,
485502
) -> HexBytes:
486503
"""Convert tokens to ETH given an input amount."""
487504
# Balance check
@@ -504,8 +521,14 @@ def _token_to_eth_swap_input(
504521
amount_out_min = int(
505522
(1 - slippage) * self._get_token_eth_input_price(input_token, qty, fee)
506523
)
524+
if fee_on_transfer:
525+
func = (
526+
self.router.functions.swapExactTokensForETHSupportingFeeOnTransferTokens
527+
)
528+
else:
529+
func = self.router.functions.swapExactTokensForETH
507530
return self._build_and_send_tx(
508-
self.router.functions.swapExactTokensForETH(
531+
func(
509532
qty,
510533
amount_out_min,
511534
[input_token, self.get_weth_address()],
@@ -514,6 +537,8 @@ def _token_to_eth_swap_input(
514537
),
515538
)
516539
elif self.version == 3:
540+
if fee_on_transfer:
541+
raise Exception("fee on transfer not supported by Uniswap v3")
517542
return self._token_to_token_swap_input(
518543
input_token, self.get_weth_address(), qty, recipient, fee, slippage
519544
)
@@ -528,6 +553,7 @@ def _token_to_token_swap_input(
528553
recipient: Optional[AddressLike],
529554
fee: int,
530555
slippage: float,
556+
fee_on_transfer: bool = False,
531557
) -> HexBytes:
532558
"""Convert tokens to tokens given an input amount."""
533559
if recipient is None:
@@ -558,8 +584,14 @@ def _token_to_token_swap_input(
558584
input_token, output_token, qty, fee=fee
559585
)
560586
)
587+
if fee_on_transfer:
588+
func = (
589+
self.router.functions.swapExactTokensForTokensSupportingFeeOnTransferTokens
590+
)
591+
else:
592+
func = self.router.functions.swapExactTokensForTokens
561593
return self._build_and_send_tx(
562-
self.router.functions.swapExactTokensForTokens(
594+
func(
563595
qty,
564596
min_tokens_bought,
565597
[input_token, self.get_weth_address(), output_token],
@@ -568,6 +600,8 @@ def _token_to_token_swap_input(
568600
),
569601
)
570602
elif self.version == 3:
603+
if fee_on_transfer:
604+
raise Exception("fee on transfer not supported by Uniswap v3")
571605
min_tokens_bought = int(
572606
(1 - slippage)
573607
* self._get_token_token_input_price(

0 commit comments

Comments
 (0)