@lifi/widget 2.0.0-alpha.4 → 2.0.0-beta.1

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 (34) hide show
  1. package/cjs/components/Insurance/Insurance.js +2 -2
  2. package/cjs/components/Insurance/InsuranceCard.js +8 -2
  3. package/cjs/components/Insurance/InsuranceCollapsed.d.ts +2 -2
  4. package/cjs/components/Insurance/InsuranceCollapsed.js +2 -2
  5. package/cjs/components/Insurance/types.d.ts +2 -0
  6. package/cjs/config/version.d.ts +1 -1
  7. package/cjs/config/version.js +1 -1
  8. package/cjs/i18n/pt.json +8 -5
  9. package/cjs/i18n/uk.json +3 -3
  10. package/cjs/pages/SelectWalletPage/SelectWalletPage.js +5 -5
  11. package/cjs/pages/SwapDetailsPage/SwapDetailsPage.js +6 -4
  12. package/cjs/pages/SwapPage/SwapPage.js +4 -1
  13. package/cjs/providers/WalletProvider/WalletProvider.js +72 -28
  14. package/cjs/providers/WalletProvider/index.d.ts +1 -1
  15. package/cjs/providers/WalletProvider/index.js +1 -1
  16. package/cjs/providers/WalletProvider/types.d.ts +1 -1
  17. package/components/Insurance/Insurance.js +2 -2
  18. package/components/Insurance/InsuranceCard.js +9 -3
  19. package/components/Insurance/InsuranceCollapsed.d.ts +2 -2
  20. package/components/Insurance/InsuranceCollapsed.js +2 -2
  21. package/components/Insurance/types.d.ts +2 -0
  22. package/config/version.d.ts +1 -1
  23. package/config/version.js +1 -1
  24. package/i18n/pt.json +8 -5
  25. package/i18n/uk.json +3 -3
  26. package/package.json +6 -6
  27. package/pages/SelectWalletPage/SelectWalletPage.js +5 -5
  28. package/pages/SwapDetailsPage/SwapDetailsPage.js +6 -4
  29. package/pages/SwapPage/SwapPage.js +4 -1
  30. package/providers/WalletProvider/WalletProvider.js +73 -29
  31. package/providers/WalletProvider/index.d.ts +1 -1
  32. package/providers/WalletProvider/index.js +1 -1
  33. package/providers/WalletProvider/types.d.ts +1 -1
  34. package/tsconfig.cjs.tsbuildinfo +1 -1
@@ -29,9 +29,11 @@ export const SwapDetailsPage = () => {
29
29
  deleteRoute(routeExecution.route.id);
30
30
  }
31
31
  };
32
- let supportId = routeExecution?.route.steps[0].execution?.process.find((process) => process.txHash)?.txHash ??
33
- routeExecution?.route.id ??
34
- '';
32
+ const sourceTxHash = routeExecution?.route.steps[0].execution?.process
33
+ .filter((process) => process.type !== 'TOKEN_ALLOWANCE')
34
+ .find((process) => process.txHash)?.txHash;
35
+ const insuranceCoverageId = sourceTxHash ?? routeExecution?.route.fromAddress;
36
+ let supportId = sourceTxHash ?? routeExecution?.route.id ?? '';
35
37
  if (process.env.NODE_ENV === 'development') {
36
38
  supportId += `_${routeExecution?.route.id}`;
37
39
  }
