-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathApiContext.tsx
More file actions
113 lines (104 loc) · 3.96 KB
/
ApiContext.tsx
File metadata and controls
113 lines (104 loc) · 3.96 KB
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
// Copyright 2022-2024 use-ink/contracts-ui authors & contributors
// SPDX-License-Identifier: GPL-3.0-only
import { createContext, useEffect, useContext, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { web3Accounts, web3Enable, web3EnablePromise } from '@polkadot/extension-dapp';
import { WsProvider } from '@polkadot/api';
import { keyring } from '@polkadot/ui-keyring';
import { LOCAL_STORAGE_KEY, POP_NETWORK_TESTNET } from '../../constants';
import { ApiPromise, ApiState, ChainProperties, Account, Status, WeightV2 } from 'types';
import { isValidWsUrl, isKeyringLoaded } from 'lib/util';
import { useLocalStorage } from 'ui/hooks/useLocalStorage';
import { NoticeBanner } from 'ui/components/common/NoticeBanner';
import { getChainProperties } from 'src/services/chain/chainProps';
// fixes internal pjs type mismatch `Type 'string' is not assignable to type '`0x${string}`'`
export interface InjectedAccountWithMetaOverride {
address: string;
meta: {
genesisHash?: `0x{string}` | null;
name?: string;
source: string;
};
}
export const ApiContext = createContext<ApiState | undefined>(undefined);
export const ApiContextProvider = ({ children }: React.PropsWithChildren<Partial<ApiState>>) => {
const [searchParams] = useSearchParams();
const rpcUrl = searchParams.get('rpc');
const [preferredEndpoint, setPreferredEndpoint] = useLocalStorage<string>(
LOCAL_STORAGE_KEY.PREFERRED_ENDPOINT,
POP_NETWORK_TESTNET.rpc,
);
const [api, setApi] = useState({} as ApiPromise);
const [endpoint, setEndpoint] = useState(preferredEndpoint);
const [accounts, setAccounts] = useState<Account[]>();
const [chainProps, setChainProps] = useState<ChainProperties>();
const [status, setStatus] = useState<Status>('loading');
const [isSupported, setIsSupported] = useState(true);
const [isEthereumChain, setIsEthereumChain] = useState(false);
useEffect(() => {
if (rpcUrl && isValidWsUrl(rpcUrl) && rpcUrl !== preferredEndpoint) {
setEndpoint(rpcUrl);
setPreferredEndpoint(rpcUrl);
window.location.reload();
}
}, [preferredEndpoint, rpcUrl, searchParams, setPreferredEndpoint]);
useEffect((): void => {
setStatus('loading');
const wsProvider = new WsProvider(endpoint);
const _api = new ApiPromise({ provider: wsProvider });
_api.on('connected', async () => {
await _api.isReady;
const _chainProps = await getChainProperties(_api);
const w2 = _api.registry.createType<WeightV2>('Weight').proofSize;
const isEth = _api.runtimeVersion.specName.toString() === 'frontier-template';
setApi(_api);
setChainProps(_chainProps);
setIsSupported(!!w2);
setStatus('connected');
setIsEthereumChain(isEth);
});
_api.on('disconnected', () => {
setStatus('error');
});
}, [endpoint]);
useEffect(() => {
const getAccounts = async () => {
if (status === 'connected' && chainProps) {
!web3EnablePromise && (await web3Enable('contracts-ui'));
const accounts = await web3Accounts();
isKeyringLoaded() ||
keyring.loadAll(
{
isDevelopment: chainProps.systemChainType.isDevelopment,
type: isEthereumChain ? 'ethereum' : 'ed25519',
},
accounts as InjectedAccountWithMetaOverride[],
);
setAccounts(keyring.getAccounts());
}
};
getAccounts().catch(e => console.error(e));
}, [chainProps, isEthereumChain, status]);
return (
<ApiContext.Provider
value={{
api,
accounts,
setEndpoint,
endpoint,
status,
...(chainProps as ChainProperties),
}}
>
<NoticeBanner endpoint={endpoint} isVisible={!isSupported} />
{children}
</ApiContext.Provider>
);
};
export const useApi = () => {
const context = useContext(ApiContext);
if (context === undefined) {
throw new Error('useApi must be used within an ApiContextProvider');
}
return context;
};