Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support swapping directly with WETH#124

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
kayibal wants to merge1 commit intouniswap-python:master
base:master
Choose a base branch
Loading
fromkayibal:support-weth-swaps
Draft
Show file tree
Hide file tree
Changes fromall 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
27 changes: 27 additions & 0 deletionstests/test_uniswap.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -330,3 +330,30 @@ def test_make_trade_output(
balance_after = client.get_token_balance(output_token)
if output_token != self.eth:
assert balance_before + qty == balance_after

@pytest.mark.parametrize(
"input_token, output_token, route, expectation",
[
(dai, weth, [dai, weth], does_not_raise),
(weth, dai, [weth, dai], does_not_raise),
(usdc, dai, [usdc, weth, dai], does_not_raise),
(weth, eth, [], lambda: pytest.raises(ValueError)),
(eth, weth, [], lambda: pytest.raises(ValueError)),
],
)
def test_get_v2_trade_route(
self,
input_token,
output_token,
route,
expectation,
client
):
if client.version == 1:
pytest.skip(
"Not supported in this version of Uniswap, or at least no liquidity"
)
with expectation():
res = client._get_v2_trade_route(input_token, output_token)

assert res == route
23 changes: 20 additions & 3 deletionsuniswap/uniswap.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -330,7 +330,7 @@ def _get_token_token_output_price(
elif is_same_address(token1, self.get_weth_address()):
return int(self._get_token_eth_output_price(token0, Wei(qty), fee))

route =[token0,self.get_weth_address(), token1]
route = self._get_v2_trade_route(token0, token1)
logger.warning(f"No route specified, assuming route: {route}")

if self.version == 2:
Expand DownExpand Up@@ -558,11 +558,12 @@ def _token_to_token_swap_input(
input_token, output_token, qty, fee=fee
)
)
route = self._get_v2_trade_route(input_token, output_token)
return self._build_and_send_tx(
self.router.functions.swapExactTokensForTokens(
qty,
min_tokens_bought,
[input_token, self.get_weth_address(), output_token],
route,
recipient,
self._deadline(),
),
Expand DownExpand Up@@ -595,6 +596,22 @@ def _token_to_token_swap_input(
else:
raise ValueError

def _get_v2_trade_route(
self, input_token: AddressLike, output_token: AddressLike
) -> List[AddressLike]:
"""Gets trade route simply through WETH.

Deals with two special cases when trading directly with WETH.
"""
weth = self.get_weth_address()
if {weth, ETH_ADDRESS} == {input_token, output_token}:
raise ValueError("Swapping between ETH and WETH not supported!")
if input_token == weth:
return [weth, output_token]
elif output_token == weth:
return [input_token, weth]
return [input_token, weth, output_token]

def _eth_to_token_swap_output(
self,
output_token: AddressLike,
Expand DownExpand Up@@ -736,7 +753,7 @@ def _token_to_token_swap_output(
self.router.functions.swapTokensForExactTokens(
qty,
amount_in_max,
[input_token,self.get_weth_address(), output_token],
self._get_v2_trade_route(input_token, output_token),
recipient,
self._deadline(),
),
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp