-
Notifications
You must be signed in to change notification settings - Fork 374
D1 [x] #481
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liquid-8
wants to merge
36
commits into
uniswap-python:dev/v4-fin
Choose a base branch
from
liquid-8:uni_v4_final
base: dev/v4-fin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
D1 [x] #481
Changes from 10 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
4c42f1f
V4 support (#418)
liquid-8 4542f83
docs: update README with v4 support — remove stale CTA, add to Suppor…
TimeToBuildBob de275ed
docs: v4 narrative guide, v2/v3 examples, and runnable example tests …
TimeToBuildBob 2ef33e2
refactor(tests): replace ganache with anvil for v1–v3 tests (#472)
TimeToBuildBob e771a29
fix: avoid duplicate WETH in swap path when input/output is WETH (clo…
botbikamordehai2-sketch dee6a77
build(deps): bump minimum Python to 3.12, update deps, drop typed_ast…
TimeToBuildBob dc31cca
Merge branch 'master' of https://github.com/uniswap-python/uniswap-py…
liquid-8 bdd2dfd
Merge pull request #2 from liquid-8/uniswap-python-master
liquid-8 8a52b4c
Merge branch 'uniswap4_RC' into master_sync
liquid-8 c6cd59c
Merge pull request #3 from liquid-8/master_sync
liquid-8 56ba3eb
docs: disclose project funding history in README (#476)
ErikBjare bc8fdf4
fix(deps): bump lru-dict to 1.2.0 in lockfile for Python 3.13 (#475)
ErikBjare c95bf09
Merge branch 'uni_v4_final' of https://github.com/liquid-8/uniswap-py…
liquid-8 e178519
Deps: web3py bump to 8.0.0; codebase migration;
liquid-8 2837957
Review fixes; check_approval support dropped.
liquid-8 2bfdece
v2 multihop fix
liquid-8 36f1fcc
missed part
liquid-8 84476b5
contructor fix
liquid-8 abf554c
sync version constructor fix
liquid-8 c91a37a
Migration fixes
liquid-8 a3542d3
v4 test_get_position_info() fix
liquid-8 3154149
Drop v1 tests; adds v2-v3 manual approve tests; multihop logic fix
liquid-8 743752f
Some more migration fix
liquid-8 90ee9a2
Adds async tests; async pool discovery
liquid-8 05105bd
Update dependencies
liquid-8 2728fb3
fix
liquid-8 0952b7c
review fix
liquid-8 f9c2e6c
async _build_and_send() migration fix
liquid-8 4783ea8
Fix async get_minted_token_id()
liquid-8 b5e0ce5
fix test.yml ; fix balance condition in make_swap() tests
liquid-8 6617a1f
fix async make_trade()/make_trade_output() routing
liquid-8 7a54246
Add async pools discovery export; restote pool discovery tests;
liquid-8 dda8f9a
fix missed parameterization
liquid-8 5c7affb
test params optimization; async pools discovery logs fetching fix
liquid-8 76374d4
fixes v3 multihop ETH swaps
liquid-8 403bd96
v2 mutihop ETH fix
liquid-8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,196 @@ | ||
| Examples | ||
| ======== | ||
|
|
||
| No examples here yet! Why don't you contribute some? | ||
| This page shows common usage patterns for Uniswap v2 and v3. For v4 examples, | ||
| see the dedicated :doc:`v4` guide. | ||
|
|
||
| In the meantime, see the :ref:`Getting started` guide. | ||
| The code snippets here mirror the `test suite | ||
| <https://github.com/uniswap-python/uniswap-python/tree/master/tests>`_, which | ||
| runs every example against a live mainnet fork using | ||
| `Anvil <https://book.getfoundry.sh/anvil/>`_. | ||
|
|
||
| .. contents:: Table of contents | ||
| :local: | ||
| :depth: 2 | ||
|
|
||
| Uniswap v2 | ||
| ---------- | ||
|
|
||
| Initialization | ||
| `````````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| from uniswap import Uniswap | ||
|
|
||
| ETH = "0x0000000000000000000000000000000000000000" | ||
| USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
| DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F" | ||
|
|
||
| uni = Uniswap( | ||
| address="0xYOUR_ADDRESS", | ||
| private_key="0xYOUR_PRIVATE_KEY", # or None for read-only | ||
| version=2, | ||
| provider="https://mainnet.infura.io/v3/YOUR_PROJECT_ID", | ||
| ) | ||
|
|
||
| Getting prices | ||
| `````````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| ONE_ETH = 10**18 | ||
|
|
||
| # How much USDC do I get for 1 ETH? | ||
| usdc_out = uni.get_price_input(ETH, USDC, ONE_ETH) | ||
| print(f"1 ETH → {usdc_out / 10**6:.2f} USDC") | ||
|
|
||
| # How much ETH do I need to buy exactly 1000 USDC? | ||
| eth_needed = uni.get_price_output(ETH, USDC, 1000 * 10**6) | ||
| print(f"ETH needed for 1000 USDC: {eth_needed / ONE_ETH:.4f}") | ||
|
|
||
| Making swaps | ||
| ```````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| # Sell 0.1 ETH, receive USDC (exact input) | ||
| tx = uni.make_trade(ETH, USDC, ONE_ETH // 10) | ||
|
|
||
| # Buy exactly 100 USDC, pay in ETH (exact output) | ||
| tx = uni.make_trade_output(ETH, USDC, 100 * 10**6) | ||
|
|
||
| # Sell ETH → DAI with a custom recipient | ||
| tx = uni.make_trade(ETH, DAI, ONE_ETH // 10, recipient="0xSOME_OTHER_ADDRESS") | ||
|
|
||
| Multi-hop swaps | ||
| ``````````````` | ||
|
|
||
| For pairs without a direct v2 pool, route through an intermediate token: | ||
|
|
||
| .. code:: python | ||
|
|
||
| WBTC = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599" | ||
|
|
||
| # ETH → WBTC (routed automatically through WETH by the v2 router) | ||
| wbtc_out = uni.get_price_input(ETH, WBTC, ONE_ETH // 10) | ||
| tx = uni.make_trade(ETH, WBTC, ONE_ETH // 10) | ||
|
|
||
| Uniswap v3 | ||
| ---------- | ||
|
|
||
| v3 adds concentrated liquidity pools at multiple fee tiers. Always specify | ||
| ``fee`` to select the pool — the right tier depends on the pair's volatility. | ||
|
|
||
| Initialization | ||
| `````````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| from uniswap import Uniswap | ||
|
|
||
| ETH = "0x0000000000000000000000000000000000000000" | ||
| USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
| WBTC = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599" | ||
| DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F" | ||
|
|
||
| uni = Uniswap( | ||
| address="0xYOUR_ADDRESS", | ||
| private_key="0xYOUR_PRIVATE_KEY", | ||
| version=3, | ||
| provider="https://mainnet.infura.io/v3/YOUR_PROJECT_ID", | ||
| ) | ||
|
|
||
| Fee tiers | ||
| ````````` | ||
|
|
||
| Common v3 fee tiers: | ||
|
|
||
| - ``500`` — 0.05% (stablecoin pairs, e.g. USDC/USDT) | ||
| - ``3000`` — 0.30% (most pairs, e.g. ETH/USDC) | ||
| - ``10000`` — 1.00% (exotic/volatile pairs) | ||
|
|
||
| Getting prices | ||
| `````````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| ONE_ETH = 10**18 | ||
|
|
||
| # Quote using the 0.30% ETH/USDC pool | ||
| usdc_out = uni.get_price_input(ETH, USDC, ONE_ETH, fee=3000) | ||
| print(f"1 ETH → {usdc_out / 10**6:.2f} USDC (0.30% pool)") | ||
|
|
||
| # Compare with the 0.05% pool (better rate for large trades) | ||
| usdc_out_low = uni.get_price_input(ETH, USDC, ONE_ETH, fee=500) | ||
| print(f"1 ETH → {usdc_out_low / 10**6:.2f} USDC (0.05% pool)") | ||
|
|
||
| # Exact output quote | ||
| eth_needed = uni.get_price_output(ETH, USDC, 1000 * 10**6, fee=500) | ||
|
|
||
| Making swaps | ||
| ```````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| # Sell 0.1 ETH for USDC via the 0.05% pool | ||
| tx = uni.make_trade(ETH, USDC, ONE_ETH // 10, fee=500) | ||
|
|
||
| # Buy exactly 100 USDC, paying in ETH | ||
| tx = uni.make_trade_output(ETH, USDC, 100 * 10**6, fee=500) | ||
|
|
||
| Multi-hop swaps | ||
| ``````````````` | ||
|
|
||
| The v3 client does not expose a multi-hop path parameter in ``make_trade``. | ||
| For pairs without a direct pool, execute two single-hop trades in sequence. | ||
| Wait for the first transaction and use the wallet's confirmed balance increase, | ||
| not its quote, as the second hop's input: | ||
|
|
||
| .. code:: python | ||
|
|
||
| DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F" | ||
|
|
||
| # ETH → USDC (first hop, 0.05% pool) | ||
| usdc_before = uni.get_token_balance(USDC) | ||
| tx1 = uni.make_trade(ETH, USDC, ONE_ETH // 10, fee=500) | ||
| uni.w3.eth.wait_for_transaction_receipt(tx1) | ||
| usdc_received = uni.get_token_balance(USDC) - usdc_before | ||
|
|
||
| # USDC → DAI (second hop, 0.01% stable pool) | ||
| tx2 = uni.make_trade(USDC, DAI, usdc_received, fee=100) | ||
|
|
||
| Liquidity management (v3) | ||
| ````````````````````````` | ||
|
|
||
| .. code:: python | ||
|
|
||
| from uniswap.util import default_tick_range | ||
|
|
||
| ETH = "0x0000000000000000000000000000000000000000" | ||
| USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" | ||
| ONE_ETH = 10**18 | ||
|
|
||
| # Get the pool contract instance | ||
| pool = uni.get_pool_instance(ETH, USDC, fee=500) | ||
|
|
||
| # Sensible full-range tick bounds for this fee tier | ||
| tick_lower, tick_upper = default_tick_range(fee=500) | ||
|
|
||
| # Mint a liquidity position (returns TxReceipt) | ||
| receipt = uni.mint_liquidity( | ||
| pool, | ||
| amount0=ONE_ETH // 10, | ||
| amount1=340 * 10**6, | ||
| tick_lower=tick_lower, | ||
| tick_upper=tick_upper, | ||
| deadline=2**64, | ||
| ) | ||
| assert receipt["status"] | ||
|
|
||
| # Get your token IDs (ERC-721 NFTs representing positions) | ||
| positions = uni.get_liquidity_positions() | ||
| token_id = positions[0] | ||
|
|
||
| # Close the position (collects fees + withdraws liquidity in one call) | ||
| receipt = uni.close_position(token_id, deadline=2**64) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.