@@ -48,7 +50,7 @@ export const SwapDetailsPage = () => {
48
50
  justifyContent: 'space-between',
49
51
  }, pb: 1, children: [_jsx(Typography, { fontSize: 12, children: new Intl.DateTimeFormat(i18n.language, { dateStyle: 'long' }).format(startedAt) }), _jsx(Typography, { fontSize: 12, children: new Intl.DateTimeFormat(i18n.language, {
50
52
  timeStyle: 'short',
51
- }).format(startedAt) })] }), getStepList(routeExecution?.route), routeExecution?.route?.insurance?.state === 'INSURED' ? (_jsx(Insurance, { mt: 2, status: routeExecution.status, feeAmountUsd: routeExecution.route.insurance.feeAmountUsd, insurableRouteId: routeExecution.route.id })) : null, _jsxs(Card, { mt: 2, children: [_jsxs(Box, { sx: {
53
+ }).format(startedAt) })] }), getStepList(routeExecution?.route), routeExecution?.route?.insurance?.state === 'INSURED' ? (_jsx(Insurance, { mt: 2, status: routeExecution.status, feeAmountUsd: routeExecution.route.insurance.feeAmountUsd, insurableRouteId: routeExecution.route.id, insuranceCoverageId: insuranceCoverageId })) : null, _jsxs(Card, { mt: 2, children: [_jsxs(Box, { sx: {
52
54
  display: 'flex',
53
55
  flex: 1,
54
56
  }, children: [_jsx(CardTitle, { flex: 1, children: t('swap.supportId') }), _jsx(Box, { mr: 1, mt: 1, children: _jsx(IconButton, { size: "medium", onClick: copySupportId, children: _jsx(ContentCopyIcon, { fontSize: "small" }) }) })] }), _jsx(Typography, { variant: "body2", pt: 1, pb: 2, px: 2, sx: { wordBreak: 'break-all' }, children: supportId })] }), _jsx(Box, { mt: 2, children: _jsx(Button, { href: "https://discord.com/channels/849912621360218112/863689862514343946", target: "_blank", rel: "nofollow noreferrer", fullWidth: true, children: t('button.contactSupport') }) }), _jsxs(Dialog, { open: open, onClose: toggleDialog, children: [_jsx(DialogTitle, { children: t('swap.warning.title.deleteSwap') }), _jsx(DialogContent, { children: _jsx(DialogContentText, { children: t('swap.warning.message.deleteSwapHistory') }) }), _jsxs(DialogActions, { children: [_jsx(Button, { onClick: toggleDialog, children: t('button.cancel') }), _jsx(Button, { variant: "contained", onClick: handleDeleteRoute, autoFocus: true, children: t('button.delete') })] })] })] }));
