@lifi/widget 1.2.0 → 1.2.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.
package/App.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect } from 'react';
2
3
  import { Route, Routes } from 'react-router-dom';
3
4
  import { AppProvider } from './AppProvider';
4
5
  import { AppContainer } from './components/AppContainer';
@@ -10,10 +11,16 @@ import { SelectWalletPage } from './pages/SelectWalletPage';
10
11
  import { SettingsPage } from './pages/SettingsPage';
11
12
  import { SwapPage } from './pages/SwapPage';
12
13
  import { SwapRoutesPage } from './pages/SwapRoutesPage';
14
+ import { useWallet } from './providers/WalletProvider';
13
15
  import { routes } from './utils/routes';
14
16
  export const App = ({ config }) => {
15
17
  return (_jsx(AppProvider, Object.assign({ config: config }, { children: _jsx(AppDefault, {}) })));
16
18
  };
17
19
  export const AppDefault = () => {
20
+ const { attemptEagerConnect } = useWallet();
21
+ useEffect(() => {
22
+ attemptEagerConnect();
23
+ // eslint-disable-next-line react-hooks/exhaustive-deps
24
+ }, []);
18
25
  return (_jsxs(AppContainer, { children: [_jsx(Header, {}), _jsxs(Routes, { children: [_jsx(Route, { path: routes.home, element: _jsx(MainPage, {}) }), _jsx(Route, { path: routes.selectWallet, element: _jsx(SelectWalletPage, {}) }), _jsx(Route, { path: routes.settings, element: _jsx(SettingsPage, {}) }), _jsx(Route, { path: routes.fromToken, element: _jsx(SelectTokenPage, { formType: "from" }) }), _jsx(Route, { path: routes.toToken, element: _jsx(SelectTokenPage, { formType: "to" }) }), _jsx(Route, { path: routes.swapRoutes, element: _jsx(SwapRoutesPage, {}) }), _jsx(Route, { path: `${routes.swapRoutes}/${routes.swap}`, element: _jsx(SwapPage, {}) }), _jsx(Route, { path: routes.swap, element: _jsx(SwapPage, {}) })] }), _jsx(Initializer, {})] }));
19
26
  };
@@ -1,9 +1,19 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
11
  import { AccountBalanceWallet as AccountBalanceWalletIcon, Logout as LogoutIcon, } from '@mui/icons-material';
3
12
  import { Box, IconButton, Typography } from '@mui/material';
4
13
  import { useTranslation } from 'react-i18next';
5
14
  import { useLocation, useNavigate } from 'react-router-dom';
6
15
  import { useWallet } from '../../providers/WalletProvider';
16
+ import { useWidgetConfig } from '../../providers/WidgetProvider';
7
17
  import { routes } from '../../utils/routes';
8
18
  import { HeaderAppBar } from './Header.style';
9
19
  export const WalletHeader = () => {
@@ -20,9 +30,15 @@ export const WalletHeader = () => {
20
30
  };
21
31
  const ConnectButton = () => {
22
32
  const { pathname } = useLocation();
33
+ const config = useWidgetConfig();
34
+ const { connect: walletConnect } = useWallet();
23
35
  const navigate = useNavigate();
24
- const connect = () => {
36
+ const connect = () => __awaiter(void 0, void 0, void 0, function* () {
37
+ if (config.disableInternalWalletManagement) {
38
+ yield walletConnect();
39
+ return;
40
+ }
25
41
  navigate(routes.selectWallet);
26
- };
42
+ });
27
43
  return (_jsx(IconButton, Object.assign({ size: "medium", "aria-label": "disconnect", edge: "end", onClick: !pathname.includes(routes.selectWallet) ? connect : undefined }, { children: _jsx(AccountBalanceWalletIcon, {}) })));
28
44
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/widget",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "LI.FI Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.",
5
5
  "sideEffects": false,
6
6
  "main": "./index.js",
@@ -41,7 +41,7 @@
41
41
  "@emotion/styled": "^11.9.3",
42
42
  "@ethersproject/experimental": "^5.6.2",
43
43
  "@ethersproject/providers": "^5.6.8",
44
- "@lifi/wallet-management": "^1.0.2",
44
+ "@lifi/wallet-management": "^1.0.3",
45
45
  "@lifinance/sdk": "^1.0.0-beta.11",
46
46
  "@mui/icons-material": "^5.8.3",
47
47
  "@mui/lab": "^5.0.0-alpha.85",
@@ -20,6 +20,7 @@ const initialContext = {
20
20
  switchChain: stub,
21
21
  addChain: stub,
22
22
  addToken: stub,
23
+ attemptEagerConnect: stub,
23
24
  account: {},
24
25
  };
25
26
  const WalletContext = createContext(initialContext);
@@ -30,35 +31,58 @@ export const WalletProvider = ({ children }) => {
30
31
  const [account, setAccount] = useState({});
31
32
  const connect = useCallback((wallet) => __awaiter(void 0, void 0, void 0, function* () {
32
33
  if (config.disableInternalWalletManagement) {
33
- // TODO
34
+ const signer = yield config.walletCallbacks.connect();
35
+ const account = yield extractAccountFromSigner(signer);
36
+ setAccount(account);
34
37
  return;
35
38
  }
36
39
  yield walletManagementConnect(wallet);
37
- }), [config.disableInternalWalletManagement, walletManagementConnect]);
40
+ }), [
41
+ config.disableInternalWalletManagement,
42
+ config.walletCallbacks,
43
+ walletManagementConnect,
44
+ ]);
38
45
  const disconnect = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
