forked from balancer/bal-mining-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
222 lines (184 loc) · 6.25 KB
/
utils.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require('dotenv').config();
const fs = require('fs');
const cliProgress = require('cli-progress');
const fetch = require('isomorphic-fetch');
const BigNumber = require('bignumber.js');
const ethers = require('ethers');
const { argv } = require('yargs');
const configFile = argv.config ? argv.config : './config';
const config = require(configFile);
const provider = new ethers.providers.WebSocketProvider(config.node);
const SUBGRAPH_URL =
process.env.SUBGRAPH_URL ||
'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer';
const MARKET_API_URL =
process.env.MARKET_API_URL || 'https://api.coingecko.com/api/v3';
const getConfig = () => config;
function bnum(val) {
return new BigNumber(val.toString());
}
const stringBnum = (val) => val.toString() + '0'.repeat(18);
const scale = (input, decimalPlaces) => {
const scalePow = bnum(decimalPlaces);
const scaleMul = bnum(10).pow(scalePow);
return bnum(input).times(scaleMul);
};
const writeData = (data, path) => {
try {
fs.writeFileSync(`./${path}.json`, JSON.stringify(data, null, 4));
} catch (err) {
console.error(err);
}
};
async function fetchAllPools(block) {
let poolResults = [];
let skip = 0;
let paginatePools = true;
while (paginatePools) {
const poolsParam = JSON.stringify(config.pools);
let query = `
{
pools (first: 1000, skip: ${skip}, block: { number: ${block} }, where: { id_in: ${poolsParam}}) {
id
publicSwap
swapFee
controller
createTime
tokensList
totalShares
shares (first: 1000) {
userAddress {
id
}
}
}
}
`;
let response = await fetch(SUBGRAPH_URL, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
}),
});
let { data } = await response.json();
poolResults = poolResults.concat(data.pools);
if (data.pools.length < 1000) {
paginatePools = false;
} else {
skip += 1000;
continue;
}
}
let finalResults = [];
for (i in poolResults) {
let pool = poolResults[i];
pool.shareHolders = pool.shares.map((a) => a.userAddress.id);
if (pool.shareHolders.length == 1000) {
let paginateShares = true;
let shareSkip = 0;
let shareResults = [];
while (paginateShares) {
let query = `
{
pools (where: { id: "${pool.id}"}, block: { number: ${block} }) {
shares (first: 1000, skip: ${shareSkip}) {
userAddress {
id
}
}
}
}
`;
let response = await fetch(SUBGRAPH_URL, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
}),
});
let { data } = await response.json();
let newShareHolders = data.pools[0].shares.map(
(a) => a.userAddress.id
);
shareResults = shareResults.concat(newShareHolders);
if (newShareHolders.length < 1000) {
paginateShares = false;
} else {
shareSkip += 1000;
continue;
}
}
pool.shareHolders = shareResults;
delete pool.shares;
finalResults.push(pool);
} else {
delete pool.shares;
finalResults.push(pool);
}
}
return finalResults;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function fetchTokenPrices(allTokens, startTime, endTime, priceProgress) {
let prices = {};
for (j in allTokens) {
const address = allTokens[j]
? ethers.utils.getAddress(allTokens[j])
: null;
const query = `coins/ethereum/contract/${address}/market_chart/range?&vs_currency=usd&from=${startTime}&to=${endTime}`;
const response = await fetch(`${MARKET_API_URL}/${query}`, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
let priceResponse = await response.json();
prices[address] = priceResponse.prices;
priceProgress.increment();
// Sleep half a second between requests to prevent rate-limiting
await sleep(1000);
}
priceProgress.stop();
return prices;
}
function checkArgsAndGetPeriodParams() {
if (!argv.period) {
console.error(`Usage: node ${argv['$0']} --period 1`);
process.exit();
}
const PERIOD = argv.period;
if (PERIOD >= config.periodBlockDelimiters.length) {
console.error(
'Period too big. Adjust config file to set block delimiters for the desired period'
);
process.exit();
}
if (PERIOD == 0) {
console.error('Period can’t be zero');
process.exit();
}
const START_BLOCK = config.periodBlockDelimiters[PERIOD - 1]; // Closest block to reference time at beginning of week
const END_BLOCK = config.periodBlockDelimiters[PERIOD]; // Closest block to reference time at end of week
const SKIP_BLOCK = argv.skipBlock;
const OUTPUT_FOLDER = argv.output ? argv.output : 'reports';
return { PERIOD, START_BLOCK, END_BLOCK, SKIP_BLOCK, OUTPUT_FOLDER };
}
module.exports = {
bnum,
stringBnum,
getConfig,
scale,
writeData,
fetchAllPools,
fetchTokenPrices,
checkArgsAndGetPeriodParams,
};