@rango-dev/widget-embedded 0.1.16-next.0 → 0.1.16-next.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/dist/QueueManager.d.ts.map +1 -1
- package/dist/components/Layout.d.ts.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/pages/Home.d.ts.map +1 -1
- package/dist/store/wallets.d.ts +6 -15
- package/dist/store/wallets.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/wallets.d.ts +7 -0
- package/dist/types/wallets.d.ts.map +1 -0
- package/dist/utils/routing.d.ts +2 -3
- package/dist/utils/routing.d.ts.map +1 -1
- package/dist/utils/swap.d.ts +8 -9
- package/dist/utils/swap.d.ts.map +1 -1
- package/dist/utils/wallets.d.ts +11 -12
- package/dist/utils/wallets.d.ts.map +1 -1
- package/dist/widget-embedded.cjs.development.js +82 -83
- package/dist/widget-embedded.cjs.development.js.map +1 -1
- package/dist/widget-embedded.cjs.production.min.js +82 -83
- package/dist/widget-embedded.cjs.production.min.js.map +1 -1
- package/dist/widget-embedded.esm.js +82 -83
- package/dist/widget-embedded.esm.js.map +1 -1
- package/package.json +2 -6
- package/src/QueueManager.tsx +4 -5
- package/src/components/Layout.tsx +27 -23
- package/src/components/TokenInfo.tsx +5 -5
- package/src/hooks/useConfirmSwap.ts +2 -2
- package/src/index.tsx +144 -14
- package/src/pages/ConfirmSwapPage.tsx +2 -2
- package/src/pages/Home.tsx +5 -3
- package/src/store/bestRoute.ts +7 -7
- package/src/store/wallets.ts +32 -46
- package/src/types/index.ts +1 -0
- package/src/types/wallets.ts +7 -0
- package/src/utils/routing.ts +2 -3
- package/src/utils/swap.ts +12 -11
- package/src/utils/wallets.ts +59 -52
- package/dist/App.d.ts +0 -9
- package/dist/App.d.ts.map +0 -1
- package/dist/lib.d.ts +0 -10
- package/dist/lib.d.ts.map +0 -1
- package/src/App.tsx +0 -123
- package/src/app.css +0 -12
- package/src/lib.tsx +0 -144
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rango-dev/widget-embedded",
|
|
3
|
-
"version": "0.1.16-next.
|
|
3
|
+
"version": "0.1.16-next.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -10,11 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
12
12
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
-
"dev": "parcel public/index.html -p 3001 --no-cache",
|
|
15
|
-
"dev-tsc": "npm-run-all --parallel watch-tsc dev",
|
|
16
|
-
"build:app": "parcel build --cache-dir=.parcel-cache",
|
|
17
|
-
"build": "tsdx build --entry ./src/lib.tsx"
|
|
13
|
+
"build": "tsdx build"
|
|
18
14
|
},
|
|
19
15
|
"husky": {
|
|
20
16
|
"hooks": {
|
package/src/QueueManager.tsx
CHANGED
|
@@ -34,13 +34,12 @@ function QueueManager(props: PropsWithChildren<{}>) {
|
|
|
34
34
|
});
|
|
35
35
|
}, []);
|
|
36
36
|
const getOneOfWalletsDetails = useWalletsStore.use.getOneOfWalletsDetails();
|
|
37
|
-
const accounts = useWalletsStore.use.accounts();
|
|
38
37
|
|
|
39
38
|
const { blockchains } = useMetaStore.use.meta();
|
|
40
|
-
const
|
|
39
|
+
const connectedWallets = useWalletsStore.use.connectedWallets();
|
|
41
40
|
|
|
42
41
|
const wallets = {
|
|
43
|
-
blockchains:
|
|
42
|
+
blockchains: connectedWallets.map((wallet) => ({
|
|
44
43
|
accounts: [wallet],
|
|
45
44
|
name: wallet.chain,
|
|
46
45
|
})),
|
|
@@ -79,12 +78,12 @@ function QueueManager(props: PropsWithChildren<{}>) {
|
|
|
79
78
|
lastStep?.id !== data.step?.id) ||
|
|
80
79
|
!!outputAmount
|
|
81
80
|
) {
|
|
82
|
-
const fromAccount =
|
|
81
|
+
const fromAccount = connectedWallets.find(
|
|
83
82
|
(account) => account.chain === step?.fromBlockchain
|
|
84
83
|
);
|
|
85
84
|
const toAccount =
|
|
86
85
|
step?.fromBlockchain !== step?.toBlockchain &&
|
|
87
|
-
|
|
86
|
+
connectedWallets.find((wallet) => wallet.chain === step?.toBlockchain);
|
|
88
87
|
|
|
89
88
|
fromAccount && getOneOfWalletsDetails(fromAccount);
|
|
90
89
|
toAccount && getOneOfWalletsDetails(toAccount);
|
|
@@ -39,7 +39,7 @@ const WalletImageContainer = styled('div', {
|
|
|
39
39
|
marginRight: '$6',
|
|
40
40
|
'& img': {
|
|
41
41
|
borderRadius: '50%',
|
|
42
|
-
}
|
|
42
|
+
},
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
export type LayoutProps = {
|
|
@@ -48,10 +48,10 @@ export type LayoutProps = {
|
|
|
48
48
|
|
|
49
49
|
export function Layout({ config }: LayoutProps) {
|
|
50
50
|
const navigate = useNavigate();
|
|
51
|
-
const {
|
|
51
|
+
const { connectedWallets, selectedWallets } = useWalletsStore();
|
|
52
52
|
const { getWalletInfo } = useWallets();
|
|
53
53
|
const connectedWalletsImages = removeDuplicateFrom(
|
|
54
|
-
getSelectableWallets(
|
|
54
|
+
getSelectableWallets(connectedWallets, selectedWallets, getWalletInfo).map(
|
|
55
55
|
(w) => w.image
|
|
56
56
|
)
|
|
57
57
|
);
|
|
@@ -63,7 +63,7 @@ export function Layout({ config }: LayoutProps) {
|
|
|
63
63
|
const setInputAmount = useBestRouteStore.use.setInputAmount();
|
|
64
64
|
const setAffiliateRef = useSettingsStore.use.setAffiliateRef();
|
|
65
65
|
|
|
66
|
-
const totalBalance = calculateWalletUsdValue(
|
|
66
|
+
const totalBalance = calculateWalletUsdValue(connectedWallets);
|
|
67
67
|
const connectWalletsButtonDisabled =
|
|
68
68
|
useUiStore.use.connectWalletsButtonDisabled();
|
|
69
69
|
const loadingMetaStatus = useMetaStore.use.loadingStatus();
|
|
@@ -71,31 +71,35 @@ export function Layout({ config }: LayoutProps) {
|
|
|
71
71
|
|
|
72
72
|
const { t } = useTranslation();
|
|
73
73
|
useEffect(() => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
74
|
+
if (loadingMetaStatus === 'success') {
|
|
75
|
+
const chain = blockchains.find(
|
|
76
|
+
(chain) => chain.name === config?.to?.blockchain
|
|
77
|
+
);
|
|
78
|
+
const token = tokens.find((t) =>
|
|
79
|
+
tokensAreEqual(t, config?.to?.token || null)
|
|
80
|
+
);
|
|
81
|
+
setToChain(chain || null);
|
|
82
|
+
setToToken(token || null);
|
|
83
|
+
}
|
|
84
|
+
}, [config?.to?.token, config?.to?.blockchain, loadingMetaStatus]);
|
|
83
85
|
|
|
84
86
|
useEffect(() => {
|
|
85
87
|
setInputAmount(config?.amount?.toString() || '');
|
|
86
88
|
}, [config?.amount]);
|
|
87
89
|
|
|
88
90
|
useEffect(() => {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
if (loadingMetaStatus === 'success') {
|
|
92
|
+
const chain = blockchains.find(
|
|
93
|
+
(chain) => chain.name === config?.from?.blockchain
|
|
94
|
+
);
|
|
95
|
+
const token = tokens.find((t) =>
|
|
96
|
+
tokensAreEqual(t, config?.from?.token || null)
|
|
97
|
+
);
|
|
95
98
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
setFromChain(chain || null);
|
|
100
|
+
setFromToken(token || null);
|
|
101
|
+
}
|
|
102
|
+
}, [config?.from?.token, config?.from?.blockchain, loadingMetaStatus]);
|
|
99
103
|
|
|
100
104
|
useEffect(() => {
|
|
101
105
|
setAffiliateRef(config?.affiliateRef || null);
|
|
@@ -126,7 +130,7 @@ export function Layout({ config }: LayoutProps) {
|
|
|
126
130
|
)}
|
|
127
131
|
<div className="balance">
|
|
128
132
|
<Typography variant="body2">
|
|
129
|
-
{!
|
|
133
|
+
{!connectedWallets?.length
|
|
130
134
|
? t('Connect Wallet')
|
|
131
135
|
: `$${totalBalance || 0}`}
|
|
132
136
|
</Typography>
|
|
@@ -37,7 +37,7 @@ const Box = styled('div', {
|
|
|
37
37
|
display: 'flex',
|
|
38
38
|
flexDirection: 'column',
|
|
39
39
|
width: '100%',
|
|
40
|
-
overflow: 'hidden'
|
|
40
|
+
overflow: 'hidden',
|
|
41
41
|
});
|
|
42
42
|
|
|
43
43
|
const Container = styled('div', {
|
|
@@ -132,7 +132,7 @@ export function TokenInfo(props: PropTypes) {
|
|
|
132
132
|
const setInputAmount = useBestRouteStore.use.setInputAmount();
|
|
133
133
|
const bestRoute = useBestRouteStore.use.bestRoute();
|
|
134
134
|
const inputAmount = useBestRouteStore.use.inputAmount();
|
|
135
|
-
const
|
|
135
|
+
const connectedWallets = useWalletsStore.use.connectedWallets();
|
|
136
136
|
const fetchingBestRoute = useBestRouteStore.use.loading();
|
|
137
137
|
const navigate = useNavigate();
|
|
138
138
|
const { t } = useTranslation();
|
|
@@ -141,7 +141,7 @@ export function TokenInfo(props: PropTypes) {
|
|
|
141
141
|
!!fromChain && !!fromToken
|
|
142
142
|
? numberToString(
|
|
143
143
|
getBalanceFromWallet(
|
|
144
|
-
|
|
144
|
+
connectedWallets,
|
|
145
145
|
fromChain?.name,
|
|
146
146
|
fromToken?.symbol,
|
|
147
147
|
fromToken?.address
|
|
@@ -154,13 +154,13 @@ export function TokenInfo(props: PropTypes) {
|
|
|
154
154
|
!!fromChain && !!fromToken
|
|
155
155
|
? numberToString(
|
|
156
156
|
getBalanceFromWallet(
|
|
157
|
-
|
|
157
|
+
connectedWallets,
|
|
158
158
|
fromChain?.name,
|
|
159
159
|
fromToken?.symbol,
|
|
160
160
|
fromToken?.address
|
|
161
161
|
)?.amount || '0',
|
|
162
162
|
getBalanceFromWallet(
|
|
163
|
-
|
|
163
|
+
connectedWallets,
|
|
164
164
|
fromChain?.name,
|
|
165
165
|
fromToken?.symbol,
|
|
166
166
|
fromToken?.address
|
|
@@ -51,7 +51,7 @@ export function useConfirmSwap(): ConfirmSwap {
|
|
|
51
51
|
const inputUsdValue = useBestRouteStore.use.inputUsdValue();
|
|
52
52
|
const affiliateRef = useSettingsStore.use.affiliateRef();
|
|
53
53
|
|
|
54
|
-
const
|
|
54
|
+
const connectedWallets = useWalletsStore.use.connectedWallets();
|
|
55
55
|
const selectedWallets = useWalletsStore.use.selectedWallets();
|
|
56
56
|
const meta = useMetaStore.use.meta();
|
|
57
57
|
const customSlippage = useSettingsStore.use.customSlippage();
|
|
@@ -108,7 +108,7 @@ export function useConfirmSwap(): ConfirmSwap {
|
|
|
108
108
|
fromToken,
|
|
109
109
|
toToken,
|
|
110
110
|
inputAmount,
|
|
111
|
-
|
|
111
|
+
connectedWallets,
|
|
112
112
|
selectedWallets,
|
|
113
113
|
disabledLiquiditySources,
|
|
114
114
|
userSlippage,
|
package/src/index.tsx
CHANGED
|
@@ -1,14 +1,144 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { SwapContainer } from '@rango-dev/ui';
|
|
2
|
+
import React, { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { AppRouter } from './components/AppRouter';
|
|
4
|
+
import { useMetaStore } from './store/meta';
|
|
5
|
+
import { Events, Provider } from '@rango-dev/wallets-core';
|
|
6
|
+
import { allProviders } from '@rango-dev/provider-all';
|
|
7
|
+
import { EventHandler } from '@rango-dev/wallets-core/dist/wallet';
|
|
8
|
+
import { Network, WalletType } from '@rango-dev/wallets-shared';
|
|
9
|
+
import {
|
|
10
|
+
prepareAccountsForWalletStore,
|
|
11
|
+
walletAndSupportedChainsNames,
|
|
12
|
+
} from './utils/wallets';
|
|
13
|
+
import { useWalletsStore } from './store/wallets';
|
|
14
|
+
import { Layout } from './components/Layout';
|
|
15
|
+
import { globalFont } from './globalStyles';
|
|
16
|
+
import { useTheme } from './hooks/useTheme';
|
|
17
|
+
import { isEvmBlockchain } from 'rango-sdk';
|
|
18
|
+
import {
|
|
19
|
+
WidgetTheme,
|
|
20
|
+
WidgetConfig,
|
|
21
|
+
WidgetColors,
|
|
22
|
+
BlockchainAndTokenConfig,
|
|
23
|
+
} from './types';
|
|
24
|
+
import useSelectLanguage from './hooks/useSelectLanguage';
|
|
25
|
+
import './i18n';
|
|
26
|
+
import QueueManager from './QueueManager';
|
|
27
|
+
import { useUiStore } from './store/ui';
|
|
28
|
+
import { navigationRoutes } from './constants/navigationRoutes';
|
|
29
|
+
import { initConfig } from './utils/configs';
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
WidgetConfig,
|
|
33
|
+
WalletType,
|
|
34
|
+
WidgetTheme,
|
|
35
|
+
WidgetColors,
|
|
36
|
+
BlockchainAndTokenConfig,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type WidgetProps = {
|
|
40
|
+
config?: WidgetConfig;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const Widget: React.FC<WidgetProps> = ({ config }) => {
|
|
44
|
+
globalFont(config?.theme?.fontFamily || 'Roboto');
|
|
45
|
+
|
|
46
|
+
const { activeTheme } = useTheme({ ...config?.theme });
|
|
47
|
+
const { blockchains } = useMetaStore.use.meta();
|
|
48
|
+
const disconnectWallet = useWalletsStore.use.disconnectWallet();
|
|
49
|
+
const connectWallet = useWalletsStore.use.connectWallet();
|
|
50
|
+
const { changeLanguage } = useSelectLanguage();
|
|
51
|
+
const clearConnectedWallet = useWalletsStore.use.clearConnectedWallet();
|
|
52
|
+
const [lastConnectedWalletWithNetwork, setLastConnectedWalletWithNetwork] =
|
|
53
|
+
useState<string>('');
|
|
54
|
+
const [disconnectedWallet, setDisconnectedWallet] = useState<WalletType>();
|
|
55
|
+
const currentPage = useUiStore.use.currentPage();
|
|
56
|
+
|
|
57
|
+
const evmBasedChainNames = blockchains
|
|
58
|
+
.filter(isEvmBlockchain)
|
|
59
|
+
.map((chain) => chain.name);
|
|
60
|
+
|
|
61
|
+
useMemo(() => {
|
|
62
|
+
if (config?.apiKey) {
|
|
63
|
+
initConfig({
|
|
64
|
+
API_KEY: config?.apiKey,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}, [config]);
|
|
68
|
+
|
|
69
|
+
const onUpdateState: EventHandler = (
|
|
70
|
+
type,
|
|
71
|
+
event,
|
|
72
|
+
value,
|
|
73
|
+
state,
|
|
74
|
+
supportedChains
|
|
75
|
+
) => {
|
|
76
|
+
if (event === Events.ACCOUNTS) {
|
|
77
|
+
if (value) {
|
|
78
|
+
const supportedChainNames: Network[] | null =
|
|
79
|
+
walletAndSupportedChainsNames(supportedChains);
|
|
80
|
+
const data = prepareAccountsForWalletStore(
|
|
81
|
+
type,
|
|
82
|
+
value,
|
|
83
|
+
evmBasedChainNames,
|
|
84
|
+
supportedChainNames
|
|
85
|
+
);
|
|
86
|
+
connectWallet(data);
|
|
87
|
+
} else {
|
|
88
|
+
disconnectWallet(type);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (event === Events.ACCOUNTS && state.connected) {
|
|
92
|
+
const key = `${type}-${state.network}-${value}`;
|
|
93
|
+
|
|
94
|
+
if (state.connected) {
|
|
95
|
+
setLastConnectedWalletWithNetwork(key);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (event === Events.NETWORK && state.network) {
|
|
100
|
+
const key = `${type}-${state.network}`;
|
|
101
|
+
setLastConnectedWalletWithNetwork(key);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
let providers = allProviders();
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
const wallets = config?.wallets;
|
|
108
|
+
clearConnectedWallet();
|
|
109
|
+
providers = !wallets
|
|
110
|
+
? allProviders()
|
|
111
|
+
: allProviders().filter((provider) => {
|
|
112
|
+
const type = provider.config.type;
|
|
113
|
+
return wallets.find((w) => w === type);
|
|
114
|
+
});
|
|
115
|
+
}, [config?.wallets]);
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
changeLanguage(config?.language || 'en');
|
|
119
|
+
}, [config?.language]);
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<Provider
|
|
123
|
+
allBlockChains={blockchains}
|
|
124
|
+
providers={providers}
|
|
125
|
+
onUpdateState={onUpdateState}
|
|
126
|
+
>
|
|
127
|
+
<div id="swap-container" className={activeTheme}>
|
|
128
|
+
<QueueManager>
|
|
129
|
+
<SwapContainer fixedHeight={currentPage !== navigationRoutes.home}>
|
|
130
|
+
<AppRouter
|
|
131
|
+
lastConnectedWallet={lastConnectedWalletWithNetwork}
|
|
132
|
+
disconnectedWallet={disconnectedWallet}
|
|
133
|
+
clearDisconnectedWallet={() => {
|
|
134
|
+
setDisconnectedWallet(undefined);
|
|
135
|
+
}}
|
|
136
|
+
>
|
|
137
|
+
<Layout config={config} />
|
|
138
|
+
</AppRouter>
|
|
139
|
+
</SwapContainer>
|
|
140
|
+
</QueueManager>
|
|
141
|
+
</div>
|
|
142
|
+
</Provider>
|
|
143
|
+
);
|
|
144
|
+
};
|
|
@@ -40,7 +40,7 @@ export function ConfirmSwapPage() {
|
|
|
40
40
|
const setSelectedSwap = useUiStore.use.setSelectedSwap();
|
|
41
41
|
const { blockchains, tokens } = useMetaStore.use.meta();
|
|
42
42
|
const loadingMetaStatus = useMetaStore.use.loadingStatus();
|
|
43
|
-
const
|
|
43
|
+
const connectedWallets = useWalletsStore.use.connectedWallets();
|
|
44
44
|
const selectedWallets = useWalletsStore.use.selectedWallets();
|
|
45
45
|
const initSelectedWallets = useWalletsStore.use.initSelectedWallets();
|
|
46
46
|
const setSelectedWallet = useWalletsStore.use.setSelectedWallet();
|
|
@@ -87,7 +87,7 @@ export function ConfirmSwapPage() {
|
|
|
87
87
|
}, []);
|
|
88
88
|
|
|
89
89
|
const selectableWallets = getSelectableWallets(
|
|
90
|
-
|
|
90
|
+
connectedWallets,
|
|
91
91
|
selectedWallets,
|
|
92
92
|
getWalletInfo
|
|
93
93
|
);
|
package/src/pages/Home.tsx
CHANGED
|
@@ -83,7 +83,7 @@ export function Home() {
|
|
|
83
83
|
const fetchingBestRoute = useBestRouteStore.use.loading();
|
|
84
84
|
const bestRouteError = useBestRouteStore.use.error();
|
|
85
85
|
const loadingMetaStatus = useMetaStore.use.loadingStatus();
|
|
86
|
-
const
|
|
86
|
+
const connectedWallets = useWalletsStore.use.connectedWallets();
|
|
87
87
|
const setCurrentPage = useUiStore.use.setCurrentPage();
|
|
88
88
|
const switchFromAndTo = useBestRouteStore.use.switchFromAndTo();
|
|
89
89
|
|
|
@@ -113,7 +113,7 @@ export function Home() {
|
|
|
113
113
|
|
|
114
114
|
const swapButtonState = getSwapButtonState(
|
|
115
115
|
loadingMetaStatus,
|
|
116
|
-
|
|
116
|
+
connectedWallets,
|
|
117
117
|
fetchingBestRoute,
|
|
118
118
|
bestRoute,
|
|
119
119
|
hasLimitError(bestRoute),
|
|
@@ -139,7 +139,9 @@ export function Home() {
|
|
|
139
139
|
title="SWAP"
|
|
140
140
|
suffix={
|
|
141
141
|
<HeaderButtons
|
|
142
|
-
onClickRefresh={
|
|
142
|
+
onClickRefresh={
|
|
143
|
+
!!bestRoute || bestRouteError ? fetchBestRoute : undefined
|
|
144
|
+
}
|
|
143
145
|
/>
|
|
144
146
|
}
|
|
145
147
|
/>
|
package/src/store/bestRoute.ts
CHANGED
|
@@ -105,11 +105,11 @@ export const useBestRouteStore = createSelectors(
|
|
|
105
105
|
set((state) => {
|
|
106
106
|
if (state.fromChain?.name === chain?.name) return {};
|
|
107
107
|
const tokens = useMetaStore.getState().meta.tokens;
|
|
108
|
-
const
|
|
108
|
+
const connectedWallets = useWalletsStore.getState().connectedWallets;
|
|
109
109
|
const sortedTokens = getSortedTokens(
|
|
110
110
|
chain,
|
|
111
111
|
tokens,
|
|
112
|
-
|
|
112
|
+
connectedWallets,
|
|
113
113
|
state.destinationTokens
|
|
114
114
|
);
|
|
115
115
|
const fromToken = getDefaultToken(sortedTokens, state.toToken);
|
|
@@ -136,11 +136,11 @@ export const useBestRouteStore = createSelectors(
|
|
|
136
136
|
set((state) => {
|
|
137
137
|
if (state.toChain?.name === chain?.name) return {};
|
|
138
138
|
const tokens = useMetaStore.getState().meta.tokens;
|
|
139
|
-
const
|
|
139
|
+
const connectedWallets = useWalletsStore.getState().connectedWallets;
|
|
140
140
|
const sortedTokens = getSortedTokens(
|
|
141
141
|
chain,
|
|
142
142
|
tokens,
|
|
143
|
-
|
|
143
|
+
connectedWallets,
|
|
144
144
|
state.destinationTokens
|
|
145
145
|
);
|
|
146
146
|
|
|
@@ -173,7 +173,7 @@ export const useBestRouteStore = createSelectors(
|
|
|
173
173
|
},
|
|
174
174
|
retry: (pendingSwap) => {
|
|
175
175
|
const { tokens, blockchains } = useMetaStore.getState().meta;
|
|
176
|
-
const
|
|
176
|
+
const connectedWallets = useWalletsStore.getState().connectedWallets;
|
|
177
177
|
const failedIndex =
|
|
178
178
|
pendingSwap.status === 'failed'
|
|
179
179
|
? pendingSwap.steps.findIndex((s) => s.status === 'failed')
|
|
@@ -210,13 +210,13 @@ export const useBestRouteStore = createSelectors(
|
|
|
210
210
|
const sortedSourceTokens = getSortedTokens(
|
|
211
211
|
fromChain,
|
|
212
212
|
tokens,
|
|
213
|
-
|
|
213
|
+
connectedWallets,
|
|
214
214
|
[]
|
|
215
215
|
);
|
|
216
216
|
const sortedDestinationTokens = getSortedTokens(
|
|
217
217
|
toChain,
|
|
218
218
|
tokens,
|
|
219
|
-
|
|
219
|
+
connectedWallets,
|
|
220
220
|
[]
|
|
221
221
|
);
|
|
222
222
|
const inputAmount = pendingSwap.inputAmount;
|
package/src/store/wallets.ts
CHANGED
|
@@ -2,26 +2,20 @@ import { SelectableWallet } from '@rango-dev/ui';
|
|
|
2
2
|
import { WalletType } from '@rango-dev/wallets-shared';
|
|
3
3
|
import { create } from 'zustand';
|
|
4
4
|
import { subscribeWithSelector } from 'zustand/middleware';
|
|
5
|
+
import { shallow } from 'zustand/shallow';
|
|
5
6
|
import { httpService } from '../services/httpService';
|
|
6
7
|
import {
|
|
7
8
|
getRequiredChains,
|
|
8
9
|
getTokensWithBalance,
|
|
9
|
-
|
|
10
|
+
isAccountAndWalletMatched,
|
|
10
11
|
makeBalanceFor,
|
|
11
|
-
|
|
12
|
-
SelectedWallet,
|
|
12
|
+
resetConnectedWalletState,
|
|
13
13
|
sortTokens,
|
|
14
14
|
} from '../utils/wallets';
|
|
15
15
|
import { useBestRouteStore } from './bestRoute';
|
|
16
16
|
import { useMetaStore } from './meta';
|
|
17
17
|
import createSelectors from './selectors';
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
export interface Account {
|
|
21
|
-
chain: string;
|
|
22
|
-
address: string;
|
|
23
|
-
walletType: WalletType;
|
|
24
|
-
}
|
|
18
|
+
import { Wallet } from '../types';
|
|
25
19
|
|
|
26
20
|
export type TokenBalance = {
|
|
27
21
|
chain: string;
|
|
@@ -35,39 +29,33 @@ export type TokenBalance = {
|
|
|
35
29
|
usdPrice: number | null;
|
|
36
30
|
};
|
|
37
31
|
|
|
38
|
-
export interface
|
|
32
|
+
export interface ConnectedWallet extends Wallet {
|
|
39
33
|
balances: TokenBalance[] | null;
|
|
40
|
-
address: string;
|
|
41
|
-
chain: string;
|
|
42
34
|
loading: boolean;
|
|
43
|
-
walletType: WalletType;
|
|
44
35
|
error: boolean;
|
|
45
36
|
explorerUrl: string | null;
|
|
46
37
|
}
|
|
47
38
|
|
|
48
39
|
interface WalletsStore {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
connectWallet: (accounts: Account[]) => void;
|
|
40
|
+
connectedWallets: ConnectedWallet[];
|
|
41
|
+
selectedWallets: Wallet[];
|
|
42
|
+
connectWallet: (accounts: Wallet[]) => void;
|
|
53
43
|
disconnectWallet: (walletType: WalletType) => void;
|
|
54
44
|
initSelectedWallets: () => void;
|
|
55
45
|
setSelectedWallet: (wallet: SelectableWallet) => void;
|
|
56
46
|
clearConnectedWallet: () => void;
|
|
57
|
-
getOneOfWalletsDetails: (account:
|
|
47
|
+
getOneOfWalletsDetails: (account: Wallet) => void;
|
|
58
48
|
}
|
|
59
49
|
|
|
60
50
|
export const useWalletsStore = createSelectors(
|
|
61
51
|
create<WalletsStore>()(
|
|
62
52
|
subscribeWithSelector((set, get) => ({
|
|
63
|
-
|
|
64
|
-
balances: [],
|
|
53
|
+
connectedWallets: [],
|
|
65
54
|
selectedWallets: [],
|
|
66
55
|
connectWallet: (accounts) => {
|
|
67
56
|
const getOneOfWalletsDetails = get().getOneOfWalletsDetails;
|
|
68
57
|
set((state) => ({
|
|
69
|
-
|
|
70
|
-
balances: state.balances.concat(
|
|
58
|
+
connectedWallets: state.connectedWallets.concat(
|
|
71
59
|
accounts.map((account) => ({
|
|
72
60
|
balances: [],
|
|
73
61
|
address: account.address,
|
|
@@ -83,10 +71,7 @@ export const useWalletsStore = createSelectors(
|
|
|
83
71
|
},
|
|
84
72
|
disconnectWallet: (walletType) => {
|
|
85
73
|
set((state) => ({
|
|
86
|
-
|
|
87
|
-
(account) => account.walletType !== walletType
|
|
88
|
-
),
|
|
89
|
-
balances: state.balances.filter(
|
|
74
|
+
connectedWallets: state.connectedWallets.filter(
|
|
90
75
|
(balance) => balance.walletType !== walletType
|
|
91
76
|
),
|
|
92
77
|
selectedWallets: state.selectedWallets.filter(
|
|
@@ -99,8 +84,8 @@ export const useWalletsStore = createSelectors(
|
|
|
99
84
|
const requiredChains = getRequiredChains(
|
|
100
85
|
useBestRouteStore.getState().bestRoute
|
|
101
86
|
);
|
|
102
|
-
const connectedWallets = state.
|
|
103
|
-
const selectedWallets:
|
|
87
|
+
const connectedWallets = state.connectedWallets;
|
|
88
|
+
const selectedWallets: Wallet[] = [];
|
|
104
89
|
requiredChains.forEach((chain) => {
|
|
105
90
|
const anyWalletSelected = !!state.selectedWallets.find(
|
|
106
91
|
(wallet) => wallet.chain === chain
|
|
@@ -134,14 +119,13 @@ export const useWalletsStore = createSelectors(
|
|
|
134
119
|
|
|
135
120
|
clearConnectedWallet: () =>
|
|
136
121
|
set(() => ({
|
|
137
|
-
|
|
138
|
-
balances: [],
|
|
122
|
+
connectedWallets: [],
|
|
139
123
|
selectedWallets: [],
|
|
140
124
|
})),
|
|
141
|
-
getOneOfWalletsDetails: async (account:
|
|
125
|
+
getOneOfWalletsDetails: async (account: Wallet) => {
|
|
142
126
|
const tokens = useMetaStore.getState().meta.tokens;
|
|
143
127
|
set((state) => ({
|
|
144
|
-
|
|
128
|
+
connectedWallets: state.connectedWallets.map((balance) => {
|
|
145
129
|
return balance.address === account.address &&
|
|
146
130
|
balance.chain === account.chain
|
|
147
131
|
? { ...balance, loading: true }
|
|
@@ -155,18 +139,20 @@ export const useWalletsStore = createSelectors(
|
|
|
155
139
|
const retrivedBalance = response.wallets[0];
|
|
156
140
|
if (retrivedBalance) {
|
|
157
141
|
set((state) => ({
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
142
|
+
connectedWallets: state.connectedWallets.map(
|
|
143
|
+
(connectedWallet) => {
|
|
144
|
+
return isAccountAndWalletMatched(account, connectedWallet)
|
|
145
|
+
? makeBalanceFor(account, retrivedBalance, tokens)
|
|
146
|
+
: connectedWallet;
|
|
147
|
+
}
|
|
148
|
+
),
|
|
163
149
|
}));
|
|
164
150
|
} else throw new Error('Wallet not found');
|
|
165
151
|
} catch (error) {
|
|
166
152
|
set((state) => ({
|
|
167
|
-
|
|
168
|
-
return
|
|
169
|
-
?
|
|
153
|
+
connectedWallets: state.connectedWallets.map((balance) => {
|
|
154
|
+
return isAccountAndWalletMatched(account, balance)
|
|
155
|
+
? resetConnectedWalletState(balance)
|
|
170
156
|
: balance;
|
|
171
157
|
}),
|
|
172
158
|
}));
|
|
@@ -177,16 +163,16 @@ export const useWalletsStore = createSelectors(
|
|
|
177
163
|
);
|
|
178
164
|
|
|
179
165
|
useWalletsStore.subscribe(
|
|
180
|
-
(state) => state.
|
|
181
|
-
(
|
|
166
|
+
(state) => state.connectedWallets,
|
|
167
|
+
(connectedWallets) => {
|
|
182
168
|
useBestRouteStore.setState(({ sourceTokens, destinationTokens }) => {
|
|
183
169
|
const sourceTokensWithBalance = getTokensWithBalance(
|
|
184
170
|
sourceTokens,
|
|
185
|
-
|
|
171
|
+
connectedWallets
|
|
186
172
|
);
|
|
187
173
|
const destinationTokensWithBalance = getTokensWithBalance(
|
|
188
174
|
destinationTokens,
|
|
189
|
-
|
|
175
|
+
connectedWallets
|
|
190
176
|
);
|
|
191
177
|
return {
|
|
192
178
|
sourceTokens: sortTokens(sourceTokensWithBalance),
|
|
@@ -200,4 +186,4 @@ useWalletsStore.subscribe(
|
|
|
200
186
|
);
|
|
201
187
|
|
|
202
188
|
export const fetchingBalanceSelector = (state: WalletsStore) =>
|
|
203
|
-
!!state.
|
|
189
|
+
!!state.connectedWallets.find((wallet) => wallet.loading);
|
package/src/types/index.ts
CHANGED
package/src/utils/routing.ts
CHANGED
|
@@ -5,8 +5,7 @@ import {
|
|
|
5
5
|
import BigNumber from 'bignumber.js';
|
|
6
6
|
import { BestRouteResponse, BlockchainMeta, Token } from 'rango-sdk';
|
|
7
7
|
import { areEqual } from './common';
|
|
8
|
-
import {
|
|
9
|
-
import { BestRouteEqualityParams } from '../types';
|
|
8
|
+
import { BestRouteEqualityParams, Wallet } from '../types';
|
|
10
9
|
import { numberToString } from './numbers';
|
|
11
10
|
import { PendingSwap } from '@rango-dev/queue-manager-rango-preset';
|
|
12
11
|
|
|
@@ -96,7 +95,7 @@ export function outToRatioHasWarning(
|
|
|
96
95
|
}
|
|
97
96
|
|
|
98
97
|
export function getRequiredBalanceOfWallet(
|
|
99
|
-
selectedWallet:
|
|
98
|
+
selectedWallet: Wallet,
|
|
100
99
|
fee: SimulationValidationStatus[] | null
|
|
101
100
|
): SimulationAssetAndAmount[] | null {
|
|
102
101
|
if (fee === null) return null;
|