39
46
  if (config.disableInternalWalletManagement) {
47
+ yield config.walletCallbacks.disconnect();
40
48
  setAccount({});
49
+ return;
41
50
  }
42
51
  yield walletManagementDisconnect();
43
- }), [config.disableInternalWalletManagement, walletManagementDisconnect]);
52
+ }), [
53
+ config.disableInternalWalletManagement,
54
+ config.walletCallbacks,
55
+ walletManagementDisconnect,
56
+ ]);
44
57
  // only for injected wallets
45
58
  const switchChain = useCallback((chainId) => __awaiter(void 0, void 0, void 0, function* () {
46
59
  if (config.disableInternalWalletManagement) {
47
- // TODO
48
- return false;
60
+ const signer = yield config.walletCallbacks.switchChain(chainId);
61
+ const account = yield extractAccountFromSigner(signer);
62
+ setAccount(account);
49
63
  }
50
64
  return walletSwitchChain(chainId);
51
- }), [config.disableInternalWalletManagement]);
65
+ }), [config.disableInternalWalletManagement, config.walletCallbacks]);
52
66
  const addChain = useCallback((chainId) => __awaiter(void 0, void 0, void 0, function* () {
53
- if (!config.disableInternalWalletManagement) {
54
- yield walletAddChain(chainId);
67
+ if (config.disableInternalWalletManagement) {
68
+ return config.walletCallbacks.addChain(chainId);
55
69
  }
56
- }), [config.disableInternalWalletManagement]);
70
+ return walletAddChain(chainId);
71
+ }), [config.disableInternalWalletManagement, config.walletCallbacks]);
57
72
  const addToken = useCallback((chainId, token) => __awaiter(void 0, void 0, void 0, function* () {
58
- if (!config.disableInternalWalletManagement) {
59
- yield switchChainAndAddToken(chainId, token);
73
+ if (config.disableInternalWalletManagement) {
74
+ return config.walletCallbacks.addToken(token, chainId);
60
75
  }
61
- }), [config.disableInternalWalletManagement]);
76
+ return switchChainAndAddToken(chainId, token);
77
+ }), [config.disableInternalWalletManagement, config.walletCallbacks]);
78
+ const attemptEagerConnect = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
79
+ if (config.disableInternalWalletManagement) {
80
+ const signer = yield config.walletCallbacks.provideSigner();
81
+ const account = yield extractAccountFromSigner(signer);
82
+ console.log(account);
83
+ setAccount((oldAccount) => (Object.assign({}, account)));
84
+ }
85
+ }), [config.disableInternalWalletManagement, config.walletCallbacks]);
62
86
  // keep account information up to date
63
87
  useEffect(() => {
64
88
  const updateAccount = () => __awaiter(void 0, void 0, void 0, function* () {
@@ -75,8 +99,17 @@ export const WalletProvider = ({ children }) => {
75
99
  switchChain,
76
100
  addChain,
77
101
  addToken,
102
+ attemptEagerConnect,
103
+ account,
104
+ }), [
78
105
  account,
79
- }), [account, addChain, addToken, connect, disconnect, switchChain]);
106
+ addChain,
107
+ addToken,
108
+ connect,
109
+ disconnect,
110
+ switchChain,
111
+ attemptEagerConnect,
112
+ ]);
80
113
  return (_jsx(WalletContext.Provider, Object.assign({ value: value }, { children: children })));
81
114
  };
82
115
  const extractAccountFromSigner = (signer) => __awaiter(void 0, void 0, void 0, function* () {
@@ -3,11 +3,12 @@ import { Token } from '@lifinance/sdk';
3
3
  import { Signer } from 'ethers';
4
4
  export interface WalletContextProps {
5
5
  account: WalletAccount;
6
- addChain(chainId: number): Promise<void>;
6
+ addChain(chainId: number): Promise<boolean>;
7
7
  addToken(chainId: number, token: Token): Promise<void>;
8
8
  disconnect(): void;
9
9
  switchChain(chainId: number): Promise<boolean>;
10
10
  connect(wallet?: Wallet | undefined): Promise<void>;
11
+ attemptEagerConnect(): Promise<void>;
11
12
  }
12
13
  export interface WalletAccount {
13
14
  address?: string;
package/types/widget.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ChainKey } from '@lifinance/sdk';
1
+ import { ChainKey, Token } from '@lifinance/sdk';
2
2
  import { PaletteMode, PaletteOptions, Shape } from '@mui/material';
3
3
  import { TypographyOptions } from '@mui/material/styles/createTypography';
4
4
  import { Signer } from 'ethers';
@@ -10,11 +10,12 @@ export declare type ThemeConfig = {
10
10
  typography?: TypographyOptions;
11
11
  };
12
12
  export interface WidgetWalletCallbacks {
13
- connect(): Signer;
14
- disconnect(): void;
15
- provideSigner(): Signer;
16
- switchChain(): Signer;
17
- addToken(): void;
13
+ connect(): Promise<Signer>;
14
+ disconnect(): Promise<void>;
15
+ provideSigner(): Promise<Signer | undefined>;
16
+ switchChain(reqChainId: number): Promise<Signer>;
17
+ addToken(token: Token, chainId: number): Promise<void>;
18
+ addChain(chainId: number): Promise<boolean>;
18
19
  }
19
20
  interface WidgetConfigBase {
20
21
  fromAmount?: number;