@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,143 @@
1
+ import { useWalletStore } from '@dodoex/wallet-web3';
2
+ import { ArrowBack, Copy } from '@dodoex/icons';
3
+ import { Trans } from '@lingui/macro';
4
+ import React from 'react';
5
+ import copy from 'copy-to-clipboard';
6
+ import { QRCodeSVG } from 'qrcode.react';
7
+ import { DialogTitle } from '../components/Dialog';
8
+ import { Box, ButtonBase, Tooltip } from '@dodoex/components';
9
+
10
+ export default function ReceiveTokenPage({
11
+ onClose,
12
+ onBack,
13
+ }: {
14
+ onClose: () => void;
15
+ onBack: () => void;
16
+ }) {
17
+ const { account } = useWalletStore();
18
+ const [showCopyTooltip, setShowCopyTooltip] = React.useState(false);
19
+ const autoCloseShowCopyTooltip = React.useCallback(() => {
20
+ const time = setTimeout(() => {
21
+ setShowCopyTooltip(false);
22
+ }, 1500);
23
+
24
+ return () => {
25
+ clearTimeout(time);
26
+ };
27
+ }, []);
28
+
29
+ if (!account) return null;
30
+
31
+ return (
32
+ <>
33
+ <DialogTitle onClose={onClose}>
34
+ <ButtonBase
35
+ onClick={onBack}
36
+ sx={{
37
+ display: 'flex',
38
+ gap: 8,
39
+ alignItems: 'center',
40
+ typography: 'caption',
41
+ }}
42
+ >
43
+ <Box
44
+ component={ArrowBack}
45
+ sx={{
46
+ width: 16,
47
+ height: 16,
48
+ }}
49
+ />
50
+ <Trans>Receive</Trans>
51
+ </ButtonBase>
52
+ </DialogTitle>
53
+ <Box
54
+ sx={{
55
+ display: 'flex',
56
+ flexDirection: 'column',
57
+ flex: 1,
58
+ overflowY: 'auto',
59
+ }}
60
+ >
61
+ <Box
62
+ sx={{
63
+ display: 'flex',
64
+ flexDirection: 'column',
65
+ justifyContent: 'space-between',
66
+ alignItems: 'center',
67
+ flex: 1,
68
+ pt: 28,
69
+ pb: 20,
70
+ width: 326,
71
+ mx: 'auto',
72
+ }}
73
+ >
74
+ <Box>
75
+ <Box
76
+ sx={{
77
+ p: 36,
78
+ borderRadius: 12,
79
+ border: 'solid 1px',
80
+ borderColor: 'border.main',
81
+ }}
82
+ >
83
+ <QRCodeSVG id="qrcode" size={252} value={account} level="M" />
84
+ </Box>
85
+ <Box
86
+ sx={{
87
+ mt: 12,
88
+ color: 'text.secondary',
89
+ wordBreak: 'break-all',
90
+ textAlign: 'center',
91
+ }}
92
+ >
93
+ {account}
94
+ </Box>
95
+ </Box>
96
+ <Tooltip
97
+ onlyClick
98
+ arrow={false}
99
+ title={<Trans>Copied</Trans>}
100
+ open={showCopyTooltip}
101
+ onOpen={() => {
102
+ setShowCopyTooltip(true);
103
+ autoCloseShowCopyTooltip();
104
+ }}
105
+ onClose={() => setShowCopyTooltip(false)}
106
+ >
107
+ <ButtonBase
108
+ onClick={() => {
109
+ copy(account);
110
+ }}
111
+ sx={{
112
+ display: 'flex',
113
+ alignItems: 'center',
114
+ justifyContent: 'center',
115
+ gap: 4,
116
+ mt: 12,
117
+ px: 16,
118
+ py: 6,
119
+ width: '100%',
120
+ height: 48,
121
+ fontWeight: 600,
122
+ backgroundColor: 'background.paperDarkContrast',
123
+ borderRadius: 8,
124
+ '&:hover': {
125
+ opacity: 0.7,
126
+ },
127
+ }}
128
+ >
129
+ <Box
130
+ component={Copy}
131
+ sx={{
132
+ width: 18,
133
+ height: 18,
134
+ }}
135
+ />
136
+ <Trans>Copy wallet address</Trans>
137
+ </ButtonBase>
138
+ </Tooltip>
139
+ </Box>
140
+ </Box>
141
+ </>
142
+ );
143
+ }
@@ -0,0 +1,249 @@
1
+ import { useWalletStore } from '@dodoex/wallet-web3';
2
+ import { isAddress } from '@ethersproject/address';
3
+ import { ArrowBack, Loading } from '@dodoex/icons';
4
+ import { useWalletConnectContext } from '../WalletConnectProvider';
5
+ import React from 'react';
6
+ import { TokenInfo } from '../components/TokenLogo';
7
+ import { useMutation } from '@tanstack/react-query';
8
+ import BigNumber from 'bignumber.js';
9
+ import { Trans } from '@lingui/macro';
10
+ import { DialogTitle } from '../components/Dialog';
11
+ import { Box, Button, ButtonBase, useTheme } from '@dodoex/components';
12
+ import { truncatePoolAddress } from '../components/AddressWithLinkAndCopy';
13
+
14
+ export default function SendTokenPage({
15
+ open,
16
+ onClose,
17
+ onBack,
18
+ }: {
19
+ open?: boolean;
20
+ onClose: () => void;
21
+ onBack: () => void;
22
+ }) {
23
+ const theme = useTheme();
24
+ const { getChain } = useWalletConnectContext();
25
+ const { chainId } = useWalletStore();
26
+ const chain = getChain(chainId);
27
+ const defaultToken = chain
28
+ ? ({
29
+ ...chain?.gasToken,
30
+ chainId,
31
+ } as TokenInfo)
32
+ : null;
33
+ const [token, setToken] = React.useState<TokenInfo | null>(defaultToken);
34
+ const [amount, setAmount] = React.useState('');
35
+ const [receiverAddress, setReceiverAddress] = React.useState('');
36
+ const [receiverAddressFormat, setReceiverAddressFormat] = React.useState('');
37
+ const [invalidReceiver, setInvalidReceiver] = React.useState(false);
38
+
39
+ const disabled =
40
+ !amount ||
41
+ !receiverAddress ||
42
+ invalidReceiver ||
43
+ !isAddress(receiverAddress);
44
+
45
+ const sendTokenMutation = useMutation({
46
+ mutationFn: async () => {
47
+ if (disabled || !token || token.decimals === undefined) return;
48
+ const amountWei = new BigNumber(amount).times(10 ** token.decimals);
49
+ const amountWeiStr = `0x${amountWei.toString(16)}`;
50
+ let data = '';
51
+ let paramsValue = '';
52
+ let to = token.address;
53
+ // if (
54
+ // token.address.toLowerCase() === defaultToken?.address?.toLowerCase()
55
+ // ) {
56
+ // paramsValue = amountWeiStr;
57
+ // data = '0x';
58
+ // to = receiverAddress;
59
+ // } else {
60
+ // paramsValue = '0x0';
61
+ // data = await TokenApi.encode.transferEncodeABI(
62
+ // receiverAddress,
63
+ // amountWeiStr,
64
+ // );
65
+ // }
66
+ // const result = await execute({
67
+ // brief: 'wallet.account.card.operate.send',
68
+ // spec: {
69
+ // opcode: OpCode.TX,
70
+ // value: paramsValue,
71
+ // to,
72
+ // data,
73
+ // },
74
+ // successBack: () => {
75
+ // // queryClient.invalidateQueries({
76
+ // // queryKey: ['graphql', 'FetchCrossChainDODOOrderList'],
77
+ // // });
78
+ // },
79
+ // metadata: {
80
+ // [MetadataFlag.sendToken]: true,
81
+ // },
82
+ // });
83
+ // if (result === ExecutionResult.Success) {
84
+ // setAmount('');
85
+ // }
86
+ },
87
+ });
88
+
89
+ React.useEffect(() => {
90
+ if (open) {
91
+ setToken(defaultToken);
92
+ setAmount('');
93
+ sendTokenMutation.reset();
94
+ }
95
+ // eslint-disable-next-line react-hooks/exhaustive-deps
96
+ }, [open]);
97
+
98
+ return (
99
+ <>
100
+ <DialogTitle onClose={onClose}>
101
+ <ButtonBase
102
+ onClick={onBack}
103
+ sx={{
104
+ display: 'flex',
105
+ gap: 8,
106
+ alignItems: 'center',
107
+ }}
108
+ >
109
+ <Box
110
+ component={ArrowBack}
111
+ sx={{
112
+ width: 16,
113
+ height: 16,
114
+ }}
115
+ />
116
+ <Trans>Send</Trans>
117
+ </ButtonBase>
118
+ </DialogTitle>
119
+ <Box
120
+ sx={{
121
+ display: 'flex',
122
+ flexDirection: 'column',
123
+ justifyContent: 'space-between',
124
+ flex: 1,
125
+ px: 20,
126
+ pb: 20,
127
+ overflowY: 'auto',
128
+ }}
129
+ >
130
+ <Box>
131
+ <Box
132
+ sx={{
133
+ p: 20,
134
+ backgroundColor: 'background.input',
135
+ borderRadius: 16,
136
+ }}
137
+ >
138
+ <Box
139
+ sx={{
140
+ color: 'text.secondary',
141
+ typography: 'h5',
142
+ }}
143
+ >
144
+ <Trans>Receiver address</Trans>
145
+ </Box>
146
+ <Box
147
+ component="input"
148
+ sx={{
149
+ mt: 12,
150
+ width: '100%',
151
+ fontSize: '36px',
152
+ lineHeight: '40px',
153
+ fontWeight: 600,
154
+ backgroundColor: 'transparent',
155
+ '&::placeholder': {
156
+ color: 'text.disabled',
157
+ },
158
+ }}
159
+ placeholder="0x..."
160
+ value={receiverAddressFormat || receiverAddress}
161
+ onChange={(evt) => {
162
+ setReceiverAddress(evt.target.value);
163
+ setInvalidReceiver(false);
164
+ }}
165
+ onFocus={() => {
166
+ setReceiverAddressFormat('');
167
+ }}
168
+ onBlur={(evt) => {
169
+ const value = evt.target.value;
170
+ if (!value) return;
171
+ if (!isAddress(value)) {
172
+ setInvalidReceiver(true);
173
+ } else {
174
+ setReceiverAddressFormat(truncatePoolAddress(value));
175
+ }
176
+ }}
177
+ />
178
+ {!!invalidReceiver && (
179
+ <Box
180
+ sx={{
181
+ mt: 4,
182
+ color: 'error.main',
183
+ typography: 'h6',
184
+ }}
185
+ >
186
+ <Trans>Invalid wallet address</Trans>
187
+ </Box>
188
+ )}
189
+ </Box>
190
+ {/* <Widget>
191
+ <TokenCard
192
+ amt={amount}
193
+ onInputChange={(v) => setAmount(v)}
194
+ token={token}
195
+ onTokenChange={(token) => setToken(token)}
196
+ showPercentage
197
+ sx={{
198
+ mt: 12,
199
+ pb: 20,
200
+ '&&& input': {
201
+ typography: 'h1',
202
+ '&::placeholder': {
203
+ typography: 'h1',
204
+ },
205
+ },
206
+ }}
207
+ />
208
+ </Widget> */}
209
+ </Box>
210
+ <Button
211
+ disabled={disabled}
212
+ fullWidth
213
+ onClick={() => {
214
+ if (sendTokenMutation.isPending) return;
215
+ sendTokenMutation.mutate();
216
+ }}
217
+ sx={{
218
+ gap: 4,
219
+ mt: 12,
220
+ width: '100%',
221
+ fontWeight: 600,
222
+ }}
223
+ >
224
+ {sendTokenMutation.isPending && (
225
+ <Box
226
+ component={Loading}
227
+ sx={{
228
+ width: 20,
229
+ '& path': {
230
+ fill: theme.palette.primary.contrastText,
231
+ },
232
+ animation: 'loadingRotate 1.1s infinite linear',
233
+ '@keyframes loadingRotate': {
234
+ '0%': {
235
+ transform: 'rotate(0deg)',
236
+ },
237
+ '100%': {
238
+ transform: 'rotate(359deg)',
239
+ },
240
+ },
241
+ }}
242
+ />
243
+ )}
244
+ <Trans>Send</Trans>
245
+ </Button>
246
+ </Box>
247
+ </>
248
+ );
249
+ }
@@ -0,0 +1,72 @@
1
+ import WalletWeb3, { useWalletStore } from '@dodoex/wallet-web3';
2
+ import AccountPage from './AccountPage';
3
+ import React from 'react';
4
+ import Dialog from '../components/Dialog';
5
+ import ConnectPage from './ConnectPage';
6
+ import ReceiveTokenPage from './ReceiveTokenPage';
7
+ import { useWalletConnectContext } from '../WalletConnectProvider';
8
+
9
+ export default function WalletDialog({
10
+ open,
11
+ onClose,
12
+ walletWeb3,
13
+ }: {
14
+ open: boolean;
15
+ onClose: () => void;
16
+ walletWeb3: WalletWeb3;
17
+ }) {
18
+ const { SendTokenPage } = useWalletConnectContext();
19
+ const { account, chainId } = useWalletStore();
20
+ const [showSendTokenPage, setShowSendTokenPage] = React.useState(false);
21
+ const [showReceiveTokenPage, setShowReceiveTokenPage] = React.useState(false);
22
+ return (
23
+ <Dialog
24
+ open={open}
25
+ onClose={onClose}
26
+ width={420}
27
+ bodySx={{
28
+ height: account ? '80vh' : undefined,
29
+ }}
30
+ >
31
+ {account ? (
32
+ <>
33
+ {showSendTokenPage ? (
34
+ <>
35
+ {!!SendTokenPage && (
36
+ <SendTokenPage
37
+ open={showSendTokenPage}
38
+ onClose={onClose}
39
+ onBack={() => setShowSendTokenPage(false)}
40
+ />
41
+ )}
42
+ </>
43
+ ) : showReceiveTokenPage ? (
44
+ <ReceiveTokenPage
45
+ onClose={onClose}
46
+ onBack={() => setShowReceiveTokenPage(false)}
47
+ />
48
+ ) : (
49
+ <>
50
+ <AccountPage
51
+ chainId={chainId}
52
+ account={account}
53
+ walletWeb3={walletWeb3}
54
+ onClose={onClose}
55
+ setShowSendTokenPage={setShowSendTokenPage}
56
+ setShowReceiveTokenPage={setShowReceiveTokenPage}
57
+ />
58
+ </>
59
+ )}
60
+ </>
61
+ ) : (
62
+ <ConnectPage
63
+ chainId={chainId}
64
+ account={account}
65
+ walletWeb3={walletWeb3}
66
+ showOtherInjectWallet
67
+ onClose={onClose}
68
+ />
69
+ )}
70
+ </Dialog>
71
+ );
72
+ }
@@ -0,0 +1,54 @@
1
+ import { GraphQLRequests, RestApiRequests } from '@dodoex/api';
2
+ import React from 'react';
3
+ import { TokenInfo } from './components/TokenLogo';
4
+ import { WalletItem } from './hooks/useConnectWalet';
5
+
6
+ const WalletConnectContext = React.createContext<{
7
+ chainId?: number;
8
+ termsOfServiceLink?: React.ReactNode;
9
+ connectTimeout?: number;
10
+ graphQLRequests?: GraphQLRequests;
11
+ tokenList: Array<TokenInfo>;
12
+ restApiRequests?: RestApiRequests;
13
+ SendTokenPage?: (params: {
14
+ open?: boolean;
15
+ onClose: () => void;
16
+ onBack: () => void;
17
+ }) => JSX.Element;
18
+ encryptFiatPriceToken?: () => string;
19
+ loadAccountListEthBalance?: (
20
+ accountList: string[],
21
+ chainId?: number,
22
+ ) => Promise<Map<string, number | null>>;
23
+ getChain: (chainId: number) => null | {
24
+ name: string;
25
+ scanUrl: string;
26
+ gasToken: {
27
+ symbol: string;
28
+ decimals: number;
29
+ };
30
+ logo: React.ReactNode;
31
+ };
32
+ getTokenLogoUrl?: (op: {
33
+ chainId?: number | null;
34
+ address?: string | null;
35
+ width?: number;
36
+ height?: number;
37
+ }) => string | null;
38
+ onConnectTimeout?: () => void;
39
+ switchNetwork?: (chainId: number) => void;
40
+ onConnected?: (chainId: number, wallet: WalletItem) => void;
41
+ onClickToken?: (token: TokenInfo) => void;
42
+ }>({
43
+ connectTimeout: 15000,
44
+ tokenList: [],
45
+ getChain: () => {
46
+ throw new Error('getChain is undefined');
47
+ },
48
+ });
49
+
50
+ export const WalletConnectProvider = WalletConnectContext.Provider;
51
+
52
+ export const useWalletConnectContext = () => {
53
+ return React.useContext(WalletConnectContext);
54
+ };