Skip to content

Commit bdd316e

Browse files
committed
Move eth2.0 related changes
1 parent f070090 commit bdd316e

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

eth2.0/testing/args.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const argv = require('argv');
2+
const _ = require('underscore');
3+
const { join } = require('path');
4+
const { lstatSync, readdirSync } = require('fs');
5+
const isDirectory = source => lstatSync(source).isDirectory();
6+
const getDirectories = source => readdirSync(source).map(name => join(source, name)).filter(isDirectory);
7+
8+
const args = argv.option([
9+
{
10+
name: 'validator',
11+
short: 'v',
12+
type: 'string',
13+
description: 'When available, an individual validator (specified by directory name in validators) to run test on, defaults to all.',
14+
example: "'npm run test-validator -- --validator=piedao'"
15+
},
16+
{
17+
name: 'timestamp',
18+
short: 'ts',
19+
type: 'number',
20+
description: 'When available, defines a unix timestamp to run the test at, defaults to latest.',
21+
example: "'npm run test-validator -- --validator=piedao -- --timestamp=1602667372'"
22+
}
23+
]).run();
24+
25+
const validators = args.options.validator ? [args.options.validator] : getDirectories('eth2.0/validators').map((dir) => dir.split('/')[dir.split('/').length - 1]);
26+
27+
module.exports = {
28+
validators: _.map(validators, (validator) => {
29+
let path = `eth2.0/validators/${validator}`;
30+
let dataObj = {};
31+
32+
try {
33+
dataObj = {
34+
...require(`../../${path}`)
35+
}
36+
} catch (error) {
37+
console.log(error.message);
38+
}
39+
40+
return {
41+
path,
42+
validator,
43+
...dataObj
44+
};
45+
}),
46+
timestamp: args.options.timestamp ? Number(args.options.timestamp) : undefined,
47+
};

eth2.0/testing/run.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const fs = require('fs');
2+
const chai = require('chai');
3+
const _ = require('underscore');
4+
const moment = require('moment');
5+
const shell = require('shelljs');
6+
const sdk = require('../../sdk');
7+
const BigNumber = require('bignumber.js');
8+
9+
const balanceCallTimeLimit = 1000 * 60 * 5;
10+
let testResult = null;
11+
12+
/**
13+
*
14+
* @param {Object} stakingAdapter
15+
* @param {String/Number} timeUnit
16+
* @param {Number} timeOffset
17+
* @returns {Promise<{output}>}
18+
* @private
19+
*/
20+
const _run = async (stakingAdapter, timeUnit = 'day', timeOffset = 0) => {
21+
try {
22+
let addresses = [];
23+
let timestamp;
24+
25+
if (typeof timeUnit === 'number') {
26+
timestamp = timeUnit;
27+
} else {
28+
timestamp = moment().utcOffset(0).startOf(timeUnit).add(timeOffset, timeUnit).unix();
29+
}
30+
31+
if (!Array.isArray(stakingAdapter.address)) {
32+
addresses = [stakingAdapter.address];
33+
}
34+
35+
const tvl = await Promise.all(addresses.map(async (address) => {
36+
const data = await sdk.api.util.testStakingAdapter(timestamp, address);
37+
38+
return {
39+
depositAddress: address,
40+
...data,
41+
}
42+
}));
43+
44+
return {
45+
timestamp,
46+
tvl,
47+
}
48+
} catch (error) {
49+
console.error(error);
50+
}
51+
};
52+
53+
/**
54+
*
55+
* @param {Object} stakingAdapter
56+
* @param {String/Number} timeUnit
57+
* @param {Number} timeOffset
58+
*/
59+
module.exports = async (stakingAdapter, timeUnit, timeOffset = 0) => {
60+
let label;
61+
62+
if (typeof timeUnit === 'number') {
63+
label = `returns valid tvl data at ${moment.unix(timeUnit).utcOffset(0).format()}`;
64+
} else {
65+
label = `returns valid tvl data at ${timeUnit} ${timeOffset}`;
66+
}
67+
68+
it(label, async function() {
69+
this.timeout(balanceCallTimeLimit);
70+
const projectRun = await _run(stakingAdapter, timeUnit, timeOffset);
71+
testResult = projectRun;
72+
const tvl = projectRun.tvl;
73+
74+
chai.expect(tvl).to.be.an('array');
75+
chai.expect(tvl.length).to.be.at.least(1);
76+
77+
_.each(tvl, ({ depositAddress, timestamp, block, balance }) => {
78+
chai.expect(depositAddress).to.be.a('string');
79+
80+
chai.expect(timestamp).to.be.a('number');
81+
chai.expect(timestamp).to.be.finite;
82+
83+
chai.expect(block).to.be.a('number');
84+
chai.expect(block).to.be.finite;
85+
86+
chai.expect(balance).to.be.a('number');
87+
chai.expect(balance).to.be.finite;
88+
chai.expect(balance).to.be.at.least(0);
89+
})
90+
});
91+
92+
93+
afterEach('save staking adapter output', () => {
94+
const time = moment.unix(testResult.timestamp).utcOffset(0).format();
95+
const path = `eth2.0/output/${stakingAdapter.name}/staking-tvl`;
96+
const name = `${time}.json`;
97+
98+
shell.mkdir('-p', path);
99+
fs.writeFileSync(`${path}/${name}`, JSON.stringify(testResult, null, 2));
100+
});
101+
};

