Uniswap V2
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_v2_utils
from ctc.toolbox.defi_utils.dex_utils.amm_utils import cpmm
In [2]:
pool = '0x9928e4046d7c6513326ccea028cd3e7a91c7590a'
Get pool metadata¶
In [3]:
pool_tokens = await uniswap_v2_utils.async_get_pool_tokens(pool)
pool_symbols = await uniswap_v2_utils.async_get_pool_symbols(pool)
pool_decimals = await uniswap_v2_utils.async_get_pool_decimals(pool)
print('pool_tokens:', pool_tokens)
print('pool_symbols:', pool_symbols)
print('pool_decimals:', pool_decimals)
pool_tokens: ['0x956f47f50a910163d8bf957cf5846d573e7f87ca', '0xc7283b66eb1eb5fb86327f08e1b5816b0720212b'] pool_symbols: ['FEI', 'TRIBE'] pool_decimals: [18, 18]
Get pool state¶
In [4]:
pool_state = await uniswap_v2_utils.async_get_pool_state(pool)
pool_state
Out[4]:
{'x_reserves': 13430875.390003089, 'y_reserves': 63585361.05796264, 'lp_total_supply': 44686131.153790146}
Get pool state at a specific block¶
In [5]:
pool_state = await uniswap_v2_utils.async_get_pool_state(pool, block=14000000)
pool_state
Out[5]:
{'x_reserves': 153539933.09509552, 'y_reserves': 125886010.40488024, 'lp_total_supply': 215052578.20642424}
Get pool state across many historical blocks¶
In [6]:
blocks = list(range(14000000, 14100000, 20000))
pool_state_by_block = await uniswap_v2_utils.async_get_pool_state_by_block(
pool,
blocks=blocks,
)
pool_state_by_block
Out[6]:
{'x_reserves': [3902.2604757534364, 3646.5911759325286, 3320.412482258139, 2803.750690192395, 2753.4720320440665], 'y_reserves': [3199.4282721811446, 3031.4310322189203, 3246.3249589194047, 3832.6757137039026, 3891.8368311451377], 'lp_total_supply': [215052578.2064242, 202288486.47575706, 199698652.82397622, 199313423.07966512, 198957800.9702922]}
Show pool Summary¶
In [7]:
cpmm.print_pool_summary(**pool_state)
- X reserves: 153.54M
- Y reserves: 125.89M
- total lp tokens: 215.05M
- X / Y price: 1.22
- Y / X price: 0.819891
- X / Y liquidity depth:
depth │ new price │ X │ Y ─────────┼──────────────┼──────────────┼────────────── -10% │ 1.1 X / Y │ buy 7.87M │ sell 6.82M -5% │ 1.16 X / Y │ buy 3.88M │ sell 3.28M -2% │ 1.2 X / Y │ buy 1.54M │ sell 1.28M 0% │ 1.22 X / Y │ 0.00 │ 0.00 +2% │ 1.24 X / Y │ sell 1.53M │ buy 1.24M +5% │ 1.28 X / Y │ sell 3.8M │ buy 3.03M +10% │ 1.34 X / Y │ sell 7.51M │ buy 5.85M
- Y / X liquidity depth:
depth │ new price │ Y │ X ─────────┼──────────────────┼──────────────┼────────────── -10% │ 0.737902 Y / X │ buy 6.45M │ sell 8.32M -5% │ 0.778896 Y / X │ buy 3.18M │ sell 3.99M -2% │ 0.803493 Y / X │ buy 1.26M │ sell 1.56M 0% │ 0.819891 Y / X │ 0.00 │ 0.00 +2% │ 0.836289 Y / X │ sell 1.25M │ buy 1.51M +5% │ 0.860886 Y / X │ sell 3.11M │ buy 3.69M +10% │ 0.90188 Y / X │ sell 6.15M │ buy 7.14M
Model pool trade¶
In [8]:
trade = cpmm.trade(
x_sold=500000,
x_reserves=pool_state['x_reserves'],
y_reserves=pool_state['y_reserves'],
)
trade
Out[8]:
{'x_bought': -500000, 'x_sold': 500000, 'y_bought': 407392.9793098554, 'y_sold': -407392.9793098554, 'fee_rate': 0.003, 'new_pool': {'x_reserves': 154039933.09509552, 'y_reserves': 125478617.42557038}}
Show trade summary¶
In [9]:
cpmm.print_trade_summary(
x_sold=500000,
x_reserves=pool_state['x_reserves'],
y_reserves=pool_state['y_reserves'],
x_name=pool_symbols[0],
y_name=pool_symbols[1],
)
- FEI sold: 500,000 - TRIBE bought: 407,392.98 - fees: 1,500 FEI - prices:
│ P_mean │ P_start │ P_end │ mean slippage │ end slippage ─────────────┼──────────┼──────────┼──────────┼───────────────┼────────────── FEI / TRIBE │ 1.23 │ 1.22 │ 1.23 │ 0.006266 │ 0.006514 TRIBE / FEI │ 0.814786 │ 0.819891 │ 0.814585 │ -0.006226 │ -0.006472
- pool reserve sizes:
│ before │ after │ change ───────┼───────────────┼────────────────┼─────────── FEI │ 153,539,933.1 │ 154,039,933.1 │ 0.003256 TRIBE │ 125,886,010.4 │ 125,478,617.43 │ -0.003236
Get pool swaps¶
In [10]:
swaps = await uniswap_v2_utils.async_get_pool_swaps(pool)
swaps
Out[10]:
transaction_hash | recipient | sold_id | bought_id | sold_amount | bought_amount | |
---|---|---|---|---|---|---|
block_number | ||||||
12168368 | 0xc9851f374701f76024c1f44f7166e0ef8a9945675046... | 0xbffb152b9392e38cddc275d818a3db7fe364596b | 0 | 1 | 385878266.886967 | 119248224.557418 |
12168369 | 0x641b95d1f76a4a9e0101df94aab04aee23f95bca349f... | 0x2ebd449e7b10b20135b6c1134b9e40c84f9fffde | 1 | 0 | 469310.538337 | 1500858.802284 |
12168369 | 0x59e7772688fe9966abfeba79408ca9aedbf316bf4761... | 0x94b0a3d511b6ecdb17ebf877278ab030acb0a878 | 1 | 0 | 305051.849919 | 966302.891883 |
12168369 | 0xb7dd780066a84cd1fc4716ed11673c64d8af03ca28ba... | 0x155ab90755eefc6f521cb3bccba4c5aedcd4cdb7 | 1 | 0 | 207200.0 | 652232.187339 |
12168369 | 0x3a3fdaaa52f99c3004370494e2b0852f9c6fb9b02a4f... | 0xe28b3058a2f7f251741a7289b633a902126260ea | 1 | 0 | 7587.187036 | 23820.490941 |
... | ... | ... | ... | ... | ... | ... |
15612495 | 0x69ae33f8e505e9c1b5abaa8260869fb057cea9602749... | 0xc6cf51f57969129654c5014748e44e2a8f6ebd81 | 1 | 0 | 5262.738555 | 1104.225943 |
15612612 | 0xc91f8451da0f12c22349bb2fbafd48789d4cab599d49... | 0xdde3e2024e270d6022d1bb4e8745e9dc6d82e0bc | 0 | 1 | 564.466359 | 2674.236899 |
15612742 | 0xbeb5c2ad1d9839abd58014bc04a817de82b51a232c1f... | 0xcbf181784a00459a9d2dec643e63d9f1565a1cee | 0 | 1 | 24990.752129 | 118172.509804 |
15613010 | 0xd5a6c5231f6f1a8b67ae6a5399ea1d827b0ad3edecd5... | 0xf2f400c138f9fb900576263af0bc7fcde2b1b8a8 | 1 | 0 | 880.67771 | 185.469717 |
15613067 | 0x992111315f732cb36397113171f30b12ab177cea53c1... | 0x9435f1085584764b081177944033d4ed2cfd23ee | 1 | 0 | 561.167131 | 118.178479 |
42679 rows × 6 columns
Get pool mints¶
In [11]:
mints = await uniswap_v2_utils.async_get_pool_mints(pool)
mints
Out[11]:
address | block_hash | transaction_hash | contract_address | event_name | event_hash | arg__sender | arg__amount0 | arg__amount1 | |||
---|---|---|---|---|---|---|---|---|---|---|---|
block_number | transaction_index | log_index | |||||||||
12168368 | 61 | 57 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x3bce142af146fec596340d67569a44688690955a065f... | 0xc9851f374701f76024c1f44f7166e0ef8a9945675046... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 2.605227e+08 | 2.000000e+08 |
12168392 | 5 | 9 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x650f7f511d4c36c5753e5c53a5a0a1be89c06ce04d88... | 0xf23c71488efbcc9f7bb065aef33862a23f163e3a3e20... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 1.673037e+04 | 7.179388e+03 |
12168405 | 44 | 48 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0xddd3da1acfddc7f772c6c80a3e765f88dd44f4037e08... | 0x7f302eeb37edd1a429b6dd8b6a29064b77a30b3a88cd... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 2.060025e+04 | 9.973594e+03 |
12168410 | 32 | 98 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x2f0a0bc05f1c8d1b846fac07d3b92d3fbb8f6d8f04f9... | 0xddd0c0cc624e7db2808217e98d37282e553f98e697f0... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 4.751617e+03 | 2.356516e+03 |
12168414 | 222 | 59 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x4989fc640b4044afa6ccaba2d25a5c6e3257cf9148c4... | 0x5258f324dfdc6c083e5802ae0045e3f4b86661f350f2... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 7.425795e+03 | 3.770426e+03 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
15121055 | 250 | 388 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x8c5a19294d630ad01ed205a8fdb662252bad819cc705... | 0xe77d40f1798594ffc93241a4d790824c85bd4d0c8665... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 4.277380e-01 | 2.793056e+00 |
15153321 | 271 | 547 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x920636c5b77319c5539da8c407d255b7eced244e9a7b... | 0xadc0361d11d36ebac3647006a307494f1ff7343ff44a... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 1.430650e+01 | 9.364430e+01 |
15328435 | 28 | 46 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x12342787e07e1f6423763e91c6a129248a7eb8175f76... | 0x3716e7ebc7097737b96a67c1ab54241ff9022a8ec811... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 4.986920e+03 | 3.119037e+04 |
15397041 | 133 | 328 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x4508a1d5ec7c01c3b427c554c9d3c7802c8e744a9190... | 0x1dd407108ac0de55406f3ceaab2b1e1594ed3a12688e... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 2.500589e+03 | 1.147334e+04 |
15557196 | 86 | 101 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0xbabc09363f96a7b99d1c6bec4d1bcf62572f21d4382d... | 0x2470dcedbade0b9d7ee71677f1739211cf3520139555... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Mint | 0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 2.000000e+00 | 8.846706e+00 |
10049 rows × 9 columns
Get pool burns¶
In [12]:
burns = await uniswap_v2_utils.async_get_pool_burns(pool)
burns
getting events from node, block range: [12125707, 15613506] saving events to file: /home/storm/ctc_data/evm/networks/mainnet/events/contract__0x9928e4046d7c6513326ccea028cd3e7a91c7590a/event__0xdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496/12125707__to__15613506.csv
Out[12]:
address | block_hash | transaction_hash | contract_address | event_name | event_hash | arg__sender | arg__to | arg__amount0 | arg__amount1 | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|
block_number | transaction_index | log_index | ||||||||||
12168478 | 71 | 92 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x9cbf1e47301905b3c876acc2c2db8762f333e3d1bbfa... | 0x71d8939b4edf07087a69fd646bfaf509527ed9211411... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0xc01b33ae2e3a4cb75d0e3bcee2f0cc3eb3fe668a | 148271.937203 | 89795.087503 |
12168483 | 123 | 157 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0xbd0256f088eeb51f8101a1b3b20d53fc35b532be0ebc... | 0xab4f3a45eab31aa9a75ee961afba7b2b5b3bef745bca... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0xd683b78e988ba4bdb9fa0e2012c4c36b7cc96aad | 4723.992545 | 2879.302038 |
12168529 | 140 | 297 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x0614693238887091262a3d86adddcd88e3020708ad1e... | 0xbd4f8703249967542414af4246fb3433c222deafc122... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0x18a5d209d58852499abc1ee73cbb27cc743dd317 | 18653.702095 | 9522.864220 |
12168537 | 55 | 35 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x7c3f742344556505b91508e12fe45183553c5d575224... | 0xd3bd1f7dc5453ac1ff625f8bfebbf2816e99f3bcc3ce... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0x4042f4142b026c9f980c7906060a80f57090d63b | 8471.241203 | 4303.648906 |
12168561 | 104 | 149 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x7dd20b35ff12825a70eb73bab51cbb5f3902c6cc5bb1... | 0xea3807c8e6a2a26e245c74a8f32f6b1834471549acf2... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0xbfe890aac8d9d8dd840115dcd2cff3c99089779d | 96329.571186 | 44065.409781 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
15551821 | 71 | 136 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x901b3422c33ce7e6818a8432a4accfdc196cb400af4c... | 0xcfa991c54911b7e355105bcf618e2d952093f0449ff7... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0x7e2e5d94f11f358345a4ee14c4f1a8521315c03b | 85.255637 | 374.935175 |
15559412 | 174 | 390 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0xddd9480f4feede79b255b19101cd3e4fc6b6e013702e... | 0x03b721863a7419ebb4714c3764939763856e601d1f66... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0x885d26765a096136cfeb1d216713ade4289641e0 | 64.535703 | 285.569863 |
15574518 | 215 | 386 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0xdccc0a9212e3f39ed6d48f5113bfa76b0ba9a02d7f79... | 0x8e816ea1fe6abb4da0031f3ac9b5d617c310e23e384b... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0xdf632ea5ba8283488aae8538e17a2f802243a61a | 596.256976 | 2739.482104 |
15612326 | 17 | 54 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x9debdfcc6e2dc0bbecc377447257cd2c2e13210a417c... | 0x2f9d506a91b1b65b0bab47f2fe08109956fd3812e361... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0xd2de083018d93c6a7c4fb154bf6f36e6f1833796 | 5756.744803 | 27333.163245 |
15613063 | 96 | 266 | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | 0x18107b4cd7d0f7271fa0a7acff48b39d50c0b765e06a... | 0x0a249e10c1e5e74a08cb1f34313eeccc8d0208c4de08... | 0x9928e4046d7c6513326ccea028cd3e7a91c7590a | Burn | 0xdccd412f0b1252819cb1fd330b93224ca42612892bb3... | 0x7a250d5630b4cf539739df2c5dacb4c659f2488d | 0x9435f1085584764b081177944033d4ed2cfd23ee | 82.440313 | 390.287643 |
5272 rows × 10 columns
Reference
- async ctc.protocols.uniswap_v2_utils.async_get_pool_burns(pool_address, *, start_block=None, end_block=None, start_time=None, end_time=None, include_timestamps=False, replace_symbols=False, normalize=True, provider=None, verbose=False)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_decimals(pool=None, *, x_address=None, y_address=None, provider=None)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_mints(pool_address, *, start_block=None, end_block=None, start_time=None, end_time=None, include_timestamps=False, replace_symbols=False, normalize=True, provider=None, verbose=False)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_state(pool, *, block=None, provider=None, normalize=True, fill_empty=True)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_state_by_block(pool, *, blocks, provider=None, normalize=True)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_swaps(pool, *, start_block=None, end_block=None, start_time=None, end_time=None, include_timestamps=False, include_prices=False, include_volumes=False, label='index', normalize=True, provider=None, verbose=False)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_symbols(pool=None, *, x_address=None, y_address=None, provider=None)
- async ctc.protocols.uniswap_v2_utils.async_get_pool_tokens(pool, provider=None)