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