Investigate Uniswap v3 and getting historical price data in good format
Closed this issue · 6 comments
- Github and other searches to find Python (and JavaScript) open source examples for getting data out from Uniswap v3
- Data structure how to store this data in the (SQL) database for historical queries
Question that needs to be answered:
- Given
(chain id, Uniswap v3 deployment address)
- What is the historical price for buy/sell
(timestamp, trading pair, token0 quantity in)
->token1 quantity out
Can be a single pool price at milestone 1, later expanded to cover auto routing and multi-hop trades.
Related Uniswap v3 in-house project
Search on Github and PyPI about Python and Uniswap v3 reveals that there isn't any one library that could do this at the moment. In short the code to get data out of Uniswap v3 is very fragmented for the time being.
Some notable ones are:
- uniswap-python: although the integration involves mostly around making trades and v3 support is still in early development, some part of the code could be useful in the future: uniswap-python/uniswap-python#95. The code also has
pytest
tests (run on mainnet fork), MIT license - Active strategy framework from GammaStrategies / VisorFinance has some functions to get pool data at https://github.com/GammaStrategies/active-strategy-framework/blob/main/GetPoolData.py. It's interesting to see several different solutions there: Google Bigquery blockchain-etl, thegraph and bitquery. No license specified
- Oracle from Stakewise: https://github.com/stakewise/oracle/blob/master/oracle/oracle/distributor/uniswap_v3.py. GPLv3 license
- Misc utils from ranges.fi: https://github.com/Ranges-fi/Uni_v3_utils. Gamma also referenced this from their code above. GPLv3 license
GraphQL based solution can use existing subgraphs like:
- Uniswap: https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v3
- Visor: https://thegraph.com/hosted-service/subgraph/visorfinance/visor
- Stakewise: https://api.thegraph.com/subgraphs/name/stakewise/uniswap-v3-mainnet
There are several onchain oracle solutions too, might not be very useful for above purpose:
We need to decide if we take JSON-RPC event-based approach or GraphQL based approach.
- GraphQL might be more generic and we can reuse existing graphs
- GraphQL adds very complex dependency if we want (and we want) to be able to run locally
Let's tackle the Uni v3 deployment first.
- We are currently only hosting Sushiswap contracts so we need to recreate the workflow to build in Uni v3 contracts compield
- The same approach that we use for Sushiswap contracts likely works for Uniswap contracts as well - we pull them in as Git submodules, then use their native tooltchain to compile contracts and then copy over the compiled binary artifacts to our
abi
folder
Some notebooks dealing with Uniswap v3 backtesting
Also saw this
Also saw this
I added a test for that method this morning: https://github.com/tradingstrategy-ai/eth-hentai/pull/6/files#diff-59cf62830f8e4e3131187561bfa500d2fdf0a14a48a9c33d91fbc1867a44ad80R111-R115
There are some good insights in the answer though