@dodoex/wallet-web3-react 0.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/babel.config.js +9 -0
  2. package/dist/index.js +72 -0
  3. package/dist/types/ClientProvider.d.ts +2 -0
  4. package/dist/types/LangProvider.d.ts +11 -0
  5. package/dist/types/WalletConnect/AccountPage.d.ts +11 -0
  6. package/dist/types/WalletConnect/ActivityList.d.ts +20 -0
  7. package/dist/types/WalletConnect/ConnectAlchemy/index.d.ts +6 -0
  8. package/dist/types/WalletConnect/ConnectAlchemy/useConnectAlchemy.d.ts +12 -0
  9. package/dist/types/WalletConnect/ConnectLedger/ErrorDialog.d.ts +4 -0
  10. package/dist/types/WalletConnect/ConnectLedger/LoadingDialog.d.ts +3 -0
  11. package/dist/types/WalletConnect/ConnectLedger/LockedDialog.d.ts +4 -0
  12. package/dist/types/WalletConnect/ConnectLedger/ProtocolDialog.d.ts +6 -0
  13. package/dist/types/WalletConnect/ConnectLedger/SelectAddressDialog.d.ts +8 -0
  14. package/dist/types/WalletConnect/ConnectLedger/SelectPathDialog.d.ts +5 -0
  15. package/dist/types/WalletConnect/ConnectLedger/helper.d.ts +2 -0
  16. package/dist/types/WalletConnect/ConnectLedger/index.d.ts +6 -0
  17. package/dist/types/WalletConnect/ConnectPage.d.ts +9 -0
  18. package/dist/types/WalletConnect/HasBalanceTokenList.d.ts +4 -0
  19. package/dist/types/WalletConnect/ReceiveTokenPage.d.ts +4 -0
  20. package/dist/types/WalletConnect/SendTokenPage.d.ts +5 -0
  21. package/dist/types/WalletConnect/WalletDialog.d.ts +6 -0
  22. package/dist/types/WalletConnectProvider.d.ts +72 -0
  23. package/dist/types/components/AddressWithLinkAndCopy.d.ts +28 -0
  24. package/dist/types/components/Dialog.d.ts +15 -0
  25. package/dist/types/components/TokenLogo.d.ts +26 -0
  26. package/dist/types/components/WalletTag.d.ts +7 -0
  27. package/dist/types/constants/localstorage.d.ts +6 -0
  28. package/dist/types/hooks/useConnectWalet.d.ts +30 -0
  29. package/dist/types/hooks/useFetchFiatPrice.d.ts +3 -0
  30. package/dist/types/hooks/useFetchTokensBalance.d.ts +14 -0
  31. package/dist/types/hooks/useHasBalanceTokenList.d.ts +21 -0
  32. package/dist/types/hooks/useTransactionList.d.ts +272 -0
  33. package/dist/types/index.d.ts +4 -0
  34. package/dist/types/utils/formatter.d.ts +20 -0
  35. package/dist/types/utils/time.d.ts +3 -0
  36. package/dist/types/utils/utils.d.ts +2 -0
  37. package/lingui.config.ts +13 -0
  38. package/locales/en.po +251 -0
  39. package/locales/en.ts +1 -0
  40. package/locales/zh.po +249 -0
  41. package/locales/zh.ts +1 -0
  42. package/package.json +68 -0
  43. package/rollup.config.mjs +64 -0
  44. package/src/ClientProvider.tsx +15 -0
  45. package/src/LangProvider.tsx +32 -0
  46. package/src/WalletConnect/AccountPage.tsx +535 -0
  47. package/src/WalletConnect/ActivityList.tsx +597 -0
  48. package/src/WalletConnect/ConnectAlchemy/index.tsx +246 -0
  49. package/src/WalletConnect/ConnectAlchemy/useConnectAlchemy.ts +101 -0
  50. package/src/WalletConnect/ConnectLedger/ErrorDialog.tsx +61 -0
  51. package/src/WalletConnect/ConnectLedger/LoadingDialog.tsx +106 -0
  52. package/src/WalletConnect/ConnectLedger/LockedDialog.tsx +54 -0
  53. package/src/WalletConnect/ConnectLedger/ProtocolDialog.tsx +61 -0
  54. package/src/WalletConnect/ConnectLedger/SelectAddressDialog.tsx +326 -0
  55. package/src/WalletConnect/ConnectLedger/SelectPathDialog.tsx +68 -0
  56. package/src/WalletConnect/ConnectLedger/helper.ts +14 -0
  57. package/src/WalletConnect/ConnectLedger/index.tsx +89 -0
  58. package/src/WalletConnect/ConnectPage.tsx +459 -0
  59. package/src/WalletConnect/HasBalanceTokenList.tsx +201 -0
  60. package/src/WalletConnect/ReceiveTokenPage.tsx +143 -0
  61. package/src/WalletConnect/SendTokenPage.tsx +249 -0
  62. package/src/WalletConnect/WalletDialog.tsx +72 -0
  63. package/src/WalletConnectProvider.tsx +54 -0
  64. package/src/components/AddressWithLinkAndCopy.tsx +200 -0
  65. package/src/components/Dialog.tsx +155 -0
  66. package/src/components/TokenLogo.tsx +165 -0
  67. package/src/components/WalletTag.tsx +113 -0
  68. package/src/constants/localstorage.ts +22 -0
  69. package/src/hooks/useConnectWalet.ts +144 -0
  70. package/src/hooks/useFetchFiatPrice.ts +51 -0
  71. package/src/hooks/useFetchTokensBalance.ts +52 -0
  72. package/src/hooks/useHasBalanceTokenList.ts +157 -0
  73. package/src/hooks/useTransactionList.ts +87 -0
  74. package/src/index.tsx +4 -0
  75. package/src/utils/formatter.ts +102 -0
  76. package/src/utils/time.ts +21 -0
  77. package/src/utils/utils.ts +8 -0
  78. package/tsconfig.json +22 -0
