-
Notifications
You must be signed in to change notification settings - Fork 16
/
Root.tsx
144 lines (138 loc) · 5.75 KB
/
Root.tsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
MAINNET,
TESTNET,
WalletConnectionProps,
WithWalletConnector,
useConnect,
useConnection,
useGrpcClient,
} from '@concordium/react-components';
import { BlockHash } from '@concordium/web-sdk';
import { useEffect, useState } from 'react';
import { Alert, Button, Col, Container, Row, Spinner } from 'react-bootstrap';
import { App } from './App';
import { ConnectedAccount } from './ConnectedAccount';
import { NetworkSelector } from './NetworkSelector';
import { WalletConnectorButton } from './WalletConnectorButton';
import { BROWSER_WALLET, WALLET_CONNECT } from './config';
import { errorString } from './util';
export default function Root() {
const [network, setNetwork] = useState(TESTNET);
return (
<Container>
<h1>Sample dApp</h1>
<NetworkSelector selected={network} options={[TESTNET, MAINNET]} select={setNetwork} />
<WithWalletConnector network={network}>{(props) => <Main {...props} />}</WithWalletConnector>
</Container>
);
}
function Main(props: WalletConnectionProps) {
const { activeConnectorType, activeConnector, activeConnectorError, network, connectedAccounts, genesisHashes } =
props;
const { connection, setConnection, account, genesisHash } = useConnection(connectedAccounts, genesisHashes);
const { connect, isConnecting, connectError } = useConnect(activeConnector, setConnection);
const [rpcGenesisHash, setRpcGenesisHash] = useState<string>();
const [rpcError, setRpcError] = useState('');
const rpc = useGrpcClient(network);
useEffect(() => {
if (rpc) {
setRpcGenesisHash(undefined);
rpc.getConsensusStatus()
.then((status) => status.genesisBlock)
.then((hash) => {
setRpcGenesisHash(BlockHash.toHexString(hash));
setRpcError('');
})
.catch((err) => {
setRpcGenesisHash(undefined);
setRpcError(errorString(err));
});
}
}, [rpc]);
return (
<>
<Row className="mt-3 mb-3">
<Col>
<WalletConnectorButton
connectorType={BROWSER_WALLET}
connectorName="Browser Wallet"
connection={connection}
{...props}
/>
</Col>
<Col>
<WalletConnectorButton
connectorType={WALLET_CONNECT}
connectorName="WalletConnect"
connection={connection}
{...props}
/>
</Col>
</Row>
<Row className="mt-3 mb-3">
<Col>
{activeConnectorError && <Alert variant="danger">Connector error: {activeConnectorError}.</Alert>}
{!activeConnectorError && activeConnectorType && !activeConnector && <Spinner />}
{connectError && <Alert variant="danger">Connection error: {connectError}.</Alert>}
{connect && !account && (
<Button type="button" onClick={connect} disabled={isConnecting}>
{isConnecting && 'Connecting...'}
{!isConnecting && activeConnectorType === BROWSER_WALLET && 'Connect Browser Wallet'}
{!isConnecting && activeConnectorType === WALLET_CONNECT && 'Connect Mobile Wallet'}
</Button>
)}
</Col>
</Row>
<Row className="mt-3 mb-3">
<Col>
<ConnectedAccount network={network} rpc={rpc} account={account} />
</Col>
</Row>
<Row className="mt-3 mb-3">
<Col>
{account && (
<NetworkInconsistencyReporter
rpcGenesisHash={rpcGenesisHash}
networkGenesisHash={network.genesisHash}
activeConnectionGenesisHash={genesisHash}
/>
)}
{rpcError && <Alert variant="warning">RPC error: {rpcError}</Alert>}
<App network={network} rpc={rpc} connection={connection} connectedAccount={account} />
</Col>
</Row>
</>
);
}
interface NetworkInconsistencyReporterProps {
rpcGenesisHash: string | undefined;
activeConnectionGenesisHash: string | undefined;
networkGenesisHash: string;
}
function NetworkInconsistencyReporter({
rpcGenesisHash,
networkGenesisHash,
activeConnectionGenesisHash,
}: NetworkInconsistencyReporterProps) {
const rpcMismatch = rpcGenesisHash && rpcGenesisHash !== networkGenesisHash;
const activeConnectionMismatch = activeConnectionGenesisHash && activeConnectionGenesisHash !== networkGenesisHash;
return (
<>
{(rpcMismatch || activeConnectionMismatch) && (
<Alert variant="danger">
Inconsistent network parameters detected!
<ul>
<li>
Reported by wallet:{' '}
{(activeConnectionGenesisHash && <code>{activeConnectionGenesisHash}</code>) || <i>N/A</i>}.
</li>
<li>Fetched from via RPC: {(rpcGenesisHash && <code>{rpcGenesisHash}</code>) || <i>N/A</i>}</li>
<li>
Expected for selected network: <code>{networkGenesisHash}</code>
</li>
</ul>
</Alert>
)}
</>
);
}