forked from snapshot-labs/snapshot-strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
209 lines (188 loc) · 7 KB
/
index.spec.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
const { JsonRpcProvider } = require('@ethersproject/providers');
const { getAddress } = require('@ethersproject/address');
const fetch = require('cross-fetch');
const snapshot = require('../').default;
const networks = require('@snapshot-labs/snapshot.js/src/networks.json');
const snapshotjs = require('@snapshot-labs/snapshot.js');
const addresses = require('./addresses.json');
const strategyArg =
process.env['npm_config_strategy'] ||
(
process.argv.find((arg) => arg.includes('--strategy=')) ||
'--strategy=erc20-balance-of'
)
.split('--strategy=')
.pop();
const moreArg =
process.env['npm_config_more'] ||
process.argv
.find((arg) => arg.includes('--more='))
?.split('--more=')
?.pop();
const strategy = Object.keys(snapshot.strategies).find((s) => strategyArg == s);
if (!strategy) throw 'Strategy not found';
const example = require(`../src/strategies/${strategy}/examples.json`)[0];
function callGetScores(example) {
return snapshot.utils.getScoresDirect(
'yam.eth',
[example.strategy],
example.network,
new JsonRpcProvider(networks[example.network].rpc[0]),
example.addresses,
example.snapshot
);
}
describe(`\nTest strategy "${strategy}"`, () => {
let scores = null;
let getScoresTime = null;
it('Strategy name should be lowercase and should not contain any special char expect hyphen', () => {
expect(strategy).toMatch(/^[a-z0-9\-]+$/);
});
it('Strategy name should be same as in example.json', () => {
expect(example.strategy.name).toBe(strategy);
});
it('Strategy should run without any errors', async () => {
const getScoresStart = performance.now();
scores = await callGetScores(example);
const getScoresEnd = performance.now();
getScoresTime = getScoresEnd - getScoresStart;
console.log(scores);
console.log(`Resolved in ${(getScoresTime / 1e3).toFixed(2)} sec.`);
}, 2e4);
it('Should return an array of object with addresses', () => {
expect(scores).toBeTruthy();
// Check array
expect(Array.isArray(scores)).toBe(true);
// Check array contains a object
expect(typeof scores[0]).toBe('object');
// Check object contains at least one address from example.json
expect(Object.keys(scores[0]).length).toBeGreaterThanOrEqual(1);
expect(
Object.keys(scores[0]).some((address) =>
example.addresses
.map((v) => v.toLowerCase())
.includes(address.toLowerCase())
)
).toBe(true);
// Check if all scores are numbers
expect(
Object.values(scores[0]).every((val) => typeof val === 'number')
).toBe(true);
});
it('Should take less than 10 sec. to resolve', () => {
expect(getScoresTime).toBeLessThanOrEqual(10000);
});
it('File examples.json should include at least 1 address with a positive score', () => {
expect(Object.values(scores[0]).some((score) => score > 0)).toBe(true);
});
it('File examples.json must use a snapshot block number in the past', async () => {
expect(typeof example.snapshot).toBe('number');
const provider = snapshot.utils.getProvider(example.network);
const blockNumber = await snapshot.utils.getBlockNumber(provider);
expect(example.snapshot).toBeLessThanOrEqual(blockNumber);
});
it('Returned addresses should be either same case as input addresses or checksum addresses', () => {
expect(
Object.keys(scores[0]).every(
(address) =>
example.addresses.includes(address) || getAddress(address) === address
)
).toBe(true);
});
});
describe(`\nTest strategy "${strategy}" with latest snapshot`, () => {
let scores = null;
let getScoresTime = null;
it('Strategy should run without any errors', async () => {
const getScoresStart = performance.now();
scores = await callGetScores({ ...example, snapshot: 'latest' });
const getScoresEnd = performance.now();
getScoresTime = getScoresEnd - getScoresStart;
console.log('Scores with latest snapshot', scores);
console.log(`Resolved in ${(getScoresTime / 1e3).toFixed(2)} sec.`);
// wait for all logs to be printed (bug: printed after results)
await new Promise((r) => setTimeout(r, 500));
}, 2e4);
it('Should return an array of object with addresses', () => {
expect(scores).toBeTruthy();
// Check array
expect(Array.isArray(scores)).toBe(true);
// Check array contains a object
expect(typeof scores[0]).toBe('object');
// Check object contains atleast one address from example.json
expect(Object.keys(scores[0]).length).toBeGreaterThanOrEqual(1);
expect(
Object.keys(scores[0]).some((address) =>
example.addresses
.map((v) => v.toLowerCase())
.includes(address.toLowerCase())
)
).toBe(true);
// Check if all scores are numbers
expect(
Object.values(scores[0]).every((val) => typeof val === 'number')
).toBe(true);
});
});
(moreArg ? describe : describe.skip)(
`\nTest strategy "${strategy}" (with ${moreArg || 500} addresses)`,
() => {
let scoresMore = null;
let getScoresTimeMore = null;
it(`Should work with ${moreArg || 500} addresses`, async () => {
example.addresses = addresses.slice(0, moreArg);
const getScoresStart = performance.now();
scoresMore = await callGetScores(example);
const getScoresEnd = performance.now();
getScoresTimeMore = getScoresEnd - getScoresStart;
console.log(`Scores with ${moreArg || 500} addresses`, scoresMore);
console.log(`Resolved in ${(getScoresTimeMore / 1e3).toFixed(2)} sec.`);
// wait for all logs to be printed (bug: printed after results)
await new Promise((r) => setTimeout(r, 500));
});
it(`Should take less than 20 sec. to resolve with ${
moreArg || 500
} addresses`, () => {
expect(getScoresTimeMore).toBeLessThanOrEqual(20000);
});
}
);
describe(`\nOthers:`, () => {
it('Author in strategy should be a valid github username', async () => {
const author = snapshot.strategies[strategy].author;
expect(typeof author).toBe('string');
const githubUserData = await fetch(
`https://api.github.com/users/${author}`
);
const githubUser = await githubUserData.json();
expect(githubUser.message).not.toEqual('Not Found');
});
it('Version in strategy should be a valid string', async () => {
const version = snapshot.strategies[strategy].author;
expect(typeof version).toBe('string');
});
let schema;
try {
schema = require(`../src/strategies/${strategy}/schema.json`);
} catch (error) {
schema = null;
}
(schema ? it : it.skip)(
'Check schema (if available) is valid with example.json',
async () => {
expect(typeof schema).toBe('object');
expect(
snapshotjs.utils.validateSchema(schema, example.strategy.params)
).toBe(true);
}
);
(schema ? it : it.skip)(
'Strategy should work even when strategy symbol is null',
async () => {
delete example.strategy.params.symbol;
expect(
snapshotjs.utils.validateSchema(schema, example.strategy.params)
).toBe(true);
}
);
});