|
| 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 | +}; |
0 commit comments