@@ -75,7 +75,10 @@ export const SwapPage = () => {
75
75
  (route?.insurance?.state === 'INSURED' ||
76
76
  (status === RouteExecutionStatus.Idle &&
77
77
  route?.insurance?.state === 'INSURABLE'));
78
- return (_jsxs(Container, { children: [getStepList(route), insuranceAvailable ? (_jsx(Insurance, { mt: 2, status: status, insurableRouteId: stateRouteId, feeAmountUsd: route?.insurance.feeAmountUsd, onChange: setRouteId })) : null, status === RouteExecutionStatus.Idle ||
78
+ const insuranceCoverageId = route?.steps[0].execution?.process
79
+ .filter((process) => process.type !== 'TOKEN_ALLOWANCE')
80
+ .find((process) => process.txHash)?.txHash ?? route?.fromAddress;
81
+ return (_jsxs(Container, { children: [getStepList(route), insuranceAvailable ? (_jsx(Insurance, { mt: 2, status: status, insurableRouteId: stateRouteId, feeAmountUsd: route?.insurance.feeAmountUsd, insuranceCoverageId: insuranceCoverageId, onChange: setRouteId })) : null, status === RouteExecutionStatus.Idle ||
79
82
  status === RouteExecutionStatus.Failed ? (_jsxs(_Fragment, { children: [_jsx(GasMessage, { mt: 2, route: route }), _jsxs(Box, { mt: 2, display: "flex", children: [_jsx(SwapButton, { text: getSwapButtonText(), onClick: handleSwapClick, route: route, insurableRouteId: stateRouteId }), status === RouteExecutionStatus.Failed ? (_jsx(Tooltip, { title: t('button.removeSwap'), placement: "bottom-end", enterDelay: 400, arrow: true, children: _jsx(Button, { onClick: handleRemoveRoute, sx: {
80
83
  minWidth: 48,
81
84
  marginLeft: 1,
@@ -1,7 +1,8 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { addChain as walletAddChain, switchChain as walletSwitchChain, switchChainAndAddToken, useLiFiWalletManagement, } from '@lifi/wallet-management';
2
+ import { LiFiWalletManagement, readActiveWallets, supportedWallets, } from '@lifi/wallet-management';
3
3
  import { createContext, useCallback, useContext, useEffect, useMemo, useState, } from 'react';
4
4
  import { useWidgetConfig } from '../WidgetProvider';
5
+ const liFiWalletManagement = new LiFiWalletManagement();
5
6
  const stub = () => {
6
7
  throw new Error(`You forgot to wrap your component in <${WalletProvider.name}>.`);
7
8
  };
@@ -17,8 +18,26 @@ const WalletContext = createContext(initialContext);
17
18
  export const useWallet = () => useContext(WalletContext);
18
19
  export const WalletProvider = ({ children }) => {
19
20
  const { walletManagement } = useWidgetConfig();
20
- const { connect: walletManagementConnect, disconnect: walletManagementDisconnect, signer, provider, } = useLiFiWalletManagement();
21
21
  const [account, setAccount] = useState({});
22
+ const [currentWallet, setCurrentWallet] = useState();
23
+ useEffect(() => {
24
+ const autoConnect = async () => {
25
+ const persistedActiveWallets = readActiveWallets();
26
+ const activeWallets = supportedWallets.filter((wallet) => persistedActiveWallets.some((perstistedWallet) => perstistedWallet.name === wallet.name));
27
+ if (!activeWallets.length) {
28
+ return;
29
+ }
30
+ await liFiWalletManagement.autoConnect(activeWallets);
31
+ activeWallets[0].on('walletAccountChanged', handleWalletUpdate);
32
+ handleWalletUpdate(activeWallets[0]);
33
+ };
34
+ autoConnect();
35
+ }, []);
36
+ const handleWalletUpdate = async (wallet) => {
37
+ setCurrentWallet(wallet);
38
+ const account = await extractAccountFromSigner(wallet?.account?.signer);
39
+ setAccount(account);
40
+ };
22
41
  const connect = useCallback(async (wallet) => {
23
42
  if (walletManagement) {
24
43
  const signer = await walletManagement.connect();
@@ -26,17 +45,22 @@ export const WalletProvider = ({ children }) => {
26
45
  setAccount(account);
27
46
  return;
28
47
  }
29
- await walletManagementConnect(wallet);
30
- }, [walletManagement, walletManagementConnect]);
48
+ await liFiWalletManagement.connect(wallet);
49
+ wallet.on('walletAccountChanged', handleWalletUpdate);
50
+ handleWalletUpdate(wallet);
51
+ }, [walletManagement]);
31
52
  const disconnect = useCallback(async () => {
32
53
  if (walletManagement) {
33
54
  await walletManagement.disconnect();
34
55
  setAccount({});
35
56
  return;
36
57
  }
37
- await walletManagementDisconnect();
38
- }, [walletManagement, walletManagementDisconnect]);
39
- // only for injected wallets
58
+ if (currentWallet) {
59
+ await liFiWalletManagement.disconnect(currentWallet);
60
+ currentWallet.removeAllListeners();
61
+ handleWalletUpdate(undefined);
62
+ }
63
+ }, [walletManagement, currentWallet]);
40
64
  const switchChain = useCallback(async (chainId) => {
41
65
  if (walletManagement?.switchChain) {
42
66
  const signer = await walletManagement.switchChain(chainId);
@@ -44,34 +68,46 @@ export const WalletProvider = ({ children }) => {
44
68
  setAccount(account);
45
69
  return true;
46
70
  }
47
- return walletSwitchChain(chainId);
48
- }, [walletManagement]);
71
+ try {
72
+ await currentWallet?.switchChain(chainId);
73
+ handleWalletUpdate(currentWallet);
74
+ return true;
75
+ }
76
+ catch {
77
+ return false;
78
+ }
79
+ }, [walletManagement, currentWallet]);
49
80
  const addChain = useCallback(async (chainId) => {
50
81
  if (walletManagement?.addChain) {
51
82
  return walletManagement.addChain(chainId);
52
83
  }
53
- return walletAddChain(chainId);
54
- }, [walletManagement]);
84
+ try {
85
+ await currentWallet?.addChain(chainId);
86
+ handleWalletUpdate(currentWallet);
87
+ return true;
88
+ }
89
+ catch {
90
+ return false;
91
+ }
92
+ }, [walletManagement, currentWallet]);
55
93
  const addToken = useCallback(async (chainId, token) => {
56
94
  if (walletManagement?.addToken) {
57
95
  return walletManagement.addToken(token, chainId);
58
96
  }
59
- return switchChainAndAddToken(chainId, token);
60
- }, [walletManagement]);
61
- // keep account information up to date
97
+ await currentWallet?.addToken(chainId, token);
98
+ handleWalletUpdate(currentWallet);
99
+ return;
100
+ }, [walletManagement, currentWallet]);
101
+ // keep widget in sync with changing external signer object
62
102
  useEffect(() => {
63
- const updateAccount = async () => {
64
- let account;
65
- if (walletManagement) {
66
- account = await extractAccountFromSigner(walletManagement?.signer);
67
- }
68
- else {
69
- account = await extractAccountFromSigner(signer);
70
- }
71
- setAccount(account);
72
- };
73
- updateAccount();
74
- }, [signer, walletManagement]);
103
+ if (walletManagement) {
104
+ const updateAccount = async () => {
105
+ const account = await extractAccountFromSigner(walletManagement?.signer);
106
+ setAccount(account);
107
+ };
108
+ updateAccount();
109
+ }
110
+ }, [walletManagement, walletManagement?.signer]);
75
111
  const value = useMemo(() => ({
76
112
  connect,
77
113
  disconnect,
@@ -79,8 +115,16 @@ export const WalletProvider = ({ children }) => {
79
115
  addChain,
80
116
  addToken,
81
117
  account,
82
- provider,
83
- }), [account, addChain, addToken, connect, disconnect, provider, switchChain]);
118
+ provider: currentWallet?.account?.provider,
119
+ }), [
120
+ account,
121
+ addChain,
122
+ addToken,
123
+ connect,
124
+ disconnect,
125
+ currentWallet,
126
+ switchChain,
127
+ ]);
84
128
  return (_jsx(WalletContext.Provider, { value: value, children: children }));
85
129
  };
86
130
  export const extractAccountFromSigner = async (signer) => {
@@ -93,7 +137,7 @@ export const extractAccountFromSigner = async (signer) => {
93
137
  };
94
138
  }
95
139
  catch (error) {
96
- console.log(error);
140
+ console.error(error);
97
141
  return {};
98
142
  }
99
143
  };
@@ -1,2 +1,2 @@
1
- export * from './types';
2
1
  export * from './WalletProvider';
2
+ export * from './types';
@@ -1,2 +1,2 @@
1
- export * from './types';
2
1
  export * from './WalletProvider';
2
+ export * from './types';
@@ -7,7 +7,7 @@ export interface WalletContextProps {
7
7
  provider?: Provider;
8
8
  addChain(chainId: number): Promise<boolean>;
9
9
  addToken(chainId: number, token: Token): Promise<void>;
10
- disconnect(): void;
10
+ disconnect(wallet?: Wallet): void;
11
11
  switchChain(chainId: number): Promise<boolean>;
12
12
  connect(wallet?: Wallet | undefined): Promise<void>;
13
13
  }