-
Notifications
You must be signed in to change notification settings - Fork 4
/
theGraph.js
63 lines (56 loc) · 1.47 KB
/
theGraph.js
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
const axios = require('axios')
async function fetchSwapForPair(pairId, timestamp, chainId) {
const query = `
query GetSwap {
swaps(first: 1, orderBy: timestamp, orderDirection: desc, where:
{ pair: "${pairId}", timestamp_lt: "${timestamp}" }
) {
pair {
token0 {
symbol
}
token1 {
symbol
}
}
amount0In
amount0Out
amount1In
amount1Out
amountUSD
to
}
}
`
console.log(`query ---> : ${query}`)
let graphUrl = ''
if(chainId === 1) {
graphUrl = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2'
} else if(chainId === 100) {
graphUrl = 'https://api.thegraph.com/subgraphs/name/1hive/uniswap-v2'
} else {
throw new Error("unsupported chainId")
}
try {
const response = await axios({
url: graphUrl,
method: 'post',
data: {
query
}
})
//console.log(`response : ${JSON.stringify(response, null, 2)}`)
return response.data.data.swaps[0]
} catch (error) {
console.error(error)
throw new Error(`Error for pairId: ${pairId}, timestamp: ${timestamp}, chainId: ${chainId}`)
}
}
async function run() {
await fetchSwapForPair("0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc", "1615588247")
await fetchSwapForPair("0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc", "1615591976")
}
// run()
module.exports = {
fetchSwapForPair
}