eth2.0/testing/test/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const _ = require('underscore');
2+
const moment = require('moment');
3+
const args = require('../args');
4+
const run = require('../run');
5+
const spotTestCount = 10;
6+
7+
_.each(args.validators, function(stakingAdapter) {
8+
describe(`${stakingAdapter.name} eth2.0 staking adapter running & output format`, function () {
9+
describe('runs for a variety of points at different times', function() {
10+
this.bail(true);
11+
12+
const latestDay = moment().utcOffset(0).add(-10, 'days').startOf('day').unix();
13+
const startDay = moment.unix(stakingAdapter.start).utcOffset(0).startOf('day').unix();
14+
const diff = latestDay - startDay;
15+
const step = diff / spotTestCount;
16+
let spotTests = _.range(latestDay, startDay + 10, -step);
17+
spotTests = _.map(spotTests, (spotTest) => {
18+
return moment.unix(spotTest).utcOffset(0).startOf('day').unix();
19+
});
20+
21+
spotTests = _.uniq(spotTests);
22+
23+
run(stakingAdapter, 'hour', 0);
24+
run(stakingAdapter, 'hour', -12);
25+
run(stakingAdapter, 'hour', -36);
26+
run(stakingAdapter, 'hour', -72);
27+
28+
_.each(spotTests, (timestamp) => {
29+
run(stakingAdapter, timestamp, 0,);
30+
});
31+
32+
run(stakingAdapter, startDay < stakingAdapter.start ? startDay + 86400 : startDay);
33+
});
34+
});
35+
});
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const _ = require('underscore');
2+
const chai = require('chai');
3+
const moment = require('moment');
4+
5+
const args = require('../args');
6+
const categories = [
7+
'Pooled',
8+
'Custodial',
9+
];
10+
11+
_.each(args.projects, (stakingAdapter) => {
12+
describe(`Checking ${stakingAdapter.name} project adapter metadata`, () => {
13+
it('has a valid name', () => {
14+
chai.assert.isString(stakingAdapter.name);
15+
});
16+
17+
it('has a valid start time', () => {
18+
chai.expect(stakingAdapter.start).to.be.at.most(moment().utcOffset(0).unix(), 'unix start time must be less than the current time')
19+
});
20+
21+
it('category matches one of the defined categories', () => {
22+
chai.expect(stakingAdapter.category).to.be.oneOf(categories);
23+
});
24+
25+
it('has a valid staking address', () => {
26+
chai.assert.isString(stakingAdapter.symbol);
27+
});
28+
});
29+
});

eth2.0/validators/f2pool/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
name: "F2Pool",
3+
category: "Pooled",
4+
start: 1602667372,
5+
address: '0x61c808d82a3ac53231750dadc13c777b59310bd9'
6+
};

eth2.0/validators/piedao/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
name: "Piedao",
3+
category: "Pooled",
4+
start: 1602667372,
5+
address: '0x66827bcd635f2bb1779d68c46aeb16541bca6ba8'
6+
};

0 commit comments

Comments
 (0)