@@ -0,0 +1,535 @@
1
+ import {
2
+ LoadingSkeleton,
3
+ Tabs,
4
+ alpha,
5
+ Box,
6
+ BoxProps,
7
+ ThemeProvider,
8
+ useTheme,
9
+ createTheme,
10
+ Tooltip,
11
+ RotatingIcon,
12
+ TabsGroup,
13
+ TabPanel,
14
+ ButtonBase,
15
+ } from '@dodoex/components';
16
+ import {
17
+ ArrowBack,
18
+ ArrowTopRightBorder,
19
+ Copy,
20
+ Disconnect,
21
+ } from '@dodoex/icons';
22
+ import copy from 'copy-to-clipboard';
23
+ import WalletWeb3, { useWalletStore } from '@dodoex/wallet-web3';
24
+ import { Trans } from '@lingui/macro';
25
+ import { BigNumber } from 'bignumber.js';
26
+ import React from 'react';
27
+ import { truncatePoolAddress } from '../components/AddressWithLinkAndCopy';
28
+ import { DialogTitle } from '../components/Dialog';
29
+ import { useHasBalanceTokenList } from '../hooks/useHasBalanceTokenList';
30
+ import { useTransactionList } from '../hooks/useTransactionList';
31
+ import { formatReadableNumber, formatShortNumber } from '../utils/formatter';
32
+ import { useWalletConnectContext } from '../WalletConnectProvider';
33
+ import ActivityList from './ActivityList';
34
+ import HasBalanceTokenList from './HasBalanceTokenList';
35
+
36
+ function CardIcon({ sx, ...other }: BoxProps) {
37
+ return (
38
+ <Box
39
+ sx={{
40
+ display: 'flex',
41
+ justifyContent: 'center',
42
+ alignItems: 'center',
43
+ width: 52,
44
+ height: 52,
45
+ borderRadius: '50%',
46
+ backgroundColor: alpha('#1a1a1b', 0.1),
47
+ cursor: 'pointer',
48
+ '&:hover': {
49
+ backgroundColor: '#FFE804',
50
+ },
51
+ ...sx,
52
+ }}
53
+ {...other}
54
+ />
55
+ );
56
+ }
57
+
58
+ function AccountCardInfoBg({
59
+ children,
60
+ chainLogo,
61
+ }: React.PropsWithChildren<{
62
+ chainLogo: React.ReactNode;
63
+ }>) {
64
+ return (
65
+ <Box
66
+ sx={{
67
+ position: 'relative',
68
+ padding: 20,
69
+ borderRadius: 12,
70
+ border: 'solid 1px',
71
+ borderColor: 'border.main',
72
+ color: 'text.primary',
73
+ zIndex: 0,
74
+ }}
75
+ >
76
+ <Box
77
+ sx={{
78
+ position: 'absolute',
79
+ top: 0,
80
+ bottom: 0,
81
+ left: 0,
82
+ right: 0,
83
+ zIndex: -1,
84
+ overflow: 'hidden',
85
+ borderRadius: 12,
86
+ }}
87
+ >
88
+ <Box
89
+ sx={{
90
+ position: 'absolute',
91
+ top: 0,
92
+ bottom: 0,
93
+ left: 0,
94
+ right: 0,
95
+ backgroundColor: '#F6F6F6',
96
+ }}
97
+ />
98
+ <Box
99
+ sx={{
100
+ position: 'absolute',
101
+ top: 0,
102
+ bottom: 0,
103
+ left: 0,
104
+ right: 0,
105
+ '& > img, & > svg': {
106
+ height: 'auto',
107
+ width: 'calc(100% + 2px)',
108
+ position: 'absolute',
109
+ '&:first-child': {
110
+ top: '50%',
111
+ transform: 'translateY(-50%)',
112
+ opacity: 0.5,
113
+ filter: 'blur(99.99999237060547px)',
114
+ },
115
+ '&:last-child': {
116
+ left: 60,
117
+ bottom: 40,
118
+ opacity: 0.2,
119
+ filter: 'blur(7.499999523162842px)',
120
+ },
121
+ },
122
+ }}
123
+ >
124
+ {chainLogo}
125
+ {chainLogo}
126
+ </Box>
127
+ <Box
128
+ sx={{
129
+ position: 'absolute',
130
+ top: 0,
131
+ bottom: 0,
132
+ left: 0,
133
+ right: 0,
134
+ background:
135
+ 'linear-gradient(108deg, rgba(255, 255, 255, 0.90) 9.8%, rgba(255, 255, 255, 0.00) 49.94%)',
136
+ }}
137
+ />
138
+ </Box>
139
+ {children}
140
+ </Box>
141
+ );
142
+ }
143
+
144
+ function AccountCardInfo({
145
+ chainId,
146
+ setShowSendTokenPage,
147
+ setShowReceiveTokenPage,
148
+ allFiatPriceBalance,
149
+ allFiatPriceBalanceLoading,
150
+ }: {
151
+ chainId: number;
152
+ setShowSendTokenPage: React.Dispatch<React.SetStateAction<boolean>>;
153
+ setShowReceiveTokenPage: React.Dispatch<React.SetStateAction<boolean>>;
154
+ allFiatPriceBalance: BigNumber;
155
+ allFiatPriceBalanceLoading: boolean;
156
+ }) {
157
+ const { getChain } = useWalletConnectContext();
158
+ const chain = getChain(chainId);
159
+ const theme = createTheme({
160
+ mode: 'light',
161
+ });
162
+
163
+ return (
164
+ <ThemeProvider theme={theme}>
165
+ <AccountCardInfoBg chainLogo={chain?.logo}>
166
+ <Box
167
+ sx={{
168
+ typography: 'body2',
169
+ color: 'text.secondary',
170
+ }}
171
+ >
172
+ <Trans>Balance</Trans>
173
+ </Box>
174
+ <LoadingSkeleton
175
+ component="div"
176
+ loading={allFiatPriceBalanceLoading}
177
+ loadingSx={{
178
+ width: 120,
179
+ }}
180
+ sx={{
181
+ mt: 4,
182
+ typography: 'h1',
183
+ }}
184
+ title={formatReadableNumber({ input: allFiatPriceBalance })}
185
+ >
186
+ ${formatShortNumber(allFiatPriceBalance)}
187
+ </LoadingSkeleton>
188
+ <Box
189
+ sx={{
190
+ display: 'flex',
191
+ justifyContent: 'space-between',
192
+ alignItems: 'center',
193
+ gap: 20,
194
+ mt: 28,
195
+ }}
196
+ >
197
+ {/* send */}
198
+ <ButtonBase
199
+ sx={{
200
+ flex: 1,
201
+ display: 'flex',
202
+ flexDirection: 'column',
203
+ gap: 12,
204
+ px: 20,
205
+ py: 12,
206
+ typography: 'body2',
207
+ backgroundColor: 'background.paperDarkContrast',
208
+ borderRadius: 12,
209
+ '&:hover': {
210
+ backgroundColor: 'primary.main',
211
+ color: 'primary.contrastText',
212
+ },
213
+ }}
214
+ onClick={() => {
215
+ setShowSendTokenPage(true);
216
+ }}
217
+ >
218
+ <Box
219
+ component={ArrowBack}
220
+ sx={{
221
+ transform: 'rotate(90deg)',
222
+ }}
223
+ />
224
+ <Trans>Send</Trans>
225
+ </ButtonBase>
226
+ {/* receive */}
227
+ <ButtonBase
228
+ sx={{
229
+ flex: 1,
230
+ display: 'flex',
231
+ flexDirection: 'column',
232
+ gap: 12,
233
+ px: 20,
234
+ py: 12,
235
+ typography: 'body2',
236
+ backgroundColor: 'background.paperDarkContrast',
237
+ borderRadius: 12,
238
+ '&:hover': {
239
+ backgroundColor: 'primary.main',
240
+ color: 'primary.contrastText',
241
+ },
242
+ }}
243
+ onClick={() => {
244
+ setShowReceiveTokenPage(true);
245
+ }}
246
+ >
247
+ <Box
248
+ component={ArrowBack}
249
+ sx={{
250
+ transform: 'rotate(-90deg)',
251
+ }}
252
+ />
253
+ <Trans>Receive</Trans>
254
+ </ButtonBase>
255
+ </Box>
256
+ </AccountCardInfoBg>
257
+ </ThemeProvider>
258
+ );
259
+ }
260
+
261
+ enum ListTab {
262
+ tokens = 'tokens',
263
+ activity = 'activity',
264
+ }
265
+ function AccountPage({
266
+ chainId,
267
+ account,
268
+ walletWeb3,
269
+ onClose,
270
+ setShowSendTokenPage,
271
+ setShowReceiveTokenPage,
272
+ }: {
273
+ chainId: number;
274
+ account?: string;
275
+ walletWeb3?: WalletWeb3;
276
+ onClose: () => void;
277
+ setShowSendTokenPage: React.Dispatch<React.SetStateAction<boolean>>;
278
+ setShowReceiveTokenPage: React.Dispatch<React.SetStateAction<boolean>>;
279
+ }) {
280
+ const { getChain } = useWalletConnectContext();
281
+ const { connected } = useWalletStore();
282
+ const connectedWallet = connected?.wallet;
283
+ const [listTab, setListTab] = React.useState(ListTab.tokens);
284
+ const scrollableNodeRef = React.useRef<HTMLDivElement>(null);
285
+ const getScrollParent = React.useCallback(() => {
286
+ if (scrollableNodeRef) return scrollableNodeRef.current;
287
+ return null;
288
+ }, [scrollableNodeRef]);
289
+
290
+ const balanceListData = useHasBalanceTokenList({
291
+ account,
292
+ chainId,
293
+ visible: listTab === ListTab.tokens,
294
+ });
295
+
296
+ const fetchTransactionQuery = useTransactionList({
297
+ account,
298
+ chainId,
299
+ });
300
+ const tabs = [
301
+ { key: ListTab.tokens, value: <Trans>Token</Trans> },
302
+ {
303
+ key: ListTab.activity,
304
+ value: (
305
+ <Tooltip title={<Trans>On-chain transaction records</Trans>}>
306
+ <Box
307
+ sx={{
308
+ display: 'flex',
309
+ alignItems: 'center',
310
+ gap: 8,
311
+ }}
312
+ >
313
+ <Trans>Activity</Trans>
314
+ {!!fetchTransactionQuery.pendingList.length && <RotatingIcon />}
315
+ </Box>
316
+ </Tooltip>
317
+ ),
318
+ },
319
+ ];
320
+
321
+ const [showCopyTooltip, setShowCopyTooltip] = React.useState(false);
322
+ const autoCloseShowCopyTooltip = React.useCallback(() => {
323
+ const time = setTimeout(() => {
324
+ setShowCopyTooltip(false);
325
+ }, 1500);
326
+
327
+ return () => {
328
+ clearTimeout(time);
329
+ };
330
+ }, []);
331
+
332
+ const chain = getChain(chainId);
333
+ if (!account || !connectedWallet) return null;
334
+ return (
335
+ <>
336
+ <DialogTitle onClose={onClose}>
337
+ <Box
338
+ sx={{
339
+ display: 'flex',
340
+ gap: 8,
341
+ alignItems: 'center',
342
+ height: 34,
343
+ borderRadius: 8,
344
+ backgroundColor: 'background.paperContrast',
345
+ }}
346
+ >
347
+ <Box
348
+ sx={{
349
+ display: 'flex',
350
+ alignItems: 'center',
351
+ gap: 4,
352
+ pl: 8,
353
+ }}
354
+ >
355
+ <Box
356
+ sx={{
357
+ position: 'relative',
358
+ pr: 6,
359
+ width: 28,
360
+ height: 22,
361
+ }}
362
+ >
363
+ {!!connectedWallet.logo && (
364
+ <img
365
+ src={connectedWallet.logo}
366
+ alt={connectedWallet.showName}
367
+ width={22}
368
+ height={22}
369
+ />
370
+ )}
371
+ {!!chain?.logo && (
372
+ <Box
373
+ sx={{
374
+ position: 'absolute',
375
+ bottom: 0,
376
+ right: 0,
377
+ width: 12,
378
+ height: 12,
379
+ '& > img, & > svg': {
380
+ position: 'absolute',
381
+ inset: 0,
382
+ width: '100%',
383
+ height: '100%',
384
+ borderRadius: '50%',
385
+ border: 'solid 1px',
386
+ borderColor: 'border.main',
387
+ },
388
+ }}
389
+ >
390
+ {chain?.logo}
391
+ </Box>
392
+ )}
393
+ </Box>
394
+ {truncatePoolAddress(account)}
395
+ </Box>
396
+ <Tooltip
397
+ onlyClick
398
+ arrow={false}
399
+ title={<Trans>Copied</Trans>}
400
+ open={showCopyTooltip}
401
+ onOpen={() => {
402
+ setShowCopyTooltip(true);
403
+ autoCloseShowCopyTooltip();
404
+ }}
405
+ onClose={() => setShowCopyTooltip(false)}
406
+ >
407
+ <Box
408
+ component={Copy}
409
+ onClick={() => {
410
+ copy(account);
411
+ }}
412
+ sx={{
413
+ width: 18,
414
+ height: 18,
415
+ cursor: 'pointer',
416
+ color: 'text.secondary',
417
+ '&:hover': {
418
+ color: 'text.primary',
419
+ },
420
+ }}
421
+ />
422
+ </Tooltip>
423
+ <Box
424
+ component="a"
425
+ rel="noopener noreferrer"
426
+ target="_blank"
427
+ href={chain?.scanUrl + '/address/' + account}
428
+ sx={{
429
+ display: 'flex',
430
+ lineHeight: 1,
431
+ color: 'text.secondary',
432
+ '&:hover': {
433
+ color: 'text.primary',
434
+ },
435
+ }}
436
+ >
437
+ <Box
438
+ component={ArrowTopRightBorder}
439
+ sx={{
440
+ width: 18,
441
+ height: 18,
442
+ }}
443
+ />
444
+ </Box>
445
+ <ButtonBase
446
+ sx={{
447
+ display: 'flex',
448
+ alignItems: 'center',
449
+ justifyContent: 'center',
450
+ height: '100%',
451
+ px: 8,
452
+ border: 'solid 0',
453
+ borderLeftWidth: 1,
454
+ borderColor: 'border.main',
455
+ color: 'error.main',
456
+ '&:hover': {
457
+ backgroundColor: 'background.tag',
458
+ },
459
+ }}
460
+ onClick={() => {
461
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
462
+ // const provider = (useWalletStore.getState().provider as any)
463
+ // ?.provider;
464
+ // if (provider._handleDisconnect) {
465
+ // provider._handleDisconnect();
466
+ // }
467
+ walletWeb3?.disconnectWallet();
468
+ onClose();
469
+ }}
470
+ >
471
+ <Box
472
+ component={Disconnect}
473
+ sx={{
474
+ width: 18,
475
+ height: 18,
476
+ }}
477
+ />
478
+ </ButtonBase>
479
+ </Box>
480
+ </DialogTitle>
481
+ <Box
482
+ sx={{
483
+ display: 'flex',
484
+
485
+ flexDirection: 'column',
486
+ overflowY: 'auto',
487
+ overflowX: 'hidden',
488
+ px: 20,
489
+ }}
490
+ ref={scrollableNodeRef}
491
+ >
492
+ <AccountCardInfo
493
+ chainId={chainId}
494
+ setShowSendTokenPage={setShowSendTokenPage}
495
+ setShowReceiveTokenPage={setShowReceiveTokenPage}
496
+ allFiatPriceBalanceLoading={
497
+ balanceListData.allFiatPriceBalanceLoading
498
+ }
499
+ allFiatPriceBalance={balanceListData.allFiatPriceBalance}
500
+ />
501
+ <Tabs
502
+ value={listTab}
503
+ onChange={(_, v) => setListTab(v as ListTab)}
504
+ placeholder=""
505
+ onPointerEnterCapture={() => {}}
506
+ onPointerLeaveCapture={() => {}}
507
+ >
508
+ <TabsGroup tabs={tabs} />
509
+ <TabPanel
510
+ value={ListTab.tokens}
511
+ placeholder=""
512
+ onPointerEnterCapture={() => {}}
513
+ onPointerLeaveCapture={() => {}}
514
+ >
515
+ <HasBalanceTokenList balanceListData={balanceListData} />
516
+ </TabPanel>
517
+ <TabPanel
518
+ value={ListTab.activity}
519
+ className="pt-2"
520
+ placeholder=""
521
+ onPointerEnterCapture={() => {}}
522
+ onPointerLeaveCapture={() => {}}
523
+ >
524
+ <ActivityList
525
+ fetchTransactionQuery={fetchTransactionQuery}
526
+ getScrollParent={getScrollParent}
527
+ />
528
+ </TabPanel>
529
+ </Tabs>
530
+ </Box>
531
+ </>
532
+ );
533
+ }
534
+
535
+ export default AccountPage;