Uniswap V3
Examples
Note
These examples are crafted as a Jupyter notebook. You can download the original notebook file here.
Also note that inside Jupyter notebooks, await
can be used freely outside of asyncio.run()
.
In [1]:
from ctc.protocols import uniswap_v3_utils
In [2]:
pool = '0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d'
Get pool metadata¶
In [3]:
pool_metadata = await uniswap_v3_utils.async_get_pool_metadata(pool)
pool_metadata
Out[3]:
{'x_symbol': 'RAI', 'y_symbol': 'DAI', 'x_address': '0x03ab458634910aad20ef5f1c8ee96f1d6ac54919', 'y_address': '0x6b175474e89094c44da98b954eedeac495271d0f', 'fee': 500}
Get pool swaps¶
In [4]:
swaps = await uniswap_v3_utils.async_get_pool_swaps(pool)
swaps
searching for creation block of 0xcB0C5d9D92f4F2F80cce7aa271a1E148c226e19D - trying block: 0 - trying block: 15613498 - trying block: 7806749 - trying block: 11710123 - trying block: 13661810 - trying block: 12685966 - trying block: 12198044 - trying block: 12442005 - trying block: 12320024 - trying block: 12381014 - trying block: 12350519 - trying block: 12365766 - trying block: 12373390 - trying block: 12377202 - trying block: 12375296 - trying block: 12374343 - trying block: 12374819 - trying block: 12375057 - trying block: 12375176 - trying block: 12375236 - trying block: 12375266 - trying block: 12375281 - trying block: 12375273 - trying block: 12375269 - trying block: 12375267 result: 12375267 getting events from node, block range: [12375267, 15613499] saving events to file: /home/storm/ctc_data/evm/networks/mainnet/events/contract__0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d/event__0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67/12375267__to__15613499.csv loading events (8.41MB across 1 files)
Out[4]:
address | block_hash | transaction_hash | contract_address | event_name | event_hash | arg__sender | arg__recipient | x_amount | y_amount | arg__sqrtPriceX96 | arg__liquidity | arg__tick | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
block_number | transaction_index | log_index | |||||||||||||
12376204 | 174 | 283 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0x732c37dec26a9b18dd5eae14c284135a6c94dfeac8e1... | 0x4273d4c137dafc078d57d6bef59be638caf0befb6e3f... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0xe592427a0aece92de3edee1f18e0157c05861564 | 0x530cf036ed4fa58f7301a9c788c9806624cefd19 | -2566.392073 | 7729.371943 | 137701145051790343205145302274 | 1279015072674050594682566 | 11055 |
12376300 | 284 | 324 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0xdec52387bfbeaca08c902e977d45adf010244206de23... | 0x7f8c5d7d3b80e5bcc44bc69724df30c2a02e08dfc797... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0xe592427a0aece92de3edee1f18e0157c05861564 | 0xfa70fe951720d912eba9afa8cbf1b58f6c73d163 | 1000.000000 | -3015.152438 | 137514372438639148649092841650 | 1279015072674050594682566 | 11028 |
12376313 | 98 | 145 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0xd4476cdad58210e7b679d84c2ceb4ccb840130f29f3d... | 0x04dcec4d9568c9c546c8eca841bd1a3ae5cd90980c74... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0xe592427a0aece92de3edee1f18e0157c05861564 | 0xfa70fe951720d912eba9afa8cbf1b58f6c73d163 | 800.000000 | -2406.239254 | 137365318749770594892820423408 | 1279015072674050594682566 | 11006 |
12376354 | 306 | 270 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0xa39d3cd53d20824f408257273ac11a8e995b0089ac1e... | 0xd23e9b136c18fb7fb40402eb7891b2eac9edfdbb0d18... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0xe592427a0aece92de3edee1f18e0157c05861564 | 0x22f9dcf4647084d6c31b2765f6910cd85c178c18 | -3664.080852 | 11028.455800 | 137472087145646725670697909903 | 8179643415011922565226242 | 11022 |
12377046 | 148 | 226 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0x8ace8c5b4669b873546e85e04d649489e16e9901f4e6... | 0x31d3aaea23e8994c40df6319426dc9dfb152ea9b546a... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0xe592427a0aece92de3edee1f18e0157c05861564 | 0xc5a215cdbb69231b2e0f70613afdddbfc1e12c2d | -3454.270677 | 10412.397172 | 137569386092052975813204860490 | 8474322160704207337819030 | 11036 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
15613248 | 2 | 6 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0x88e729e7bda2a8fd56b14ee32db574f2b2fb56670e3e... | 0xa9b500e6c785e3f4d884798956affa5c69fb1564b5c3... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0x0000000000007f150bd6f54c40a34d7c3d5e9f56 | 0x0000000000007f150bd6f54c40a34d7c3d5e9f56 | 2104.882553 | -6006.778233 | 133873006685643048934614245587 | 379875942887692857622351809 | 10491 |
15613307 | 13 | 40 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0xc964e8e19629eef13bbcf2ef96bac3a98e7c5aa6336e... | 0x450b156956a948482169845ea587ad2f48150580f19e... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0x0000000000007f150bd6f54c40a34d7c3d5e9f56 | 0x8ae720a71622e824f576b4a8c03031066548a3b1 | -1051.682647 | 3004.213604 | 133873632940921688707618164694 | 379875942887692857622351809 | 10491 |
15613329 | 158 | 351 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0xd976fc81fc9f11f20b5c2433a211483fb91ea3adefe5... | 0xcd37922bc3e8718c708258c92f23984c657c1e5caade... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0x0000000000304a767881fdccb30fceb51f6221e2 | 0x8ae720a71622e824f576b4a8c03031066548a3b1 | -726.069290 | 2074.090024 | 133874065303595372065514727143 | 379875942887692857622351809 | 10491 |
15613351 | 4 | 12 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0xbd90741fd2f2e8516f635cfd697b37570ad99e053aef... | 0xa7f57c6419cc6045e4f91627c6123737456f10f5d471... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0x0000000000007f150bd6f54c40a34d7c3d5e9f56 | 0x8ae720a71622e824f576b4a8c03031066548a3b1 | -982.459699 | 2806.516369 | 133874650347111739584518944911 | 379875942887692857622351809 | 10491 |
15613391 | 46 | 142 | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | 0x8f5db4cfbf517712d69ffc8d6aad99ecff7c5e31c7bd... | 0x81f8c1bf2aef89255db34b213cb1edeb630d5488cb84... | 0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d | Swap | 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb... | 0x0000000000304a767881fdccb30fceb51f6221e2 | 0x8ae720a71622e824f576b4a8c03031066548a3b1 | -672.522239 | 1921.156225 | 133875050829362923132566273186 | 379875942887692857622351809 | 10492 |
17410 rows × 13 columns
Quote trades¶
In [5]:
await uniswap_v3_utils.async_quote_exact_input_single(
amount_in=int(500e18),
token_in=pool_metadata['x_address'],
token_out=pool_metadata['y_address'],
fee=pool_metadata['fee']
)
Out[5]:
1426894839696235279726
In [6]:
await uniswap_v3_utils.async_quote_exact_output_single(
amount_out=int(500e18),
token_in=pool_metadata['x_address'],
token_out=pool_metadata['y_address'],
fee=pool_metadata['fee']
)
Out[6]:
175205370461472672533
Reference
- async ctc.protocols.uniswap_v3_utils.async_get_pool_metadata(pool_address, **rpc_kwargs)
- async ctc.protocols.uniswap_v3_utils.async_get_pool_swaps(pool_address, *, start_block=None, end_block=None, start_time=None, end_time=None, include_timestamps=False, replace_symbols=False, normalize=True)
- async ctc.protocols.uniswap_v3_utils.async_quote_exact_input_single(token_in, token_out, *, fee, amount_in, sqrt_price_limit_x96=0, provider=None, block=None)
- async ctc.protocols.uniswap_v3_utils.async_quote_exact_output_single(token_in, token_out, *, fee, amount_out, sqrt_price_limit_x96=0, provider=None, block=None)