2022-04-20 19:37:44 +00:00
|
|
|
import * as Sentry from '@sentry/react';
|
2022-06-07 22:08:40 +00:00
|
|
|
import { toBigNum } from '@vegaprotocol/react-helpers';
|
2022-04-20 19:37:44 +00:00
|
|
|
import { Splash } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { useVegaWallet, useEagerConnect } from '@vegaprotocol/wallet';
|
2022-09-18 11:15:05 +00:00
|
|
|
import { useEnvironment } from '@vegaprotocol/environment';
|
2022-04-20 19:37:44 +00:00
|
|
|
import { useWeb3React } from '@web3-react/core';
|
|
|
|
import React from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
|
|
import { SplashError } from './components/splash-error';
|
|
|
|
import { SplashLoader } from './components/splash-loader';
|
|
|
|
import { Flags } from './config';
|
|
|
|
import {
|
|
|
|
AppStateActionType,
|
|
|
|
useAppState,
|
|
|
|
} from './contexts/app-state/app-state-context';
|
|
|
|
import { useContracts } from './contexts/contracts/contracts-context';
|
|
|
|
import { useRefreshAssociatedBalances } from './hooks/use-refresh-associated-balances';
|
|
|
|
import { Connectors } from './lib/vega-connectors';
|
|
|
|
|
|
|
|
export const AppLoader = ({ children }: { children: React.ReactElement }) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const { account } = useWeb3React();
|
2022-09-18 11:15:05 +00:00
|
|
|
const { VEGA_URL } = useEnvironment();
|
2022-10-03 18:12:34 +00:00
|
|
|
const { pubKey } = useVegaWallet();
|
2022-04-20 19:37:44 +00:00
|
|
|
const { appDispatch } = useAppState();
|
|
|
|
const { token, staking, vesting } = useContracts();
|
|
|
|
const setAssociatedBalances = useRefreshAssociatedBalances();
|
|
|
|
const [balancesLoaded, setBalancesLoaded] = React.useState(false);
|
|
|
|
const vegaConnecting = useEagerConnect(Connectors);
|
|
|
|
|
|
|
|
const loaded = balancesLoaded && !vegaConnecting;
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const run = async () => {
|
|
|
|
try {
|
|
|
|
const [
|
|
|
|
supply,
|
|
|
|
totalAssociatedWallet,
|
|
|
|
totalAssociatedVesting,
|
|
|
|
decimals,
|
|
|
|
] = await Promise.all([
|
|
|
|
token.totalSupply(),
|
2022-07-25 08:48:19 +00:00
|
|
|
staking.total_staked(),
|
|
|
|
vesting.total_staked(),
|
2022-04-20 19:37:44 +00:00
|
|
|
token.decimals(),
|
|
|
|
]);
|
2022-06-07 22:08:40 +00:00
|
|
|
|
|
|
|
const totalSupply = toBigNum(supply, decimals);
|
|
|
|
const totalWallet = toBigNum(totalAssociatedWallet, decimals);
|
|
|
|
const totalVesting = toBigNum(totalAssociatedVesting, decimals);
|
|
|
|
|
2022-04-20 19:37:44 +00:00
|
|
|
appDispatch({
|
|
|
|
type: AppStateActionType.SET_TOKEN,
|
|
|
|
decimals,
|
2022-06-07 22:08:40 +00:00
|
|
|
totalSupply,
|
|
|
|
totalAssociated: totalWallet.plus(totalVesting),
|
2022-04-20 19:37:44 +00:00
|
|
|
});
|
|
|
|
setBalancesLoaded(true);
|
|
|
|
} catch (err) {
|
|
|
|
Sentry.captureException(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Flags.NETWORK_DOWN) {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
}, [token, appDispatch, staking, vesting]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-10-03 18:12:34 +00:00
|
|
|
if (account && pubKey) {
|
|
|
|
setAssociatedBalances(account, pubKey);
|
2022-04-20 19:37:44 +00:00
|
|
|
}
|
2022-10-03 18:12:34 +00:00
|
|
|
}, [setAssociatedBalances, account, pubKey]);
|
2022-04-20 19:37:44 +00:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-09-18 11:15:05 +00:00
|
|
|
const networkLimitsEndpoint = new URL('/network/limits', VEGA_URL).href;
|
|
|
|
const statsEndpoint = new URL('/statistics', VEGA_URL).href;
|
2022-04-20 19:37:44 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
let interval: any = null;
|
|
|
|
|
|
|
|
const getNetworkLimits = async () => {
|
|
|
|
try {
|
|
|
|
const [networkLimits, stats] = await Promise.all([
|
|
|
|
fetch(networkLimitsEndpoint).then((res) => res.json()),
|
|
|
|
fetch(statsEndpoint).then((res) => res.json()),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const restoreBlock = Number(
|
|
|
|
networkLimits.networkLimits.bootstrapBlockCount
|
|
|
|
);
|
|
|
|
const currentBlock = Number(stats.statistics.blockHeight);
|
|
|
|
|
|
|
|
if (currentBlock <= restoreBlock) {
|
|
|
|
appDispatch({
|
|
|
|
type: AppStateActionType.SET_BANNER_MESSAGE,
|
|
|
|
message: t('networkRestoring', {
|
|
|
|
bootstrapBlockCount: restoreBlock,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!interval) {
|
|
|
|
startPoll();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
appDispatch({
|
|
|
|
type: AppStateActionType.SET_BANNER_MESSAGE,
|
|
|
|
message: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (interval) {
|
|
|
|
stopPoll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Sentry.captureException(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopPoll = () => {
|
|
|
|
clearInterval(interval);
|
|
|
|
interval = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const startPoll = () => {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
getNetworkLimits();
|
|
|
|
}, 10000);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only begin polling if network limits flag is set, as this is a new API not yet on mainnet 7/3/22
|
|
|
|
if (Flags.NETWORK_LIMITS) {
|
|
|
|
getNetworkLimits();
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
stopPoll();
|
|
|
|
};
|
2022-09-18 11:15:05 +00:00
|
|
|
}, [appDispatch, VEGA_URL, t]);
|
2022-04-20 19:37:44 +00:00
|
|
|
|
|
|
|
if (Flags.NETWORK_DOWN) {
|
|
|
|
return (
|
|
|
|
<Splash>
|
|
|
|
<SplashError />
|
|
|
|
</Splash>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loaded) {
|
|
|
|
return (
|
|
|
|
<Splash>
|
|
|
|
<SplashLoader />
|
|
|
|
</Splash>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
};
|