@dodoex/wallet-web3-react 0.2.0 → 0.3.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 (40) hide show
  1. package/babel.config.js +9 -9
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.js +1 -1
  4. package/lingui.config.ts +13 -13
  5. package/package.json +91 -76
  6. package/rollup.config.mjs +100 -100
  7. package/src/ClientProvider.tsx +17 -15
  8. package/src/LangProvider.tsx +36 -34
  9. package/src/WalletConnect/AccountPage.tsx +496 -494
  10. package/src/WalletConnect/ActivityList.tsx +606 -604
  11. package/src/WalletConnect/ConnectAlchemy/index.tsx +248 -246
  12. package/src/WalletConnect/ConnectAlchemy/useConnectAlchemy.ts +105 -105
  13. package/src/WalletConnect/ConnectDialog.tsx +35 -33
  14. package/src/WalletConnect/ConnectLedger/ErrorDialog.tsx +61 -61
  15. package/src/WalletConnect/ConnectLedger/LockedDialog.tsx +54 -54
  16. package/src/WalletConnect/ConnectLedger/helper.ts +14 -14
  17. package/src/WalletConnect/ConnectLedger/index.tsx +2 -0
  18. package/src/WalletConnect/ConnectPage.tsx +508 -506
  19. package/src/WalletConnect/HasBalanceTokenList.tsx +202 -200
  20. package/src/WalletConnect/ReceiveTokenPage.tsx +145 -143
  21. package/src/WalletConnect/SendTokenPage.tsx +251 -249
  22. package/src/WalletConnect/WalletDialog.tsx +80 -78
  23. package/src/WalletConnectProvider.tsx +57 -55
  24. package/src/components/AddressWithLinkAndCopy.tsx +202 -200
  25. package/src/components/Dialog.tsx +158 -156
  26. package/src/components/TokenLogo.tsx +167 -165
  27. package/src/components/WalletTag.tsx +117 -115
  28. package/src/constants/localstorage.ts +24 -22
  29. package/src/hooks/useConnectWallet.ts +150 -146
  30. package/src/hooks/useFetchFiatPrice.ts +53 -51
  31. package/src/hooks/useFetchTokensBalance.ts +53 -51
  32. package/src/hooks/useHasBalanceTokenList.ts +95 -93
  33. package/src/hooks/useTransactionList.ts +89 -87
  34. package/src/index.tsx +7 -7
  35. package/src/locales/en.po +51 -51
  36. package/src/locales/zh.po +51 -51
  37. package/src/utils/formatter.ts +102 -102
  38. package/src/utils/time.ts +21 -21
  39. package/src/utils/utils.ts +8 -8
  40. package/tsconfig.json +23 -23
