-
Notifications
You must be signed in to change notification settings - Fork 1
/
index_fees.js
170 lines (147 loc) · 6.08 KB
/
index_fees.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require('dotenv').config();
const axios = require('axios');
const fs = require('fs');
const axiosRateLimit = require('axios-rate-limit');
const { assert } = require('console');
const https = require('https');
const createCSV = require('csv-writer').createObjectCsvWriter;
const GRAPH_BASE_URL = 'https://api.thegraph.com/subgraphs/name/paraswap/paraswap-subgraph';
const GRAPH_PAGE_SIZE = 1000;
const GRAPH_MAX_SKIP = 5000;
const GRAPH_RETRIES = 10;
const GRAPH_RETRIES_SLEEP_MS = 1 * 1000;
const CHAINS_CONFIGURATION = {
'1': {},
'10': { urlSuffix: '-optimism' },
'56': { urlSuffix: '-bsc' },
'137': { urlSuffix: '-polygon' },
'250': { urlSuffix: '-fantom' },
'42161': { urlSuffix: '-arbitrum' },
'43114': { urlSuffix: '-avalanche' },
}
const httpsAgent = new https.Agent({ keepAlive: true });
const http = axiosRateLimit(axios.create({ httpsAgent }), { maxRPS: 5 });
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchSupbgraphPage(referrer, url, blockNumber, skip) {
console.info(`Fetching blockNumber: ${blockNumber}, skip: ${skip}} from ${url}`);
const query =
`
{
swaps(
first: ${GRAPH_PAGE_SIZE},
skip: ${skip},
orderBy: timestamp,
orderDirection: asc,
where: { blockNumber_gte: ${blockNumber}, referrer: "${referrer}", referrerFee_gt: 0 }
) {
id
blockNumber
feeToken
referrer
referrerFee
}
}
`.trim()
const response = await http.post(url, { query });
const { data: { data: { swaps } } } = response;
return swaps;
}
async function getAllChainFees(referrer) {
const dir = `./data/${referrer}`
if (!fs.existsSync(dir)){
fs.mkdirSync(dir, { recursive: true });
}
const chainsKeys = Object.keys(CHAINS_CONFIGURATION);
for (const c of chainsKeys) {
console.info(`Starting fee indexing for chain ${c}`);
const subgraphUrl = CHAINS_CONFIGURATION[c].urlSuffix ? GRAPH_BASE_URL + CHAINS_CONFIGURATION[c].urlSuffix : GRAPH_BASE_URL;
let chainResults = {};
let blockNumber = 0;
let skip = 0;
const lastIndexedReferrerFees = new Set();
while (true) { // external loop for fetching pages while we have more
let retry = 0;
let swaps = [];
do { // internal do..while loop for retires
try {
swaps = await fetchSupbgraphPage(referrer, subgraphUrl, blockNumber, skip);
if (swaps && swaps.length) {
// find the last one we indexed
let i = 0
while (i < swaps.length) {
if (lastIndexedReferrerFees.has(swaps[i].id)) {
i++;
} else {
break; // break the search for last index indexed
}
}
console.log(`Staring page indexing from index ${i}`);
lastIndexedReferrerFees.clear();
// index the new referrer fees
for (let s = i; s < swaps.length; s++) {
const { id, feeToken, referrer, referrerFee } = swaps[s];
lastIndexedReferrerFees.add(id);
if (!(referrer in chainResults)) {
chainResults[referrer] = {};
}
const referrerResults = chainResults[referrer];
if (!(feeToken in referrerResults)) {
referrerResults[feeToken] = 0n;
}
referrerResults[feeToken] += BigInt(referrerFee);
}
console.info(`Indexed ${swaps.length} swaps`);
} else {
console.warn('Found no swaps in last request!');
}
break; // break the internal do..while retry loop
} catch (e) {
console.error('Failed querying subgraph', e);
await sleep(GRAPH_RETRIES_SLEEP_MS);
}
} while (retry++ < GRAPH_RETRIES)
if (swaps.length < GRAPH_PAGE_SIZE) {
console.info('No more data for chain');
break; // break the external while(true) loop
} else {
// Increment skip if we have more room
if (skip + GRAPH_PAGE_SIZE <= GRAPH_MAX_SKIP) {
skip += GRAPH_PAGE_SIZE;
continue;
}
// Increment blockNumber to the last block we got
blockNumber = swaps.slice(-1)[0].blockNumber;
skip = 0;
}
} // loop single chain
if(!Object.keys(chainResults).length){
console.info(`No records for ${referrer} in ${c}`);
continue;
}
// write results
const filename = `./${dir}/referrer_fees_${c}.csv`;
console.info(`Writing data to csv '${filename}'`);
const csv = createCSV({
path: `./${filename}`,
alwaysQuote: true,
header: [
{ id: 'r', title: 'referrer' },
{ id: 't', title: 'token' },
{ id: 'f', title: 'fees' },
]
});
const records = [];
for (const [referrer, referrerTokens] of Object.entries(chainResults)) {
for (const [referrerToken, fees] of Object.entries(referrerTokens)) {
records.push({ r: referrer, t: referrerToken, f: fees })
}
}
csv.writeRecords(records);
console.info(`Wrote ${records.length} records to csv '${filename}'`);
} // loop chains
}
if(!process.env.REFERRERS) throw new Error("Missing referrer in .env");
const referrers = process.env.REFERRERS.split(',');
referrers.forEach(getAllChainFees);