@reown/appkit-core-react-native 0.0.0-develop-20250728153935 → 0.0.0-develop-20251008155354
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/lib/commonjs/controllers/ApiController.js +157 -91
- package/lib/commonjs/controllers/ApiController.js.map +1 -1
- package/lib/commonjs/controllers/AssetController.js +0 -4
- package/lib/commonjs/controllers/AssetController.js.map +1 -1
- package/lib/commonjs/controllers/BlockchainApiController.js +180 -78
- package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionsController.js +425 -0
- package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
- package/lib/commonjs/controllers/EnsController.js +2 -0
- package/lib/commonjs/controllers/EnsController.js.map +1 -1
- package/lib/commonjs/controllers/EventsController.js +51 -1
- package/lib/commonjs/controllers/EventsController.js.map +1 -1
- package/lib/commonjs/controllers/LogController.js +188 -0
- package/lib/commonjs/controllers/LogController.js.map +1 -0
- package/lib/commonjs/controllers/ModalController.js +9 -7
- package/lib/commonjs/controllers/ModalController.js.map +1 -1
- package/lib/commonjs/controllers/OnRampController.js +47 -20
- package/lib/commonjs/controllers/OnRampController.js.map +1 -1
- package/lib/commonjs/controllers/OptionsController.js +28 -10
- package/lib/commonjs/controllers/OptionsController.js.map +1 -1
- package/lib/commonjs/controllers/PublicStateController.js.map +1 -1
- package/lib/commonjs/controllers/RouterController.js +7 -22
- package/lib/commonjs/controllers/RouterController.js.map +1 -1
- package/lib/commonjs/controllers/SendController.js +111 -95
- package/lib/commonjs/controllers/SendController.js.map +1 -1
- package/lib/commonjs/controllers/SnackController.js +29 -5
- package/lib/commonjs/controllers/SnackController.js.map +1 -1
- package/lib/commonjs/controllers/SwapController.js +162 -151
- package/lib/commonjs/controllers/SwapController.js.map +1 -1
- package/lib/commonjs/controllers/ThemeController.js +14 -1
- package/lib/commonjs/controllers/ThemeController.js.map +1 -1
- package/lib/commonjs/controllers/TransactionsController.js +24 -18
- package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
- package/lib/commonjs/controllers/WcController.js +73 -0
- package/lib/commonjs/controllers/WcController.js.map +1 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +272 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
- package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
- package/lib/commonjs/features/reown-authentication/index.js +28 -0
- package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
- package/lib/commonjs/index.js +33 -40
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/utils/ApiUtil.js +1 -1
- package/lib/commonjs/utils/ApiUtil.js.map +1 -1
- package/lib/commonjs/utils/AssetUtil.js +8 -18
- package/lib/commonjs/utils/AssetUtil.js.map +1 -1
- package/lib/commonjs/utils/ConstantsUtil.js +117 -5
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/CoreHelperUtil.js +32 -9
- package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
- package/lib/commonjs/utils/EventUtil.js.map +1 -1
- package/lib/commonjs/utils/FetchUtil.js +7 -2
- package/lib/commonjs/utils/FetchUtil.js.map +1 -1
- package/lib/commonjs/utils/LogUtils.js +131 -0
- package/lib/commonjs/utils/LogUtils.js.map +1 -0
- package/lib/commonjs/utils/StorageUtil.js +131 -150
- package/lib/commonjs/utils/StorageUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapApiUtil.js +15 -21
- package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -1
- package/lib/commonjs/utils/WalletUtil.js +23 -0
- package/lib/commonjs/utils/WalletUtil.js.map +1 -0
- package/lib/module/controllers/ApiController.js +159 -91
- package/lib/module/controllers/ApiController.js.map +1 -1
- package/lib/module/controllers/AssetController.js +2 -4
- package/lib/module/controllers/AssetController.js.map +1 -1
- package/lib/module/controllers/BlockchainApiController.js +182 -78
- package/lib/module/controllers/BlockchainApiController.js.map +1 -1
- package/lib/module/controllers/ConnectionsController.js +422 -0
- package/lib/module/controllers/ConnectionsController.js.map +1 -0
- package/lib/module/controllers/EnsController.js +4 -0
- package/lib/module/controllers/EnsController.js.map +1 -1
- package/lib/module/controllers/EventsController.js +53 -1
- package/lib/module/controllers/EventsController.js.map +1 -1
- package/lib/module/controllers/LogController.js +185 -0
- package/lib/module/controllers/LogController.js.map +1 -0
- package/lib/module/controllers/ModalController.js +11 -7
- package/lib/module/controllers/ModalController.js.map +1 -1
- package/lib/module/controllers/OnRampController.js +45 -16
- package/lib/module/controllers/OnRampController.js.map +1 -1
- package/lib/module/controllers/OptionsController.js +30 -10
- package/lib/module/controllers/OptionsController.js.map +1 -1
- package/lib/module/controllers/PublicStateController.js +2 -0
- package/lib/module/controllers/PublicStateController.js.map +1 -1
- package/lib/module/controllers/RouterController.js +9 -22
- package/lib/module/controllers/RouterController.js.map +1 -1
- package/lib/module/controllers/SendController.js +113 -95
- package/lib/module/controllers/SendController.js.map +1 -1
- package/lib/module/controllers/SnackController.js +31 -5
- package/lib/module/controllers/SnackController.js.map +1 -1
- package/lib/module/controllers/SwapController.js +164 -151
- package/lib/module/controllers/SwapController.js.map +1 -1
- package/lib/module/controllers/ThemeController.js +16 -1
- package/lib/module/controllers/ThemeController.js.map +1 -1
- package/lib/module/controllers/TransactionsController.js +26 -18
- package/lib/module/controllers/TransactionsController.js.map +1 -1
- package/lib/module/controllers/WcController.js +70 -0
- package/lib/module/controllers/WcController.js.map +1 -0
- package/lib/module/features/reown-authentication/ReownAuthentication.js +268 -0
- package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
- package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
- package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
- package/lib/module/features/reown-authentication/index.js +5 -0
- package/lib/module/features/reown-authentication/index.js.map +1 -0
- package/lib/module/index.js +13 -8
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/ApiUtil.js +3 -1
- package/lib/module/utils/ApiUtil.js.map +1 -1
- package/lib/module/utils/AssetUtil.js +10 -18
- package/lib/module/utils/AssetUtil.js.map +1 -1
- package/lib/module/utils/ConstantsUtil.js +119 -5
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/CoreHelperUtil.js +34 -7
- package/lib/module/utils/CoreHelperUtil.js.map +1 -1
- package/lib/module/utils/EventUtil.js +2 -0
- package/lib/module/utils/EventUtil.js.map +1 -1
- package/lib/module/utils/FetchUtil.js +10 -2
- package/lib/module/utils/FetchUtil.js.map +1 -1
- package/lib/module/utils/LogUtils.js +121 -0
- package/lib/module/utils/LogUtils.js.map +1 -0
- package/lib/module/utils/StorageUtil.js +134 -150
- package/lib/module/utils/StorageUtil.js.map +1 -1
- package/lib/module/utils/SwapApiUtil.js +17 -21
- package/lib/module/utils/SwapApiUtil.js.map +1 -1
- package/lib/module/utils/SwapCalculationUtil.js +3 -0
- package/lib/module/utils/SwapCalculationUtil.js.map +1 -1
- package/lib/module/utils/WalletUtil.js +19 -0
- package/lib/module/utils/WalletUtil.js.map +1 -0
- package/lib/typescript/controllers/ApiController.d.ts +9 -8
- package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/AssetController.d.ts +0 -2
- package/lib/typescript/controllers/AssetController.d.ts.map +1 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts +29 -11
- package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionsController.d.ts +47 -0
- package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
- package/lib/typescript/controllers/EnsController.d.ts +1 -1
- package/lib/typescript/controllers/EnsController.d.ts.map +1 -1
- package/lib/typescript/controllers/EventsController.d.ts +13 -1
- package/lib/typescript/controllers/EventsController.d.ts.map +1 -1
- package/lib/typescript/controllers/LogController.d.ts +65 -0
- package/lib/typescript/controllers/LogController.d.ts.map +1 -0
- package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
- package/lib/typescript/controllers/OnRampController.d.ts +2 -2
- package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
- package/lib/typescript/controllers/OptionsController.d.ts +11 -6
- package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/PublicStateController.d.ts +1 -1
- package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -1
- package/lib/typescript/controllers/RouterController.d.ts +6 -20
- package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
- package/lib/typescript/controllers/SendController.d.ts +5 -7
- package/lib/typescript/controllers/SendController.d.ts.map +1 -1
- package/lib/typescript/controllers/SnackController.d.ts +3 -3
- package/lib/typescript/controllers/SnackController.d.ts.map +1 -1
- package/lib/typescript/controllers/SwapController.d.ts +12 -12
- package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
- package/lib/typescript/controllers/ThemeController.d.ts +3 -1
- package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
- package/lib/typescript/controllers/TransactionsController.d.ts +3 -3
- package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/WcController.d.ts +27 -0
- package/lib/typescript/controllers/WcController.d.ts.map +1 -0
- package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
- package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
- package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
- package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
- package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
- package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +7 -9
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/ApiUtil.d.ts +1 -1
- package/lib/typescript/utils/AssetUtil.d.ts +3 -4
- package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
- package/lib/typescript/utils/ConstantsUtil.d.ts +11 -2
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/CoreHelperUtil.d.ts +8 -6
- package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
- package/lib/typescript/utils/EventUtil.d.ts +1 -1
- package/lib/typescript/utils/EventUtil.d.ts.map +1 -1
- package/lib/typescript/utils/FetchUtil.d.ts +4 -4
- package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
- package/lib/typescript/utils/LogUtils.d.ts +15 -0
- package/lib/typescript/utils/LogUtils.d.ts.map +1 -0
- package/lib/typescript/utils/StorageUtil.d.ts +15 -16
- package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapApiUtil.d.ts +3 -4
- package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapCalculationUtil.d.ts +1 -1
- package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -1
- package/lib/typescript/utils/WalletUtil.d.ts +5 -0
- package/lib/typescript/utils/WalletUtil.d.ts.map +1 -0
- package/package.json +11 -24
- package/src/controllers/ApiController.ts +167 -84
- package/src/controllers/AssetController.ts +0 -6
- package/src/controllers/BlockchainApiController.ts +168 -66
- package/src/controllers/ConnectionsController.ts +575 -0
- package/src/controllers/EnsController.ts +3 -1
- package/src/controllers/EventsController.ts +73 -2
- package/src/controllers/LogController.ts +250 -0
- package/src/controllers/ModalController.ts +10 -7
- package/src/controllers/OnRampController.ts +54 -19
- package/src/controllers/OptionsController.ts +46 -18
- package/src/controllers/PublicStateController.ts +1 -1
- package/src/controllers/RouterController.ts +20 -57
- package/src/controllers/SendController.ts +147 -111
- package/src/controllers/SnackController.ts +31 -5
- package/src/controllers/SwapController.ts +185 -183
- package/src/controllers/ThemeController.ts +18 -2
- package/src/controllers/TransactionsController.ts +27 -19
- package/src/controllers/WcController.ts +93 -0
- package/src/features/reown-authentication/ReownAuthentication.ts +475 -0
- package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
- package/src/features/reown-authentication/index.ts +2 -0
- package/src/index.ts +17 -20
- package/src/utils/ApiUtil.ts +1 -1
- package/src/utils/AssetUtil.ts +9 -22
- package/src/utils/ConstantsUtil.ts +121 -6
- package/src/utils/CoreHelperUtil.ts +66 -10
- package/src/utils/EventUtil.ts +1 -1
- package/src/utils/FetchUtil.ts +14 -5
- package/src/utils/LogUtils.ts +179 -0
- package/src/utils/StorageUtil.ts +191 -176
- package/src/utils/SwapApiUtil.ts +30 -37
- package/src/utils/SwapCalculationUtil.ts +1 -2
- package/src/utils/WalletUtil.ts +14 -0
- package/lib/commonjs/controllers/AccountController.js +0 -93
- package/lib/commonjs/controllers/AccountController.js.map +0 -1
- package/lib/commonjs/controllers/ConnectionController.js +0 -126
- package/lib/commonjs/controllers/ConnectionController.js.map +0 -1
- package/lib/commonjs/controllers/ConnectorController.js +0 -49
- package/lib/commonjs/controllers/ConnectorController.js.map +0 -1
- package/lib/commonjs/controllers/NetworkController.js +0 -90
- package/lib/commonjs/controllers/NetworkController.js.map +0 -1
- package/lib/commonjs/controllers/WebviewController.js +0 -52
- package/lib/commonjs/controllers/WebviewController.js.map +0 -1
- package/lib/commonjs/utils/ConnectionUtil.js +0 -33
- package/lib/commonjs/utils/ConnectionUtil.js.map +0 -1
- package/lib/commonjs/utils/NetworkUtil.js +0 -46
- package/lib/commonjs/utils/NetworkUtil.js.map +0 -1
- package/lib/commonjs/utils/RouterUtil.js +0 -25
- package/lib/commonjs/utils/RouterUtil.js.map +0 -1
- package/lib/commonjs/utils/TypeUtil.js +0 -35
- package/lib/commonjs/utils/TypeUtil.js.map +0 -1
- package/lib/module/controllers/AccountController.js +0 -88
- package/lib/module/controllers/AccountController.js.map +0 -1
- package/lib/module/controllers/ConnectionController.js +0 -121
- package/lib/module/controllers/ConnectionController.js.map +0 -1
- package/lib/module/controllers/ConnectorController.js +0 -44
- package/lib/module/controllers/ConnectorController.js.map +0 -1
- package/lib/module/controllers/NetworkController.js +0 -85
- package/lib/module/controllers/NetworkController.js.map +0 -1
- package/lib/module/controllers/WebviewController.js +0 -47
- package/lib/module/controllers/WebviewController.js.map +0 -1
- package/lib/module/utils/ConnectionUtil.js +0 -27
- package/lib/module/utils/ConnectionUtil.js.map +0 -1
- package/lib/module/utils/NetworkUtil.js +0 -40
- package/lib/module/utils/NetworkUtil.js.map +0 -1
- package/lib/module/utils/RouterUtil.js +0 -19
- package/lib/module/utils/RouterUtil.js.map +0 -1
- package/lib/module/utils/TypeUtil.js +0 -28
- package/lib/module/utils/TypeUtil.js.map +0 -1
- package/lib/typescript/controllers/AccountController.d.ts +0 -33
- package/lib/typescript/controllers/AccountController.d.ts.map +0 -1
- package/lib/typescript/controllers/ConnectionController.d.ts +0 -66
- package/lib/typescript/controllers/ConnectionController.d.ts.map +0 -1
- package/lib/typescript/controllers/ConnectorController.d.ts +0 -18
- package/lib/typescript/controllers/ConnectorController.d.ts.map +0 -1
- package/lib/typescript/controllers/NetworkController.d.ts +0 -34
- package/lib/typescript/controllers/NetworkController.d.ts.map +0 -1
- package/lib/typescript/controllers/WebviewController.d.ts +0 -21
- package/lib/typescript/controllers/WebviewController.d.ts.map +0 -1
- package/lib/typescript/utils/ConnectionUtil.d.ts +0 -4
- package/lib/typescript/utils/ConnectionUtil.d.ts.map +0 -1
- package/lib/typescript/utils/NetworkUtil.d.ts +0 -8
- package/lib/typescript/utils/NetworkUtil.d.ts.map +0 -1
- package/lib/typescript/utils/RouterUtil.d.ts +0 -5
- package/lib/typescript/utils/RouterUtil.d.ts.map +0 -1
- package/lib/typescript/utils/TypeUtil.d.ts +0 -850
- package/lib/typescript/utils/TypeUtil.d.ts.map +0 -1
- package/src/controllers/AccountController.ts +0 -128
- package/src/controllers/ConnectionController.ts +0 -200
- package/src/controllers/ConnectorController.ts +0 -63
- package/src/controllers/NetworkController.ts +0 -120
- package/src/controllers/WebviewController.ts +0 -63
- package/src/utils/ConnectionUtil.ts +0 -27
- package/src/utils/NetworkUtil.ts +0 -33
- package/src/utils/RouterUtil.ts +0 -21
- package/src/utils/TypeUtil.ts +0 -1039
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import { proxy, ref } from 'valtio';
|
|
2
|
+
import { derive } from 'derive-valtio';
|
|
3
|
+
import {
|
|
4
|
+
EVMAdapter,
|
|
5
|
+
type AppKitNetwork,
|
|
6
|
+
type BlockchainAdapter,
|
|
7
|
+
type CaipAddress,
|
|
8
|
+
type CaipNetworkId,
|
|
9
|
+
type ChainNamespace,
|
|
10
|
+
type Balance,
|
|
11
|
+
type WalletInfo,
|
|
12
|
+
type ConnectionProperties,
|
|
13
|
+
type AccountType,
|
|
14
|
+
type Connection,
|
|
15
|
+
SolanaBaseAdapter,
|
|
16
|
+
BitcoinBaseAdapter,
|
|
17
|
+
type Identity
|
|
18
|
+
} from '@reown/appkit-common-react-native';
|
|
19
|
+
import { StorageUtil } from '../utils/StorageUtil';
|
|
20
|
+
import { BlockchainApiController } from './BlockchainApiController';
|
|
21
|
+
import { SnackController } from './SnackController';
|
|
22
|
+
import { LogController } from './LogController';
|
|
23
|
+
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
|
|
24
|
+
|
|
25
|
+
// -- Types --------------------------------------------- //
|
|
26
|
+
export interface ConnectionsControllerState {
|
|
27
|
+
activeNamespace?: ChainNamespace;
|
|
28
|
+
connections: Map<ChainNamespace, Connection>;
|
|
29
|
+
networks: AppKitNetwork[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// -- State --------------------------------------------- //
|
|
33
|
+
const baseState = proxy<ConnectionsControllerState>({
|
|
34
|
+
activeNamespace: undefined,
|
|
35
|
+
connections: new Map<ChainNamespace, Connection>(),
|
|
36
|
+
networks: []
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// -- Helper Functions --------------------------------------------- //
|
|
40
|
+
const getActiveConnection = (snap: ConnectionsControllerState): Connection | undefined => {
|
|
41
|
+
if (!snap.activeNamespace) return undefined;
|
|
42
|
+
|
|
43
|
+
return snap.connections.get(snap.activeNamespace);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const hasValidAccounts = (connection: Connection): boolean => {
|
|
47
|
+
return connection?.accounts && connection.accounts.length > 0;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const findSmartAccountForNetwork = (connection: Connection): CaipAddress | undefined => {
|
|
51
|
+
return connection.properties?.smartAccounts?.find(account =>
|
|
52
|
+
account.startsWith(connection.caipNetwork)
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const findEOAForNetwork = (connection: Connection): CaipAddress | undefined => {
|
|
57
|
+
const smartAccounts = connection.properties?.smartAccounts || [];
|
|
58
|
+
|
|
59
|
+
return connection.accounts.find(
|
|
60
|
+
account => account.startsWith(connection.caipNetwork) && !smartAccounts.includes(account)
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const getActiveAddress = (connection: Connection): CaipAddress | undefined => {
|
|
65
|
+
if (!hasValidAccounts(connection)) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// For smart accounts, prioritize smart account addresses
|
|
70
|
+
if (connection.type === 'smartAccount') {
|
|
71
|
+
const smartAccount = findSmartAccountForNetwork(connection);
|
|
72
|
+
if (smartAccount) {
|
|
73
|
+
return smartAccount;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Fall back to EOA or any account that matches the network
|
|
78
|
+
return findEOAForNetwork(connection);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const updateConnection = (
|
|
82
|
+
namespace: ChainNamespace,
|
|
83
|
+
connection: Connection,
|
|
84
|
+
updates: Partial<Connection>
|
|
85
|
+
): Connection => {
|
|
86
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
87
|
+
const newConnection = { ...connection, ...updates };
|
|
88
|
+
newConnectionsMap.set(namespace, newConnection);
|
|
89
|
+
baseState.connections = newConnectionsMap;
|
|
90
|
+
|
|
91
|
+
return newConnection;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const getActiveIdentity = (connection: Connection): Identity | undefined => {
|
|
95
|
+
const activeAddress = getActiveAddress(connection);
|
|
96
|
+
if (!activeAddress) return undefined;
|
|
97
|
+
|
|
98
|
+
return connection.identities?.get(activeAddress);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const derivedState = derive(
|
|
102
|
+
{
|
|
103
|
+
isConnected: (get): boolean => {
|
|
104
|
+
const snap = get(baseState);
|
|
105
|
+
|
|
106
|
+
return !!snap.activeNamespace && !!snap.connections.size;
|
|
107
|
+
},
|
|
108
|
+
activeAddress: (get): CaipAddress | undefined => {
|
|
109
|
+
const snap = get(baseState);
|
|
110
|
+
const connection = getActiveConnection(snap);
|
|
111
|
+
|
|
112
|
+
return connection ? getActiveAddress(connection) : undefined;
|
|
113
|
+
},
|
|
114
|
+
identity: (get): Identity | undefined => {
|
|
115
|
+
const snap = get(baseState);
|
|
116
|
+
const connection = getActiveConnection(snap);
|
|
117
|
+
|
|
118
|
+
return connection ? getActiveIdentity(connection) : undefined;
|
|
119
|
+
},
|
|
120
|
+
activeBalance: (get): Balance | undefined => {
|
|
121
|
+
const snap = get(baseState);
|
|
122
|
+
const connection = getActiveConnection(snap);
|
|
123
|
+
|
|
124
|
+
if (!connection) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const activeAddress = getActiveAddress(connection);
|
|
129
|
+
if (!activeAddress || !connection.balances || connection.balances.size === 0) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const addressBalances = connection.balances.get(activeAddress);
|
|
134
|
+
if (!addressBalances || addressBalances.length === 0) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Return the native token (first balance without address)
|
|
139
|
+
const nativeToken = addressBalances.find(balance => !balance.address);
|
|
140
|
+
if (nativeToken) {
|
|
141
|
+
return nativeToken;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Fallback to first available balance
|
|
145
|
+
return addressBalances[0];
|
|
146
|
+
},
|
|
147
|
+
activeNetwork: (get): AppKitNetwork | undefined => {
|
|
148
|
+
const snap = get(baseState);
|
|
149
|
+
const connection = getActiveConnection(snap);
|
|
150
|
+
|
|
151
|
+
if (!connection) return undefined;
|
|
152
|
+
|
|
153
|
+
return snap.networks.find(
|
|
154
|
+
network =>
|
|
155
|
+
network.chainNamespace === snap.activeNamespace &&
|
|
156
|
+
network.id?.toString() === connection.caipNetwork?.split(':')[1]
|
|
157
|
+
);
|
|
158
|
+
},
|
|
159
|
+
activeCaipNetworkId: (get): CaipNetworkId | undefined => {
|
|
160
|
+
const snap = get(baseState);
|
|
161
|
+
const connection = getActiveConnection(snap);
|
|
162
|
+
|
|
163
|
+
return connection?.caipNetwork;
|
|
164
|
+
},
|
|
165
|
+
accountType: (get): AccountType | undefined => {
|
|
166
|
+
const snap = get(baseState);
|
|
167
|
+
const connection = getActiveConnection(snap);
|
|
168
|
+
|
|
169
|
+
return connection?.type;
|
|
170
|
+
},
|
|
171
|
+
connection: (get): Connection | undefined => {
|
|
172
|
+
const snap = get(baseState);
|
|
173
|
+
|
|
174
|
+
return getActiveConnection(snap);
|
|
175
|
+
},
|
|
176
|
+
balances: (get): Balance[] | undefined => {
|
|
177
|
+
const snap = get(baseState);
|
|
178
|
+
|
|
179
|
+
const _connection = getActiveConnection(snap);
|
|
180
|
+
|
|
181
|
+
if (!_connection) {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const _activeAddress = getActiveAddress(_connection);
|
|
186
|
+
|
|
187
|
+
if (!_activeAddress) return [];
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
_connection?.balances
|
|
191
|
+
.get(_activeAddress)
|
|
192
|
+
// Filter out tokens with no quantity
|
|
193
|
+
?.filter(balance => balance?.quantity?.numeric)
|
|
194
|
+
);
|
|
195
|
+
},
|
|
196
|
+
walletInfo: (get): WalletInfo | undefined => {
|
|
197
|
+
const snap = get(baseState);
|
|
198
|
+
const connection = getActiveConnection(snap);
|
|
199
|
+
|
|
200
|
+
return connection?.wallet;
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
proxy: baseState // Link derived proxy to the base state proxy
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// -- Controller ---------------------------------------- //
|
|
209
|
+
export const ConnectionsController = {
|
|
210
|
+
state: derivedState,
|
|
211
|
+
|
|
212
|
+
setActiveNamespace(namespace?: ChainNamespace) {
|
|
213
|
+
if (baseState.activeNamespace === namespace) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
baseState.activeNamespace = namespace;
|
|
218
|
+
StorageUtil.setActiveNamespace(namespace);
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
setConnection({
|
|
222
|
+
accounts,
|
|
223
|
+
adapter,
|
|
224
|
+
caipNetwork,
|
|
225
|
+
namespace,
|
|
226
|
+
properties,
|
|
227
|
+
wallet
|
|
228
|
+
}: {
|
|
229
|
+
accounts: CaipAddress[];
|
|
230
|
+
adapter: BlockchainAdapter;
|
|
231
|
+
caipNetwork: CaipNetworkId;
|
|
232
|
+
namespace: ChainNamespace;
|
|
233
|
+
properties?: ConnectionProperties;
|
|
234
|
+
wallet?: WalletInfo;
|
|
235
|
+
}) {
|
|
236
|
+
const type: AccountType =
|
|
237
|
+
properties?.smartAccounts?.length &&
|
|
238
|
+
properties.smartAccounts.find(account => account.startsWith(caipNetwork))
|
|
239
|
+
? 'smartAccount'
|
|
240
|
+
: 'eoa';
|
|
241
|
+
|
|
242
|
+
const newConnectionEntry: Connection = {
|
|
243
|
+
balances: new Map<CaipAddress, Balance[]>(),
|
|
244
|
+
caipNetwork,
|
|
245
|
+
adapter: ref(adapter),
|
|
246
|
+
accounts,
|
|
247
|
+
wallet,
|
|
248
|
+
properties,
|
|
249
|
+
type
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// Create a new Map to ensure Valtio detects the change
|
|
253
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
254
|
+
newConnectionsMap.set(namespace, newConnectionEntry);
|
|
255
|
+
baseState.connections = newConnectionsMap;
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
updateAccounts(namespace: ChainNamespace, accounts: CaipAddress[]): boolean {
|
|
259
|
+
const connection = baseState.connections.get(namespace);
|
|
260
|
+
if (!connection) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (connection.accounts.toString() === accounts.toString()) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
updateConnection(namespace, connection, { accounts });
|
|
269
|
+
|
|
270
|
+
return true;
|
|
271
|
+
},
|
|
272
|
+
|
|
273
|
+
updateBalance(namespace: ChainNamespace, address: CaipAddress, balance: Balance) {
|
|
274
|
+
const connection = baseState.connections.get(namespace);
|
|
275
|
+
if (!connection) {
|
|
276
|
+
console.warn(`No connection found for namespace: ${namespace}`);
|
|
277
|
+
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const newBalances = new Map(connection.balances);
|
|
281
|
+
const existingBalances = connection.balances.get(address) || [];
|
|
282
|
+
// Check if this token already exists by contract address or symbol
|
|
283
|
+
const existingIndex = existingBalances.findIndex(existingBalance => {
|
|
284
|
+
if (balance.address && existingBalance.address) {
|
|
285
|
+
return (
|
|
286
|
+
CoreHelperUtil.getPlainAddress(existingBalance.address) ===
|
|
287
|
+
CoreHelperUtil.getPlainAddress(balance.address)
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return existingBalance.symbol === balance.symbol;
|
|
292
|
+
});
|
|
293
|
+
let updatedBalances: Balance[];
|
|
294
|
+
if (existingIndex >= 0) {
|
|
295
|
+
// Update existing token
|
|
296
|
+
updatedBalances = [...existingBalances];
|
|
297
|
+
updatedBalances[existingIndex] = {
|
|
298
|
+
...updatedBalances[existingIndex],
|
|
299
|
+
...balance
|
|
300
|
+
};
|
|
301
|
+
} else {
|
|
302
|
+
// Add new token
|
|
303
|
+
updatedBalances = [...existingBalances, balance];
|
|
304
|
+
}
|
|
305
|
+
newBalances.set(address, updatedBalances);
|
|
306
|
+
updateConnection(namespace, connection, { balances: newBalances });
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
updateIdentity(
|
|
310
|
+
namespace: ChainNamespace,
|
|
311
|
+
connection: Connection,
|
|
312
|
+
plainAddress: string,
|
|
313
|
+
identity: Identity
|
|
314
|
+
) {
|
|
315
|
+
const accounts = connection.accounts.filter(
|
|
316
|
+
account => CoreHelperUtil.getPlainAddress(account) === plainAddress
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
if (accounts.length > 0) {
|
|
320
|
+
const newIdentities = new Map(connection.identities || []);
|
|
321
|
+
let hasChanges = false;
|
|
322
|
+
|
|
323
|
+
accounts.forEach(account => {
|
|
324
|
+
const existingIdentity = newIdentities.get(account);
|
|
325
|
+
if (!existingIdentity || existingIdentity.name !== identity.name) {
|
|
326
|
+
newIdentities.set(account, identity);
|
|
327
|
+
hasChanges = true;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
if (hasChanges) {
|
|
332
|
+
updateConnection(namespace, connection, { identities: newIdentities });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
|
|
337
|
+
setActiveNetwork(namespace: ChainNamespace, networkId: CaipNetworkId) {
|
|
338
|
+
const connection = baseState.connections.get(namespace);
|
|
339
|
+
|
|
340
|
+
if (!connection) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
updateConnection(namespace, connection, { caipNetwork: networkId });
|
|
345
|
+
|
|
346
|
+
this.setActiveNamespace(namespace);
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
setNetworks(networks: AppKitNetwork[]) {
|
|
350
|
+
baseState.networks = networks;
|
|
351
|
+
},
|
|
352
|
+
|
|
353
|
+
getConnectedNetworks() {
|
|
354
|
+
const connection = getActiveConnection(baseState);
|
|
355
|
+
if (!connection) {
|
|
356
|
+
return [];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return baseState.networks.filter(
|
|
360
|
+
network =>
|
|
361
|
+
baseState.connections
|
|
362
|
+
.get(network.chainNamespace)
|
|
363
|
+
?.accounts.some(account => account.startsWith(network.caipNetworkId))
|
|
364
|
+
);
|
|
365
|
+
},
|
|
366
|
+
|
|
367
|
+
getAvailableNetworks() {
|
|
368
|
+
const connection = getActiveConnection(baseState);
|
|
369
|
+
|
|
370
|
+
if (!connection) {
|
|
371
|
+
return baseState.networks;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const canAddEvmChain = connection.properties?.canAddEvmChain;
|
|
375
|
+
|
|
376
|
+
return baseState.networks.filter(
|
|
377
|
+
network =>
|
|
378
|
+
baseState.connections
|
|
379
|
+
.get(network.chainNamespace)
|
|
380
|
+
?.accounts.some(account => account.startsWith(network.caipNetworkId)) ||
|
|
381
|
+
(canAddEvmChain && network.chainNamespace === 'eip155')
|
|
382
|
+
);
|
|
383
|
+
},
|
|
384
|
+
|
|
385
|
+
setAccountType(namespace: ChainNamespace, type: AccountType) {
|
|
386
|
+
const connection = baseState.connections.get(namespace);
|
|
387
|
+
if (!connection) return;
|
|
388
|
+
|
|
389
|
+
const newConnection = updateConnection(namespace, connection, { type });
|
|
390
|
+
|
|
391
|
+
return getActiveAddress(newConnection);
|
|
392
|
+
},
|
|
393
|
+
|
|
394
|
+
getActiveNetworkId(namespace?: ChainNamespace) {
|
|
395
|
+
const connection = namespace
|
|
396
|
+
? baseState.connections.get(namespace)
|
|
397
|
+
: getActiveConnection(baseState);
|
|
398
|
+
if (!connection) return undefined;
|
|
399
|
+
|
|
400
|
+
return connection.caipNetwork;
|
|
401
|
+
},
|
|
402
|
+
|
|
403
|
+
async disconnect(namespace: ChainNamespace, isInternal = true) {
|
|
404
|
+
const connection = baseState.connections.get(namespace);
|
|
405
|
+
if (!connection) return;
|
|
406
|
+
|
|
407
|
+
// Get the current connector from the adapter
|
|
408
|
+
const connector = connection.adapter.connector;
|
|
409
|
+
if (!connector) return;
|
|
410
|
+
|
|
411
|
+
// Find all namespaces that use the same connector
|
|
412
|
+
const namespacesUsingConnector = Array.from(baseState.connections.keys()).filter(
|
|
413
|
+
ns => baseState.connections.get(ns)?.adapter.connector === connector
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
// Unsubscribe all event listeners from the adapter
|
|
417
|
+
namespacesUsingConnector.forEach(ns => {
|
|
418
|
+
const _connection = baseState.connections.get(ns);
|
|
419
|
+
if (_connection?.adapter) {
|
|
420
|
+
_connection.adapter.removeAllListeners();
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
// Disconnect the adapter
|
|
425
|
+
if (isInternal) {
|
|
426
|
+
await connection.adapter.disconnect();
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Remove all namespaces that used this connector
|
|
430
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
431
|
+
namespacesUsingConnector.forEach(ns => {
|
|
432
|
+
newConnectionsMap.delete(ns);
|
|
433
|
+
});
|
|
434
|
+
baseState.connections = newConnectionsMap;
|
|
435
|
+
|
|
436
|
+
// Remove activeNamespace if it is in the list of namespaces using the connector
|
|
437
|
+
if (
|
|
438
|
+
baseState.activeNamespace &&
|
|
439
|
+
(baseState.activeNamespace === namespace ||
|
|
440
|
+
namespacesUsingConnector.includes(baseState.activeNamespace))
|
|
441
|
+
) {
|
|
442
|
+
baseState.activeNamespace = undefined;
|
|
443
|
+
StorageUtil.setActiveNamespace(undefined);
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
|
|
447
|
+
parseUnits(value: string, decimals: number) {
|
|
448
|
+
if (!baseState.activeNamespace) return undefined;
|
|
449
|
+
|
|
450
|
+
return baseState.connections
|
|
451
|
+
.get(baseState.activeNamespace)
|
|
452
|
+
?.adapter.parseUnits(value, decimals);
|
|
453
|
+
},
|
|
454
|
+
|
|
455
|
+
async signMessage(address: CaipAddress, message: string) {
|
|
456
|
+
if (!baseState.activeNamespace) return undefined;
|
|
457
|
+
|
|
458
|
+
const [namespace, chainId, plainAddress] = address.split(':');
|
|
459
|
+
|
|
460
|
+
if (!namespace || namespace !== baseState.activeNamespace || !chainId || !plainAddress) {
|
|
461
|
+
return undefined;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
465
|
+
|
|
466
|
+
if (
|
|
467
|
+
(adapter instanceof EVMAdapter ||
|
|
468
|
+
adapter instanceof SolanaBaseAdapter ||
|
|
469
|
+
adapter instanceof BitcoinBaseAdapter) &&
|
|
470
|
+
plainAddress &&
|
|
471
|
+
chainId
|
|
472
|
+
) {
|
|
473
|
+
return adapter.signMessage(plainAddress, message, chainId);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return undefined;
|
|
477
|
+
},
|
|
478
|
+
|
|
479
|
+
async sendTransaction(args: any) {
|
|
480
|
+
if (!baseState.activeNamespace) return undefined;
|
|
481
|
+
|
|
482
|
+
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
483
|
+
if (adapter instanceof EVMAdapter || adapter instanceof SolanaBaseAdapter) {
|
|
484
|
+
return adapter.sendTransaction(args);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
return undefined;
|
|
488
|
+
},
|
|
489
|
+
|
|
490
|
+
async estimateGas(args: any) {
|
|
491
|
+
if (!baseState.activeNamespace || baseState.activeNamespace !== 'eip155') return undefined;
|
|
492
|
+
|
|
493
|
+
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
494
|
+
if (adapter instanceof EVMAdapter) {
|
|
495
|
+
return adapter.estimateGas(args);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return undefined;
|
|
499
|
+
},
|
|
500
|
+
|
|
501
|
+
async writeContract(args: any) {
|
|
502
|
+
if (!baseState.activeNamespace) return undefined;
|
|
503
|
+
|
|
504
|
+
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
505
|
+
if (adapter instanceof EVMAdapter) {
|
|
506
|
+
return adapter.writeContract(args);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return undefined;
|
|
510
|
+
},
|
|
511
|
+
|
|
512
|
+
async fetchBalance(forceUpdateAddresses?: CaipAddress[]) {
|
|
513
|
+
const connection = getActiveConnection(baseState);
|
|
514
|
+
if (!connection) {
|
|
515
|
+
throw new Error('No active connection found for balance fetch');
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const chainId = connection.caipNetwork;
|
|
519
|
+
const address = getActiveAddress(connection);
|
|
520
|
+
const namespace = baseState.activeNamespace;
|
|
521
|
+
if (!namespace || !address || !chainId) {
|
|
522
|
+
throw new Error('Missing required data for balance fetch');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
try {
|
|
526
|
+
const response = await BlockchainApiController.getBalance(address, forceUpdateAddresses);
|
|
527
|
+
if (!response) {
|
|
528
|
+
throw new Error('Failed to fetch token balance');
|
|
529
|
+
}
|
|
530
|
+
// Update balances for each token in the response
|
|
531
|
+
response.balances.forEach(balance => {
|
|
532
|
+
this.updateBalance(namespace, address, {
|
|
533
|
+
name: balance.name,
|
|
534
|
+
symbol: balance.symbol,
|
|
535
|
+
amount: balance.quantity.numeric,
|
|
536
|
+
address: balance.address,
|
|
537
|
+
quantity: balance.quantity,
|
|
538
|
+
price: balance.price,
|
|
539
|
+
value: balance.value,
|
|
540
|
+
iconUrl: balance.iconUrl
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
} catch (error) {
|
|
544
|
+
LogController.sendError(error, 'ConnectionsController.ts', 'fetchBalance', {
|
|
545
|
+
address,
|
|
546
|
+
namespace
|
|
547
|
+
});
|
|
548
|
+
SnackController.showError('Failed to get account balance');
|
|
549
|
+
}
|
|
550
|
+
},
|
|
551
|
+
|
|
552
|
+
getSmartAccountEnabledNetworks(): AppKitNetwork[] {
|
|
553
|
+
const activeConnection = getActiveConnection(baseState);
|
|
554
|
+
if (!activeConnection) {
|
|
555
|
+
return [];
|
|
556
|
+
}
|
|
557
|
+
if (!activeConnection.properties?.smartAccounts?.length) {
|
|
558
|
+
return [];
|
|
559
|
+
}
|
|
560
|
+
const smartAccountNetworks = new Set<CaipNetworkId>();
|
|
561
|
+
activeConnection.properties.smartAccounts.forEach(smartAccount => {
|
|
562
|
+
const parts = smartAccount.split(':');
|
|
563
|
+
if (parts.length >= 2) {
|
|
564
|
+
const networkId: CaipNetworkId = `${parts[0]}:${parts[1]}`;
|
|
565
|
+
smartAccountNetworks.add(networkId);
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
return baseState.networks.filter(network => {
|
|
570
|
+
const networkId: CaipNetworkId = `${network.chainNamespace}:${network.id}`;
|
|
571
|
+
|
|
572
|
+
return smartAccountNetworks.has(networkId);
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { subscribeKey as subKey } from 'valtio/utils';
|
|
2
2
|
import { proxy, subscribe as sub } from 'valtio';
|
|
3
3
|
import { BlockchainApiController } from './BlockchainApiController';
|
|
4
|
-
import type { BlockchainApiEnsError } from '
|
|
4
|
+
import type { BlockchainApiEnsError } from '@reown/appkit-common-react-native';
|
|
5
|
+
import { LogController } from './LogController';
|
|
5
6
|
|
|
6
7
|
// -- Types --------------------------------------------- //
|
|
7
8
|
|
|
@@ -32,6 +33,7 @@ export const EnsController = {
|
|
|
32
33
|
try {
|
|
33
34
|
return await BlockchainApiController.lookupEnsName(name);
|
|
34
35
|
} catch (e) {
|
|
36
|
+
LogController.sendError(e, 'EnsController.ts', 'resolveName');
|
|
35
37
|
const error = e as BlockchainApiEnsError;
|
|
36
38
|
throw new Error(error?.reasons?.[0]?.description || 'Error resolving name');
|
|
37
39
|
}
|
|
@@ -3,17 +3,25 @@ import { ApiController } from './ApiController';
|
|
|
3
3
|
import { OptionsController } from './OptionsController';
|
|
4
4
|
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
|
|
5
5
|
import { FetchUtil } from '../utils/FetchUtil';
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
Event,
|
|
8
|
+
EventName,
|
|
9
|
+
WalletImpressionItem,
|
|
10
|
+
WcWallet
|
|
11
|
+
} from '@reown/appkit-common-react-native';
|
|
7
12
|
|
|
8
13
|
// -- Helpers ------------------------------------------- //
|
|
9
14
|
const baseUrl = CoreHelperUtil.getAnalyticsUrl();
|
|
10
15
|
const api = new FetchUtil({ baseUrl });
|
|
11
16
|
const excluded = ['MODAL_CREATED'];
|
|
17
|
+
const IMPRESSION_TIMEOUT = 3000;
|
|
12
18
|
|
|
13
19
|
// -- Types --------------------------------------------- //
|
|
14
20
|
export interface EventsControllerState {
|
|
15
21
|
timestamp: number;
|
|
16
22
|
data: Event;
|
|
23
|
+
pendingWalletImpressions: WalletImpressionItem[];
|
|
24
|
+
pendingImpressionTimeout?: NodeJS.Timeout;
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
// -- State --------------------------------------------- //
|
|
@@ -22,7 +30,9 @@ const state = proxy<EventsControllerState>({
|
|
|
22
30
|
data: {
|
|
23
31
|
type: 'track',
|
|
24
32
|
event: 'MODAL_CREATED' // just for init purposes
|
|
25
|
-
}
|
|
33
|
+
},
|
|
34
|
+
pendingWalletImpressions: [],
|
|
35
|
+
pendingImpressionTimeout: undefined
|
|
26
36
|
});
|
|
27
37
|
|
|
28
38
|
// -- Controller ---------------------------------------- //
|
|
@@ -41,6 +51,53 @@ export const EventsController = {
|
|
|
41
51
|
});
|
|
42
52
|
},
|
|
43
53
|
|
|
54
|
+
trackWalletImpression(props: {
|
|
55
|
+
wallet: WcWallet;
|
|
56
|
+
view: 'Connect' | 'AllWallets';
|
|
57
|
+
displayIndex: number;
|
|
58
|
+
query?: string;
|
|
59
|
+
installed?: boolean;
|
|
60
|
+
}) {
|
|
61
|
+
state.pendingWalletImpressions.push({
|
|
62
|
+
name: props.wallet.name ?? 'Unknown',
|
|
63
|
+
walletRank: props.wallet.order,
|
|
64
|
+
explorerId: props.wallet.id,
|
|
65
|
+
certified: props.wallet.badge_type === 'certified',
|
|
66
|
+
displayIndex: props.displayIndex,
|
|
67
|
+
view: props.view,
|
|
68
|
+
query: props.query,
|
|
69
|
+
installed: props.installed
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (state.pendingImpressionTimeout) {
|
|
73
|
+
clearTimeout(state.pendingImpressionTimeout);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
state.pendingImpressionTimeout = setTimeout(() => {
|
|
77
|
+
EventsController.sendWalletImpressions();
|
|
78
|
+
}, IMPRESSION_TIMEOUT);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
sendWalletImpressions() {
|
|
82
|
+
if (state.pendingImpressionTimeout) {
|
|
83
|
+
clearTimeout(state.pendingImpressionTimeout);
|
|
84
|
+
state.pendingImpressionTimeout = undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const impressions = state.pendingWalletImpressions;
|
|
88
|
+
|
|
89
|
+
if (impressions.length === 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
state.pendingWalletImpressions = [];
|
|
94
|
+
EventsController.sendEvent({
|
|
95
|
+
type: 'track',
|
|
96
|
+
event: 'WALLET_IMPRESSION',
|
|
97
|
+
items: impressions
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
|
|
44
101
|
async _sendAnalyticsEvent(data: EventsControllerState['data'], timestamp: number) {
|
|
45
102
|
if (excluded.includes(data.event)) {
|
|
46
103
|
return;
|
|
@@ -50,6 +107,7 @@ export const EventsController = {
|
|
|
50
107
|
await api.post({
|
|
51
108
|
path: '/e',
|
|
52
109
|
headers: ApiController._getApiHeaders(),
|
|
110
|
+
params: ApiController._getApiParams(),
|
|
53
111
|
body: {
|
|
54
112
|
eventId: CoreHelperUtil.getUUID(),
|
|
55
113
|
bundleId: CoreHelperUtil.getBundleId(),
|
|
@@ -71,5 +129,18 @@ export const EventsController = {
|
|
|
71
129
|
if (OptionsController.state.enableAnalytics) {
|
|
72
130
|
EventsController._sendAnalyticsEvent(data, timestamp);
|
|
73
131
|
}
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
resetState() {
|
|
135
|
+
if (state.pendingImpressionTimeout) {
|
|
136
|
+
clearTimeout(state.pendingImpressionTimeout);
|
|
137
|
+
state.pendingImpressionTimeout = undefined;
|
|
138
|
+
}
|
|
139
|
+
state.pendingWalletImpressions = [];
|
|
140
|
+
state.data = {
|
|
141
|
+
type: 'track',
|
|
142
|
+
event: 'MODAL_CREATED'
|
|
143
|
+
};
|
|
144
|
+
state.timestamp = Date.now();
|
|
74
145
|
}
|
|
75
146
|
};
|