@rango-dev/widget-embedded 0.36.1-next.8 → 0.37.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.
- 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 +0 -2
- 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/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/TokenList/TokenList.d.ts.map +1 -1
- package/dist/components/WalletStatefulConnect/DerivationPath.helpers.d.ts +1 -2
- package/dist/components/WalletStatefulConnect/DerivationPath.helpers.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 +0 -2
- 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/Wallets/Wallets.d.ts.map +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.d.ts.map +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts +2 -2
- package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts.map +1 -1
- package/dist/hooks/useFetchCustomToken.d.ts.map +1 -1
- package/dist/hooks/useObserveBalanceChanges/index.d.ts +2 -0
- package/dist/hooks/useObserveBalanceChanges/index.d.ts.map +1 -0
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts +4 -0
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts.map +1 -0
- package/dist/hooks/useStatefulConnect/useStatefulConnect.d.ts +1 -2
- package/dist/hooks/useStatefulConnect/useStatefulConnect.d.ts.map +1 -1
- package/dist/hooks/useStatefulConnect/useStatefulConnect.types.d.ts +1 -1
- 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 -1
- 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/Home.d.ts.map +1 -1
- package/dist/pages/SelectSwapItemsPage.d.ts.map +1 -1
- package/dist/store/app.d.ts +4 -2
- package/dist/store/app.d.ts.map +1 -1
- package/dist/store/slices/config.d.ts.map +1 -1
- package/dist/store/slices/data.d.ts.map +1 -1
- package/dist/store/wallets.d.ts +66 -0
- package/dist/store/wallets.d.ts.map +1 -0
- package/dist/types/config.d.ts +3 -6
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/wallets.d.ts +2 -3
- package/dist/types/wallets.d.ts.map +1 -1
- package/dist/utils/providers.d.ts +8 -3
- package/dist/utils/providers.d.ts.map +1 -1
- package/dist/utils/settings.d.ts +0 -1
- package/dist/utils/settings.d.ts.map +1 -1
- package/dist/utils/swap.d.ts +1 -1
- package/dist/utils/swap.d.ts.map +1 -1
- package/dist/utils/wallets.d.ts +14 -12
- package/dist/utils/wallets.d.ts.map +1 -1
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +7 -7
- package/src/QueueManager.tsx +7 -12
- package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +5 -3
- package/src/components/ConfirmWalletsModal/WalletList.tsx +7 -10
- package/src/components/Layout/Layout.tsx +3 -1
- package/src/components/Quote/Quote.tsx +3 -22
- package/src/components/Quote/Quote.types.ts +0 -2
- package/src/components/Quote/QuoteCostDetails.styles.ts +1 -0
- package/src/components/Quote/QuoteCostDetails.tsx +4 -23
- package/src/components/StatefulConnectModal/StatefulConnectModal.tsx +1 -1
- package/src/components/SwapDetails/SwapDetails.tsx +1 -6
- package/src/components/SwapDetailsAlerts/SwapDetailsAlerts.types.ts +1 -1
- package/src/components/TokenList/TokenList.tsx +27 -4
- package/src/components/WalletStatefulConnect/DerivationPath.helpers.ts +1 -2
- package/src/components/WalletStatefulConnect/Namespaces.tsx +1 -1
- package/src/components/WalletStatefulConnect/Namespaces.types.tsx +1 -1
- package/src/constants/quote.ts +0 -2
- package/src/containers/ExpandedQuotes/ExpandedQuotes.styles.ts +0 -1
- package/src/containers/ExpandedQuotes/ExpandedQuotes.tsx +1 -1
- package/src/containers/Inputs/Inputs.tsx +7 -20
- package/src/containers/Wallets/Wallets.tsx +14 -20
- package/src/containers/WidgetInfo/WidgetInfo.tsx +8 -10
- package/src/containers/WidgetInfo/WidgetInfo.types.ts +2 -2
- package/src/hooks/useFetchCustomToken.ts +0 -5
- package/src/hooks/useObserveBalanceChanges/index.ts +1 -0
- package/src/hooks/useObserveBalanceChanges/useObserveBalanceChanges.ts +68 -0
- package/src/hooks/useStatefulConnect/useStatefulConnect.ts +7 -42
- package/src/hooks/useStatefulConnect/useStatefulConnect.types.ts +1 -1
- package/src/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.ts +6 -7
- package/src/hooks/useSwapInput.ts +2 -1
- package/src/hooks/useWalletList.ts +3 -1
- package/src/hooks/useWalletProviders/useWalletProviders.ts +7 -3
- package/src/index.ts +1 -1
- package/src/pages/Home.tsx +3 -5
- package/src/pages/SelectSwapItemsPage.tsx +2 -1
- package/src/store/app.ts +4 -4
- package/src/store/slices/config.ts +0 -3
- package/src/store/slices/data.ts +2 -3
- package/src/store/wallets.ts +268 -0
- package/src/types/config.ts +3 -7
- package/src/types/wallets.ts +2 -3
- package/src/utils/providers.ts +12 -68
- package/src/utils/settings.ts +0 -4
- package/src/utils/swap.ts +1 -1
- package/src/utils/wallets.ts +137 -74
- package/dist/store/slices/types.d.ts +0 -6
- package/dist/store/slices/types.d.ts.map +0 -1
- package/dist/store/slices/wallets.d.ts +0 -58
- package/dist/store/slices/wallets.d.ts.map +0 -1
- package/dist/store/utils/wallets.d.ts +0 -17
- package/dist/store/utils/wallets.d.ts.map +0 -1
- package/dist/utils/hub.d.ts +0 -4
- package/dist/utils/hub.d.ts.map +0 -1
- package/src/store/slices/types.ts +0 -9
- package/src/store/slices/wallets.ts +0 -477
- package/src/store/utils/wallets.ts +0 -108
- package/src/utils/hub.ts +0 -21
|
@@ -1,477 +0,0 @@
|
|
|
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 { eventEmitter } from '../../services/eventEmitter';
|
|
8
|
-
import { httpService } from '../../services/httpService';
|
|
9
|
-
import {
|
|
10
|
-
type Balance,
|
|
11
|
-
type Wallet,
|
|
12
|
-
WalletEventTypes,
|
|
13
|
-
WidgetEvents,
|
|
14
|
-
} from '../../types';
|
|
15
|
-
import { isAccountAndWalletMatched } from '../../utils/wallets';
|
|
16
|
-
import {
|
|
17
|
-
createAssetKey,
|
|
18
|
-
createBalanceKey,
|
|
19
|
-
createBalanceStateForNewAccount,
|
|
20
|
-
extractAssetFromBalanceKey,
|
|
21
|
-
removeBalanceFromAggregatedBalance,
|
|
22
|
-
updateAggregatedBalanceStateForNewAccount,
|
|
23
|
-
} from '../utils/wallets';
|
|
24
|
-
|
|
25
|
-
type WalletAddress = string;
|
|
26
|
-
type TokenAddress = string;
|
|
27
|
-
type TokenSymbol = string;
|
|
28
|
-
type BlockchainId = string;
|
|
29
|
-
/** format: `BlockchainId-TokenAddress-TokenSymbol` */
|
|
30
|
-
export type AssetKey = `${BlockchainId}-${TokenAddress}-${TokenSymbol}`;
|
|
31
|
-
/** format: `BlockchainId-TokenAddress-TokenSymbol-WalletAddress` */
|
|
32
|
-
export type BalanceKey =
|
|
33
|
-
`${BlockchainId}-${TokenAddress}-${TokenSymbol}-${WalletAddress}`;
|
|
34
|
-
|
|
35
|
-
export type BalanceState = {
|
|
36
|
-
[key: BalanceKey]: Balance;
|
|
37
|
-
};
|
|
38
|
-
export type AggregatedBalanceState = {
|
|
39
|
-
[key: AssetKey]: BalanceKey[];
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export interface ConnectedWallet extends Wallet {
|
|
43
|
-
explorerUrl: string | null;
|
|
44
|
-
selected: boolean;
|
|
45
|
-
loading: boolean;
|
|
46
|
-
error: boolean;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface WalletsSlice {
|
|
50
|
-
_balances: BalanceState;
|
|
51
|
-
_aggregatedBalances: AggregatedBalanceState;
|
|
52
|
-
connectedWallets: ConnectedWallet[];
|
|
53
|
-
fetchingWallets: boolean;
|
|
54
|
-
|
|
55
|
-
setConnectedWalletAsRefetching: (walletType: string) => void;
|
|
56
|
-
setConnectedWalletHasError: (walletType: string) => void;
|
|
57
|
-
setConnectedWalletRetrievedData: (walletType: string) => void;
|
|
58
|
-
removeBalancesForWallet: (walletType: string) => void;
|
|
59
|
-
addConnectedWallet: (accounts: Wallet[]) => void;
|
|
60
|
-
setWalletsAsSelected: (
|
|
61
|
-
wallets: { walletType: string; chain: string }[]
|
|
62
|
-
) => void;
|
|
63
|
-
/**
|
|
64
|
-
* Add new accounts to store and fetch balances for them.
|
|
65
|
-
*/
|
|
66
|
-
newWalletConnected: (accounts: Wallet[]) => Promise<void>;
|
|
67
|
-
/**
|
|
68
|
-
* Disconnect a wallet and clean up balances after that.
|
|
69
|
-
*/
|
|
70
|
-
disconnectWallet: (walletType: string) => void;
|
|
71
|
-
clearConnectedWallet: () => void;
|
|
72
|
-
fetchBalances: (
|
|
73
|
-
accounts: Wallet[],
|
|
74
|
-
options?: { retryOnFailedBalances?: boolean }
|
|
75
|
-
) => Promise<void>;
|
|
76
|
-
getBalanceFor: (token: Token) => Balance | null;
|
|
77
|
-
getBalances: () => BalanceState;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const createWalletsSlice: StateCreator<
|
|
81
|
-
AppStoreState,
|
|
82
|
-
[],
|
|
83
|
-
[],
|
|
84
|
-
WalletsSlice
|
|
85
|
-
> = (set, get) => ({
|
|
86
|
-
_balances: {},
|
|
87
|
-
_aggregatedBalances: {},
|
|
88
|
-
connectedWallets: [],
|
|
89
|
-
fetchingWallets: false,
|
|
90
|
-
|
|
91
|
-
// Actions
|
|
92
|
-
setConnectedWalletAsRefetching: (walletType: string) => {
|
|
93
|
-
set((state) => {
|
|
94
|
-
return {
|
|
95
|
-
fetchingWallets: true,
|
|
96
|
-
connectedWallets: state.connectedWallets.map((connectedWallet) => {
|
|
97
|
-
if (connectedWallet.walletType === walletType) {
|
|
98
|
-
return {
|
|
99
|
-
...connectedWallet,
|
|
100
|
-
loading: true,
|
|
101
|
-
error: false,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return connectedWallet;
|
|
106
|
-
}),
|
|
107
|
-
};
|
|
108
|
-
});
|
|
109
|
-
},
|
|
110
|
-
setConnectedWalletRetrievedData: (walletType: string) => {
|
|
111
|
-
set((state) => {
|
|
112
|
-
return {
|
|
113
|
-
fetchingWallets: false,
|
|
114
|
-
connectedWallets: state.connectedWallets.map((connectedWallet) => {
|
|
115
|
-
if (connectedWallet.walletType === walletType) {
|
|
116
|
-
return {
|
|
117
|
-
...connectedWallet,
|
|
118
|
-
loading: false,
|
|
119
|
-
error: false,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return connectedWallet;
|
|
124
|
-
}),
|
|
125
|
-
};
|
|
126
|
-
});
|
|
127
|
-
},
|
|
128
|
-
setConnectedWalletHasError: (walletType: string) => {
|
|
129
|
-
set((state) => {
|
|
130
|
-
return {
|
|
131
|
-
fetchingWallets: false,
|
|
132
|
-
connectedWallets: state.connectedWallets.map((connectedWallet) => {
|
|
133
|
-
if (connectedWallet.walletType === walletType) {
|
|
134
|
-
return {
|
|
135
|
-
...connectedWallet,
|
|
136
|
-
loading: false,
|
|
137
|
-
error: true,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return connectedWallet;
|
|
142
|
-
}),
|
|
143
|
-
};
|
|
144
|
-
});
|
|
145
|
-
},
|
|
146
|
-
addConnectedWallet: (accounts: Wallet[]) => {
|
|
147
|
-
/*
|
|
148
|
-
* When we are going to add a new account, there are two thing that can be happens:
|
|
149
|
-
* 1. Wallet hasn't add yet.
|
|
150
|
-
* 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.
|
|
151
|
-
*
|
|
152
|
-
* For handling this, we need to only keep not-added-account, then only add those.
|
|
153
|
-
*
|
|
154
|
-
* Note:
|
|
155
|
-
* The second option would be useful for hub particularly.
|
|
156
|
-
*/
|
|
157
|
-
const connectedWallets = get().connectedWallets;
|
|
158
|
-
const walletsNeedToBeAdded = accounts.filter(
|
|
159
|
-
(account) =>
|
|
160
|
-
!connectedWallets.some((connectedWallet) =>
|
|
161
|
-
isAccountAndWalletMatched(account, connectedWallet)
|
|
162
|
-
)
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
if (walletsNeedToBeAdded.length > 0) {
|
|
166
|
-
const newConnectedWallets = walletsNeedToBeAdded.map((account) => {
|
|
167
|
-
/*
|
|
168
|
-
* 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.
|
|
169
|
-
* 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
|
|
170
|
-
*/
|
|
171
|
-
const shouldMarkWalletAsSelected = !connectedWallets.some(
|
|
172
|
-
(connectedWallet) =>
|
|
173
|
-
connectedWallet.chain === account.chain &&
|
|
174
|
-
connectedWallet.selected &&
|
|
175
|
-
/**
|
|
176
|
-
* Sometimes, the connect function can be called multiple times for a particular wallet type when using the auto-connect feature.
|
|
177
|
-
* This check is there to make sure the chosen wallet doesn't end up unselected.
|
|
178
|
-
*/
|
|
179
|
-
connectedWallet.walletType !== account.walletType
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
return {
|
|
183
|
-
address: account.address,
|
|
184
|
-
chain: account.chain,
|
|
185
|
-
explorerUrl: null,
|
|
186
|
-
walletType: account.walletType,
|
|
187
|
-
selected: shouldMarkWalletAsSelected,
|
|
188
|
-
|
|
189
|
-
loading: false,
|
|
190
|
-
error: false,
|
|
191
|
-
};
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
set((state) => {
|
|
195
|
-
/*
|
|
196
|
-
* If wallet connected before and only need to update the address we should remove the old value and then add new conncted value.
|
|
197
|
-
* This scenario happens when user wants to change account inside the wallet.
|
|
198
|
-
* So the assumption here is the wallet has only one active address for a blockchain at the moment.
|
|
199
|
-
*/
|
|
200
|
-
const connectedWalletsWithoutSameWalletAndBlockchain =
|
|
201
|
-
state.connectedWallets.filter((currentConnectedWallet) => {
|
|
202
|
-
return !newConnectedWallets.some(
|
|
203
|
-
(newConnectedWallet) =>
|
|
204
|
-
newConnectedWallet.walletType ===
|
|
205
|
-
currentConnectedWallet.walletType &&
|
|
206
|
-
newConnectedWallet.chain === currentConnectedWallet.chain
|
|
207
|
-
);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
return {
|
|
211
|
-
connectedWallets: [
|
|
212
|
-
...connectedWalletsWithoutSameWalletAndBlockchain,
|
|
213
|
-
...newConnectedWallets,
|
|
214
|
-
],
|
|
215
|
-
};
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
},
|
|
219
|
-
setWalletsAsSelected: (wallets) => {
|
|
220
|
-
const nextConnectedWalletsWithUpdatedSelectedStatus =
|
|
221
|
-
get().connectedWallets.map((connectedWallet) => {
|
|
222
|
-
const walletSelected = !!wallets.find(
|
|
223
|
-
(wallet) =>
|
|
224
|
-
wallet.chain === connectedWallet.chain &&
|
|
225
|
-
wallet.walletType !== connectedWallet.walletType &&
|
|
226
|
-
connectedWallet.selected
|
|
227
|
-
);
|
|
228
|
-
const walletNotSelected = !!wallets.find(
|
|
229
|
-
(wallet) =>
|
|
230
|
-
wallet.chain === connectedWallet.chain &&
|
|
231
|
-
wallet.walletType === connectedWallet.walletType &&
|
|
232
|
-
!connectedWallet.selected
|
|
233
|
-
);
|
|
234
|
-
if (walletSelected) {
|
|
235
|
-
return { ...connectedWallet, selected: false };
|
|
236
|
-
} else if (walletNotSelected) {
|
|
237
|
-
return { ...connectedWallet, selected: true };
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return connectedWallet;
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
set({
|
|
244
|
-
connectedWallets: nextConnectedWalletsWithUpdatedSelectedStatus,
|
|
245
|
-
});
|
|
246
|
-
},
|
|
247
|
-
newWalletConnected: async (accounts) => {
|
|
248
|
-
eventEmitter.emit(WidgetEvents.WalletEvent, {
|
|
249
|
-
type: WalletEventTypes.CONNECT,
|
|
250
|
-
payload: { walletType: accounts[0].walletType, accounts },
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
get().addConnectedWallet(accounts);
|
|
254
|
-
|
|
255
|
-
void get().fetchBalances(accounts);
|
|
256
|
-
},
|
|
257
|
-
removeBalancesForWallet: (walletType) => {
|
|
258
|
-
let walletsNeedsToBeRemoved = get().connectedWallets.filter(
|
|
259
|
-
(connectedWallet) => connectedWallet.walletType === walletType
|
|
260
|
-
);
|
|
261
|
-
/*
|
|
262
|
-
* We only need to delete balances where there is no connected wallets with same chain and address for that balance.
|
|
263
|
-
* Consider both Metamask and Solana having support for `0xblahblahblahblah` for Ethereum.
|
|
264
|
-
* If Phantom is disconnecting, we should keep the balance since Metamask has access to same address yet.
|
|
265
|
-
* So we only delete balance when there is no connected wallet that has access to that specific chain and address.
|
|
266
|
-
*/
|
|
267
|
-
get().connectedWallets.forEach((connectedWallet) => {
|
|
268
|
-
if (connectedWallet.walletType !== walletType) {
|
|
269
|
-
walletsNeedsToBeRemoved = walletsNeedsToBeRemoved.filter((wallet) => {
|
|
270
|
-
const isAnotherWalletHasSameAddressAndChain =
|
|
271
|
-
wallet.chain === connectedWallet.chain &&
|
|
272
|
-
wallet.address === connectedWallet.address;
|
|
273
|
-
return !isAnotherWalletHasSameAddressAndChain;
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
const nextBalancesState: BalanceState = {};
|
|
279
|
-
let nextAggregatedBalanceState: AggregatedBalanceState =
|
|
280
|
-
get()._aggregatedBalances;
|
|
281
|
-
const currentBalancesState = get()._balances;
|
|
282
|
-
const balanceKeys = Object.keys(currentBalancesState) as BalanceKey[];
|
|
283
|
-
|
|
284
|
-
balanceKeys.forEach((key) => {
|
|
285
|
-
const asset = extractAssetFromBalanceKey(key);
|
|
286
|
-
|
|
287
|
-
const shouldBalanceBeRemoved = !!walletsNeedsToBeRemoved.find(
|
|
288
|
-
(wallet) =>
|
|
289
|
-
createBalanceKey(wallet.address, {
|
|
290
|
-
address: asset.address,
|
|
291
|
-
blockchain: wallet.chain,
|
|
292
|
-
symbol: asset.symbol,
|
|
293
|
-
}) === key
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
if (!shouldBalanceBeRemoved) {
|
|
297
|
-
nextBalancesState[key] = currentBalancesState[key];
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// if a balance should be removed, we need to remove its caches in _aggregatedBalances as wel.
|
|
301
|
-
if (shouldBalanceBeRemoved) {
|
|
302
|
-
nextAggregatedBalanceState = removeBalanceFromAggregatedBalance(
|
|
303
|
-
nextAggregatedBalanceState,
|
|
304
|
-
key
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
set({
|
|
309
|
-
_balances: nextBalancesState,
|
|
310
|
-
_aggregatedBalances: nextAggregatedBalanceState,
|
|
311
|
-
});
|
|
312
|
-
},
|
|
313
|
-
disconnectWallet: (walletType) => {
|
|
314
|
-
const isTargetWalletExistsInConnectedWallets = get().connectedWallets.find(
|
|
315
|
-
(wallet) => wallet.walletType === walletType
|
|
316
|
-
);
|
|
317
|
-
if (isTargetWalletExistsInConnectedWallets) {
|
|
318
|
-
eventEmitter.emit(WidgetEvents.WalletEvent, {
|
|
319
|
-
type: WalletEventTypes.DISCONNECT,
|
|
320
|
-
payload: { walletType },
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
// This should be called before updating connectedWallets since we need the old state to remove balances.
|
|
324
|
-
get().removeBalancesForWallet(walletType);
|
|
325
|
-
|
|
326
|
-
let targetWalletWasSelectedForBlockchains = get()
|
|
327
|
-
.connectedWallets.filter(
|
|
328
|
-
(connectedWallet) =>
|
|
329
|
-
connectedWallet.selected &&
|
|
330
|
-
connectedWallet.walletType === walletType
|
|
331
|
-
)
|
|
332
|
-
.map((connectedWallet) => connectedWallet.chain);
|
|
333
|
-
|
|
334
|
-
// Remove target wallet from connectedWallets
|
|
335
|
-
let nextConnectedWallets = get().connectedWallets.filter(
|
|
336
|
-
(connectedWallet) => connectedWallet.walletType !== walletType
|
|
337
|
-
);
|
|
338
|
-
|
|
339
|
-
/*
|
|
340
|
-
* If we are disconnecting a wallet that has `selected` for some blockchains,
|
|
341
|
-
* For those blockchains we will fallback to first connected wallet
|
|
342
|
-
* which means selected wallet will change.
|
|
343
|
-
*/
|
|
344
|
-
if (targetWalletWasSelectedForBlockchains.length > 0) {
|
|
345
|
-
nextConnectedWallets = nextConnectedWallets.map((connectedWallet) => {
|
|
346
|
-
if (
|
|
347
|
-
targetWalletWasSelectedForBlockchains.includes(
|
|
348
|
-
connectedWallet.chain
|
|
349
|
-
)
|
|
350
|
-
) {
|
|
351
|
-
targetWalletWasSelectedForBlockchains =
|
|
352
|
-
targetWalletWasSelectedForBlockchains.filter(
|
|
353
|
-
(blockchain) => blockchain !== connectedWallet.chain
|
|
354
|
-
);
|
|
355
|
-
return {
|
|
356
|
-
...connectedWallet,
|
|
357
|
-
selected: true,
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
return connectedWallet;
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
set({
|
|
366
|
-
connectedWallets: nextConnectedWallets,
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
|
-
clearConnectedWallet: () => set({ connectedWallets: [] }),
|
|
371
|
-
fetchBalances: async (accounts, options) => {
|
|
372
|
-
// All the `accounts` have same `walletType` so we can pick the first one.
|
|
373
|
-
const walletType = accounts[0].walletType;
|
|
374
|
-
|
|
375
|
-
get().setConnectedWalletAsRefetching(walletType);
|
|
376
|
-
|
|
377
|
-
const addressesToFetch = accounts.map((account) => ({
|
|
378
|
-
address: account.address,
|
|
379
|
-
blockchain: account.chain,
|
|
380
|
-
}));
|
|
381
|
-
const response = await httpService().getWalletsDetails(addressesToFetch);
|
|
382
|
-
|
|
383
|
-
const listWalletsWithBalances = response.wallets;
|
|
384
|
-
|
|
385
|
-
if (listWalletsWithBalances) {
|
|
386
|
-
const { retryOnFailedBalances = true } = options || {};
|
|
387
|
-
if (retryOnFailedBalances) {
|
|
388
|
-
const failedWallets: Wallet[] = listWalletsWithBalances
|
|
389
|
-
.filter((wallet) => wallet.failed)
|
|
390
|
-
.map((wallet) => ({
|
|
391
|
-
chain: wallet.blockChain,
|
|
392
|
-
walletType: walletType,
|
|
393
|
-
address: wallet.address,
|
|
394
|
-
}));
|
|
395
|
-
if (failedWallets.length > 0) {
|
|
396
|
-
void get().fetchBalances(failedWallets, {
|
|
397
|
-
retryOnFailedBalances: false,
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
let nextBalances: BalanceState = {};
|
|
403
|
-
let nextAggregatedBalances: AggregatedBalanceState =
|
|
404
|
-
get()._aggregatedBalances;
|
|
405
|
-
listWalletsWithBalances.forEach((wallet) => {
|
|
406
|
-
if (wallet.failed) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
const balancesForWallet = createBalanceStateForNewAccount(wallet, get);
|
|
411
|
-
|
|
412
|
-
nextAggregatedBalances = updateAggregatedBalanceStateForNewAccount(
|
|
413
|
-
nextAggregatedBalances,
|
|
414
|
-
balancesForWallet
|
|
415
|
-
);
|
|
416
|
-
|
|
417
|
-
nextBalances = {
|
|
418
|
-
...nextBalances,
|
|
419
|
-
...balancesForWallet,
|
|
420
|
-
};
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
set((state) => ({
|
|
424
|
-
_balances: {
|
|
425
|
-
...state._balances,
|
|
426
|
-
...nextBalances,
|
|
427
|
-
},
|
|
428
|
-
_aggregatedBalances: nextAggregatedBalances,
|
|
429
|
-
}));
|
|
430
|
-
|
|
431
|
-
get().setConnectedWalletRetrievedData(walletType);
|
|
432
|
-
} else {
|
|
433
|
-
get().setConnectedWalletHasError(walletType);
|
|
434
|
-
throw new Error(
|
|
435
|
-
`We couldn't fetch your account balances. Seem there is no information on blockchain for them yet.`
|
|
436
|
-
);
|
|
437
|
-
}
|
|
438
|
-
},
|
|
439
|
-
getBalances: () => {
|
|
440
|
-
return get()._balances;
|
|
441
|
-
},
|
|
442
|
-
getBalanceFor: (token) => {
|
|
443
|
-
const balances = get().getBalances();
|
|
444
|
-
|
|
445
|
-
/*
|
|
446
|
-
* The old implementation wasn't considering user's address.
|
|
447
|
-
* it can be problematic when two separate address has same token, both of them will override on same key.
|
|
448
|
-
*
|
|
449
|
-
* For keeping the same behavior, here we pick the most amount and also will not consider user's address in key.
|
|
450
|
-
*/
|
|
451
|
-
|
|
452
|
-
// Note: balance key is created using asset key + wallet address
|
|
453
|
-
const assetKey = createAssetKey(token);
|
|
454
|
-
const targetBalanceKeys = get()._aggregatedBalances[assetKey] || [];
|
|
455
|
-
|
|
456
|
-
if (targetBalanceKeys.length === 0) {
|
|
457
|
-
return null;
|
|
458
|
-
} else if (targetBalanceKeys.length === 1) {
|
|
459
|
-
const targetKey = targetBalanceKeys[0];
|
|
460
|
-
return balances[targetKey];
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
// If there are multiple balances for an specific token, we pick the maximum.
|
|
464
|
-
const firstTargetBalance = balances[targetBalanceKeys[0]];
|
|
465
|
-
let maxBalance: Balance = firstTargetBalance;
|
|
466
|
-
targetBalanceKeys.forEach((targetBalanceKey) => {
|
|
467
|
-
const currentBalance = balances[targetBalanceKey];
|
|
468
|
-
const currentBalanceAmount = new BigNumber(currentBalance.amount);
|
|
469
|
-
const prevBalanceAmount = new BigNumber(maxBalance.amount);
|
|
470
|
-
|
|
471
|
-
if (currentBalanceAmount.isGreaterThan(prevBalanceAmount)) {
|
|
472
|
-
maxBalance = currentBalance;
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
return maxBalance;
|
|
476
|
-
},
|
|
477
|
-
});
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { Balance } from '../../types';
|
|
2
|
-
import type { AppStoreState } from '../app';
|
|
3
|
-
import type {
|
|
4
|
-
AggregatedBalanceState,
|
|
5
|
-
AssetKey,
|
|
6
|
-
BalanceKey,
|
|
7
|
-
BalanceState,
|
|
8
|
-
} from '../slices/wallets';
|
|
9
|
-
import type { Asset, WalletDetail } from 'rango-types';
|
|
10
|
-
|
|
11
|
-
import BigNumber from 'bignumber.js';
|
|
12
|
-
|
|
13
|
-
import { ZERO } from '../../constants/numbers';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Note: We need to use `symbol` as well since native coins and cosmos blockchains don't have `address`
|
|
17
|
-
* output format: BlockchainId-TokenAddress-TokenSymbol
|
|
18
|
-
*/
|
|
19
|
-
export function createAssetKey(asset: Asset): AssetKey {
|
|
20
|
-
return `${asset.blockchain}-${asset.address}-${asset.symbol}`;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* output format: BlockchainId-TokenAddress-TokenSymbol-WalletAddress
|
|
25
|
-
*/
|
|
26
|
-
export function createBalanceKey(
|
|
27
|
-
accountAddress: string,
|
|
28
|
-
asset: Asset
|
|
29
|
-
): BalanceKey {
|
|
30
|
-
const assetKey = createAssetKey(asset);
|
|
31
|
-
return `${assetKey}-${accountAddress}`;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function extractAssetFromBalanceKey(key: BalanceKey): Asset {
|
|
35
|
-
const [assetChain, assetAddress, assetSymbol] = key.split('-');
|
|
36
|
-
return {
|
|
37
|
-
address: assetAddress,
|
|
38
|
-
blockchain: assetChain,
|
|
39
|
-
symbol: assetSymbol,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function createBalanceStateForNewAccount(
|
|
44
|
-
account: WalletDetail,
|
|
45
|
-
store: () => AppStoreState
|
|
46
|
-
): BalanceState {
|
|
47
|
-
const state: BalanceState = {};
|
|
48
|
-
|
|
49
|
-
account.balances?.forEach((accountBalance) => {
|
|
50
|
-
const key = createBalanceKey(account.address, accountBalance.asset);
|
|
51
|
-
const amount = accountBalance.amount.amount;
|
|
52
|
-
const decimals = accountBalance.amount.decimals;
|
|
53
|
-
|
|
54
|
-
const usdPrice = store().findToken(accountBalance.asset)?.usdPrice;
|
|
55
|
-
const usdValue = usdPrice
|
|
56
|
-
? new BigNumber(usdPrice ?? ZERO).multipliedBy(amount).toString()
|
|
57
|
-
: '';
|
|
58
|
-
|
|
59
|
-
const balance: Balance = {
|
|
60
|
-
amount,
|
|
61
|
-
decimals,
|
|
62
|
-
usdValue,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
state[key] = balance;
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
return state;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function updateAggregatedBalanceStateForNewAccount(
|
|
72
|
-
aggregatedBalances: AggregatedBalanceState,
|
|
73
|
-
balanceState: BalanceState
|
|
74
|
-
) {
|
|
75
|
-
for (const balanceKey in balanceState) {
|
|
76
|
-
const asset = extractAssetFromBalanceKey(balanceKey as BalanceKey);
|
|
77
|
-
const assetKey = createAssetKey(asset);
|
|
78
|
-
|
|
79
|
-
if (!aggregatedBalances[assetKey]) {
|
|
80
|
-
aggregatedBalances[assetKey] = [];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (!aggregatedBalances[assetKey].includes(balanceKey as BalanceKey)) {
|
|
84
|
-
aggregatedBalances[assetKey] = [
|
|
85
|
-
...aggregatedBalances[assetKey],
|
|
86
|
-
balanceKey as BalanceKey,
|
|
87
|
-
];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return aggregatedBalances;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function removeBalanceFromAggregatedBalance(
|
|
95
|
-
aggregatedBalances: AggregatedBalanceState,
|
|
96
|
-
balanceKey: BalanceKey
|
|
97
|
-
) {
|
|
98
|
-
const asset = extractAssetFromBalanceKey(balanceKey);
|
|
99
|
-
const assetKey = createAssetKey(asset);
|
|
100
|
-
|
|
101
|
-
if (aggregatedBalances[assetKey]) {
|
|
102
|
-
aggregatedBalances[assetKey] = aggregatedBalances[assetKey].filter(
|
|
103
|
-
(aggregatedBalance) => aggregatedBalance !== balanceKey
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return aggregatedBalances;
|
|
108
|
-
}
|
package/src/utils/hub.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { CommonNamespaceKeys } from '@rango-dev/wallets-core';
|
|
2
|
-
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
|
|
3
|
-
|
|
4
|
-
export function convertCommonNamespacesKeysToLegacyNamespace(
|
|
5
|
-
namespaces: CommonNamespaceKeys[]
|
|
6
|
-
): Namespace[] {
|
|
7
|
-
return namespaces.map((namespace) => {
|
|
8
|
-
switch (namespace) {
|
|
9
|
-
case 'evm':
|
|
10
|
-
return 'EVM';
|
|
11
|
-
case 'solana':
|
|
12
|
-
return 'Solana';
|
|
13
|
-
case 'cosmos':
|
|
14
|
-
return 'Cosmos';
|
|
15
|
-
default:
|
|
16
|
-
throw new Error(
|
|
17
|
-
'Can not convert this common namespace key to a proper legacy key.'
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|