@dodoex/wallet-web3-react 0.4.0 → 0.4.2
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.
- package/babel.config.js +9 -9
- package/dist/cjs/index.cjs +4 -13
- package/dist/cjs/locales/en.js +1 -1
- package/dist/cjs/locales/zh.js +1 -1
- package/dist/index.js +4 -13
- package/dist/locales/en.js +1 -1
- package/dist/locales/zh.js +1 -1
- package/dist/types/ClientProvider.d.ts +1 -1
- package/dist/types/LangProvider.d.ts +1 -1
- package/dist/types/WalletConnect/AccountPage.d.ts +1 -1
- package/dist/types/WalletConnect/ActivityList.d.ts +5 -4
- package/dist/types/WalletConnect/ConnectAlchemy/index.d.ts +2 -1
- package/dist/types/WalletConnect/ConnectDialog.d.ts +2 -1
- package/dist/types/WalletConnect/ConnectLedger/ErrorDialog.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectLedger/LoadingDialog.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectLedger/LockedDialog.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectLedger/ProtocolDialog.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectLedger/SelectAddressDialog.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectLedger/SelectPathDialog.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectLedger/index.d.ts +1 -1
- package/dist/types/WalletConnect/ConnectPage.d.ts +1 -1
- package/dist/types/WalletConnect/HasBalanceTokenList.d.ts +1 -1
- package/dist/types/WalletConnect/ReceiveTokenPage.d.ts +2 -1
- package/dist/types/WalletConnect/SendTokenPage.d.ts +2 -1
- package/dist/types/WalletConnect/WalletDialog.d.ts +2 -1
- package/dist/types/WalletConnectProvider.d.ts +2 -2
- package/dist/types/components/AddressWithLinkAndCopy.d.ts +2 -2
- package/dist/types/components/Dialog.d.ts +3 -3
- package/dist/types/components/WalletTag.d.ts +1 -1
- package/lingui.config.ts +13 -13
- package/package.json +90 -90
- package/rollup.config.mjs +106 -103
- package/src/ClientProvider.tsx +17 -17
- package/src/LangProvider.tsx +36 -36
- package/src/WalletConnect/AccountPage.tsx +498 -498
- package/src/WalletConnect/ActivityList.tsx +606 -606
- package/src/WalletConnect/ConnectAlchemy/index.tsx +248 -248
- package/src/WalletConnect/ConnectAlchemy/useConnectAlchemy.ts +105 -105
- package/src/WalletConnect/ConnectDialog.tsx +35 -35
- package/src/WalletConnect/ConnectLedger/ErrorDialog.tsx +61 -61
- package/src/WalletConnect/ConnectLedger/LockedDialog.tsx +54 -54
- package/src/WalletConnect/ConnectLedger/helper.ts +14 -14
- package/src/WalletConnect/ConnectPage.tsx +508 -508
- package/src/WalletConnect/HasBalanceTokenList.tsx +202 -202
- package/src/WalletConnect/ReceiveTokenPage.tsx +145 -145
- package/src/WalletConnect/SendTokenPage.tsx +251 -251
- package/src/WalletConnect/WalletDialog.tsx +80 -80
- package/src/WalletConnectProvider.tsx +57 -57
- package/src/components/AddressWithLinkAndCopy.tsx +202 -202
- package/src/components/Dialog.tsx +158 -158
- package/src/components/TokenLogo.tsx +167 -167
- package/src/components/WalletTag.tsx +117 -117
- package/src/constants/localstorage.ts +24 -24
- package/src/hooks/useConnectWallet.ts +150 -150
- package/src/hooks/useFetchFiatPrice.ts +53 -53
- package/src/hooks/useFetchTokensBalance.ts +53 -53
- package/src/hooks/useHasBalanceTokenList.ts +95 -95
- package/src/hooks/useTransactionList.ts +89 -89
- package/src/index.tsx +7 -7
- package/src/locales/en.js +1 -1
- package/src/locales/en.po +8 -8
- package/src/locales/zh.js +1 -1
- package/src/locales/zh.po +4 -4
- package/src/react19-types.d.ts +12 -0
- package/src/utils/formatter.ts +102 -102
- package/src/utils/time.ts +21 -21
- package/src/utils/utils.ts +8 -8
- package/tsconfig.json +22 -22
|
@@ -1,117 +1,117 @@
|
|
|
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
|
+
'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,24 +1,24 @@
|
|
|
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
|
+
'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,150 +1,150 @@
|
|
|
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
|
-
}
|
|
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
|
+
}
|