-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathuniPoolData.mjs
141 lines (116 loc) · 3.06 KB
/
uniPoolData.mjs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fetch from 'node-fetch'
const urlForProtocol = (protocol) => {
return protocol === 1 ? "https://api.thegraph.com/subgraphs/name/ianlapham/optimism-post-regenesis" :
protocol === 2 ? "https://api.thegraph.com/subgraphs/name/ianlapham/arbitrum-minimal" :
protocol === 3 ? "https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v3-polygon" :
protocol === 4 ? "https://api.thegraph.com/subgraphs/name/perpetual-protocol/perpetual-v2-optimism" :
"https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3";
}
const requestBody = (request) => {
if(!request.query) return;
const body = {
method:'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: request.query,
variables: request.variables || {}
})
}
if (request.signal) body.signal = request.signal;
return body;
}
export const getPoolHourData = async (pool, fromdate, todate, protocol) => {
const query = `query PoolHourDatas($pool: ID!, $fromdate: Int!, $todate: Int!) {
poolHourDatas ( where:{ pool:$pool, periodStartUnix_gt:$fromdate periodStartUnix_lt:$todate close_gt: 0}, orderBy:periodStartUnix, orderDirection:desc, first:1000) {
periodStartUnix
liquidity
high
low
pool {
id
totalValueLockedUSD
totalValueLockedToken1
totalValueLockedToken0
token0
{decimals}
token1
{decimals}
}
close
feeGrowthGlobal0X128
feeGrowthGlobal1X128
}
}
`
const url = urlForProtocol(protocol);
try {
const response = await fetch(url, requestBody({query: query, variables: {pool: pool, fromdate: fromdate, todate} }));
const data = await response.json();
if (data && data.data && data.data.poolHourDatas) {
return data.data.poolHourDatas;
}
else {
console.log("nothing returned from getPoolHourData")
return null;
}
} catch (error) {
return {error: error};
}
}
export const poolById = async (id, protocol) => {
const url = urlForProtocol(protocol);
const poolQueryFields = `{
id
feeTier
totalValueLockedUSD
totalValueLockedETH
token0Price
token1Price
token0 {
id
symbol
name
decimals
}
token1 {
id
symbol
name
decimals
}
poolDayData(orderBy: date, orderDirection:desc,first:1)
{
date
volumeUSD
tvlUSD
feesUSD
liquidity
high
low
volumeToken0
volumeToken1
close
open
}
}`
const query = `query Pools($id: ID!) { id: pools(where: { id: $id } orderBy:totalValueLockedETH, orderDirection:desc)
${poolQueryFields}
}`
try {
const response = await fetch(url, requestBody({query: query, variables: {id: id}}));
const data = await response.json();
if (data && data.data) {
const pools = data.data;
if (pools.id && pools.id.length && pools.id.length === 1) {
return pools.id[0]
}
}
else {
return null;
}
} catch (error) {
return {error: error};
}
}