forked from mason-hz/aelf-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
23 lines (20 loc) · 847 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { AElfDappBridge } from '@aelf-react/types';
let endpoint = '';
// options & connect is aelf-bridge only
export function isAElfBridge(aelfBridge: AElfDappBridge) {
return !!(aelfBridge.options && aelfBridge.connect);
}
export function isCurrentAElfBridge(aelfBridge: AElfDappBridge) {
return endpoint === aelfBridge.options?.endpoint;
}
export async function reConnectAElfBridge(aelfBridge: AElfDappBridge) {
const isConnected = await aelfBridge.connect?.();
if (!isConnected) throw Error('Reconnect Failed');
endpoint = aelfBridge.options?.endpoint || '';
}
// aelf-bridge only supports one node and needs to check whether it is connected
export async function checkAElfBridge(aelfBridge: AElfDappBridge) {
if (isAElfBridge(aelfBridge) && !isCurrentAElfBridge(aelfBridge)) {
await reConnectAElfBridge(aelfBridge);
}
}