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