-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_query.js
89 lines (81 loc) · 2.2 KB
/
save_query.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
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
const axios = require('axios');
require("dotenv").config();
const fs = require('fs'); // Import the 'fs' module for file system operations
const query = `
query TopTrades {
swaps(first: 10, orderBy: amountUSD, orderDirection: desc) {
id
amountUSD
transaction {
id
}
pair {
token0 {
symbol
}
token1 {
symbol
}
}
}
}
`;
const fetchTopTrades = async () => {
const url = process.env.UNISWAP_SUBGRAPH_URL;
console.log("Constructed URL:", url);
try {
const response = await axios.post(
url,
{
query: query,
variables: {},
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.GRAPH_API_KEY}`, // Include if API key is needed
},
},
);
if (response.data.errors) {
console.error("GraphQL Errors:", response.data.errors);
return;
}
const trades = response.data.data.swaps;
const tradeData = trades.map((trade) => {
const token0Symbol =
trade.pair.token0.symbol === "unknown"
? "Unknown Token"
: trade.pair.token0.symbol;
const token1Symbol =
trade.pair.token1.symbol === "unknown"
? "Unknown Token"
: trade.pair.token1.symbol;
return {
tradeId: trade.id,
transactionId: trade.transaction.id,
amountUSD: parseFloat(trade.amountUSD).toExponential(),
pair: `${token0Symbol} / ${token1Symbol}`,
};
});
// Write the data to a file (append mode)
fs.appendFile('trade_data.json', JSON.stringify(tradeData, null, 2), (err) => {
if (err) {
console.error("Error writing to file:", err);
} else {
console.log("Trade data appended to trade_data.json");
}
});
} catch (error) {
console.error("Error Details:", error);
if (error.response) {
console.error("Error Response:", error.response.data);
} else if (error.request) {
console.error("No Response:", error.request);
} else {
console.error("Axios Error:", error.message);
}
console.error("Error Config:", error.config);
}
};
fetchTopTrades();