2022-06-21 23:20:53 +00:00
|
|
|
import type { ReactNode } from 'react';
|
2022-12-07 09:24:40 +00:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { NetworkLoader, useEnvironment } from '@vegaprotocol/environment';
|
2022-11-10 13:08:12 +00:00
|
|
|
import type { InMemoryCacheConfig } from '@apollo/client';
|
2022-12-07 09:24:40 +00:00
|
|
|
import {
|
|
|
|
useEthereumConfig,
|
|
|
|
createConnectors,
|
|
|
|
Web3Provider as Web3ProviderInternal,
|
|
|
|
useWeb3ConnectStore,
|
|
|
|
} from '@vegaprotocol/web3';
|
2022-12-08 08:51:01 +00:00
|
|
|
import { AsyncRenderer, Splash } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { t } from '@vegaprotocol/react-helpers';
|
2022-03-11 00:28:48 +00:00
|
|
|
|
|
|
|
interface AppLoaderProps {
|
|
|
|
children: ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-23 12:21:54 +00:00
|
|
|
* Component to handle any app initialization, startup queries and other things
|
2022-03-11 00:28:48 +00:00
|
|
|
* that must happen for it can be used
|
|
|
|
*/
|
|
|
|
export function AppLoader({ children }: AppLoaderProps) {
|
2022-12-08 08:51:01 +00:00
|
|
|
return (
|
|
|
|
<NetworkLoader
|
|
|
|
skeleton={<Splash>{t('Loading...')}</Splash>}
|
|
|
|
cache={cacheConfig}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</NetworkLoader>
|
|
|
|
);
|
2022-12-07 09:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Web3Provider = ({ children }: { children: ReactNode }) => {
|
|
|
|
const { config, loading, error } = useEthereumConfig();
|
|
|
|
const { ETHEREUM_PROVIDER_URL } = useEnvironment();
|
|
|
|
const [connectors, initializeConnectors] = useWeb3ConnectStore((store) => [
|
|
|
|
store.connectors,
|
|
|
|
store.initialize,
|
|
|
|
]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (config?.chain_id) {
|
|
|
|
return initializeConnectors(
|
|
|
|
createConnectors(ETHEREUM_PROVIDER_URL, Number(config?.chain_id)),
|
|
|
|
Number(config.chain_id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [config?.chain_id, ETHEREUM_PROVIDER_URL, initializeConnectors]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AsyncRenderer
|
|
|
|
loading={loading}
|
|
|
|
error={error}
|
|
|
|
data={connectors}
|
|
|
|
noDataCondition={(d) => {
|
|
|
|
if (!d) return true;
|
|
|
|
return d.length < 1;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Web3ProviderInternal connectors={connectors}>
|
|
|
|
<>{children}</>
|
|
|
|
</Web3ProviderInternal>
|
|
|
|
</AsyncRenderer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const cacheConfig: InMemoryCacheConfig = {
|
|
|
|
typePolicies: {
|
|
|
|
Account: {
|
|
|
|
keyFields: false,
|
|
|
|
fields: {
|
|
|
|
balanceFormatted: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Instrument: {
|
|
|
|
keyFields: false,
|
|
|
|
},
|
|
|
|
TradableInstrument: {
|
|
|
|
keyFields: ['instrument'],
|
|
|
|
},
|
|
|
|
Product: {
|
|
|
|
keyFields: ['settlementAsset', ['id']],
|
|
|
|
},
|
|
|
|
MarketData: {
|
|
|
|
keyFields: ['market', ['id']],
|
|
|
|
},
|
|
|
|
Node: {
|
|
|
|
keyFields: false,
|
|
|
|
},
|
|
|
|
Withdrawal: {
|
|
|
|
fields: {
|
|
|
|
pendingOnForeignChain: {
|
|
|
|
read: (isPending = false) => isPending,
|
2022-11-10 13:08:12 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-07 09:24:40 +00:00
|
|
|
},
|
|
|
|
ERC20: {
|
|
|
|
keyFields: ['contractAddress'],
|
|
|
|
},
|
|
|
|
PositionUpdate: {
|
|
|
|
keyFields: false,
|
|
|
|
},
|
|
|
|
AccountUpdate: {
|
|
|
|
keyFields: false,
|
|
|
|
},
|
|
|
|
Party: {
|
|
|
|
keyFields: false,
|
|
|
|
},
|
|
|
|
Fees: {
|
|
|
|
keyFields: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|