Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b60ca94ee | ||
|
|
ed2ed28f93 | ||
|
|
2d93a68c6e | ||
|
|
1251c8b89b | ||
|
|
a796a38200 | ||
|
|
cf83fe9469 |
No files matched your search
@@ -11,7 +11,7 @@
|
||||
"noPropertyAccessFromIndexSignature": false,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"lib": ["es5", "es6", "dom", "dom.iterable"]
|
||||
"lib": ["es2021", "dom", "dom.iterable"]
|
||||
},
|
||||
"exclude": ["./src/types/explorer.d.ts"],
|
||||
"include": [],
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": false,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"lib": ["es5", "es6", "dom", "dom.iterable"]
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": [],
|
||||
"references": [
|
||||
|
||||
@@ -2761,7 +2761,7 @@
|
||||
"tranche_end": "2023-12-05T00:00:00.000Z",
|
||||
"total_added": "86666.297",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "66363.3098645820638555771",
|
||||
"locked_amount": "66243.522619730595190252",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "86666.297",
|
||||
@@ -2827,7 +2827,7 @@
|
||||
"tranche_end": "2023-06-01T00:00:00.000Z",
|
||||
"total_added": "2500",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "1270.50582519332525",
|
||||
"locked_amount": "1263.57600732600725",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "2500",
|
||||
@@ -2948,7 +2948,7 @@
|
||||
"tranche_end": "2023-09-01T00:00:00.000Z",
|
||||
"total_added": "17500",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "17500",
|
||||
"locked_amount": "17498.8903985507255",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "12500",
|
||||
@@ -3215,7 +3215,7 @@
|
||||
"tranche_end": "2023-08-01T00:00:00.000Z",
|
||||
"total_added": "37500",
|
||||
"total_removed": "3376.096819125",
|
||||
"locked_amount": "31800.99946286065125",
|
||||
"locked_amount": "31696.477900552485",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "7500",
|
||||
@@ -3370,7 +3370,7 @@
|
||||
"tranche_end": "2023-12-05T00:00:00.000Z",
|
||||
"total_added": "129999.45",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "66302.761694482463735865",
|
||||
"locked_amount": "66183.0837404197105596",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "129999.45",
|
||||
@@ -3436,7 +3436,7 @@
|
||||
"tranche_end": "2023-09-03T00:00:00.000Z",
|
||||
"total_added": "62600",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "31984.79667681380592",
|
||||
"locked_amount": "31898.27305936073208",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "10000",
|
||||
@@ -3629,7 +3629,7 @@
|
||||
"tranche_end": "2023-09-17T00:00:00.000Z",
|
||||
"total_added": "5000",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "2746.477042110604",
|
||||
"locked_amount": "2739.566210045662",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "5000",
|
||||
@@ -3840,7 +3840,7 @@
|
||||
"tranche_end": "2023-04-05T00:00:00.000Z",
|
||||
"total_added": "97499.58",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "8259.0344635706695729506",
|
||||
"locked_amount": "8141.6415391408092444606",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "97499.58",
|
||||
@@ -3873,7 +3873,7 @@
|
||||
"tranche_end": "2023-04-05T00:00:00.000Z",
|
||||
"total_added": "135173.4239508",
|
||||
"total_removed": "98230.390980249184455396",
|
||||
"locked_amount": "11288.674248884641168728801336",
|
||||
"locked_amount": "11128.218388231931879326063704",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "135173.4239508",
|
||||
@@ -3919,7 +3919,7 @@
|
||||
"tranche_end": "2023-04-05T00:00:00.000Z",
|
||||
"total_added": "32499.86",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "3474.433172927820788048",
|
||||
"locked_amount": "3425.0479968875506615442",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "32499.86",
|
||||
@@ -3952,7 +3952,7 @@
|
||||
"tranche_end": "2023-04-05T00:00:00.000Z",
|
||||
"total_added": "10833.29",
|
||||
"total_removed": "0",
|
||||
"locked_amount": "1130.8942826865467812457",
|
||||
"locked_amount": "1114.8198871078435790832",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "10833.29",
|
||||
@@ -3985,7 +3985,7 @@
|
||||
"tranche_end": "2023-04-05T00:00:00.000Z",
|
||||
"total_added": "22749.93",
|
||||
"total_removed": "4720.860935375",
|
||||
"locked_amount": "4227.535409358637757934",
|
||||
"locked_amount": "4167.4457285340302852478",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "6500",
|
||||
@@ -4137,7 +4137,7 @@
|
||||
"tranche_end": "2023-05-01T00:00:00.000Z",
|
||||
"total_added": "22500",
|
||||
"total_removed": "4913.13257745",
|
||||
"locked_amount": "7644.13558931859975",
|
||||
"locked_amount": "7581.4226519337009",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "7500",
|
||||
@@ -4412,7 +4412,7 @@
|
||||
"tranche_end": "2023-06-02T00:00:00.000Z",
|
||||
"total_added": "1939928.38",
|
||||
"total_removed": "928642.9598472029154",
|
||||
"locked_amount": "496902.418486694568232482",
|
||||
"locked_amount": "494221.11463625557046169",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "1852091.69",
|
||||
@@ -36792,7 +36792,7 @@
|
||||
"tranche_end": "2023-06-05T00:00:00.000Z",
|
||||
"total_added": "3732368.4671",
|
||||
"total_removed": "615950.831401702010493",
|
||||
"locked_amount": "788067.33889606523904645906",
|
||||
"locked_amount": "783947.108408790236382843098",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "1998.95815",
|
||||
@@ -38140,8 +38140,8 @@
|
||||
"tranche_start": "2022-06-05T00:00:00.000Z",
|
||||
"tranche_end": "2023-12-05T00:00:00.000Z",
|
||||
"total_added": "15870102.715470999700000001",
|
||||
"total_removed": "582201.49676312452960452",
|
||||
"locked_amount": "8094123.7706069735612090126362156915950257",
|
||||
"total_removed": "584996.67274505202169452",
|
||||
"locked_amount": "8079513.697843178693280892781044398053528",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "16249.93",
|
||||
@@ -38670,6 +38670,16 @@
|
||||
"user": "0x20CD77B9FC2f1fEDfb6F184E25f7127BFE991C8b",
|
||||
"tx": "0x1438fbbc780cb382033855d295fa2511deed15cd64506a62ae4e3cfe82b3678e"
|
||||
},
|
||||
{
|
||||
"amount": "2345.954988",
|
||||
"user": "0xF4c75FdbAe821C6B5DBB9f001bB23cBA58D1bA37",
|
||||
"tx": "0x1887d1fb77d9fa555dd5b55da307edc91e395f3a7e0ca5f874740f5a10077cdf"
|
||||
},
|
||||
{
|
||||
"amount": "449.22099392749209",
|
||||
"user": "0xe3eB4CF43C072658401996b71D43eE26E573562D",
|
||||
"tx": "0x3c76f03b71481a44c6ba2c7cea407f5f1bac60bcaa9242af18de287f94c8ed90"
|
||||
},
|
||||
{
|
||||
"amount": "535.4042378778335",
|
||||
"user": "0x20CD77B9FC2f1fEDfb6F184E25f7127BFE991C8b",
|
||||
@@ -41899,6 +41909,12 @@
|
||||
"tranche_id": 2,
|
||||
"tx": "0x525f9427c131bc2fdea7f556d16b7781faaa41e09c7735d1c02d1f7585cc49ab"
|
||||
},
|
||||
{
|
||||
"amount": "449.22099392749209",
|
||||
"user": "0xe3eB4CF43C072658401996b71D43eE26E573562D",
|
||||
"tranche_id": 2,
|
||||
"tx": "0x3c76f03b71481a44c6ba2c7cea407f5f1bac60bcaa9242af18de287f94c8ed90"
|
||||
},
|
||||
{
|
||||
"amount": "1640.93679110232897",
|
||||
"user": "0xe3eB4CF43C072658401996b71D43eE26E573562D",
|
||||
@@ -42141,8 +42157,8 @@
|
||||
}
|
||||
],
|
||||
"total_tokens": "150551.801",
|
||||
"withdrawn_tokens": "73436.73551735372338",
|
||||
"remaining_tokens": "77115.06548264627662"
|
||||
"withdrawn_tokens": "73885.95651128121547",
|
||||
"remaining_tokens": "76665.84448871878453"
|
||||
},
|
||||
{
|
||||
"address": "0x4d982Ab0823fD2f48e934a7be2bb0a5374a26148",
|
||||
@@ -42344,6 +42360,12 @@
|
||||
}
|
||||
],
|
||||
"withdrawals": [
|
||||
{
|
||||
"amount": "2345.954988",
|
||||
"user": "0xF4c75FdbAe821C6B5DBB9f001bB23cBA58D1bA37",
|
||||
"tranche_id": 2,
|
||||
"tx": "0x1887d1fb77d9fa555dd5b55da307edc91e395f3a7e0ca5f874740f5a10077cdf"
|
||||
},
|
||||
{
|
||||
"amount": "2820.55961",
|
||||
"user": "0xF4c75FdbAe821C6B5DBB9f001bB23cBA58D1bA37",
|
||||
@@ -42556,8 +42578,8 @@
|
||||
}
|
||||
],
|
||||
"total_tokens": "200000",
|
||||
"withdrawn_tokens": "95806.869254",
|
||||
"remaining_tokens": "104193.130746"
|
||||
"withdrawn_tokens": "98152.824242",
|
||||
"remaining_tokens": "101847.175758"
|
||||
},
|
||||
{
|
||||
"address": "0x1b956E6c00E238194B331eddEFF72Cb5f28A8d01",
|
||||
@@ -43916,7 +43938,7 @@
|
||||
"tranche_end": "2023-05-05T00:00:00.000Z",
|
||||
"total_added": "14597706.0446472999",
|
||||
"total_removed": "5577113.077460725181649906",
|
||||
"locked_amount": "1750998.157265811220227145011388614",
|
||||
"locked_amount": "1737510.231376472448763806568635843",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "129284.449",
|
||||
@@ -50984,8 +51006,8 @@
|
||||
"tranche_start": "2021-10-05T00:00:00.000Z",
|
||||
"tranche_end": "2023-04-05T00:00:00.000Z",
|
||||
"total_added": "5778205.3912159303",
|
||||
"total_removed": "3063522.787392713967070295",
|
||||
"locked_amount": "374926.558344229352055062527996886",
|
||||
"total_removed": "3077573.36197477479025324",
|
||||
"locked_amount": "369597.397251054113600109778074383",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "552496.6455",
|
||||
@@ -51134,6 +51156,16 @@
|
||||
"user": "0xafa64cCa337eFEE0AD827F6C2684e69275226e90",
|
||||
"tx": "0xb1d21b6f9cce4bfaf8870592e4cbc7a40f1ca7c13cd7c8c8bb1defeb49bc31b8"
|
||||
},
|
||||
{
|
||||
"amount": "2341.4608038512383296",
|
||||
"user": "0x6ae83EAB68b7112BaD5AfD72d6B24546AbFF137D",
|
||||
"tx": "0xda4d9f820cb3c34b3c743ba99fef0c1963499bd0410ce0367f184627dc35c790"
|
||||
},
|
||||
{
|
||||
"amount": "11709.113778209584853345",
|
||||
"user": "0xC24da173A250e9Ca5c54870639EbE5f88be5102d",
|
||||
"tx": "0x99d518cf3c7dcee238caa3a3297a291cd0810913f9275a8c28b5544ef6379e7c"
|
||||
},
|
||||
{
|
||||
"amount": "990.74735267405816",
|
||||
"user": "0xBc934494675a6ceB639B9EfEe5b9C0f017D35a75",
|
||||
@@ -52230,6 +52262,12 @@
|
||||
}
|
||||
],
|
||||
"withdrawals": [
|
||||
{
|
||||
"amount": "2341.4608038512383296",
|
||||
"user": "0x6ae83EAB68b7112BaD5AfD72d6B24546AbFF137D",
|
||||
"tranche_id": 4,
|
||||
"tx": "0xda4d9f820cb3c34b3c743ba99fef0c1963499bd0410ce0367f184627dc35c790"
|
||||
},
|
||||
{
|
||||
"amount": "2174.40565875071598655",
|
||||
"user": "0x6ae83EAB68b7112BaD5AfD72d6B24546AbFF137D",
|
||||
@@ -52322,8 +52360,8 @@
|
||||
}
|
||||
],
|
||||
"total_tokens": "92082.572555",
|
||||
"withdrawn_tokens": "83806.4553378137829759",
|
||||
"remaining_tokens": "8276.1172171862170241"
|
||||
"withdrawn_tokens": "86147.9161416650213055",
|
||||
"remaining_tokens": "5934.6564133349786945"
|
||||
},
|
||||
{
|
||||
"address": "0x1dC9B91DE003fd503F25cB5d114cf0fc68F7aFe6",
|
||||
@@ -52351,6 +52389,12 @@
|
||||
}
|
||||
],
|
||||
"withdrawals": [
|
||||
{
|
||||
"amount": "11709.113778209584853345",
|
||||
"user": "0xC24da173A250e9Ca5c54870639EbE5f88be5102d",
|
||||
"tranche_id": 4,
|
||||
"tx": "0x99d518cf3c7dcee238caa3a3297a291cd0810913f9275a8c28b5544ef6379e7c"
|
||||
},
|
||||
{
|
||||
"amount": "10871.612867264090097103",
|
||||
"user": "0xC24da173A250e9Ca5c54870639EbE5f88be5102d",
|
||||
@@ -52467,8 +52511,8 @@
|
||||
}
|
||||
],
|
||||
"total_tokens": "460415.072915097",
|
||||
"withdrawn_tokens": "419033.352956190837612989",
|
||||
"remaining_tokens": "41381.719958906162387011"
|
||||
"withdrawn_tokens": "430742.466734400422466334",
|
||||
"remaining_tokens": "29672.606180696577533666"
|
||||
},
|
||||
{
|
||||
"address": "0xd66e4853c0880df150e7329974715BFC8d2da47D",
|
||||
@@ -53034,8 +53078,8 @@
|
||||
"tranche_start": "2022-06-05T00:00:00.000Z",
|
||||
"tranche_end": "2023-06-05T00:00:00.000Z",
|
||||
"total_added": "472355.6199999996",
|
||||
"total_removed": "34799.2735287010685",
|
||||
"locked_amount": "124873.77463304148219969237087774",
|
||||
"total_removed": "34992.4553336210685",
|
||||
"locked_amount": "124220.90056009124187109393607304",
|
||||
"deposits": [
|
||||
{
|
||||
"amount": "3000",
|
||||
@@ -59679,6 +59723,16 @@
|
||||
"user": "0xB3ae40b11af91BbD456022Bd42B4f36A8C0AEA9D",
|
||||
"tx": "0x7d1caeef005902492763cabe888b5dc76fd9f224520323ac30c1927ae91a1778"
|
||||
},
|
||||
{
|
||||
"amount": "45.996042618",
|
||||
"user": "0xfeF4b56A370135A17Eef5ba8f5c308451a64Cf86",
|
||||
"tx": "0xaad1a0343ca68a9af7c639ed61387a33a27418ee9df60aed2ad89a3a683873c2"
|
||||
},
|
||||
{
|
||||
"amount": "147.185762302",
|
||||
"user": "0x454D87e666a106F3545B7cde91F32aE8B9616a72",
|
||||
"tx": "0xcefe8932dc5a8ef8bbbb4f4ac1ec86c2de0e064cab91c381e222411c4661d34a"
|
||||
},
|
||||
{
|
||||
"amount": "57.900076104",
|
||||
"user": "0x96d882E908C06cD697Ea07266Fb8D14ae129A50b",
|
||||
@@ -66530,6 +66584,12 @@
|
||||
}
|
||||
],
|
||||
"withdrawals": [
|
||||
{
|
||||
"amount": "45.996042618",
|
||||
"user": "0xfeF4b56A370135A17Eef5ba8f5c308451a64Cf86",
|
||||
"tranche_id": 5,
|
||||
"tx": "0xaad1a0343ca68a9af7c639ed61387a33a27418ee9df60aed2ad89a3a683873c2"
|
||||
},
|
||||
{
|
||||
"amount": "51.030511162",
|
||||
"user": "0xfeF4b56A370135A17Eef5ba8f5c308451a64Cf86",
|
||||
@@ -66550,8 +66610,8 @@
|
||||
}
|
||||
],
|
||||
"total_tokens": "200",
|
||||
"withdrawn_tokens": "101.15768011",
|
||||
"remaining_tokens": "98.84231989"
|
||||
"withdrawn_tokens": "147.153722728",
|
||||
"remaining_tokens": "52.846277272"
|
||||
},
|
||||
{
|
||||
"address": "0x84390738e072ac47EbC85171663Ce76608553abC",
|
||||
@@ -75694,10 +75754,17 @@
|
||||
"tx": "0xb1425d9b0d5f10c5b06236d6f9d61ebddd21c4027e0f41deb5ec227a1753c715"
|
||||
}
|
||||
],
|
||||
"withdrawals": [],
|
||||
"withdrawals": [
|
||||
{
|
||||
"amount": "147.185762302",
|
||||
"user": "0x454D87e666a106F3545B7cde91F32aE8B9616a72",
|
||||
"tranche_id": 5,
|
||||
"tx": "0xcefe8932dc5a8ef8bbbb4f4ac1ec86c2de0e064cab91c381e222411c4661d34a"
|
||||
}
|
||||
],
|
||||
"total_tokens": "200",
|
||||
"withdrawn_tokens": "0",
|
||||
"remaining_tokens": "200"
|
||||
"withdrawn_tokens": "147.185762302",
|
||||
"remaining_tokens": "52.814237698"
|
||||
},
|
||||
{
|
||||
"address": "0xD611CCE7daE8359371b190AFa289B0610B53B928",
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { act } from 'react-dom/test-utils';
|
||||
import type { ClientOptions } from '@vegaprotocol/apollo-client';
|
||||
import { createClient } from '@vegaprotocol/apollo-client';
|
||||
import { Networks } from '../types';
|
||||
import { useEnvironment } from './use-environment';
|
||||
import { STORAGE_KEY, useEnvironment } from './use-environment';
|
||||
|
||||
const noop = () => {
|
||||
/* no op*/
|
||||
};
|
||||
|
||||
jest.mock('@vegaprotocol/apollo-client', () => ({
|
||||
createClient: () => ({
|
||||
jest.mock('@vegaprotocol/apollo-client');
|
||||
jest.mock('zustand');
|
||||
|
||||
const mockCreateClient = createClient as jest.Mock;
|
||||
const createDefaultMockClient = () => {
|
||||
return () => ({
|
||||
query: () =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
@@ -25,12 +31,10 @@ jest.mock('@vegaprotocol/apollo-client', () => ({
|
||||
obj.next();
|
||||
},
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
jest.mock('zustand');
|
||||
});
|
||||
};
|
||||
|
||||
global.fetch = jest.fn();
|
||||
|
||||
// eslint-disable-next-line
|
||||
const setupFetch = (result: any) => {
|
||||
return () => {
|
||||
@@ -56,6 +60,17 @@ const mockEnvVars = {
|
||||
|
||||
describe('useEnvironment', () => {
|
||||
const env = process.env;
|
||||
// eslint-disable-next-line
|
||||
let warn: any;
|
||||
|
||||
beforeAll(() => {
|
||||
warn = console.warn;
|
||||
console.warn = noop;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
@@ -73,6 +88,9 @@ describe('useEnvironment', () => {
|
||||
|
||||
// @ts-ignore clear mocked node config fetch
|
||||
fetch.mockClear();
|
||||
|
||||
// reset default apollo client behaviour
|
||||
mockCreateClient.mockImplementation(createDefaultMockClient());
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -92,6 +110,7 @@ describe('useEnvironment', () => {
|
||||
];
|
||||
// @ts-ignore: typscript doesn't recognise the mock implementation
|
||||
global.fetch.mockImplementation(setupFetch({ hosts: nodes }));
|
||||
|
||||
const { result } = setup();
|
||||
|
||||
expect(result.current.status).toBe('default');
|
||||
@@ -107,10 +126,169 @@ describe('useEnvironment', () => {
|
||||
});
|
||||
|
||||
// resulting VEGA_URL should be one of the nodes from the config
|
||||
expect(
|
||||
result.current.VEGA_URL === nodes[0] ||
|
||||
result.current.VEGA_URL === nodes[1]
|
||||
).toBe(true);
|
||||
expect(nodes.includes(result.current.VEGA_URL as string)).toBe(true);
|
||||
expect(result.current).toMatchObject({
|
||||
...mockEnvVars,
|
||||
nodes,
|
||||
});
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(fetch).toHaveBeenCalledWith(configUrl);
|
||||
});
|
||||
|
||||
it('uses the first successfully responding node', async () => {
|
||||
jest.useFakeTimers();
|
||||
const configUrl = 'https://vega.xyz/testnet-config.json';
|
||||
process.env['NX_VEGA_CONFIG_URL'] = configUrl;
|
||||
|
||||
const slowNode = 'https://api.n00.foo.vega.xyz';
|
||||
const slowWait = 2000;
|
||||
const fastNode = 'https://api.n01.foo.vega.xyz';
|
||||
const fastWait = 1000;
|
||||
const nodes = [slowNode, fastNode];
|
||||
// @ts-ignore: typscript doesn't recognise the mock implementation
|
||||
global.fetch.mockImplementation(setupFetch({ hosts: nodes }));
|
||||
|
||||
mockCreateClient.mockImplementation((obj: ClientOptions) => ({
|
||||
query: () =>
|
||||
new Promise((resolve) => {
|
||||
const wait = obj.url === fastNode ? fastWait : slowWait;
|
||||
setTimeout(() => {
|
||||
resolve({
|
||||
data: {
|
||||
statistics: {
|
||||
chainId: 'chain-id',
|
||||
blockHeight: '100',
|
||||
vegaTime: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
});
|
||||
}, wait);
|
||||
}),
|
||||
subscribe: () => ({
|
||||
// eslint-disable-next-line
|
||||
subscribe: (obj: any) => {
|
||||
obj.next();
|
||||
},
|
||||
}),
|
||||
}));
|
||||
const { result } = setup();
|
||||
|
||||
expect(result.current.status).toBe('default');
|
||||
|
||||
act(() => {
|
||||
result.current.initialize();
|
||||
});
|
||||
|
||||
expect(result.current.status).toBe('pending');
|
||||
|
||||
// wait for nodes request to finish before running timer
|
||||
await waitFor(() => {
|
||||
expect(result.current.nodes).toEqual(nodes);
|
||||
});
|
||||
|
||||
jest.runAllTimers();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.status).toEqual('success');
|
||||
});
|
||||
|
||||
expect(result.current.VEGA_URL).toBe(fastNode);
|
||||
expect(localStorage.getItem(STORAGE_KEY)).toBe(fastNode);
|
||||
expect(result.current).toMatchObject({
|
||||
...mockEnvVars,
|
||||
nodes,
|
||||
});
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(fetch).toHaveBeenCalledWith(configUrl);
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('passes a node if both queries and subscriptions working', async () => {
|
||||
const configUrl = 'https://vega.xyz/testnet-config.json';
|
||||
process.env['NX_VEGA_CONFIG_URL'] = configUrl;
|
||||
const successNode = 'https://api.n01.foo.vega.xyz';
|
||||
const failNode = 'https://api.n00.foo.vega.xyz';
|
||||
const nodes = [failNode, successNode];
|
||||
// @ts-ignore: typscript doesn't recognise the mock implementation
|
||||
global.fetch.mockImplementation(setupFetch({ hosts: nodes }));
|
||||
mockCreateClient.mockImplementation((clientOptions: ClientOptions) => ({
|
||||
query: () =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
statistics: {
|
||||
chainId: 'chain-id',
|
||||
blockHeight: '100',
|
||||
vegaTime: new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
}),
|
||||
subscribe: () => ({
|
||||
// eslint-disable-next-line
|
||||
subscribe: (obj: any) => {
|
||||
if (clientOptions.url === failNode) {
|
||||
// make n00 fail the subscription
|
||||
obj.error();
|
||||
} else {
|
||||
obj.next();
|
||||
}
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
const { result } = setup();
|
||||
|
||||
expect(result.current.status).toBe('default');
|
||||
|
||||
act(() => {
|
||||
result.current.initialize();
|
||||
});
|
||||
|
||||
expect(result.current.status).toBe('pending');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.status).toBe('success');
|
||||
});
|
||||
|
||||
expect(result.current.VEGA_URL).toBe(successNode);
|
||||
expect(localStorage.getItem(STORAGE_KEY)).toBe(successNode);
|
||||
});
|
||||
|
||||
it('fails initialization if no suitable node is found', async () => {
|
||||
const configUrl = 'https://vega.xyz/testnet-config.json';
|
||||
process.env['NX_VEGA_CONFIG_URL'] = configUrl;
|
||||
const nodes = [
|
||||
'https://api.n00.foo.vega.xyz',
|
||||
'https://api.n01.foo.vega.xyz',
|
||||
];
|
||||
// @ts-ignore: typscript doesn't recognise the mock implementation
|
||||
global.fetch.mockImplementation(setupFetch({ hosts: nodes }));
|
||||
|
||||
// set all clients to fail both query and subscription
|
||||
mockCreateClient.mockImplementation(() => ({
|
||||
query: () => Promise.reject(),
|
||||
subscribe: () => ({
|
||||
// eslint-disable-next-line
|
||||
subscribe: (obj: any) => {
|
||||
obj.error();
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
const { result } = setup();
|
||||
|
||||
expect(result.current.status).toBe('default');
|
||||
|
||||
act(() => {
|
||||
result.current.initialize();
|
||||
});
|
||||
|
||||
expect(result.current.status).toBe('pending');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.status).toBe('failed');
|
||||
});
|
||||
|
||||
expect(result.current.VEGA_URL).toBe(undefined); // VEGA_URL is unset, app should handle some UI
|
||||
expect(result.current).toMatchObject({
|
||||
...mockEnvVars,
|
||||
nodes,
|
||||
@@ -154,9 +332,12 @@ describe('useEnvironment', () => {
|
||||
process.env['NX_VEGA_URL'] = url;
|
||||
process.env['NX_VEGA_CONFIG_URL'] = undefined;
|
||||
const { result } = setup();
|
||||
await act(async () => {
|
||||
act(() => {
|
||||
result.current.initialize();
|
||||
});
|
||||
await waitFor(() => {
|
||||
expect(result.current.status).toBe('success');
|
||||
});
|
||||
expect(result.current).toMatchObject({
|
||||
VEGA_URL: url,
|
||||
VEGA_CONFIG_URL: undefined,
|
||||
@@ -174,16 +355,16 @@ describe('useEnvironment', () => {
|
||||
// @ts-ignore setup mock fetch for config url
|
||||
global.fetch.mockImplementation(setupFetch({ hosts: nodes }));
|
||||
const { result } = setup();
|
||||
await act(async () => {
|
||||
act(() => {
|
||||
result.current.initialize();
|
||||
});
|
||||
expect(typeof result.current.VEGA_URL).toEqual('string');
|
||||
expect(result.current.VEGA_URL).not.toBeFalsy();
|
||||
await waitFor(() => {
|
||||
expect(result.current.status).toBe('success');
|
||||
});
|
||||
expect(nodes.includes(result.current.VEGA_URL as string)).toBe(true);
|
||||
});
|
||||
|
||||
it('handles error if node config cannot be fetched', async () => {
|
||||
const warn = console.warn;
|
||||
console.warn = noop;
|
||||
const configUrl = 'https://vega.xyz/testnet-config.json';
|
||||
process.env['NX_VEGA_CONFIG_URL'] = configUrl;
|
||||
process.env['NX_VEGA_URL'] = undefined;
|
||||
@@ -196,16 +377,14 @@ describe('useEnvironment', () => {
|
||||
result.current.initialize();
|
||||
});
|
||||
expect(result.current.status).toEqual('failed');
|
||||
expect(typeof result.current.error).toBe('string');
|
||||
expect(result.current.error).toBeTruthy();
|
||||
expect(result.current.error).toBe(
|
||||
`Failed to fetch node config from ${configUrl}`
|
||||
);
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(fetch).toHaveBeenCalledWith(configUrl);
|
||||
console.warn = warn;
|
||||
});
|
||||
|
||||
it('handles an invalid node config', async () => {
|
||||
const warn = console.warn;
|
||||
console.warn = noop;
|
||||
const configUrl = 'https://vega.xyz/testnet-config.json';
|
||||
process.env['NX_VEGA_CONFIG_URL'] = configUrl;
|
||||
process.env['NX_VEGA_URL'] = undefined;
|
||||
@@ -216,11 +395,11 @@ describe('useEnvironment', () => {
|
||||
result.current.initialize();
|
||||
});
|
||||
expect(result.current.status).toEqual('failed');
|
||||
expect(typeof result.current.error).toBe('string');
|
||||
expect(result.current.error).toBeTruthy();
|
||||
expect(result.current.error).toBe(
|
||||
`Failed to fetch node config from ${configUrl}`
|
||||
);
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(fetch).toHaveBeenCalledWith(configUrl);
|
||||
console.warn = warn;
|
||||
});
|
||||
|
||||
it('uses stored url', async () => {
|
||||
@@ -231,7 +410,7 @@ describe('useEnvironment', () => {
|
||||
setupFetch({ hosts: ['http://foo.bar.com'] })
|
||||
);
|
||||
const url = 'https://api.n00.foo.com';
|
||||
localStorage.setItem('vega_url', url);
|
||||
localStorage.setItem(STORAGE_KEY, url);
|
||||
const { result } = setup();
|
||||
await act(async () => {
|
||||
result.current.initialize();
|
||||
@@ -254,6 +433,6 @@ describe('useEnvironment', () => {
|
||||
result.current.setUrl(newUrl);
|
||||
});
|
||||
expect(result.current.VEGA_URL).toBe(newUrl);
|
||||
expect(localStorage.getItem('vega_url')).toBe(newUrl);
|
||||
expect(localStorage.getItem(STORAGE_KEY)).toBe(newUrl);
|
||||
});
|
||||
});
|
||||
@@ -31,7 +31,7 @@ type Actions = {
|
||||
export type Env = Environment & EnvState;
|
||||
export type EnvStore = Env & Actions;
|
||||
|
||||
const STORAGE_KEY = 'vega_url';
|
||||
export const STORAGE_KEY = 'vega_url';
|
||||
const SUBSCRIPTION_TIMEOUT = 3000;
|
||||
|
||||
export const useEnvironment = create<EnvStore>((set, get) => ({
|
||||
@@ -168,9 +168,15 @@ const fetchConfig = async (url?: string) => {
|
||||
* Find a suitable node by running a test query and test
|
||||
* subscription, against a list of clients, first to resolve wins
|
||||
*/
|
||||
const findNode = (clients: ClientCollection): Promise<string | null> => {
|
||||
const findNode = async (clients: ClientCollection): Promise<string | null> => {
|
||||
const tests = Object.entries(clients).map((args) => testNode(...args));
|
||||
return Promise.race(tests);
|
||||
try {
|
||||
const url = await Promise.any(tests);
|
||||
return url;
|
||||
} catch {
|
||||
// All tests rejected, no suitable node found
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -180,19 +186,21 @@ const testNode = async (
|
||||
url: string,
|
||||
client: Client
|
||||
): Promise<string | null> => {
|
||||
try {
|
||||
const results = await Promise.all([
|
||||
testQuery(client),
|
||||
testSubscription(client),
|
||||
]);
|
||||
if (results[0] && results[1]) {
|
||||
return url;
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
console.warn(`Tests failed for ${url}`);
|
||||
return null;
|
||||
const results = await Promise.all([
|
||||
// these promises will only resolve with true/false
|
||||
testQuery(client),
|
||||
testSubscription(client),
|
||||
]);
|
||||
if (results[0] && results[1]) {
|
||||
return url;
|
||||
}
|
||||
|
||||
const message = `Tests failed for node: ${url}`;
|
||||
console.warn(message);
|
||||
|
||||
// throwing here will mean this tests is ignored and a different
|
||||
// node that hopefully does resolve will fulfill the Promise.any
|
||||
throw new Error(message);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
"importHelpers": true,
|
||||
"target": "es2015",
|
||||
"module": "esnext",
|
||||
"lib": ["es2017", "dom"],
|
||||
"lib": ["es2021", "dom"],
|
||||
"skipLibCheck": true,
|
||||
"skipDefaultLibCheck": true,
|
||||
"baseUrl": ".",
|
||||
|
||||
Reference in new issue
Block a user