-
Notifications
You must be signed in to change notification settings - Fork 586
/
Copy pathutils.ts
359 lines (332 loc) · 9.11 KB
/
utils.ts
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import fetch from 'cross-fetch';
import { Interface } from '@ethersproject/abi';
import { Contract } from '@ethersproject/contracts';
import { hash, normalize } from '@ensdomains/eth-ens-namehash';
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import Multicaller from './utils/multicaller';
import { getSnapshots } from './utils/blockfinder';
import getProvider from './utils/provider';
import { signMessage, getBlockNumber } from './utils/web3';
import { getHash, verify } from './sign/utils';
import gateways from './gateways.json';
import networks from './networks.json';
interface Options {
url?: string;
}
interface Strategy {
name: string;
network?: string;
params: any;
}
export const SNAPSHOT_SUBGRAPH_URL = {
'1': 'https://gateway.thegraph.com/api/0f15b42bdeff7a063a4e1757d7e2f99e/deployments/id/QmXvEzRJXby7KFuTr7NJsM47hGefM5VckEXZrQyZzL9eJd',
'4': 'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-rinkeby',
'42': 'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-kovan',
'56': 'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-binance-smart-chain',
'100':
'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-gnosis-chain',
'137':
'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-polygon',
'250': 'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-fantom'
};
const ENS_RESOLVER_ABI = [
'function text(bytes32 node, string calldata key) external view returns (string memory)'
];
export async function call(provider, abi: any[], call: any[], options?) {
const contract = new Contract(call[0], abi, provider);
try {
const params = call[2] || [];
return await contract[call[1]](...params, options || {});
} catch (e) {
return Promise.reject(e);
}
}
export async function multicall(
network: string,
provider,
abi: any[],
calls: any[],
options?
) {
const multicallAbi = [
'function aggregate(tuple(address target, bytes callData)[] calls) view returns (uint256 blockNumber, bytes[] returnData)'
];
const multi = new Contract(
networks[network].multicall,
multicallAbi,
provider
);
const itf = new Interface(abi);
try {
const max = options?.limit || 500;
const pages = Math.ceil(calls.length / max);
const promises: any = [];
Array.from(Array(pages)).forEach((x, i) => {
const callsInPage = calls.slice(max * i, max * (i + 1));
promises.push(
multi.aggregate(
callsInPage.map((call) => [
call[0].toLowerCase(),
itf.encodeFunctionData(call[1], call[2])
]),
options || {}
)
);
});
let results: any = await Promise.all(promises);
results = results.reduce((prev: any, [, res]: any) => prev.concat(res), []);
return results.map((call, i) =>
itf.decodeFunctionResult(calls[i][1], call)
);
} catch (e) {
return Promise.reject(e);
}
}
export async function subgraphRequest(url: string, query, options: any = {}) {
const res = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...options?.headers
},
body: JSON.stringify({ query: jsonToGraphQLQuery({ query }) })
});
const responseData = await res.json();
if (responseData.errors) {
throw new Error(
'Errors found in subgraphRequest: ' +
url +
JSON.stringify(responseData.errors)
);
}
const { data } = responseData;
return data || {};
}
export function getUrl(uri, gateway = gateways[0]) {
const ipfsGateway = `https://${gateway}`;
if (!uri) return null;
if (
!uri.startsWith('ipfs://') &&
!uri.startsWith('ipns://') &&
!uri.startsWith('https://') &&
!uri.startsWith('http://')
)
return `${ipfsGateway}/ipfs/${uri}`;
const uriScheme = uri.split('://')[0];
if (uriScheme === 'ipfs')
return uri.replace('ipfs://', `${ipfsGateway}/ipfs/`);
if (uriScheme === 'ipns')
return uri.replace('ipns://', `${ipfsGateway}/ipns/`);
return uri;
}
export async function getJSON(uri) {
const url = getUrl(uri);
return fetch(url).then((res) => res.json());
}
export async function ipfsGet(
gateway: string,
ipfsHash: string,
protocolType = 'ipfs'
) {
const url = `https://${gateway}/${protocolType}/${ipfsHash}`;
return fetch(url).then((res) => res.json());
}
export async function sendTransaction(
web3,
contractAddress: string,
abi: any[],
action: string,
params: any[],
overrides = {}
) {
const signer = web3.getSigner();
const contract = new Contract(contractAddress, abi, web3);
const contractWithSigner = contract.connect(signer);
// overrides.gasLimit = 12e6;
return await contractWithSigner[action](...params, overrides);
}
export async function getScores(
space: string,
strategies: Strategy[],
network: string,
addresses: string[],
snapshot: number | string = 'latest',
scoreApiUrl = 'https://score.snapshot.org/api/scores'
) {
try {
const params = {
space,
network,
snapshot,
strategies,
addresses
};
const res = await fetch(scoreApiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ params })
});
const obj = await res.json();
return obj.result.scores;
} catch (e) {
return Promise.reject(e);
}
}
export async function getVp(
address: string,
network: string,
strategies: Strategy[],
snapshot: number | 'latest',
space: string,
delegation: boolean,
options?: Options
) {
if (!options) options = {};
if (!options.url) options.url = 'https://score.snapshot.org';
const init = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'get_vp',
params: {
address,
network,
strategies,
snapshot,
space,
delegation
},
id: null
})
};
const res = await fetch(options.url, init);
return (await res.json()).result;
}
export function validateSchema(schema, data) {
const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, $data: true });
// @ts-ignore
addFormats(ajv);
// Custom URL format to allow empty string values
// https://github.com/snapshot-labs/snapshot.js/pull/541/files
ajv.addFormat('customUrl', {
type: 'string',
validate: (str) => {
if (!str.length) return true;
return (
str.startsWith('http://') ||
str.startsWith('https://') ||
str.startsWith('ipfs://') ||
str.startsWith('ipns://') ||
str.startsWith('snapshot://')
);
}
});
const validate = ajv.compile(schema);
const valid = validate(data);
return valid ? valid : validate.errors;
}
export function getEnsTextRecord(ens: string, record: string, network = '1') {
const address = networks[network].ensResolver || networks['1'].ensResolver;
const ensHash = hash(normalize(ens));
const provider = getProvider(network);
return call(provider, ENS_RESOLVER_ABI, [address, 'text', [ensHash, record]]);
}
export async function getSpaceUri(id, network = '1') {
try {
return await getEnsTextRecord(id, 'snapshot', network);
} catch (e) {
console.log('getSpaceUriFromTextRecord failed', id, e);
}
return false;
}
export async function getDelegatesBySpace(
network: string,
space: string,
snapshot = 'latest'
) {
if (!SNAPSHOT_SUBGRAPH_URL[network]) {
return Promise.reject(
`Delegation subgraph not available for network ${network}`
);
}
const spaceIn = ['', space];
if (space.includes('.eth')) spaceIn.push(space.replace('.eth', ''));
const PAGE_SIZE = 1000;
let result = [];
let page = 0;
const params: any = {
delegations: {
__args: {
where: {
space_in: spaceIn
},
first: PAGE_SIZE,
skip: 0
},
delegator: true,
space: true,
delegate: true
}
};
if (snapshot !== 'latest') {
params.delegations.__args.block = { number: snapshot };
}
while (true) {
params.delegations.__args.skip = page * PAGE_SIZE;
const pageResult = await subgraphRequest(
SNAPSHOT_SUBGRAPH_URL[network],
params
);
const pageDelegations = pageResult.delegations || [];
result = result.concat(pageDelegations);
page++;
if (pageDelegations.length < PAGE_SIZE) break;
}
return result;
}
export function clone(item) {
return JSON.parse(JSON.stringify(item));
}
export async function sleep(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
export function getNumberWithOrdinal(n) {
const s = ['th', 'st', 'nd', 'rd'],
v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
}
export default {
call,
multicall,
subgraphRequest,
ipfsGet,
getUrl,
getJSON,
sendTransaction,
getScores,
getVp,
validateSchema,
getEnsTextRecord,
getSpaceUri,
getDelegatesBySpace,
clone,
sleep,
getNumberWithOrdinal,
getProvider,
signMessage,
getBlockNumber,
Multicaller,
getSnapshots,
getHash,
verify,
SNAPSHOT_SUBGRAPH_URL
};