Calculating Total Value Locked in Liquidity Pools using DEX Data
Automated Market Makers (AMMs) are an innovative new class of smart contracts introduced in decentralized exchange protocols like Uniswap, allowing users to permissionlessly provision liquidity for digital asset trading activity without needing a traditional central order book. However, an important aspect of providing users of these pools is understanding how the total USD value of the assets allocated to the contract can fluctuate over time, leading to impermanent loss for liquidity providers. In this notebook, we explore how Coin Metrics DEX market metadata can be combined with Reference Rates and ATLAS search engine capabilities to construct a timeseries representation pool TVL, allowing market participants to make more informed decisions about DEX market making and trading.
Resources
This notebook demonstrates basic functionality offered by the Coin Metrics Python API Client, ATLAS blockchain search engine, and DEX Market Data.
Coin Metrics offers a vast assortment of data for hundreds of cryptoassets. The Python API Client allows for easy access to this data using Python without needing to create your own wrappers using requests
and other such libraries.
To understand the data that Coin Metrics offers, feel free to peruse the resources below.
The Coin Metrics API v4 website contains the full set of endpoints and data offered by Coin Metrics.
The Coin Metrics Product Documentation gives detailed, conceptual explanations of the data that Coin Metrics offers.
The API Spec contains a full list of functions.
File Download
Download the entire notebook as either a jupyter notebook to run yourself or as a pdf from the two links below
Notebook Setup
DEX Market Reference Data
The reference-data/markets endpoint returns a list of available markets meeting specified criteria. Users can pass in a list of markets, exchanges, or market types (spot, futures, options). For DEX markets, the endpoint also returns key liquidity pool metadata, such as fee tier and pool contract address.
market | exchange | base | quote | pair | symbol | type | size_asset | margin_asset | strike | ... | order_price_increment | order_price_min | order_price_max | order_size_min | order_taker_fee | order_maker_fee | margin_trading_enabled | experimental | base_native | quote_native | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | uniswap_v3_eth-1-1inch-dai-spot | uniswap_v3_eth | 1inch | dai | 1inch-dai | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1 | uniswap_v3_eth-1-1inch-usdc-spot | uniswap_v3_eth | 1inch | usdc | 1inch-usdc | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
2 | uniswap_v3_eth-1-1inch-weth-spot | uniswap_v3_eth | 1inch | weth | 1inch-weth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
3 | uniswap_v3_eth-1-ageur_eth-eurcv_eth-spot | uniswap_v3_eth | ageur_eth | eurcv_eth | ageur_eth-eurcv_eth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
4 | uniswap_v3_eth-1-ageur_eth-usdc-spot | uniswap_v3_eth | ageur_eth | usdc | ageur_eth-usdc | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
1638 | uniswap_v3_eth-agg-yfi-cvx-spot | uniswap_v3_eth | yfi | cvx | yfi-cvx | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1639 | uniswap_v3_eth-agg-yfi-link-spot | uniswap_v3_eth | yfi | link | yfi-link | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1640 | uniswap_v3_eth-agg-yfi-usdc-spot | uniswap_v3_eth | yfi | usdc | yfi-usdc | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1641 | uniswap_v3_eth-agg-yfi-wbtc-spot | uniswap_v3_eth | yfi | wbtc | yfi-wbtc | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1642 | uniswap_v3_eth-agg-yfi-weth-spot | uniswap_v3_eth | yfi | weth | yfi-weth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1643 rows × 39 columns
market | exchange | base | quote | pair | symbol | type | size_asset | margin_asset | strike | ... | order_price_increment | order_price_min | order_price_max | order_size_min | order_taker_fee | order_maker_fee | margin_trading_enabled | experimental | base_native | quote_native | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
72 | uniswap_v3_eth-1-usdc-weth-spot | uniswap_v3_eth | usdc | weth | usdc-weth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
281 | uniswap_v3_eth-2-usdc-weth-spot | uniswap_v3_eth | usdc | weth | usdc-weth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
650 | uniswap_v3_eth-3-usdc-weth-spot | uniswap_v3_eth | usdc | weth | usdc-weth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
1045 | uniswap_v3_eth-4-usdc-weth-spot | uniswap_v3_eth | usdc | weth | usdc-weth | <NA> | spot | <NA> | <NA> | <NA> | ... | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | True | <NA> | <NA> |
4 rows × 39 columns
Fetch contract balances over time with ATLAS
Now that we have a list of target liquidity pool contracts, we can use ATLAS blockchain search engine to query for balance updates in the pool for each asset.
chain_sequence_number | account | account_creation_height | change | previous_balance | new_balance | transaction_sequence_number | n_debits | n_credits | block_hash | ... | consensus_time | credit | total_received | total_sent | previous_debit_height | previous_credit_height | previous_chain_sequence_number | txid | asset | market | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 88964646899286016 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -60426.466497 | 72674047.982769 | 72613621.516272 | 0 | 4019999 | 3619510 | 96131a3e3702feb38e1ce25c71773962cdf6ed8beb5ee2... | ... | 2024-09-09 14:37:23+00:00 | False | 738732040663.426392 | 738659427041.910034 | 20713695 | 20713694 | 88964642604318722 | a8f5e44bb9ae9f93a3d1b11b5fce58fe2bcad8ba73561f... | usdc | uniswap_v3_eth-2-usdc-weth-spot |
1 | 88964646899286022 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -551.745218 | 72613621.516272 | 72613069.771054 | 0 | 4020000 | 3619510 | 96131a3e3702feb38e1ce25c71773962cdf6ed8beb5ee2... | ... | 2024-09-09 14:37:23+00:00 | False | 738732040663.426392 | 738659427593.655396 | 20713696 | 20713694 | 88964646899286016 | 3072cd48442d1e61b7232590812737dabdd52d60ed2181... | usdc | uniswap_v3_eth-2-usdc-weth-spot |
2 | 88964651194253318 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -1189.382003 | 72613069.771054 | 72611880.389051 | 0 | 4020001 | 3619510 | d1b93582ac0242d6c4d78a2e15fa7a14cb8f23cdd315a7... | ... | 2024-09-09 14:37:35+00:00 | False | 738732040663.426392 | 738659428783.037231 | 20713696 | 20713694 | 88964646899286022 | 785845bb01a0c4c95a4fce1a5b684b607eff59db5e35a0... | usdc | uniswap_v3_eth-2-usdc-weth-spot |
3 | 88964651194253320 | e0554a476a092703abdb3ef35c80e0d76d32939f | 13616454 | -1185.605465 | 601863.20449 | 600677.599025 | 1 | 231967 | 225267 | d1b93582ac0242d6c4d78a2e15fa7a14cb8f23cdd315a7... | ... | 2024-09-09 14:37:35+00:00 | False | 5991391248.046802 | 5990790570.447777 | 20713695 | 20713695 | 88964642604318733 | 785845bb01a0c4c95a4fce1a5b684b607eff59db5e35a0... | usdc | uniswap_v3_eth-1-usdc-weth-spot |
4 | 88964651194253338 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -1178.099514 | 72611880.389051 | 72610702.289537 | 0 | 4020002 | 3619510 | d1b93582ac0242d6c4d78a2e15fa7a14cb8f23cdd315a7... | ... | 2024-09-09 14:37:35+00:00 | False | 738732040663.426392 | 738659429961.136841 | 20713697 | 20713694 | 88964651194253318 | e8c9bf688ee4d41f22d95f34936a425edb4f61244318b7... | usdc | uniswap_v3_eth-2-usdc-weth-spot |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
273787 | 89893038440054825 | e0554a476a092703abdb3ef35c80e0d76d32939f | 13616454 | 11.45984 | 2397.232798 | 2408.692638 | 1 | 292522 | 301912 | de4360d0ae6f824e4ca7525da2268497de62906fec744c... | ... | 2024-10-09 18:33:11+00:00 | True | 4128265.590843 | 4125856.898204 | 20929853 | 20929850 | 89893034145087672 | aefc38b1fa84766bb2e50b8565d7e6b23031d98a28ccd8... | weth | uniswap_v3_eth-1-usdc-weth-spot |
273788 | 89893038440054892 | e0554a476a092703abdb3ef35c80e0d76d32939f | 13616454 | -0.015523 | 2408.692638 | 2408.677115 | 0 | 292523 | 301912 | de4360d0ae6f824e4ca7525da2268497de62906fec744c... | ... | 2024-10-09 18:33:11+00:00 | False | 4128265.590843 | 4125856.913727 | 20929853 | 20929854 | 89893038440054825 | 9b83a0daab76cd6db6aedbafc5f1e61adc0f5d31e72e47... | weth | uniswap_v3_eth-1-usdc-weth-spot |
273789 | 89893038440054968 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -2.123209 | 27357.193086 | 27355.069877 | 0 | 3685872 | 4078043 | de4360d0ae6f824e4ca7525da2268497de62906fec744c... | ... | 2024-10-09 18:33:11+00:00 | False | 378530260.402821 | 378502905.332944 | 20929852 | 20929854 | 89893038440054823 | e79fe9981166fccb9fb15b872da4ee5f8ea4f581a86e0d... | weth | uniswap_v3_eth-2-usdc-weth-spot |
273790 | 89893051324956808 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -0.119392 | 27355.069877 | 27354.950485 | 0 | 3685873 | 4078043 | 2823efc897ab6e43f4382e7670f1cfa4aa621571e79d9d... | ... | 2024-10-09 18:33:47+00:00 | False | 378530260.402821 | 378502905.452336 | 20929854 | 20929854 | 89893038440054968 | a46cab246dea451a42d4b26c79994c5fc06ce1a10a53a7... | weth | uniswap_v3_eth-2-usdc-weth-spot |
273791 | 89893051324956894 | 88e6a0c2ddd26feeb64f039a2c41296fcb3f5640 | 12376729 | -0.825174 | 27354.950485 | 27354.125311 | 0 | 3685874 | 4078043 | 2823efc897ab6e43f4382e7670f1cfa4aa621571e79d9d... | ... | 2024-10-09 18:33:47+00:00 | False | 378530260.402821 | 378502906.27751 | 20929857 | 20929854 | 89893051324956808 | 3004b50008190b1b36f5cdf602b63b9f9792267d89f2fe... | weth | uniswap_v3_eth-2-usdc-weth-spot |
547648 rows × 21 columns
market | consensus_time | asset | new_balance | |
---|---|---|---|---|
1 | uniswap_v3_eth-2-usdc-weth-spot | 2024-09-09 14:37:23+00:00 | weth | -3.126659e+04 |
1 | uniswap_v3_eth-2-usdc-weth-spot | 2024-09-09 14:37:23+00:00 | usdc | 7.261307e+07 |
3 | uniswap_v3_eth-1-usdc-weth-spot | 2024-09-09 14:37:35+00:00 | weth | -4.664425e+02 |
3 | uniswap_v3_eth-1-usdc-weth-spot | 2024-09-09 14:37:35+00:00 | usdc | 6.006776e+05 |
4 | uniswap_v3_eth-2-usdc-weth-spot | 2024-09-09 14:37:35+00:00 | weth | -3.126763e+04 |
... | ... | ... | ... | ... |
273850 | uniswap_v3_eth-2-usdc-weth-spot | 2024-10-09 18:36:11+00:00 | usdc | 8.006068e+07 |
273853 | uniswap_v3_eth-1-usdc-weth-spot | 2024-10-09 18:36:23+00:00 | usdc | 2.503766e+06 |
273851 | uniswap_v3_eth-2-usdc-weth-spot | 2024-10-09 18:36:23+00:00 | usdc | 8.005964e+07 |
273854 | uniswap_v3_eth-2-usdc-weth-spot | 2024-10-09 18:36:47+00:00 | usdc | 8.005916e+07 |
273855 | uniswap_v3_eth-1-usdc-weth-spot | 2024-10-09 18:36:59+00:00 | usdc | 2.493272e+06 |
382679 rows × 4 columns
Retrieve Reference Rates to calculate the equivalent USD value for TVL
To normalize pool TVL into USD-denominated terms, we'll leverage the Coin Metrics Reference Rate, which represents a volume-weighted median price across a subset of the asset's most highly-liquid markets.
asset | usdc | weth |
---|---|---|
time | ||
2024-09-09 14:38:00+00:00 | 1.000228 | 2281.628477 |
2024-09-09 14:39:00+00:00 | 1.000066 | 2278.047686 |
2024-09-09 14:40:00+00:00 | 0.99975 | 2279.877181 |
2024-09-09 14:41:00+00:00 | 0.999917 | 2278.211943 |
2024-09-09 14:42:00+00:00 | 0.999867 | 2279.118291 |
... | ... | ... |
2024-10-09 18:34:00+00:00 | 0.999862 | 2422.199211 |
2024-10-09 18:35:00+00:00 | 0.999822 | 2418.294121 |
2024-10-09 18:36:00+00:00 | 0.999827 | 2420.816853 |
2024-10-09 18:37:00+00:00 | 0.999789 | 2420.182517 |
2024-10-09 18:38:00+00:00 | 0.999873 | 2421.764058 |
43441 rows × 2 columns
asset | usdc | weth |
---|---|---|
time | ||
2024-09-09 14:38:00+00:00 | 580007.558512 | -1085063.725878 |
2024-09-09 14:39:00+00:00 | 591547.333378 | -1071736.155646 |
2024-09-09 14:40:00+00:00 | 591664.869298 | -1072292.759274 |
2024-09-09 14:41:00+00:00 | 584444.264977 | -1078824.970611 |
2024-09-09 14:42:00+00:00 | 604433.557236 | -1059255.13815 |
... | ... | ... |
2024-10-09 18:34:00+00:00 | 2480138.279715 | -5834295.808101 |
2024-10-09 18:35:00+00:00 | 2506710.799699 | -5824889.708489 |
2024-10-09 18:36:00+00:00 | 2492840.090089 | -5830966.153299 |
2024-10-09 18:37:00+00:00 | 2492746.887517 | -5829438.242635 |
2024-10-09 18:38:00+00:00 | 2492954.27474 | -5833247.665283 |
43441 rows × 2 columns
Plot USD-denominated TVL for target liquidity pools
Last updated