@rango-dev/widget-embedded 0.39.1-next.9 → 0.40.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/components/SwapDetailsModal/SwapDetailsCompleteModal.d.ts.map +1 -1
- package/dist/components/SwapDetailsModal/SwapDetailsModal.styles.d.ts +160 -0
- package/dist/components/SwapDetailsModal/SwapDetailsModal.styles.d.ts.map +1 -1
- package/dist/constants/profileBanner.d.ts +3 -0
- package/dist/constants/profileBanner.d.ts.map +1 -0
- package/dist/constants/wallets.d.ts +1 -0
- package/dist/constants/wallets.d.ts.map +1 -1
- package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
- package/dist/containers/Wallets/Wallets.helpers.d.ts +4 -0
- package/dist/containers/Wallets/Wallets.helpers.d.ts.map +1 -0
- package/dist/containers/Wallets/Wallets.types.d.ts +6 -2
- package/dist/containers/Wallets/Wallets.types.d.ts.map +1 -1
- package/dist/containers/Wallets/useUpdates.d.ts +13 -0
- package/dist/containers/Wallets/useUpdates.d.ts.map +1 -0
- package/dist/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/store/middlewares/keepLastUpdated.d.ts +8 -0
- package/dist/store/middlewares/keepLastUpdated.d.ts.map +1 -0
- package/dist/store/slices/wallets.d.ts +19 -13
- package/dist/store/slices/wallets.d.ts.map +1 -1
- package/dist/store/utils/wallets.d.ts +3 -3
- package/dist/store/utils/wallets.d.ts.map +1 -1
- package/dist/utils/common.d.ts +5 -0
- package/dist/utils/common.d.ts.map +1 -1
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +12 -12
- package/src/components/ConfirmWalletsModal/WalletList.tsx +1 -1
- package/src/components/SwapDetailsModal/SwapDetailsCompleteModal.tsx +14 -7
- package/src/components/SwapDetailsModal/SwapDetailsModal.styles.ts +4 -0
- package/src/constants/profileBanner.ts +3 -0
- package/src/constants/wallets.ts +2 -0
- package/src/containers/Wallets/Wallets.helpers.ts +26 -0
- package/src/containers/Wallets/Wallets.tsx +23 -127
- package/src/containers/Wallets/Wallets.types.ts +16 -2
- package/src/containers/Wallets/useUpdates.ts +240 -0
- package/src/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.ts +19 -9
- package/src/store/middlewares/keepLastUpdated.ts +19 -0
- package/src/store/slices/wallets.ts +546 -414
- package/src/store/utils/wallets.ts +12 -11
- package/src/utils/common.ts +24 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rango-dev/widget-embedded",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@lingui/core": "4.2.1",
|
|
26
26
|
"@lingui/react": "4.2.1",
|
|
27
|
-
"@rango-dev/logging-core": "^0.
|
|
28
|
-
"@rango-dev/provider-all": "^0.
|
|
29
|
-
"@rango-dev/queue-manager-core": "^0.
|
|
30
|
-
"@rango-dev/queue-manager-rango-preset": "^0.
|
|
31
|
-
"@rango-dev/queue-manager-react": "^0.
|
|
32
|
-
"@rango-dev/signer-solana": "^0.
|
|
33
|
-
"@rango-dev/ui": "^0.
|
|
34
|
-
"@rango-dev/wallets-core": "^0.
|
|
35
|
-
"@rango-dev/wallets-react": "^0.
|
|
36
|
-
"@rango-dev/wallets-shared": "^0.
|
|
27
|
+
"@rango-dev/logging-core": "^0.7.0",
|
|
28
|
+
"@rango-dev/provider-all": "^0.43.0",
|
|
29
|
+
"@rango-dev/queue-manager-core": "^0.28.0",
|
|
30
|
+
"@rango-dev/queue-manager-rango-preset": "^0.43.0",
|
|
31
|
+
"@rango-dev/queue-manager-react": "^0.28.0",
|
|
32
|
+
"@rango-dev/signer-solana": "^0.37.0",
|
|
33
|
+
"@rango-dev/ui": "^0.44.0",
|
|
34
|
+
"@rango-dev/wallets-core": "^0.41.0",
|
|
35
|
+
"@rango-dev/wallets-react": "^0.28.0",
|
|
36
|
+
"@rango-dev/wallets-shared": "^0.42.0",
|
|
37
37
|
"bignumber.js": "^9.1.1",
|
|
38
38
|
"copy-to-clipboard": "^3.3.3",
|
|
39
39
|
"dayjs": "^1.11.7",
|
|
@@ -54,4 +54,4 @@
|
|
|
54
54
|
"publishConfig": {
|
|
55
55
|
"access": "public"
|
|
56
56
|
}
|
|
57
|
-
}
|
|
57
|
+
}
|
|
@@ -9,13 +9,19 @@ import {
|
|
|
9
9
|
Typography,
|
|
10
10
|
} from '@rango-dev/ui';
|
|
11
11
|
import React from 'react';
|
|
12
|
-
import { useNavigate } from 'react-router-dom';
|
|
12
|
+
import { Link, useNavigate } from 'react-router-dom';
|
|
13
13
|
|
|
14
14
|
import { WIDGET_UI_ID } from '../../constants';
|
|
15
|
+
import {
|
|
16
|
+
PROFILE_BANNER_IMAGE_SOURCE,
|
|
17
|
+
PROFILE_BANNER_NAVIGATION_LINK,
|
|
18
|
+
} from '../../constants/profileBanner';
|
|
15
19
|
import { useUiStore } from '../../store/ui';
|
|
16
20
|
import { getContainer } from '../../utils/common';
|
|
17
21
|
import { WatermarkedModal } from '../common/WatermarkedModal';
|
|
18
22
|
|
|
23
|
+
import { ProfileBanner } from './SwapDetailsModal.styles';
|
|
24
|
+
|
|
19
25
|
export function SwapDetailsCompleteModal(props: CompleteModalPropTypes) {
|
|
20
26
|
const {
|
|
21
27
|
open,
|
|
@@ -79,12 +85,13 @@ export function SwapDetailsCompleteModal(props: CompleteModalPropTypes) {
|
|
|
79
85
|
<>
|
|
80
86
|
{showProfileBanner && (
|
|
81
87
|
<>
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
<Link to={PROFILE_BANNER_NAVIGATION_LINK}>
|
|
89
|
+
<ProfileBanner
|
|
90
|
+
src={PROFILE_BANNER_IMAGE_SOURCE}
|
|
91
|
+
alt="Profile Banner"
|
|
92
|
+
/>
|
|
93
|
+
</Link>
|
|
94
|
+
|
|
88
95
|
<Divider size={30} />
|
|
89
96
|
</>
|
|
90
97
|
)}
|
package/src/constants/wallets.ts
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { OnUpdateState } from './Wallets.types';
|
|
2
|
+
import type { LegacyEventHandler } from '@rango-dev/wallets-core/legacy';
|
|
3
|
+
|
|
4
|
+
import { LegacyEvents } from '@rango-dev/wallets-core/legacy';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* propagate updates for Dapps using external wallets
|
|
8
|
+
*
|
|
9
|
+
* Note: to take more control over the public interface and wallets-core interface (which may be changed) we use this layer
|
|
10
|
+
*/
|
|
11
|
+
export function propagateEvents(
|
|
12
|
+
cb: OnUpdateState,
|
|
13
|
+
eventParams: Parameters<LegacyEventHandler>
|
|
14
|
+
): void {
|
|
15
|
+
const [walletType, event, value, coreState, info] = eventParams;
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* PROVIDER_DISCONNECTED has conflict with WalletEventTypes.DISCONNECT since they are doing samething, the first one is using only for Hub, the second one is what we exposed to the lib users.
|
|
19
|
+
* so for backward-compat we need to keep the behavior of WalletEventTypes.DISCONNECT
|
|
20
|
+
*/
|
|
21
|
+
if (event === LegacyEvents.PROVIDER_DISCONNECTED) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
cb(walletType, event, value, coreState, info);
|
|
26
|
+
}
|
|
@@ -4,25 +4,18 @@ import type {
|
|
|
4
4
|
WidgetContextInterface,
|
|
5
5
|
} from './Wallets.types';
|
|
6
6
|
import type { ProvidersOptions } from '../../utils/providers';
|
|
7
|
-
import type { LegacyEventHandler
|
|
7
|
+
import type { LegacyEventHandler } from '@rango-dev/wallets-core/dist/legacy/mod';
|
|
8
8
|
import type { PropsWithChildren } from 'react';
|
|
9
9
|
|
|
10
|
-
import {
|
|
11
|
-
legacyFormatAddressWithNetwork as formatAddressWithNetwork,
|
|
12
|
-
legacyReadAccountAddress as readAccountAddress,
|
|
13
|
-
} from '@rango-dev/wallets-core/legacy';
|
|
14
|
-
import { Events, Provider } from '@rango-dev/wallets-react';
|
|
15
|
-
import { type Network } from '@rango-dev/wallets-shared';
|
|
16
|
-
import { isEvmBlockchain } from 'rango-sdk';
|
|
10
|
+
import { Provider } from '@rango-dev/wallets-react';
|
|
17
11
|
import React, { createContext, useEffect, useMemo, useRef } from 'react';
|
|
18
12
|
|
|
19
13
|
import { useWalletProviders } from '../../hooks/useWalletProviders';
|
|
20
14
|
import { AppStoreProvider, useAppStore } from '../../store/AppStore';
|
|
21
15
|
import { useUiStore } from '../../store/ui';
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
} from '../../utils/wallets';
|
|
16
|
+
|
|
17
|
+
import { useUpdates } from './useUpdates';
|
|
18
|
+
import { propagateEvents } from './Wallets.helpers';
|
|
26
19
|
|
|
27
20
|
export const WidgetContext = createContext<WidgetContextInterface>({
|
|
28
21
|
onConnectWallet: () => {
|
|
@@ -39,11 +32,8 @@ function Main(props: PropsWithChildren<PropTypes>) {
|
|
|
39
32
|
updateSettings,
|
|
40
33
|
fetch: fetchMeta,
|
|
41
34
|
fetchStatus,
|
|
42
|
-
removeBalancesForWallet,
|
|
43
35
|
} = useAppStore();
|
|
44
36
|
const blockchains = useAppStore().blockchains();
|
|
45
|
-
const { newWalletConnected, disconnectWallet, connectedWallets } =
|
|
46
|
-
useAppStore();
|
|
47
37
|
const config = useAppStore().config;
|
|
48
38
|
|
|
49
39
|
const walletOptions: ProvidersOptions = {
|
|
@@ -57,6 +47,10 @@ function Main(props: PropsWithChildren<PropTypes>) {
|
|
|
57
47
|
const { providers } = useWalletProviders(config.wallets, walletOptions);
|
|
58
48
|
const onConnectWalletHandler = useRef<OnWalletConnectionChange>();
|
|
59
49
|
const onDisconnectWalletHandler = useRef<OnWalletConnectionChange>();
|
|
50
|
+
const { handler: handleEvent } = useUpdates({
|
|
51
|
+
onConnectWalletHandler,
|
|
52
|
+
onDisconnectWalletHandler,
|
|
53
|
+
});
|
|
60
54
|
|
|
61
55
|
useEffect(() => {
|
|
62
56
|
void fetchMeta().catch(console.log);
|
|
@@ -73,117 +67,6 @@ function Main(props: PropsWithChildren<PropTypes>) {
|
|
|
73
67
|
}
|
|
74
68
|
}, [props.config, fetchStatus]);
|
|
75
69
|
|
|
76
|
-
const evmBasedChainNames = blockchains
|
|
77
|
-
.filter(isEvmBlockchain)
|
|
78
|
-
.map((chain) => chain.name);
|
|
79
|
-
|
|
80
|
-
const onUpdateState: EventHandler = (type, event, value, state, meta) => {
|
|
81
|
-
if (event === Events.ACCOUNTS) {
|
|
82
|
-
const supportedChainNames: Network[] | null =
|
|
83
|
-
walletAndSupportedChainsNames(meta.supportedBlockchains);
|
|
84
|
-
|
|
85
|
-
/*
|
|
86
|
-
* When a wallet is connecting to an evm account, we will consider it as the account exists on other evm-compatible blockchains
|
|
87
|
-
* To get their balances.
|
|
88
|
-
*
|
|
89
|
-
* The logic here is for handling switching account functionality in wallets. when a wallet is switching to another account
|
|
90
|
-
* we need to clean the balances for old accounts.
|
|
91
|
-
*/
|
|
92
|
-
const evmAccounts: string[] = [];
|
|
93
|
-
const nonEvmAccounts: string[] = [];
|
|
94
|
-
|
|
95
|
-
value?.forEach((account: string) => {
|
|
96
|
-
const { network } = readAccountAddress(account);
|
|
97
|
-
if (evmBasedChainNames.includes(network)) {
|
|
98
|
-
evmAccounts.push(account);
|
|
99
|
-
} else {
|
|
100
|
-
nonEvmAccounts.push(account);
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const previousAccounts = connectedWallets
|
|
105
|
-
.filter((wallet) => wallet.walletType === type)
|
|
106
|
-
.map((wallet) =>
|
|
107
|
-
formatAddressWithNetwork(wallet.address, wallet.chain)
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
if (previousAccounts.length > 0) {
|
|
111
|
-
if (evmAccounts.length > 0) {
|
|
112
|
-
// We use same logic for removing as we use for adding.
|
|
113
|
-
const data = prepareAccountsForWalletStore(
|
|
114
|
-
type,
|
|
115
|
-
evmAccounts,
|
|
116
|
-
evmBasedChainNames,
|
|
117
|
-
supportedChainNames,
|
|
118
|
-
meta.isContractWallet
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
removeBalancesForWallet(type, {
|
|
122
|
-
chains: data.map((account) => account.chain),
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (nonEvmAccounts.length > 0) {
|
|
127
|
-
removeBalancesForWallet(type, {
|
|
128
|
-
chains: nonEvmAccounts.map((account) => {
|
|
129
|
-
const { network } = readAccountAddress(account);
|
|
130
|
-
return network;
|
|
131
|
-
}),
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// After cleaning up balances, it's time to add new accounts.
|
|
137
|
-
if (value) {
|
|
138
|
-
const data = prepareAccountsForWalletStore(
|
|
139
|
-
type,
|
|
140
|
-
value,
|
|
141
|
-
evmBasedChainNames,
|
|
142
|
-
supportedChainNames,
|
|
143
|
-
meta.isContractWallet
|
|
144
|
-
);
|
|
145
|
-
if (data.length) {
|
|
146
|
-
void newWalletConnected(data);
|
|
147
|
-
}
|
|
148
|
-
} else {
|
|
149
|
-
disconnectWallet(type);
|
|
150
|
-
if (!!onDisconnectWalletHandler.current) {
|
|
151
|
-
onDisconnectWalletHandler.current(type);
|
|
152
|
-
} else {
|
|
153
|
-
console.warn(
|
|
154
|
-
`onDisconnectWallet handler hasn't been set. Are you sure?`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (
|
|
160
|
-
(event === Events.ACCOUNTS && state.connected) ||
|
|
161
|
-
// Hub works differently, and this check should be enough.
|
|
162
|
-
(event === Events.ACCOUNTS && meta.isHub)
|
|
163
|
-
) {
|
|
164
|
-
const key = `${type}-${state.network}-${value}`;
|
|
165
|
-
|
|
166
|
-
if (!!onConnectWalletHandler.current) {
|
|
167
|
-
onConnectWalletHandler.current(key);
|
|
168
|
-
} else {
|
|
169
|
-
console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (event === Events.NETWORK && state.network) {
|
|
174
|
-
const key = `${type}-${state.network}`;
|
|
175
|
-
if (!!onConnectWalletHandler.current) {
|
|
176
|
-
onConnectWalletHandler.current(key);
|
|
177
|
-
} else {
|
|
178
|
-
console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// propagate updates for Dapps using external wallets
|
|
183
|
-
if (props.onUpdateState) {
|
|
184
|
-
props.onUpdateState(type, event, value, state, meta);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
70
|
const isActiveTab = useUiStore.use.isActiveTab();
|
|
188
71
|
|
|
189
72
|
const handlers = useMemo(
|
|
@@ -203,7 +86,20 @@ function Main(props: PropsWithChildren<PropTypes>) {
|
|
|
203
86
|
<Provider
|
|
204
87
|
allBlockChains={blockchains}
|
|
205
88
|
providers={providers}
|
|
206
|
-
onUpdateState={
|
|
89
|
+
onUpdateState={(type, event, value, state, info) => {
|
|
90
|
+
const eventParams: Parameters<LegacyEventHandler> = [
|
|
91
|
+
type,
|
|
92
|
+
event,
|
|
93
|
+
value,
|
|
94
|
+
state,
|
|
95
|
+
info,
|
|
96
|
+
];
|
|
97
|
+
handleEvent(...eventParams);
|
|
98
|
+
|
|
99
|
+
if (props.onUpdateState) {
|
|
100
|
+
propagateEvents(props.onUpdateState, eventParams);
|
|
101
|
+
}
|
|
102
|
+
}}
|
|
207
103
|
autoConnect={!!isActiveTab}
|
|
208
104
|
configs={{
|
|
209
105
|
wallets: config.wallets,
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { WidgetConfig } from '../../types';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
LegacyEventHandler as EventHandler,
|
|
4
|
+
LegacyEvents,
|
|
5
|
+
} from '@rango-dev/wallets-core/legacy';
|
|
3
6
|
|
|
4
7
|
export type OnWalletConnectionChange = (key: string) => void;
|
|
5
8
|
export interface WidgetContextInterface {
|
|
@@ -17,7 +20,18 @@ export interface WidgetContextInterface {
|
|
|
17
20
|
onDisconnectWallet(handler: OnWalletConnectionChange): void;
|
|
18
21
|
}
|
|
19
22
|
|
|
23
|
+
type EventHandlerParams = Parameters<EventHandler>;
|
|
24
|
+
type EventParam = Exclude<LegacyEvents, LegacyEvents.PROVIDER_DISCONNECTED>;
|
|
25
|
+
|
|
26
|
+
export type OnUpdateState = (
|
|
27
|
+
type: EventHandlerParams[0],
|
|
28
|
+
event: EventParam,
|
|
29
|
+
value: EventHandlerParams[2],
|
|
30
|
+
coreState: EventHandlerParams[3],
|
|
31
|
+
info: EventHandlerParams[4]
|
|
32
|
+
) => void;
|
|
33
|
+
|
|
20
34
|
export interface PropTypes {
|
|
21
|
-
onUpdateState?:
|
|
35
|
+
onUpdateState?: OnUpdateState;
|
|
22
36
|
config: WidgetConfig;
|
|
23
37
|
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import type { OnWalletConnectionChange } from './Wallets.types';
|
|
2
|
+
import type {
|
|
3
|
+
LegacyEventHandler as EventHandler,
|
|
4
|
+
LegacyEventHandler,
|
|
5
|
+
} from '@rango-dev/wallets-core/legacy';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
legacyFormatAddressWithNetwork as formatAddressWithNetwork,
|
|
9
|
+
legacyReadAccountAddress as readAccountAddress,
|
|
10
|
+
} from '@rango-dev/wallets-core/legacy';
|
|
11
|
+
import { Events } from '@rango-dev/wallets-react';
|
|
12
|
+
import { type Network } from '@rango-dev/wallets-shared';
|
|
13
|
+
import { isEvmBlockchain } from 'rango-sdk';
|
|
14
|
+
|
|
15
|
+
import { useAppStore } from '../../store/AppStore';
|
|
16
|
+
import {
|
|
17
|
+
prepareAccountsForWalletStore,
|
|
18
|
+
walletAndSupportedChainsNames,
|
|
19
|
+
} from '../../utils/wallets';
|
|
20
|
+
|
|
21
|
+
interface UseUpdatesParams {
|
|
22
|
+
onConnectWalletHandler: React.MutableRefObject<
|
|
23
|
+
OnWalletConnectionChange | undefined
|
|
24
|
+
>;
|
|
25
|
+
onDisconnectWalletHandler: React.MutableRefObject<
|
|
26
|
+
OnWalletConnectionChange | undefined
|
|
27
|
+
>;
|
|
28
|
+
}
|
|
29
|
+
interface UseUpdates {
|
|
30
|
+
handler: EventHandler;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function useUpdates(params: UseUpdatesParams): UseUpdates {
|
|
34
|
+
const {
|
|
35
|
+
newWalletConnected,
|
|
36
|
+
disconnectWallet,
|
|
37
|
+
disconnectNamespaces,
|
|
38
|
+
connectedWallets,
|
|
39
|
+
removeBalancesForWallet,
|
|
40
|
+
blockchains,
|
|
41
|
+
} = useAppStore();
|
|
42
|
+
|
|
43
|
+
const { onConnectWalletHandler, onDisconnectWalletHandler } = params;
|
|
44
|
+
const evmBasedChainNames = blockchains()
|
|
45
|
+
.filter(isEvmBlockchain)
|
|
46
|
+
.map((chain) => chain.name);
|
|
47
|
+
|
|
48
|
+
const onAccountsEvent = (
|
|
49
|
+
event: Parameters<LegacyEventHandler>,
|
|
50
|
+
params: {
|
|
51
|
+
supportedChainNames: Network[] | null;
|
|
52
|
+
}
|
|
53
|
+
) => {
|
|
54
|
+
const [type, , value, , info] = event;
|
|
55
|
+
|
|
56
|
+
const data = prepareAccountsForWalletStore(
|
|
57
|
+
type,
|
|
58
|
+
value,
|
|
59
|
+
evmBasedChainNames,
|
|
60
|
+
params.supportedChainNames,
|
|
61
|
+
info.isContractWallet
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (data.length) {
|
|
65
|
+
void newWalletConnected(data, info.namespace);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const handleUpdatesForHub: EventHandler = (
|
|
70
|
+
type,
|
|
71
|
+
event,
|
|
72
|
+
value,
|
|
73
|
+
state,
|
|
74
|
+
info
|
|
75
|
+
) => {
|
|
76
|
+
if (event === Events.ACCOUNTS) {
|
|
77
|
+
const supportedChainNames: Network[] | null =
|
|
78
|
+
walletAndSupportedChainsNames(info.supportedBlockchains);
|
|
79
|
+
|
|
80
|
+
// After cleaning up balances, it's time to add new accounts.
|
|
81
|
+
if (value) {
|
|
82
|
+
onAccountsEvent([type, event, value, state, info], {
|
|
83
|
+
supportedChainNames,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (event === Events.CONNECTED) {
|
|
89
|
+
const key = `${type}-${state.network}-${value}`;
|
|
90
|
+
|
|
91
|
+
if (!!onConnectWalletHandler.current) {
|
|
92
|
+
onConnectWalletHandler.current(key);
|
|
93
|
+
} else {
|
|
94
|
+
console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (event === Events.PROVIDER_DISCONNECTED) {
|
|
99
|
+
disconnectWallet(type);
|
|
100
|
+
if (!!onDisconnectWalletHandler.current) {
|
|
101
|
+
onDisconnectWalletHandler.current(type);
|
|
102
|
+
} else {
|
|
103
|
+
console.warn(
|
|
104
|
+
`onDisconnectWallet handler hasn't been set. Are you sure?`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (event === Events.NAMESPACE_DISCONNECTED) {
|
|
110
|
+
disconnectNamespaces(type, value);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const handleUpdatesForLegacy: EventHandler = (
|
|
115
|
+
type,
|
|
116
|
+
event,
|
|
117
|
+
value,
|
|
118
|
+
state,
|
|
119
|
+
info
|
|
120
|
+
) => {
|
|
121
|
+
if (event === Events.ACCOUNTS) {
|
|
122
|
+
const supportedChainNames: Network[] | null =
|
|
123
|
+
walletAndSupportedChainsNames(info.supportedBlockchains);
|
|
124
|
+
|
|
125
|
+
/*
|
|
126
|
+
* When a wallet is connecting to an evm account, we will consider it as the account exists on other evm-compatible blockchains
|
|
127
|
+
* To get their balances.
|
|
128
|
+
*
|
|
129
|
+
* The logic here is for handling switching account functionality in wallets. when a wallet is switching to another account
|
|
130
|
+
* we need to clean the balances for old accounts.
|
|
131
|
+
*
|
|
132
|
+
* Note: hub will do the cleanup on namespace diconnected event.
|
|
133
|
+
*/
|
|
134
|
+
const evmAccounts: string[] = [];
|
|
135
|
+
const nonEvmAccounts: string[] = [];
|
|
136
|
+
|
|
137
|
+
value?.forEach((account: string) => {
|
|
138
|
+
const { network } = readAccountAddress(account);
|
|
139
|
+
if (evmBasedChainNames.includes(network)) {
|
|
140
|
+
evmAccounts.push(account);
|
|
141
|
+
} else {
|
|
142
|
+
nonEvmAccounts.push(account);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const previousAccounts = connectedWallets
|
|
147
|
+
.filter((wallet) => wallet.walletType === type)
|
|
148
|
+
.map((wallet) =>
|
|
149
|
+
formatAddressWithNetwork(wallet.address, wallet.chain)
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
if (previousAccounts.length > 0) {
|
|
153
|
+
if (evmAccounts.length > 0) {
|
|
154
|
+
// We use same logic for removing as we use for adding.
|
|
155
|
+
const data = prepareAccountsForWalletStore(
|
|
156
|
+
type,
|
|
157
|
+
evmAccounts,
|
|
158
|
+
evmBasedChainNames,
|
|
159
|
+
supportedChainNames,
|
|
160
|
+
info.isContractWallet
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
removeBalancesForWallet(type, {
|
|
164
|
+
chains: data.map((account) => account.chain),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (nonEvmAccounts.length > 0) {
|
|
169
|
+
removeBalancesForWallet(type, {
|
|
170
|
+
chains: nonEvmAccounts.map((account) => {
|
|
171
|
+
const { network } = readAccountAddress(account);
|
|
172
|
+
return network;
|
|
173
|
+
}),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// After cleaning up balances, it's time to add new accounts.
|
|
179
|
+
if (value) {
|
|
180
|
+
onAccountsEvent([type, event, value, state, info], {
|
|
181
|
+
supportedChainNames,
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
disconnectWallet(type);
|
|
185
|
+
if (!!onDisconnectWalletHandler.current) {
|
|
186
|
+
onDisconnectWalletHandler.current(type);
|
|
187
|
+
} else {
|
|
188
|
+
console.warn(
|
|
189
|
+
`onDisconnectWallet handler hasn't been set. Are you sure?`
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/*
|
|
196
|
+
* TODO: not sure why we used `Events.Acccounts` for detecting if a provider gets connected or not, if we can use `CONNCECTED` for the legacy
|
|
197
|
+
* we can only rely Events.Connected and remove the legacy conidition
|
|
198
|
+
*/
|
|
199
|
+
if (event === Events.ACCOUNTS && state.connected) {
|
|
200
|
+
const key = `${type}-${state.network}-${value}`;
|
|
201
|
+
|
|
202
|
+
if (!!onConnectWalletHandler.current) {
|
|
203
|
+
onConnectWalletHandler.current(key);
|
|
204
|
+
} else {
|
|
205
|
+
console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const handleUpdatesForBoth: EventHandler = (
|
|
211
|
+
type,
|
|
212
|
+
event,
|
|
213
|
+
_value,
|
|
214
|
+
state,
|
|
215
|
+
_info
|
|
216
|
+
) => {
|
|
217
|
+
if (event === Events.NETWORK && state.network) {
|
|
218
|
+
const key = `${type}-${state.network}`;
|
|
219
|
+
if (!!onConnectWalletHandler.current) {
|
|
220
|
+
onConnectWalletHandler.current(key);
|
|
221
|
+
} else {
|
|
222
|
+
console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const handler: EventHandler = (type, event, value, state, info) => {
|
|
228
|
+
if (info.isHub) {
|
|
229
|
+
handleUpdatesForHub(type, event, value, state, info);
|
|
230
|
+
} else {
|
|
231
|
+
handleUpdatesForLegacy(type, event, value, state, info);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
handleUpdatesForBoth(type, event, value, state, info);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
return {
|
|
238
|
+
handler,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
@@ -27,20 +27,30 @@ export function useSubscribeToWidgetEvents() {
|
|
|
27
27
|
event.type === StepEventType.SUCCEEDED;
|
|
28
28
|
|
|
29
29
|
if (shouldRefetchBalance) {
|
|
30
|
+
const fromWallet = route.wallets[step?.fromBlockchain];
|
|
30
31
|
const fromAccount = connectedWallets.find(
|
|
31
|
-
(
|
|
32
|
+
(connectedWallet) =>
|
|
33
|
+
connectedWallet.address?.toLocaleLowerCase() ===
|
|
34
|
+
fromWallet.address?.toLocaleLowerCase() &&
|
|
35
|
+
connectedWallet.walletType === fromWallet.walletType &&
|
|
36
|
+
connectedWallet.chain === step?.fromBlockchain
|
|
32
37
|
);
|
|
33
|
-
const toAccount =
|
|
34
|
-
step?.fromBlockchain !== step?.toBlockchain &&
|
|
35
|
-
connectedWallets.find(
|
|
36
|
-
(wallet) => wallet.chain === step?.toBlockchain
|
|
37
|
-
);
|
|
38
|
-
|
|
39
38
|
if (fromAccount) {
|
|
40
39
|
void fetchBalances([fromAccount]);
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
|
|
42
|
+
if (step?.fromBlockchain !== step?.toBlockchain) {
|
|
43
|
+
const toWallet = route.wallets[step?.toBlockchain];
|
|
44
|
+
const toAccount = connectedWallets.find(
|
|
45
|
+
(connectedWallet) =>
|
|
46
|
+
connectedWallet.address?.toLocaleLowerCase() ===
|
|
47
|
+
toWallet.address?.toLocaleLowerCase() &&
|
|
48
|
+
connectedWallet.walletType === toWallet.walletType &&
|
|
49
|
+
connectedWallet.chain === step?.toBlockchain
|
|
50
|
+
);
|
|
51
|
+
if (toAccount) {
|
|
52
|
+
void fetchBalances([toAccount]);
|
|
53
|
+
}
|
|
44
54
|
}
|
|
45
55
|
}
|
|
46
56
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { StateCreator } from 'zustand';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* you can wrap your slice in this middleware, it will update `lastUpdatedAt` for that slice when a zustand's `set` is calling
|
|
5
|
+
*/
|
|
6
|
+
export const keepLastUpdated: <Store, Slice extends { lastUpdatedAt: number }>(
|
|
7
|
+
config: StateCreator<Store, [], [], Slice>
|
|
8
|
+
) => StateCreator<Store, [], [], Slice> = (slice) => (set, get, api) => {
|
|
9
|
+
const modifedSet: typeof set = (...params) => {
|
|
10
|
+
set((state) => {
|
|
11
|
+
return {
|
|
12
|
+
...state,
|
|
13
|
+
lastUpdatedAt: +new Date(),
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
set(...params);
|
|
17
|
+
};
|
|
18
|
+
return slice(modifedSet, get, api);
|
|
19
|
+
};
|