-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.tsx
235 lines (208 loc) · 6.22 KB
/
App.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* @DEV: If the sandbox is throwing dependency errors, chances are you need to clear your browser history.
* This will trigger a re-install of the dependencies in the sandbox – which should fix things right up.
* Alternatively, you can fork this sandbox to refresh the dependencies manually.
*/
import React, { useState, useEffect, useCallback, useMemo, useRef } from 'react';
import styled from 'styled-components';
import {
configureChains,
createConfig,
WagmiConfig,
useAccount,
useSignMessage,
useSendTransaction,
usePrepareSendTransaction,
useDisconnect,
} from 'wagmi';
import { publicProvider } from 'wagmi/providers/public';
import { goerli } from 'wagmi/chains';
import { TLog } from './types';
import { Logs, Sidebar } from './components';
import { parseGwei } from 'viem';
import { PhantomConnector } from 'phantom-wagmi-connector';
// =============================================================================
// wagmi configuration
// =============================================================================
const { publicClient, webSocketPublicClient, chains } = configureChains([goerli], [publicProvider()]);
const wagmiConfig = createConfig({
publicClient,
webSocketPublicClient,
connectors: [new PhantomConnector({ chains })],
});
// =============================================================================
// Styled Components
// =============================================================================
const StyledApp = styled.div`
display: flex;
flex-direction: row;
height: 100vh;
@media (max-width: 768px) {
flex-direction: column;
}
`;
// =============================================================================
// Constants
// =============================================================================
const MESSAGE = 'To avoid digital dognappers, sign below to authenticate with CryptoCorgis.';
// =============================================================================
// Typedefs
// =============================================================================
export type ConnectedMethods =
| {
name: string;
onClick: () => void;
}
| {
name: string;
onClick: () => Promise<void>;
};
interface Props {
// address: string | null;
// connectedMethods: ConnectedMethods[];
logs: TLog[];
clearLogs: () => void;
createLog: (log: TLog) => void;
logsVisibility: boolean;
toggleLogs: () => void;
}
// =============================================================================
// Hooks
// =============================================================================
/**
* @DEVELOPERS
* The fun stuff!
*/
const useProps = (): Props => {
const [logs, setLogs] = useState<TLog[]>([]);
const [logsVisibility, setLogsVisibility] = useState(false);
const createLog = useCallback(
(log: TLog) => {
return setLogs((logs) => [...logs, log]);
},
[setLogs]
);
const clearLogs = useCallback(() => {
setLogs([]);
}, [setLogs]);
const toggleLogs = () => {
setLogsVisibility(!logsVisibility);
};
return {
createLog,
logs,
clearLogs,
logsVisibility,
toggleLogs,
};
};
// =============================================================================
// Stateless Component
// =============================================================================
const Stateless = React.memo((props: Props) => {
const { createLog, logs, clearLogs, logsVisibility, toggleLogs } = props;
const { address, status } = useAccount();
let prevStatus = useRef(status);
useEffect(() => {
switch (status) {
case 'disconnected':
if (status === prevStatus.current) break;
createLog({
status: 'warning',
method: 'disconnect',
message: 'user disconnected wallet',
});
break;
case 'connected':
createLog({
status: 'success',
method: 'connect',
message: `Connected to app with account ${address}}`,
});
break;
case 'connecting':
createLog({
status: 'info',
method: 'connect',
message: 'user connecting...',
});
break;
}
prevStatus.current = status;
}, [createLog, address, status]);
const { signMessage } = useSignMessage({
message: MESSAGE,
onSettled(data, error) {
if (error) {
createLog({
status: 'error',
method: 'signMessage',
message: error.message,
});
return;
}
createLog({
status: 'success',
method: 'signMessage',
message: `Message signed: ${data}`,
});
},
});
const { config } = usePrepareSendTransaction({
to: '0x0000000000000000000000000000000000000000', // Common for burning ETH
value: parseGwei('1', 'wei'),
});
const { sendTransaction } = useSendTransaction({
...config,
onSettled(data, error) {
if (error) {
createLog({
status: 'error',
method: 'eth_sendTransaction',
message: `Error occurred: ${error.message}`,
});
return;
}
createLog({
status: 'success',
method: 'eth_sendTransaction',
message: `Transaction success: ${data.hash}`,
});
},
});
const { disconnect } = useDisconnect();
const connectedMethods = useMemo(() => {
return [
{
name: 'Sign Message',
onClick: () => signMessage(),
},
{
name: 'Send Transaction (burn 1 wei on Goerli)',
onClick: () => sendTransaction?.(),
},
{
name: 'Disconnect',
onClick: () => disconnect(),
},
];
}, [signMessage, sendTransaction, disconnect]);
return (
<StyledApp>
<Sidebar connectedMethods={connectedMethods} logsVisibility={logsVisibility} toggleLogs={toggleLogs} />
{logsVisibility && <Logs address={address} logs={logs} clearLogs={clearLogs} />}
</StyledApp>
);
});
// =============================================================================
// Main Component
// =============================================================================
const App = () => {
const props = useProps();
return (
<WagmiConfig config={wagmiConfig}>
<Stateless {...props} />
</WagmiConfig>
);
};
export default App;