-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest.mjs
144 lines (107 loc) · 4.43 KB
/
test.mjs
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
import assert from 'assert';
import Convert from './dist/index.js';
import {formatNumber} from './dist/helpers.js';
const convert = new Convert();
const exchanges = ['binance', 'bitfinex', 'coinbase', 'kraken'];
describe('Rests API Main', function () {
it('Crypto to Fiat', async function () {
await convert.ready();
assert.strictEqual(typeof convert.BTC.USD(1), "number");
assert.strictEqual(typeof convert.ETH.EUR(1), "number");
assert.strictEqual(typeof convert.TRX.INR(1), "number");
assert.strictEqual(typeof convert.LTC.JPY(1), "number");
});
it('Fiat to Crypto', async function () {
await convert.ready();
assert.strictEqual(typeof convert.USD.BTC(1), "number");
assert.strictEqual(typeof convert.EUR.ETH(1), "number");
assert.strictEqual(typeof convert.JPY.TRX(1), "number");
assert.strictEqual(typeof convert.INR.LTC(1), "number");
});
it('Crypto to Crypto', async function () {
await convert.ready();
assert.strictEqual(typeof convert.LTC.BTC(1), "number");
assert.strictEqual(typeof convert.BTC.LTC(1), "number");
assert.strictEqual(typeof convert.DOGE.ETH(1), "number");
assert.strictEqual(typeof convert.ETH.DOGE(1), "number");
});
it('Fiat to Fiat', async function () {
await convert.ready();
assert.equal(formatNumber(1 / convert.EUR.USD(1), 0), formatNumber(convert.USD.EUR(1), 0))
assert.equal(formatNumber(1 / convert.JPY.INR(1), 0), formatNumber(convert.INR.JPY(1), 0))
});
it('Update options correctly', async function () {
await convert.ready();
let first = convert.BTC.USD(1);
convert.setOptions({
binance: false,
bitfinex: false,
});
let second = convert.BTC.USD(1);
assert.notEqual(first, second);
convert.setOptions({
binance: true,
bitfinex: true,
});
let third = convert.BTC.USD(1);
assert.equal(first, third);
});
it('Custom currencies', async function () {
convert.addCurrency('TESTING', 'USD', async ()=>{
return Math.floor((Math.random() * 1000))
}, 5000);
await convert.ready();
assert.strictEqual(typeof convert.TESTING.USD(1), "number");
assert.strictEqual(typeof convert.USD.TESTING(1), "number");
assert.strictEqual(typeof convert.BTC.TESTING(1), "number");
assert.strictEqual(typeof convert.TESTING.LTC(1), "number");
});
/**
* Compare all exchange pairs to look for major differences that indicate that the pricing is not correct.
*/
it('Prices are correct', async function () {
await convert.ready();
const allPricesCompare = {};
for(const exchange of exchanges){
const pairs = convert.ticker.crypto[exchange];
for(const pair in pairs){
allPricesCompare[pair] = allPricesCompare[pair] || {};
allPricesCompare[pair][exchange] = pairs[pair];
}
}
let differentPrices = 0;
for(const pair in allPricesCompare){
const prices = allPricesCompare[pair];
let compare = [];
let isBigDifference = false;
for(const exchange in prices){
const price = prices[exchange];
if(!compare.length){
compare.push([
exchange, price, 0
])
continue;
}
let currentDifference = Math.ceil((1 - (compare[0][1] / price)) * 100);
if(Math.abs(currentDifference) > 10){
isBigDifference = true;
differentPrices++;
}
compare.push([
exchange, price, currentDifference
]);
}
if(isBigDifference){
console.warn(`[${pair}] A 10% difference was detected on this pair: `);
for(const [exchange, price, difference] of compare){
console.info(` ${exchange}: ${price} | ${difference > 0 ? '+' + difference: difference}%`);
}
console.info('\r\n');
}
}
if(differentPrices){
console.warn(`[!] ${differentPrices} prices are not matching.`);
}
assert.strictEqual(differentPrices < 20, true);
});
});