@@ -1,115 +1,117 @@
1
- import { alpha, Box, RotatingIcon, useTheme } from '@dodoex/components';
2
- import { WalletType } from '@dodoex/wallet-web3';
3
- import { Trans } from '@lingui/macro';
4
- import { WalletItem } from '../hooks/useConnectWallet';
5
-
6
- export interface WalletTagProps {
7
- loading: boolean;
8
- wallet: WalletItem;
9
- isChecked: boolean;
10
- installed: boolean;
11
- };
12
-
13
- export default function WalletTag({
14
- loading,
15
- wallet,
16
- isChecked,
17
- installed,
18
- }: WalletTagProps) {
19
- const theme = useTheme();
20
- const greenColor = theme.palette.success.main;
21
- const connectedColor = theme.palette.mode === 'light' ? '#EB8D27' : '#FFE804';
22
- const connectedColorBg = alpha('#FFE804', 0.1);
23
- if (loading) {
24
- return (
25
- <Box
26
- sx={{
27
- display: 'flex',
28
- alignItems: 'center',
29
- gap: 4,
30
- p: theme.spacing(4, 8),
31
- borderRadius: 6,
32
- typography: 'body2',
33
- backgroundColor: alpha(theme.palette.text.primary, 0.1),
34
- color: 'text.secondary',
35
- }}
36
- >
37
- <RotatingIcon
38
- sx={{
39
- width: 18,
40
- height: 18,
41
- color: 'text.secondary',
42
- }}
43
- />
44
- <Trans>Connecting</Trans>··
45
- </Box>
46
- );
47
- }
48
-
49
- if (isChecked) {
50
- return (
51
- <Box
52
- sx={{
53
- p: theme.spacing(4, 8),
54
- borderRadius: 6,
55
- typography: 'body2',
56
- backgroundColor: connectedColorBg,
57
- color: connectedColor,
58
- }}
59
- >
60
- <Trans>Connected</Trans>
61
- </Box>
62
- );
63
- }
64
-
65
- if (wallet.isLastConnection) {
66
- return (
67
- <Box
68
- sx={{
69
- p: theme.spacing(4, 8),
70
- borderRadius: 6,
71
- typography: 'body2',
72
- backgroundColor: alpha(theme.palette.text.primary, 0.1),
73
- color: 'text.secondary',
74
- }}
75
- >
76
- <Trans>Last connection</Trans>
77
- </Box>
78
- );
79
- }
80
-
81
- if (wallet.currentType === WalletType.SocialLogin) {
82
- return (
83
- <Box
84
- sx={{
85
- mt: 4,
86
- padding: theme.spacing(4, 8),
87
- borderRadius: 6,
88
- typography: 'body2',
89
- backgroundColor: alpha(greenColor, 0.1),
90
- color: greenColor,
91
- }}
92
- >
93
- New
94
- </Box>
95
- );
96
- }
97
-
98
- if (installed) {
99
- return (
100
- <Box
101
- sx={{
102
- p: theme.spacing(4, 8),
103
- borderRadius: 6,
104
- typography: 'body2',
105
- backgroundColor: alpha(theme.palette.text.primary, 0.1),
106
- color: 'text.secondary',
107
- }}
108
- >
109
- <Trans>Installed Wallet</Trans>
110
- </Box>
111
- );
112
- }
113
-
114
- return null;
115
- }
1
+ 'use client';
2
+
3
+ import { alpha, Box, RotatingIcon, useTheme } from '@dodoex/components';
4
+ import { WalletType } from '@dodoex/wallet-web3';
5
+ import { Trans } from '@lingui/macro';
6
+ import { WalletItem } from '../hooks/useConnectWallet';
7
+
8
+ export interface WalletTagProps {
9
+ loading: boolean;
10
+ wallet: WalletItem;
11
+ isChecked: boolean;
12
+ installed: boolean;
13
+ };
14
+
15
+ export default function WalletTag({
16
+ loading,
17
+ wallet,
18
+ isChecked,
19
+ installed,
20
+ }: WalletTagProps) {
21
+ const theme = useTheme();
22
+ const greenColor = theme.palette.success.main;
23
+ const connectedColor = theme.palette.mode === 'light' ? '#EB8D27' : '#FFE804';
24
+ const connectedColorBg = alpha('#FFE804', 0.1);
25
+ if (loading) {
26
+ return (
27
+ <Box
28
+ sx={{
29
+ display: 'flex',
30
+ alignItems: 'center',
31
+ gap: 4,
32
+ p: theme.spacing(4, 8),
33
+ borderRadius: 6,
34
+ typography: 'body2',
35
+ backgroundColor: alpha(theme.palette.text.primary, 0.1),
36
+ color: 'text.secondary',
37
+ }}
38
+ >
39
+ <RotatingIcon
40
+ sx={{
41
+ width: 18,
42
+ height: 18,
43
+ color: 'text.secondary',
44
+ }}
45
+ />
46
+ <Trans>Connecting</Trans>··
47
+ </Box>
48
+ );
49
+ }
50
+
51
+ if (isChecked) {
52
+ return (
53
+ <Box
54
+ sx={{
55
+ p: theme.spacing(4, 8),
56
+ borderRadius: 6,
57
+ typography: 'body2',
58
+ backgroundColor: connectedColorBg,
59
+ color: connectedColor,
60
+ }}
61
+ >
62
+ <Trans>Connected</Trans>
63
+ </Box>
64
+ );
65
+ }
66
+
67
+ if (wallet.isLastConnection) {
68
+ return (
69
+ <Box
70
+ sx={{
71
+ p: theme.spacing(4, 8),
72
+ borderRadius: 6,
73
+ typography: 'body2',
74
+ backgroundColor: alpha(theme.palette.text.primary, 0.1),
75
+ color: 'text.secondary',
76
+ }}
77
+ >
78
+ <Trans>Last connection</Trans>
79
+ </Box>
80
+ );
81
+ }
82
+
83
+ if (wallet.currentType === WalletType.SocialLogin) {
84
+ return (
85
+ <Box
86
+ sx={{
87
+ mt: 4,
88
+ padding: theme.spacing(4, 8),
89
+ borderRadius: 6,
90
+ typography: 'body2',
91
+ backgroundColor: alpha(greenColor, 0.1),
92
+ color: greenColor,
93
+ }}
94
+ >
95
+ New
96
+ </Box>
97
+ );
98
+ }
99
+
100
+ if (installed) {
101
+ return (
102
+ <Box
103
+ sx={{
104
+ p: theme.spacing(4, 8),
105
+ borderRadius: 6,
106
+ typography: 'body2',
107
+ backgroundColor: alpha(theme.palette.text.primary, 0.1),
108
+ color: 'text.secondary',
109
+ }}
110
+ >
111
+ <Trans>Installed Wallet</Trans>
112
+ </Box>
113
+ );
114
+ }
115
+
116
+ return null;
117
+ }
@@ -1,22 +1,24 @@
1
- const TOKEN_FIAT_PRICE_LIST = 'DODO_WIDGET_TOKEN_FIAT_PRICE_LIST';
2
- interface FiatPriceCacheList {
3
- [k: string]: number;
4
- }
5
- export function setTokenFiatPriceList(value: FiatPriceCacheList) {
6
- const oldList = getTokenFiatPriceList();
7
- const newList = {
8
- ...oldList,
9
- ...value,
10
- };
11
- localStorage.setItem(TOKEN_FIAT_PRICE_LIST, JSON.stringify(newList));
12
- }
13
- export function getTokenFiatPriceList(): FiatPriceCacheList {
14
- const storage = localStorage.getItem(TOKEN_FIAT_PRICE_LIST);
15
- try {
16
- if (!storage) return {};
17
- return JSON.parse(storage);
18
- } catch (e) {
19
- console.error(e);
20
- return {};
21
- }
22
- }
1
+ 'use client';
2
+
3
+ const TOKEN_FIAT_PRICE_LIST = 'DODO_WIDGET_TOKEN_FIAT_PRICE_LIST';
4
+ interface FiatPriceCacheList {
5
+ [k: string]: number;
6
+ }
7
+ export function setTokenFiatPriceList(value: FiatPriceCacheList) {
8
+ const oldList = getTokenFiatPriceList();
9
+ const newList = {
10
+ ...oldList,
11
+ ...value,
12
+ };
13
+ localStorage.setItem(TOKEN_FIAT_PRICE_LIST, JSON.stringify(newList));
14
+ }
15
+ export function getTokenFiatPriceList(): FiatPriceCacheList {
16
+ const storage = localStorage.getItem(TOKEN_FIAT_PRICE_LIST);
17
+ try {
18
+ if (!storage) return {};
19
+ return JSON.parse(storage);
20
+ } catch (e) {
21
+ console.error(e);
22
+ return {};
23
+ }
24
+ }
@@ -1,146 +1,150 @@
1
- import WalletWeb3, { useWalletStore, Wallet } from '@dodoex/wallet-web3';
2
- import React from 'react';
3
-
4
- const SETTINGS_ACCOUNT_RISK_CONFIRM = '0.settings:account:risk-confirm';
5
-
6
- export interface WalletItem {
7
- title: string;
8
- icon: string;
9
- currentType: string;
10
- onClick: (
11
- providerPackageOptions?: Partial<WalletWeb3['providerConfig']>,
12
- ) => void;
13
- disabled?: boolean;
14
- isLastConnection: boolean;
15
- isInstalled?: boolean;
16
- source: Wallet;
17
- }
18
-
19
- export const useConnectWallet = ({
20
- walletWeb3,
21
- matchTestChain,
22
- showOtherInjectWallet,
23
- selectedChainId,
24
- hasTermsOfServiceLink,
25
- }: {
26
- walletWeb3?: WalletWeb3;
27
- /** Match the test chain and display the first bit of showName and chainIds as the matching test chain. If it is false or not transmitted, the main network information will still be displayed. */
28
- matchTestChain?: boolean;
29
- /**
30
- * No need to display when switching wallets
31
- */
32
- showOtherInjectWallet?: boolean;
33
- selectedChainId?: number;
34
- hasTermsOfServiceLink?: boolean;
35
- }) => {
36
- const activeWalletType = useWalletStore((state) => state.walletType);
37
-
38
- const { walletList } = useWalletListByNetwork(
39
- selectedChainId,
40
- walletWeb3,
41
- showOtherInjectWallet,
42
- );
43
- const [userReadAndChecked, setUserReadAndChecked] = React.useState<boolean>(
44
- hasTermsOfServiceLink
45
- ? localStorage.getItem(SETTINGS_ACCOUNT_RISK_CONFIRM) === '1' || true
46
- : true,
47
- );
48
-
49
- const handleChangeUserReadAndChecked = (val: boolean) => {
50
- localStorage.setItem(SETTINGS_ACCOUNT_RISK_CONFIRM, val ? '1' : '0');
51
- setUserReadAndChecked(val);
52
- };
53
-
54
- return {
55
- walletList,
56
- userReadAndChecked,
57
- handleChangeUserReadAndChecked,
58
- activeWalletType,
59
- };
60
- };
61
-
62
- export const connectToWallet = async (
63
- walletWeb3: WalletWeb3 | undefined,
64
- wallet: Wallet,
65
- config?: Partial<WalletWeb3['providerConfig']>,
66
- ) => {
67
- if (!walletWeb3 || !walletWeb3.connectWalletBefore(wallet)) return;
68
- const providerConfig = {
69
- ...config,
70
- };
71
-
72
- return walletWeb3.connectToWallet(wallet, providerConfig);
73
- };
74
-
75
- export function useWalletListByNetwork(
76
- chainId?: number,
77
- walletWeb3?: WalletWeb3,
78
- showOtherInjectWallet?: boolean,
79
- closeSingleWalletConnectModal?: () => void,
80
- ) {
81
- const [walletList, setWalletList] = React.useState<WalletItem[]>([]);
82
- const disabledWalletKeyList = useWalletStore(
83
- (state) => state.disabledWalletKeyList,
84
- );
85
- const eip6963WalletList = useWalletStore((state) => state.eip6963WalletList);
86
-
87
- React.useEffect(() => {
88
- const computed = async () => {
89
- if (walletWeb3 && chainId !== undefined) {
90
- const walletListOrigin = walletWeb3.getWalletList(chainId, {
91
- eip6963WalletList,
92
- });
93
- const list = [] as WalletItem[];
94
- let len = walletListOrigin.length;
95
- for (let i = 0; i < len; i++) {
96
- const item = walletListOrigin[i];
97
- let isInstalled = false;
98
- if (item.injected) {
99
- isInstalled = item.injected();
100
- }
101
- if (!isInstalled && item.isInstalled) {
102
- isInstalled = await item.isInstalled?.();
103
- }
104
- list.push({
105
- title: item.showName,
106
- icon: item.logo,
107
- currentType: item.type,
108
- source: item,
109
- onClick: (providerPackageOptions) => {
110
- try {
111
- return connectToWallet(
112
- walletWeb3,
113
- item,
114
- providerPackageOptions,
115
- );
116
- } catch (error) {
117
- if (closeSingleWalletConnectModal) {
118
- closeSingleWalletConnectModal();
119
- }
120
- throw error;
121
- }
122
- },
123
- disabled: disabledWalletKeyList.includes(
124
- `${item.type}-${item.showName}`,
125
- ),
126
- isLastConnection:
127
- item.type === walletWeb3.connectController.getLastConnectType(),
128
- isInstalled,
129
- });
130
- }
131
- setWalletList(list);
132
- }
133
- };
134
- computed();
135
- }, [
136
- walletWeb3,
137
- chainId,
138
- showOtherInjectWallet,
139
- disabledWalletKeyList,
140
- eip6963WalletList,
141
- ]);
142
-
143
- return {
144
- walletList,
145
- };
146
- }
1
+ 'use client';
2
+
3
+ import WalletWeb3, { useWalletStore, Wallet } from '@dodoex/wallet-web3';
4
+ import React from 'react';
5
+
6
+ const SETTINGS_ACCOUNT_RISK_CONFIRM = '0.settings:account:risk-confirm';
7
+
8
+ export interface WalletItem {
9
+ title: string;
10
+ icon: string;
11
+ currentType: string;
12
+ onClick: (
13
+ providerPackageOptions?: Partial<WalletWeb3['providerConfig']>,
14
+ ) => void;
15
+ disabled?: boolean;
16
+ isLastConnection: boolean;
17
+ isInstalled?: boolean;
18
+ source: Wallet;
19
+ }
20
+
21
+ export const useConnectWallet = ({
22
+ walletWeb3,
23
+ matchTestChain,
24
+ showOtherInjectWallet,
25
+ selectedChainId,
26
+ hasTermsOfServiceLink,
27
+ }: {
28
+ walletWeb3?: WalletWeb3;
29
+ /** Match the test chain and display the first bit of showName and chainIds as the matching test chain. If it is false or not transmitted, the main network information will still be displayed. */
30
+ matchTestChain?: boolean;
31
+ /**
32
+ * No need to display when switching wallets
33
+ */
34
+ showOtherInjectWallet?: boolean;
35
+ selectedChainId?: number;
36
+ hasTermsOfServiceLink?: boolean;
37
+ }) => {
38
+ const activeWalletType = useWalletStore((state) => state.walletType);
39
+
40
+ const { walletList } = useWalletListByNetwork(
41
+ selectedChainId,
42
+ walletWeb3,
43
+ showOtherInjectWallet,
44
+ );
45
+ const [userReadAndChecked, setUserReadAndChecked] = React.useState<boolean>(
46
+ hasTermsOfServiceLink
47
+ ? localStorage.getItem(SETTINGS_ACCOUNT_RISK_CONFIRM) === '1' || true
48
+ : true,
49
+ );
50
+
51
+ const handleChangeUserReadAndChecked = (val: boolean) => {
52
+ localStorage.setItem(SETTINGS_ACCOUNT_RISK_CONFIRM, val ? '1' : '0');
53
+ setUserReadAndChecked(val);
54
+ };
55
+
56
+ return {
57
+ walletList,
58
+ userReadAndChecked,
59
+ handleChangeUserReadAndChecked,
60
+ activeWalletType,
61
+ };
62
+ };
63
+
64
+ export const connectToWallet = async (
65
+ walletWeb3: WalletWeb3 | undefined,
66
+ wallet: Wallet,
67
+ config?: Partial<WalletWeb3['providerConfig']>,
68
+ ) => {
69
+ if (!walletWeb3 || !walletWeb3.connectWalletBefore(wallet)) return;
70
+ const providerConfig = {
71
+ ...config,
72
+ };
73
+
74
+ return walletWeb3.connectToWallet(wallet, providerConfig);
75
+ };
76
+
77
+ export function useWalletListByNetwork(
78
+ chainId?: number,
79
+ walletWeb3?: WalletWeb3,
80
+ showOtherInjectWallet?: boolean,
81
+ closeSingleWalletConnectModal?: () => void,
82
+ ) {
83
+ const [walletList, setWalletList] = React.useState<WalletItem[]>([]);
84
+ const disabledWalletKeyList = useWalletStore(
85
+ (state) => state.disabledWalletKeyList,
86
+ );
87
+ const eip6963WalletList = useWalletStore((state) => state.eip6963WalletList);
88
+
89
+ React.useEffect(() => {
90
+ const computed = async () => {
91
+ if (walletWeb3 && chainId !== undefined) {
92
+ // Reset disabled state before getting wallet list
93
+ await walletWeb3.setDisabledState();
94
+ const walletListOrigin = walletWeb3.getWalletList(chainId, {
95
+ eip6963WalletList,
96
+ });
97
+ const list = [] as WalletItem[];
98
+ let len = walletListOrigin.length;
99
+ for (let i = 0; i < len; i++) {
100
+ const item = walletListOrigin[i];
101
+ let isInstalled = false;
102
+ if (item.injected) {
103
+ isInstalled = item.injected();
104
+ }
105
+ if (!isInstalled && item.isInstalled) {
106
+ isInstalled = await item.isInstalled?.();
107
+ }
108
+ list.push({
109
+ title: item.showName,
110
+ icon: item.logo,
111
+ currentType: item.type,
112
+ source: item,
113
+ onClick: (providerPackageOptions) => {
114
+ try {
115
+ return connectToWallet(
116
+ walletWeb3,
117
+ item,
118
+ providerPackageOptions,
119
+ );
120
+ } catch (error) {
121
+ if (closeSingleWalletConnectModal) {
122
+ closeSingleWalletConnectModal();
123
+ }
124
+ throw error;
125
+ }
126
+ },
127
+ disabled: disabledWalletKeyList.includes(
128
+ `${item.type}-${item.showName}`,
129
+ ),
130
+ isLastConnection:
131
+ item.type === walletWeb3.connectController.getLastConnectType(),
132
+ isInstalled,
133
+ });
134
+ }
135
+ setWalletList(list);
136
+ }
137
+ };
138
+ computed();
139
+ }, [
140
+ walletWeb3,
141
+ chainId,
142
+ showOtherInjectWallet,
143
+ disabledWalletKeyList,
144
+ eip6963WalletList,
145
+ ]);
146
+
147
+ return {
148
+ walletList,
149
+ };
150
+ }