@rango-dev/widget-embedded 0.37.0 → 0.37.1-next.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/CHANGELOG.md +4 -0
- package/dist/QueueManager.d.ts.map +1 -1
- package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
- package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
- package/dist/components/Layout/Layout.d.ts.map +1 -1
- package/dist/components/Quote/Quote.d.ts.map +1 -1
- package/dist/components/Quote/Quote.styles.d.ts +1 -1
- package/dist/components/Quote/Quote.types.d.ts +2 -0
- package/dist/components/Quote/Quote.types.d.ts.map +1 -1
- package/dist/components/Quote/QuoteCostDetails.d.ts.map +1 -1
- package/dist/components/Quote/QuoteCostDetails.styles.d.ts.map +1 -1
- package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.d.ts.map +1 -1
- package/dist/components/Quotes/Quotes.d.ts.map +1 -1
- package/dist/components/RefreshModal/RefreshModal.styles.d.ts +1 -1
- package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
- package/dist/components/SwapDetailsAlerts/SwapDetailsAlerts.types.d.ts +1 -1
- package/dist/components/SwapDetailsAlerts/SwapDetailsAlerts.types.d.ts.map +1 -1
- package/dist/components/SwapDetailsModal/SwapDetailsCompleteModal.d.ts.map +1 -1
- package/dist/components/TokenList/TokenList.d.ts.map +1 -1
- package/dist/components/WalletStatefulConnect/DerivationPath.helpers.d.ts +2 -1
- package/dist/components/WalletStatefulConnect/DerivationPath.helpers.d.ts.map +1 -1
- package/dist/components/WalletStatefulConnect/Namespaces.d.ts.map +1 -1
- package/dist/components/WalletStatefulConnect/Namespaces.types.d.ts +1 -1
- package/dist/components/WalletStatefulConnect/Namespaces.types.d.ts.map +1 -1
- package/dist/constants/quote.d.ts +10 -0
- package/dist/constants/quote.d.ts.map +1 -1
- package/dist/containers/ExpandedQuotes/ExpandedQuotes.styles.d.ts.map +1 -1
- package/dist/containers/Inputs/Inputs.d.ts.map +1 -1
- package/dist/containers/QuoteInfo/QuoteInfo.d.ts.map +1 -1
- package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.d.ts.map +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.helpers.d.ts +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts +3 -3
- package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts.map +1 -1
- package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts.map +1 -1
- package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts +6 -4
- package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts.map +1 -1
- package/dist/hooks/useFetchApiConfig.d.ts.map +1 -1
- package/dist/hooks/useFetchCustomToken.d.ts.map +1 -1
- package/dist/hooks/useStatefulConnect/useStatefulConnect.d.ts +2 -1
- package/dist/hooks/useStatefulConnect/useStatefulConnect.d.ts.map +1 -1
- package/dist/hooks/useStatefulConnect/useStatefulConnect.types.d.ts +3 -5
- package/dist/hooks/useStatefulConnect/useStatefulConnect.types.d.ts.map +1 -1
- package/dist/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.d.ts.map +1 -1
- package/dist/hooks/useSwapInput.d.ts.map +1 -1
- package/dist/hooks/useWalletList.d.ts.map +1 -1
- package/dist/hooks/useWalletProviders/useWalletProviders.d.ts +2 -3
- package/dist/hooks/useWalletProviders/useWalletProviders.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
- package/dist/pages/Home.d.ts.map +1 -1
- package/dist/pages/SelectSwapItemsPage.d.ts.map +1 -1
- package/dist/store/app.d.ts +2 -4
- package/dist/store/app.d.ts.map +1 -1
- package/dist/store/quote.d.ts.map +1 -1
- package/dist/store/slices/config.d.ts +4 -0
- package/dist/store/slices/config.d.ts.map +1 -1
- package/dist/store/slices/data.d.ts.map +1 -1
- package/dist/store/slices/types.d.ts +6 -0
- package/dist/store/slices/types.d.ts.map +1 -0
- package/dist/store/slices/wallets.d.ts +80 -0
- package/dist/store/slices/wallets.d.ts.map +1 -0
- package/dist/store/ui.d.ts +4 -0
- package/dist/store/ui.d.ts.map +1 -1
- package/dist/store/utils/wallets.d.ts +17 -0
- package/dist/store/utils/wallets.d.ts.map +1 -0
- package/dist/types/config.d.ts +6 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/quote.d.ts +8 -17
- package/dist/types/quote.d.ts.map +1 -1
- package/dist/types/wallets.d.ts +3 -7
- package/dist/types/wallets.d.ts.map +1 -1
- package/dist/utils/providers.d.ts +8 -9
- package/dist/utils/providers.d.ts.map +1 -1
- package/dist/utils/quote.d.ts +5 -6
- package/dist/utils/quote.d.ts.map +1 -1
- package/dist/utils/settings.d.ts +1 -0
- package/dist/utils/settings.d.ts.map +1 -1
- package/dist/utils/swap.d.ts +5 -5
- package/dist/utils/swap.d.ts.map +1 -1
- package/dist/utils/wallets.d.ts +12 -14
- package/dist/utils/wallets.d.ts.map +1 -1
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +9 -9
- package/src/QueueManager.tsx +17 -11
- package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +5 -24
- package/src/components/ConfirmWalletsModal/WalletList.tsx +10 -7
- package/src/components/Layout/Layout.tsx +1 -3
- package/src/components/Quote/Quote.tsx +22 -3
- package/src/components/Quote/Quote.types.ts +2 -0
- package/src/components/Quote/QuoteCostDetails.styles.ts +0 -1
- package/src/components/Quote/QuoteCostDetails.tsx +23 -4
- package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.ts +42 -22
- package/src/components/Quotes/Quotes.tsx +2 -3
- package/src/components/StatefulConnectModal/StatefulConnectModal.tsx +1 -1
- package/src/components/SwapDetails/SwapDetails.tsx +14 -6
- package/src/components/SwapDetailsAlerts/SwapDetailsAlerts.types.ts +1 -1
- package/src/components/SwapDetailsModal/SwapDetailsCompleteModal.tsx +26 -13
- package/src/components/TokenList/TokenList.tsx +4 -27
- package/src/components/WalletStatefulConnect/DerivationPath.helpers.ts +2 -1
- package/src/components/WalletStatefulConnect/Namespaces.tsx +31 -38
- package/src/components/WalletStatefulConnect/Namespaces.types.tsx +1 -1
- package/src/constants/quote.ts +25 -0
- package/src/containers/ExpandedQuotes/ExpandedQuotes.styles.ts +1 -0
- package/src/containers/ExpandedQuotes/ExpandedQuotes.tsx +1 -1
- package/src/containers/Inputs/Inputs.tsx +20 -7
- package/src/containers/QuoteInfo/QuoteInfo.tsx +3 -9
- package/src/containers/Wallets/Wallets.tsx +82 -16
- package/src/containers/WidgetInfo/WidgetInfo.helpers.ts +2 -2
- package/src/containers/WidgetInfo/WidgetInfo.tsx +10 -8
- package/src/containers/WidgetInfo/WidgetInfo.types.ts +3 -3
- package/src/hooks/useConfirmSwap/useConfirmSwap.helpers.ts +29 -66
- package/src/hooks/useConfirmSwap/useConfirmSwap.ts +10 -15
- package/src/hooks/useFetchApiConfig.ts +5 -1
- package/src/hooks/useFetchCustomToken.ts +5 -0
- package/src/hooks/useStatefulConnect/useStatefulConnect.ts +50 -17
- package/src/hooks/useStatefulConnect/useStatefulConnect.types.ts +3 -5
- package/src/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.ts +7 -6
- package/src/hooks/useSwapInput.ts +3 -11
- package/src/hooks/useWalletList.ts +6 -8
- package/src/hooks/useWalletProviders/useWalletProviders.ts +8 -12
- package/src/index.ts +1 -1
- package/src/pages/ConfirmSwapPage.tsx +7 -15
- package/src/pages/Home.tsx +5 -3
- package/src/pages/SelectSwapItemsPage.tsx +1 -2
- package/src/store/app.ts +4 -4
- package/src/store/quote.ts +3 -13
- package/src/store/slices/config.ts +76 -0
- package/src/store/slices/data.test.ts +23 -1
- package/src/store/slices/data.ts +3 -2
- package/src/store/slices/types.ts +9 -0
- package/src/store/slices/wallets.ts +577 -0
- package/src/store/ui.ts +6 -0
- package/src/store/utils/wallets.ts +111 -0
- package/src/types/config.ts +7 -3
- package/src/types/quote.ts +10 -24
- package/src/types/wallets.ts +3 -7
- package/src/utils/providers.ts +88 -36
- package/src/utils/quote.ts +57 -45
- package/src/utils/settings.ts +4 -0
- package/src/utils/swap.ts +56 -37
- package/src/utils/wallets.ts +76 -141
- package/dist/constants/warnings.d.ts +0 -3
- package/dist/constants/warnings.d.ts.map +0 -1
- package/dist/hooks/useObserveBalanceChanges/index.d.ts +0 -2
- package/dist/hooks/useObserveBalanceChanges/index.d.ts.map +0 -1
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts +0 -4
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts.map +0 -1
- package/dist/store/wallets.d.ts +0 -66
- package/dist/store/wallets.d.ts.map +0 -1
- package/src/constants/warnings.ts +0 -26
- package/src/hooks/useObserveBalanceChanges/index.ts +0 -1
- package/src/hooks/useObserveBalanceChanges/useObserveBalanceChanges.ts +0 -68
- package/src/store/wallets.ts +0 -268
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import type { AppStoreState } from './types';
|
|
2
|
+
import type { Token } from 'rango-sdk';
|
|
3
|
+
import type { StateCreator } from 'zustand';
|
|
4
|
+
|
|
5
|
+
import BigNumber from 'bignumber.js';
|
|
6
|
+
|
|
7
|
+
import { ZERO } from '../../constants/numbers';
|
|
8
|
+
import { eventEmitter } from '../../services/eventEmitter';
|
|
9
|
+
import { httpService } from '../../services/httpService';
|
|
10
|
+
import {
|
|
11
|
+
type Balance,
|
|
12
|
+
type Wallet,
|
|
13
|
+
WalletEventTypes,
|
|
14
|
+
WidgetEvents,
|
|
15
|
+
} from '../../types';
|
|
16
|
+
import { isAccountAndWalletMatched } from '../../utils/wallets';
|
|
17
|
+
import {
|
|
18
|
+
createAssetKey,
|
|
19
|
+
createBalanceKey,
|
|
20
|
+
createBalanceStateForNewAccount,
|
|
21
|
+
extractAssetFromBalanceKey,
|
|
22
|
+
removeBalanceFromAggregatedBalance,
|
|
23
|
+
updateAggregatedBalanceStateForNewAccount,
|
|
24
|
+
} from '../utils/wallets';
|
|
25
|
+
|
|
26
|
+
type WalletAddress = string;
|
|
27
|
+
type TokenAddress = string;
|
|
28
|
+
type TokenSymbol = string;
|
|
29
|
+
type BlockchainId = string;
|
|
30
|
+
/** format: `BlockchainId-TokenAddress-TokenSymbol` */
|
|
31
|
+
export type AssetKey = `${BlockchainId}-${TokenAddress}-${TokenSymbol}`;
|
|
32
|
+
/** format: `BlockchainId-TokenAddress-TokenSymbol-WalletAddress` */
|
|
33
|
+
export type BalanceKey =
|
|
34
|
+
`${BlockchainId}-${TokenAddress}-${TokenSymbol}-${WalletAddress}`;
|
|
35
|
+
|
|
36
|
+
export type BalanceState = {
|
|
37
|
+
[key: BalanceKey]: Balance;
|
|
38
|
+
};
|
|
39
|
+
export type AggregatedBalanceState = {
|
|
40
|
+
[key: AssetKey]: BalanceKey[];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export interface ConnectedWallet extends Wallet {
|
|
44
|
+
explorerUrl: string | null;
|
|
45
|
+
selected: boolean;
|
|
46
|
+
loading: boolean;
|
|
47
|
+
error: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface DeprecatedTokenBalance {
|
|
51
|
+
chain: string;
|
|
52
|
+
symbol: string;
|
|
53
|
+
ticker: string;
|
|
54
|
+
address: string | null;
|
|
55
|
+
rawAmount: string;
|
|
56
|
+
decimal: number | null;
|
|
57
|
+
amount: string;
|
|
58
|
+
logo: string | null;
|
|
59
|
+
usdPrice: number | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface DeprecatedWalletDetail extends ConnectedWallet {
|
|
63
|
+
balances: DeprecatedTokenBalance[] | null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface WalletsSlice {
|
|
67
|
+
_balances: BalanceState;
|
|
68
|
+
_aggregatedBalances: AggregatedBalanceState;
|
|
69
|
+
connectedWallets: ConnectedWallet[];
|
|
70
|
+
fetchingWallets: boolean;
|
|
71
|
+
|
|
72
|
+
setConnectedWalletAsRefetching: (walletType: string) => void;
|
|
73
|
+
setConnectedWalletHasError: (walletType: string) => void;
|
|
74
|
+
setConnectedWalletRetrievedData: (walletType: string) => void;
|
|
75
|
+
removeBalancesForWallet: (
|
|
76
|
+
walletType: string,
|
|
77
|
+
options?: {
|
|
78
|
+
chains?: string[];
|
|
79
|
+
}
|
|
80
|
+
) => void;
|
|
81
|
+
addConnectedWallet: (accounts: Wallet[]) => void;
|
|
82
|
+
setWalletsAsSelected: (
|
|
83
|
+
wallets: { walletType: string; chain: string }[]
|
|
84
|
+
) => void;
|
|
85
|
+
/**
|
|
86
|
+
* Add new accounts to store and fetch balances for them.
|
|
87
|
+
*/
|
|
88
|
+
newWalletConnected: (accounts: Wallet[]) => Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Disconnect a wallet and clean up balances after that.
|
|
91
|
+
*/
|
|
92
|
+
disconnectWallet: (walletType: string) => void;
|
|
93
|
+
clearConnectedWallet: () => void;
|
|
94
|
+
fetchBalances: (
|
|
95
|
+
accounts: Wallet[],
|
|
96
|
+
options?: { retryOnFailedBalances?: boolean }
|
|
97
|
+
) => Promise<void>;
|
|
98
|
+
getBalanceFor: (token: Token) => Balance | null;
|
|
99
|
+
getBalances: () => BalanceState;
|
|
100
|
+
getBalancesForWalletAddress: (address: string) => BalanceState;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @deprecated
|
|
104
|
+
* This is been added for backward compatibilty for WidgetInfo
|
|
105
|
+
*/
|
|
106
|
+
getConnectedWalletsDetails: () => DeprecatedWalletDetail[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const createWalletsSlice: StateCreator<
|
|
110
|
+
AppStoreState,
|
|
111
|
+
[],
|
|
112
|
+
[],
|
|
113
|
+
WalletsSlice
|
|
114
|
+
> = (set, get) => ({
|
|
115
|
+
_balances: {},
|
|
116
|
+
_aggregatedBalances: {},
|
|
117
|
+
connectedWallets: [],
|
|
118
|
+
fetchingWallets: false,
|
|
119
|
+
|
|
120
|
+
// Actions
|
|
121
|
+
setConnectedWalletAsRefetching: (walletType: string) => {
|
|
122
|
+
set((state) => {
|
|
123
|
+
return {
|
|
124
|
+
fetchingWallets: true,
|
|
125
|
+
connectedWallets: state.connectedWallets.map((connectedWallet) => {
|
|
126
|
+
if (connectedWallet.walletType === walletType) {
|
|
127
|
+
return {
|
|
128
|
+
...connectedWallet,
|
|
129
|
+
loading: true,
|
|
130
|
+
error: false,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return connectedWallet;
|
|
135
|
+
}),
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
setConnectedWalletRetrievedData: (walletType: string) => {
|
|
140
|
+
set((state) => {
|
|
141
|
+
return {
|
|
142
|
+
fetchingWallets: false,
|
|
143
|
+
connectedWallets: state.connectedWallets.map((connectedWallet) => {
|
|
144
|
+
if (connectedWallet.walletType === walletType) {
|
|
145
|
+
return {
|
|
146
|
+
...connectedWallet,
|
|
147
|
+
loading: false,
|
|
148
|
+
error: false,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return connectedWallet;
|
|
153
|
+
}),
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
setConnectedWalletHasError: (walletType: string) => {
|
|
158
|
+
set((state) => {
|
|
159
|
+
return {
|
|
160
|
+
fetchingWallets: false,
|
|
161
|
+
connectedWallets: state.connectedWallets.map((connectedWallet) => {
|
|
162
|
+
if (connectedWallet.walletType === walletType) {
|
|
163
|
+
return {
|
|
164
|
+
...connectedWallet,
|
|
165
|
+
loading: false,
|
|
166
|
+
error: true,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return connectedWallet;
|
|
171
|
+
}),
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
addConnectedWallet: (accounts: Wallet[]) => {
|
|
176
|
+
/*
|
|
177
|
+
* When we are going to add a new account, there are two thing that can be happens:
|
|
178
|
+
* 1. Wallet hasn't add yet.
|
|
179
|
+
* 2. Wallet has added, and there are some more account that needs to added to connected wallet. consider we've added an ETH and Pol account, then we need to add Arb account later as well.
|
|
180
|
+
*
|
|
181
|
+
* For handling this, we need to only keep not-added-account, then only add those.
|
|
182
|
+
*
|
|
183
|
+
* Note:
|
|
184
|
+
* The second option would be useful for hub particularly.
|
|
185
|
+
*/
|
|
186
|
+
const connectedWallets = get().connectedWallets;
|
|
187
|
+
const walletsNeedToBeAdded = accounts.filter(
|
|
188
|
+
(account) =>
|
|
189
|
+
!connectedWallets.some((connectedWallet) =>
|
|
190
|
+
isAccountAndWalletMatched(account, connectedWallet)
|
|
191
|
+
)
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
if (walletsNeedToBeAdded.length > 0) {
|
|
195
|
+
const newConnectedWallets = walletsNeedToBeAdded.map((account) => {
|
|
196
|
+
/*
|
|
197
|
+
* When a wallet is connecting, we will check if there is any `selected` wallet before, if not, we will consider this new wallet as connected.
|
|
198
|
+
* In this way, when user tries to swap, we selected a wallet by default and don't need to do an extra click in ConfirmWalletModal
|
|
199
|
+
*/
|
|
200
|
+
const shouldMarkWalletAsSelected = !connectedWallets.some(
|
|
201
|
+
(connectedWallet) =>
|
|
202
|
+
connectedWallet.chain === account.chain &&
|
|
203
|
+
connectedWallet.selected &&
|
|
204
|
+
/**
|
|
205
|
+
* Sometimes, the connect function can be called multiple times for a particular wallet type when using the auto-connect feature.
|
|
206
|
+
* This check is there to make sure the chosen wallet doesn't end up unselected.
|
|
207
|
+
*/
|
|
208
|
+
connectedWallet.walletType !== account.walletType
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
address: account.address,
|
|
213
|
+
chain: account.chain,
|
|
214
|
+
explorerUrl: null,
|
|
215
|
+
walletType: account.walletType,
|
|
216
|
+
selected: shouldMarkWalletAsSelected,
|
|
217
|
+
|
|
218
|
+
loading: false,
|
|
219
|
+
error: false,
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
set((state) => {
|
|
224
|
+
/*
|
|
225
|
+
* If wallet connected before and only need to update the address we should remove the old value and then add new conncted value.
|
|
226
|
+
* This scenario happens when user wants to change account inside the wallet.
|
|
227
|
+
* So the assumption here is the wallet has only one active address for a blockchain at the moment.
|
|
228
|
+
*/
|
|
229
|
+
const connectedWalletsWithoutSameWalletAndBlockchain =
|
|
230
|
+
state.connectedWallets.filter((currentConnectedWallet) => {
|
|
231
|
+
return !newConnectedWallets.some(
|
|
232
|
+
(newConnectedWallet) =>
|
|
233
|
+
newConnectedWallet.walletType ===
|
|
234
|
+
currentConnectedWallet.walletType &&
|
|
235
|
+
newConnectedWallet.chain === currentConnectedWallet.chain
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
connectedWallets: [
|
|
241
|
+
...connectedWalletsWithoutSameWalletAndBlockchain,
|
|
242
|
+
...newConnectedWallets,
|
|
243
|
+
],
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
setWalletsAsSelected: (wallets) => {
|
|
249
|
+
const nextConnectedWalletsWithUpdatedSelectedStatus =
|
|
250
|
+
get().connectedWallets.map((connectedWallet) => {
|
|
251
|
+
const walletSelected = !!wallets.find(
|
|
252
|
+
(wallet) =>
|
|
253
|
+
wallet.chain === connectedWallet.chain &&
|
|
254
|
+
wallet.walletType !== connectedWallet.walletType &&
|
|
255
|
+
connectedWallet.selected
|
|
256
|
+
);
|
|
257
|
+
const walletNotSelected = !!wallets.find(
|
|
258
|
+
(wallet) =>
|
|
259
|
+
wallet.chain === connectedWallet.chain &&
|
|
260
|
+
wallet.walletType === connectedWallet.walletType &&
|
|
261
|
+
!connectedWallet.selected
|
|
262
|
+
);
|
|
263
|
+
if (walletSelected) {
|
|
264
|
+
return { ...connectedWallet, selected: false };
|
|
265
|
+
} else if (walletNotSelected) {
|
|
266
|
+
return { ...connectedWallet, selected: true };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return connectedWallet;
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
set({
|
|
273
|
+
connectedWallets: nextConnectedWalletsWithUpdatedSelectedStatus,
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
newWalletConnected: async (accounts) => {
|
|
277
|
+
eventEmitter.emit(WidgetEvents.WalletEvent, {
|
|
278
|
+
type: WalletEventTypes.CONNECT,
|
|
279
|
+
payload: { walletType: accounts[0].walletType, accounts },
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
get().addConnectedWallet(accounts);
|
|
283
|
+
|
|
284
|
+
void get().fetchBalances(accounts);
|
|
285
|
+
},
|
|
286
|
+
removeBalancesForWallet: (walletType, options) => {
|
|
287
|
+
let walletsNeedsToBeRemoved = get().connectedWallets.filter(
|
|
288
|
+
(connectedWallet) => connectedWallet.walletType === walletType
|
|
289
|
+
);
|
|
290
|
+
/*
|
|
291
|
+
* We only need to delete balances where there is no connected wallets with same chain and address for that balance.
|
|
292
|
+
* Consider both Metamask and Solana having support for `0xblahblahblahblah` for Ethereum.
|
|
293
|
+
* If Phantom is disconnecting, we should keep the balance since Metamask has access to same address yet.
|
|
294
|
+
* So we only delete balance when there is no connected wallet that has access to that specific chain and address.
|
|
295
|
+
*/
|
|
296
|
+
get().connectedWallets.forEach((connectedWallet) => {
|
|
297
|
+
if (connectedWallet.walletType !== walletType) {
|
|
298
|
+
walletsNeedsToBeRemoved = walletsNeedsToBeRemoved.filter((wallet) => {
|
|
299
|
+
const isAnotherWalletHasSameAddressAndChain =
|
|
300
|
+
wallet.chain === connectedWallet.chain &&
|
|
301
|
+
wallet.address === connectedWallet.address;
|
|
302
|
+
return !isAnotherWalletHasSameAddressAndChain;
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
if (!!options?.chains && options.chains.length > 0) {
|
|
308
|
+
walletsNeedsToBeRemoved = walletsNeedsToBeRemoved.filter((wallet) => {
|
|
309
|
+
return options.chains?.includes(wallet.chain);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const nextBalancesState: BalanceState = {};
|
|
314
|
+
let nextAggregatedBalanceState: AggregatedBalanceState =
|
|
315
|
+
get()._aggregatedBalances;
|
|
316
|
+
const currentBalancesState = get()._balances;
|
|
317
|
+
const balanceKeys = Object.keys(currentBalancesState) as BalanceKey[];
|
|
318
|
+
|
|
319
|
+
balanceKeys.forEach((key) => {
|
|
320
|
+
const asset = extractAssetFromBalanceKey(key);
|
|
321
|
+
|
|
322
|
+
const shouldBalanceBeRemoved = !!walletsNeedsToBeRemoved.find(
|
|
323
|
+
(wallet) =>
|
|
324
|
+
createBalanceKey(wallet.address, {
|
|
325
|
+
address: asset.address,
|
|
326
|
+
blockchain: wallet.chain,
|
|
327
|
+
symbol: asset.symbol,
|
|
328
|
+
}) === key
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
if (!shouldBalanceBeRemoved) {
|
|
332
|
+
nextBalancesState[key] = currentBalancesState[key];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// if a balance should be removed, we need to remove its caches in _aggregatedBalances as wel.
|
|
336
|
+
if (shouldBalanceBeRemoved) {
|
|
337
|
+
nextAggregatedBalanceState = removeBalanceFromAggregatedBalance(
|
|
338
|
+
nextAggregatedBalanceState,
|
|
339
|
+
key
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
set({
|
|
344
|
+
_balances: nextBalancesState,
|
|
345
|
+
_aggregatedBalances: nextAggregatedBalanceState,
|
|
346
|
+
});
|
|
347
|
+
},
|
|
348
|
+
disconnectWallet: (walletType) => {
|
|
349
|
+
const isTargetWalletExistsInConnectedWallets = get().connectedWallets.find(
|
|
350
|
+
(wallet) => wallet.walletType === walletType
|
|
351
|
+
);
|
|
352
|
+
if (isTargetWalletExistsInConnectedWallets) {
|
|
353
|
+
eventEmitter.emit(WidgetEvents.WalletEvent, {
|
|
354
|
+
type: WalletEventTypes.DISCONNECT,
|
|
355
|
+
payload: { walletType },
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// This should be called before updating connectedWallets since we need the old state to remove balances.
|
|
359
|
+
get().removeBalancesForWallet(walletType);
|
|
360
|
+
|
|
361
|
+
let targetWalletWasSelectedForBlockchains = get()
|
|
362
|
+
.connectedWallets.filter(
|
|
363
|
+
(connectedWallet) =>
|
|
364
|
+
connectedWallet.selected &&
|
|
365
|
+
connectedWallet.walletType === walletType
|
|
366
|
+
)
|
|
367
|
+
.map((connectedWallet) => connectedWallet.chain);
|
|
368
|
+
|
|
369
|
+
// Remove target wallet from connectedWallets
|
|
370
|
+
let nextConnectedWallets = get().connectedWallets.filter(
|
|
371
|
+
(connectedWallet) => connectedWallet.walletType !== walletType
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
/*
|
|
375
|
+
* If we are disconnecting a wallet that has `selected` for some blockchains,
|
|
376
|
+
* For those blockchains we will fallback to first connected wallet
|
|
377
|
+
* which means selected wallet will change.
|
|
378
|
+
*/
|
|
379
|
+
if (targetWalletWasSelectedForBlockchains.length > 0) {
|
|
380
|
+
nextConnectedWallets = nextConnectedWallets.map((connectedWallet) => {
|
|
381
|
+
if (
|
|
382
|
+
targetWalletWasSelectedForBlockchains.includes(
|
|
383
|
+
connectedWallet.chain
|
|
384
|
+
)
|
|
385
|
+
) {
|
|
386
|
+
targetWalletWasSelectedForBlockchains =
|
|
387
|
+
targetWalletWasSelectedForBlockchains.filter(
|
|
388
|
+
(blockchain) => blockchain !== connectedWallet.chain
|
|
389
|
+
);
|
|
390
|
+
return {
|
|
391
|
+
...connectedWallet,
|
|
392
|
+
selected: true,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return connectedWallet;
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
set({
|
|
401
|
+
connectedWallets: nextConnectedWallets,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
clearConnectedWallet: () => set({ connectedWallets: [] }),
|
|
406
|
+
fetchBalances: async (accounts, options) => {
|
|
407
|
+
// All the `accounts` have same `walletType` so we can pick the first one.
|
|
408
|
+
const walletType = accounts[0].walletType;
|
|
409
|
+
|
|
410
|
+
get().setConnectedWalletAsRefetching(walletType);
|
|
411
|
+
|
|
412
|
+
const addressesToFetch = accounts.map((account) => ({
|
|
413
|
+
address: account.address,
|
|
414
|
+
blockchain: account.chain,
|
|
415
|
+
}));
|
|
416
|
+
const response = await httpService().getWalletsDetails(addressesToFetch);
|
|
417
|
+
|
|
418
|
+
const listWalletsWithBalances = response.wallets;
|
|
419
|
+
|
|
420
|
+
if (listWalletsWithBalances) {
|
|
421
|
+
const { retryOnFailedBalances = true } = options || {};
|
|
422
|
+
if (retryOnFailedBalances) {
|
|
423
|
+
const failedWallets: Wallet[] = listWalletsWithBalances
|
|
424
|
+
.filter((wallet) => wallet.failed)
|
|
425
|
+
.map((wallet) => ({
|
|
426
|
+
chain: wallet.blockChain,
|
|
427
|
+
walletType: walletType,
|
|
428
|
+
address: wallet.address,
|
|
429
|
+
}));
|
|
430
|
+
if (failedWallets.length > 0) {
|
|
431
|
+
void get().fetchBalances(failedWallets, {
|
|
432
|
+
retryOnFailedBalances: false,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
let nextBalances: BalanceState = {};
|
|
438
|
+
let nextAggregatedBalances: AggregatedBalanceState =
|
|
439
|
+
get()._aggregatedBalances;
|
|
440
|
+
listWalletsWithBalances.forEach((wallet) => {
|
|
441
|
+
if (wallet.failed) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const balancesForWallet = createBalanceStateForNewAccount(wallet, get);
|
|
446
|
+
|
|
447
|
+
nextAggregatedBalances = updateAggregatedBalanceStateForNewAccount(
|
|
448
|
+
nextAggregatedBalances,
|
|
449
|
+
balancesForWallet
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
nextBalances = {
|
|
453
|
+
...nextBalances,
|
|
454
|
+
...balancesForWallet,
|
|
455
|
+
};
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
set((state) => ({
|
|
459
|
+
_balances: {
|
|
460
|
+
...state._balances,
|
|
461
|
+
...nextBalances,
|
|
462
|
+
},
|
|
463
|
+
_aggregatedBalances: nextAggregatedBalances,
|
|
464
|
+
}));
|
|
465
|
+
|
|
466
|
+
get().setConnectedWalletRetrievedData(walletType);
|
|
467
|
+
} else {
|
|
468
|
+
get().setConnectedWalletHasError(walletType);
|
|
469
|
+
throw new Error(
|
|
470
|
+
`We couldn't fetch your account balances. Seem there is no information on blockchain for them yet.`
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
},
|
|
474
|
+
getBalances: () => {
|
|
475
|
+
return get()._balances;
|
|
476
|
+
},
|
|
477
|
+
getBalanceFor: (token) => {
|
|
478
|
+
const balances = get().getBalances();
|
|
479
|
+
|
|
480
|
+
/*
|
|
481
|
+
* The old implementation wasn't considering user's address.
|
|
482
|
+
* it can be problematic when two separate address has same token, both of them will override on same key.
|
|
483
|
+
*
|
|
484
|
+
* For keeping the same behavior, here we pick the most amount and also will not consider user's address in key.
|
|
485
|
+
*/
|
|
486
|
+
|
|
487
|
+
// Note: balance key is created using asset key + wallet address
|
|
488
|
+
const assetKey = createAssetKey(token);
|
|
489
|
+
const targetBalanceKeys = get()._aggregatedBalances[assetKey] || [];
|
|
490
|
+
|
|
491
|
+
if (targetBalanceKeys.length === 0) {
|
|
492
|
+
return null;
|
|
493
|
+
} else if (targetBalanceKeys.length === 1) {
|
|
494
|
+
const targetKey = targetBalanceKeys[0];
|
|
495
|
+
return balances[targetKey];
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// If there are multiple balances for an specific token, we pick the maximum.
|
|
499
|
+
const firstTargetBalance = balances[targetBalanceKeys[0]];
|
|
500
|
+
let maxBalance: Balance = firstTargetBalance;
|
|
501
|
+
targetBalanceKeys.forEach((targetBalanceKey) => {
|
|
502
|
+
const currentBalance = balances[targetBalanceKey];
|
|
503
|
+
const currentBalanceAmount = new BigNumber(currentBalance.amount);
|
|
504
|
+
const prevBalanceAmount = new BigNumber(maxBalance.amount);
|
|
505
|
+
|
|
506
|
+
if (currentBalanceAmount.isGreaterThan(prevBalanceAmount)) {
|
|
507
|
+
maxBalance = currentBalance;
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
return maxBalance;
|
|
511
|
+
},
|
|
512
|
+
getBalancesForWalletAddress: (address: string) => {
|
|
513
|
+
const balances = get().getBalances();
|
|
514
|
+
const balanceKeys = Object.keys(balances) as BalanceKey[];
|
|
515
|
+
|
|
516
|
+
const balancesForTargetWalletAddress = balanceKeys.reduce(
|
|
517
|
+
(output, balanceKey) => {
|
|
518
|
+
const balance = balances[balanceKey];
|
|
519
|
+
|
|
520
|
+
const [, , , balanceWalletAddreess] = balanceKey.split('-');
|
|
521
|
+
if (balanceWalletAddreess === address) {
|
|
522
|
+
output[balanceKey] = balance;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return output;
|
|
526
|
+
},
|
|
527
|
+
{} as BalanceState
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
return balancesForTargetWalletAddress;
|
|
531
|
+
},
|
|
532
|
+
|
|
533
|
+
getConnectedWalletsDetails: () => {
|
|
534
|
+
const connectedWallets = get().connectedWallets;
|
|
535
|
+
return connectedWallets.map((wallet) => {
|
|
536
|
+
const balances = get().getBalancesForWalletAddress(wallet.address);
|
|
537
|
+
const balancesKeys = Object.keys(balances) as BalanceKey[];
|
|
538
|
+
|
|
539
|
+
return {
|
|
540
|
+
...wallet,
|
|
541
|
+
balances: balancesKeys.reduce((output, balanceKey) => {
|
|
542
|
+
const balance = balances[balanceKey];
|
|
543
|
+
const asset = extractAssetFromBalanceKey(balanceKey);
|
|
544
|
+
|
|
545
|
+
if (asset.blockchain === wallet.chain) {
|
|
546
|
+
const token = get().findToken(asset);
|
|
547
|
+
|
|
548
|
+
if (!!token) {
|
|
549
|
+
const amount = balance.amount
|
|
550
|
+
? new BigNumber(balance.amount).shiftedBy(-balance.decimals)
|
|
551
|
+
: ZERO;
|
|
552
|
+
|
|
553
|
+
output.push({
|
|
554
|
+
chain: wallet.chain,
|
|
555
|
+
symbol: token.symbol,
|
|
556
|
+
ticker: token.symbol,
|
|
557
|
+
address: token.address,
|
|
558
|
+
rawAmount: balance.amount,
|
|
559
|
+
decimal: balance.decimals,
|
|
560
|
+
amount: amount.toString(),
|
|
561
|
+
logo: token.image,
|
|
562
|
+
usdPrice: token.usdPrice,
|
|
563
|
+
});
|
|
564
|
+
} else {
|
|
565
|
+
console.debug(
|
|
566
|
+
"Looking for asset but it couldn't be found in tokens store. May not be provided in meta.'",
|
|
567
|
+
asset
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return output;
|
|
573
|
+
}, [] as DeprecatedTokenBalance[]),
|
|
574
|
+
};
|
|
575
|
+
});
|
|
576
|
+
},
|
|
577
|
+
});
|
package/src/store/ui.ts
CHANGED
|
@@ -12,12 +12,14 @@ interface UiState {
|
|
|
12
12
|
tabManagerInitiated: boolean;
|
|
13
13
|
showActivateTabModal: boolean;
|
|
14
14
|
watermark: Watermark;
|
|
15
|
+
showProfileBanner: boolean;
|
|
15
16
|
activateCurrentTab: (
|
|
16
17
|
setCurrentTabAsActive: () => void,
|
|
17
18
|
hasRunningSwaps: boolean
|
|
18
19
|
) => void;
|
|
19
20
|
setShowActivateTabModal: (flag: boolean) => void;
|
|
20
21
|
setWatermark: (watermark: Watermark) => void;
|
|
22
|
+
setShowProfileBanner: (showProfileBanner: boolean) => void;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
export const useUiStore = createSelectors(
|
|
@@ -26,6 +28,7 @@ export const useUiStore = createSelectors(
|
|
|
26
28
|
tabManagerInitiated: false,
|
|
27
29
|
showActivateTabModal: false,
|
|
28
30
|
watermark: 'NONE',
|
|
31
|
+
showProfileBanner: false,
|
|
29
32
|
fetchingApiConfig: false,
|
|
30
33
|
activateCurrentTab: (setCurrentTabAsActive, hasRunningSwaps) => {
|
|
31
34
|
const { showActivateTabModal } = get();
|
|
@@ -42,6 +45,9 @@ export const useUiStore = createSelectors(
|
|
|
42
45
|
setWatermark: (watermark) => {
|
|
43
46
|
set({ watermark });
|
|
44
47
|
},
|
|
48
|
+
setShowProfileBanner: (showProfileBanner) => {
|
|
49
|
+
set({ showProfileBanner });
|
|
50
|
+
},
|
|
45
51
|
}))
|
|
46
52
|
);
|
|
47
53
|
|