forked from paralink-network/paralink-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_uniswap.py
67 lines (58 loc) · 1.79 KB
/
graphql_uniswap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
from asyncio import get_event_loop
from aiohttp import ClientSession
the_graph_uniswap_query = """{
uniswapFactories(first: 5) {
id
pairCount
totalVolumeUSD
}
tokens(first: 5) {
tradeVolumeUSD
}
}
"""
async def main():
"""Obtains a sum of the tradeVolumeUSD for the first 5 tokens on uniswap."""
url = "http://127.0.0.1:7424"
the_graph_uniswap_trade_volume = {
"name": "GraphQL POST HTTP",
"psql_version": "0.1",
"sources": [
{
"name": "Uniswap data from The Graph",
"pipeline": [
{
"step": "extract",
"method": "http.post",
"uri": "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2",
"params": {"query": the_graph_uniswap_query},
},
{
"step": "traverse",
"method": "json",
"params": ["data"],
},
{
"step": "query.sql",
"method": ["json", "json"],
"query": "SELECT sum(tradeVolumeUSD) FROM tokens",
"result": True,
},
],
},
],
}
# Construct JSON RPC request
request = {
"jsonrpc": "2.0",
"method": "execute_pql",
"params": json.dumps(the_graph_uniswap_trade_volume),
"id": 1,
}
async with ClientSession() as session:
async with session.post(url + "/rpc", json=request) as resp:
response = await resp.json()
print(response)
if __name__ == "__main__":
get_event_loop().run_until_complete(main())