Skip to content

Instantly share code, notes, and snippets.

@aaronstjohn
Last active October 4, 2023 07:18
Show Gist options
  • Save aaronstjohn/f0748c92e232b5229648fb3a0dc1840c to your computer and use it in GitHub Desktop.
Save aaronstjohn/f0748c92e232b5229648fb3a0dc1840c to your computer and use it in GitHub Desktop.
All pairs uniswap query: `pip install python_graphql_client`
from python_graphql_client import GraphqlClient
import json
from pprint import pformat
from multiprocessing import Pool
import concurrent.futures
from itertools import repeat , chain
import math
import os
pairs_query = """
query pairs($skip: Int!) {
pairs(first: 1000, orderBy: reserveUSD, orderDirection: desc, skip: $skip) {
id
token0{
id
name
symbol
decimals
tradeVolume
tradeVolumeUSD
untrackedVolumeUSD
txCount
totalLiquidity
derivedETH
}
token1{
id
name
symbol
decimals
tradeVolume
tradeVolumeUSD
untrackedVolumeUSD
txCount
totalLiquidity
derivedETH
}
reserve0
reserve1
totalSupply
reserveETH
reserveUSD
trackedReserveETH
token0Price
token1Price
volumeToken0
volumeToken1
volumeUSD
untrackedVolumeUSD
txCount
createdAtTimestamp
createdAtBlockNumber
}
}
"""
factory_query="""
{
uniswapFactory(id: "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"){
pairCount
}
}
"""
def graphql_query(query_string,subkeys=None,variables=None):
client = GraphqlClient(endpoint="https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2")
result= client.execute(query=query_string,variables=variables)
if subkeys is not None:
for key in subkeys:
result = result[key]
return result
pair_count = int(graphql_query(factory_query,['data','uniswapFactory','pairCount']))
print(f"PairCount is: {pair_count}")
# skips = 1
SKIP_SIZE = 1000
skips = math.ceil(pair_count/SKIP_SIZE)
chunksize = math.ceil(skips/os.cpu_count())
print(f"There are {pair_count} Pairs, {skips} Skips and Chunksize is : {chunksize}")
all_pairs_chunks =[]
with concurrent.futures.ProcessPoolExecutor() as executor:
# Start the load operations and mark each future with its URL
query_futures = [executor.submit(graphql_query, pairs_query,subkeys=['data','pairs'], variables={'skip':SKIP_SIZE*i}) for i in range(skips)]
for future in concurrent.futures.as_completed(query_futures):
try:
all_pairs_chunks.append(future.result())
except Exception as exc:
print(f"Exception in query {exc}")
else:
print(f"Total {SKIP_SIZE} entry Chunks Read: {len(all_pairs_chunks)}")
all_pairs = list(chain.from_iterable(all_pairs_chunks))
print(f"Read: {len(all_pairs)} Pairs")
with open(f"../.data/pairs.json",'w') as fp:
json.dump(all_pairs,fp,indent='\t')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment