@reown/appkit-core-react-native 2.0.0-alpha.1 → 2.0.0-alpha.3
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 +16 -21
- 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 +102 -112
- package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionsController.js +248 -51
- package/lib/commonjs/controllers/ConnectionsController.js.map +1 -1
- 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 +117 -53
- package/lib/commonjs/controllers/OnRampController.js.map +1 -1
- package/lib/commonjs/controllers/OptionsController.js +10 -4
- package/lib/commonjs/controllers/OptionsController.js.map +1 -1
- package/lib/commonjs/controllers/PublicStateController.js.map +1 -1
- package/lib/commonjs/controllers/RouterController.js +6 -21
- package/lib/commonjs/controllers/RouterController.js.map +1 -1
- package/lib/commonjs/controllers/SendController.js +100 -100
- 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 +109 -117
- package/lib/commonjs/controllers/SwapController.js.map +1 -1
- package/lib/commonjs/controllers/ThemeController.js.map +1 -1
- package/lib/commonjs/controllers/TransactionsController.js +23 -19
- 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 +3 -38
- 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 +6 -14
- package/lib/commonjs/utils/AssetUtil.js.map +1 -1
- package/lib/commonjs/utils/ConstantsUtil.js +13 -425
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/CoreHelperUtil.js +13 -8
- package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
- package/lib/commonjs/utils/EventUtil.js.map +1 -1
- package/lib/commonjs/utils/FetchUtil.js +34 -10
- package/lib/commonjs/utils/FetchUtil.js.map +1 -1
- package/lib/commonjs/utils/RouterUtil.js.map +1 -1
- package/lib/commonjs/utils/StorageUtil.js +77 -129
- package/lib/commonjs/utils/StorageUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapApiUtil.js +2 -17
- package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -1
- package/lib/module/controllers/ApiController.js +17 -21
- 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 +103 -111
- package/lib/module/controllers/BlockchainApiController.js.map +1 -1
- package/lib/module/controllers/ConnectionsController.js +250 -51
- package/lib/module/controllers/ConnectionsController.js.map +1 -1
- package/lib/module/controllers/EnsController.js +4 -2
- package/lib/module/controllers/EnsController.js.map +1 -1
- package/lib/module/controllers/EventsController.js +3 -1
- 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 +118 -52
- package/lib/module/controllers/OnRampController.js.map +1 -1
- package/lib/module/controllers/OptionsController.js +12 -4
- 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 +8 -21
- package/lib/module/controllers/RouterController.js.map +1 -1
- package/lib/module/controllers/SendController.js +99 -97
- 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 +111 -117
- package/lib/module/controllers/SwapController.js.map +1 -1
- package/lib/module/controllers/ThemeController.js +2 -0
- package/lib/module/controllers/ThemeController.js.map +1 -1
- package/lib/module/controllers/TransactionsController.js +23 -17
- 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 +5 -6
- 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 +8 -14
- package/lib/module/utils/AssetUtil.js.map +1 -1
- package/lib/module/utils/ConstantsUtil.js +15 -425
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/CoreHelperUtil.js +15 -6
- 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 +36 -10
- 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 +80 -129
- package/lib/module/utils/StorageUtil.js.map +1 -1
- package/lib/module/utils/SwapApiUtil.js +4 -17
- 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/typescript/controllers/ApiController.d.ts +3 -4
- 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 +21 -13
- package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionsController.d.ts +19 -17
- package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -1
- 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 +1 -1
- package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
- package/lib/typescript/controllers/OnRampController.d.ts +4 -3
- package/lib/typescript/controllers/OnRampController.d.ts.map +1 -1
- package/lib/typescript/controllers/OptionsController.d.ts +4 -4
- package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/RouterController.d.ts +5 -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 +11 -11
- package/lib/typescript/controllers/SwapController.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 +1 -7
- 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 +7 -415
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/CoreHelperUtil.d.ts +4 -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 +1 -1
- package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
- package/lib/typescript/utils/StorageUtil.d.ts +8 -19
- 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/package.json +9 -23
- package/src/controllers/ApiController.ts +24 -28
- package/src/controllers/AssetController.ts +0 -6
- package/src/controllers/BlockchainApiController.ts +76 -121
- package/src/controllers/ConnectionsController.ts +308 -78
- package/src/controllers/EnsController.ts +3 -3
- package/src/controllers/EventsController.ts +2 -2
- package/src/controllers/ModalController.ts +7 -9
- package/src/controllers/OnRampController.ts +179 -82
- package/src/controllers/OptionsController.ts +24 -8
- package/src/controllers/RouterController.ts +12 -54
- package/src/controllers/SendController.ts +123 -112
- package/src/controllers/SnackController.ts +31 -5
- package/src/controllers/SwapController.ts +130 -146
- package/src/controllers/TransactionsController.ts +24 -18
- package/src/controllers/WcController.ts +93 -0
- package/src/index.ts +2 -21
- package/src/utils/AssetUtil.ts +7 -17
- package/src/utils/ConstantsUtil.ts +15 -429
- package/src/utils/CoreHelperUtil.ts +31 -8
- package/src/utils/EventUtil.ts +1 -1
- package/src/utils/FetchUtil.ts +38 -11
- package/src/utils/StorageUtil.ts +146 -157
- package/src/utils/SwapApiUtil.ts +18 -37
- package/src/utils/SwapCalculationUtil.ts +1 -2
- package/lib/commonjs/controllers/AccountController.js +0 -93
- package/lib/commonjs/controllers/AccountController.js.map +0 -1
- package/lib/commonjs/controllers/ConnectionController.js +0 -132
- package/lib/commonjs/controllers/ConnectionController.js.map +0 -1
- package/lib/commonjs/controllers/ConnectorController.js +0 -50
- 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/NetworkUtil.js +0 -46
- package/lib/commonjs/utils/NetworkUtil.js.map +0 -1
- package/lib/commonjs/utils/TypeUtil.js +0 -6
- 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 -127
- package/lib/module/controllers/ConnectionController.js.map +0 -1
- package/lib/module/controllers/ConnectorController.js +0 -45
- 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/NetworkUtil.js +0 -40
- package/lib/module/utils/NetworkUtil.js.map +0 -1
- package/lib/module/utils/TypeUtil.js +0 -2
- 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 -68
- 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/NetworkUtil.d.ts +0 -8
- package/lib/typescript/utils/NetworkUtil.d.ts.map +0 -1
- package/lib/typescript/utils/TypeUtil.d.ts +0 -817
- package/lib/typescript/utils/TypeUtil.d.ts.map +0 -1
- package/src/controllers/AccountController.ts +0 -128
- package/src/controllers/ConnectionController.ts +0 -208
- package/src/controllers/ConnectorController.ts +0 -64
- package/src/controllers/NetworkController.ts +0 -120
- package/src/controllers/WebviewController.ts +0 -63
- package/src/utils/NetworkUtil.ts +0 -33
- package/src/utils/TypeUtil.ts +0 -995
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { proxy, ref } from 'valtio';
|
|
2
|
-
import { derive } from 'valtio
|
|
2
|
+
import { derive } from 'derive-valtio';
|
|
3
3
|
import {
|
|
4
4
|
EVMAdapter,
|
|
5
5
|
type AppKitNetwork,
|
|
@@ -7,23 +7,20 @@ import {
|
|
|
7
7
|
type CaipAddress,
|
|
8
8
|
type CaipNetworkId,
|
|
9
9
|
type ChainNamespace,
|
|
10
|
-
type
|
|
11
|
-
type WalletInfo
|
|
10
|
+
type Balance,
|
|
11
|
+
type WalletInfo,
|
|
12
|
+
type ConnectionProperties,
|
|
13
|
+
type AccountType,
|
|
14
|
+
type Connection,
|
|
15
|
+
SolanaBaseAdapter,
|
|
16
|
+
type Identity
|
|
12
17
|
} from '@reown/appkit-common-react-native';
|
|
13
18
|
import { StorageUtil } from '../utils/StorageUtil';
|
|
19
|
+
import { BlockchainApiController } from './BlockchainApiController';
|
|
20
|
+
import { SnackController } from './SnackController';
|
|
21
|
+
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
|
|
14
22
|
|
|
15
23
|
// -- Types --------------------------------------------- //
|
|
16
|
-
type Balance = GetBalanceResponse;
|
|
17
|
-
|
|
18
|
-
//TODO: balance could be elsewhere
|
|
19
|
-
interface Connection {
|
|
20
|
-
accounts: CaipAddress[];
|
|
21
|
-
balances: Record<CaipAddress, Balance>; //TODO: make this an array of balances
|
|
22
|
-
adapter: BlockchainAdapter;
|
|
23
|
-
caipNetwork: CaipNetworkId;
|
|
24
|
-
wallet?: WalletInfo;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
24
|
export interface ConnectionsControllerState {
|
|
28
25
|
activeNamespace?: ChainNamespace;
|
|
29
26
|
connections: Map<ChainNamespace, Connection>;
|
|
@@ -37,59 +34,115 @@ const baseState = proxy<ConnectionsControllerState>({
|
|
|
37
34
|
networks: []
|
|
38
35
|
});
|
|
39
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
|
+
|
|
40
97
|
const derivedState = derive(
|
|
41
98
|
{
|
|
42
|
-
|
|
99
|
+
isConnected: (get): boolean => {
|
|
43
100
|
const snap = get(baseState);
|
|
44
101
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const connection =
|
|
50
|
-
|
|
51
|
-
if (!connection || !connection.accounts || connection.accounts.length === 0) {
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
102
|
+
return !!snap.activeNamespace && !!snap.connections.size;
|
|
103
|
+
},
|
|
104
|
+
activeAddress: (get): CaipAddress | undefined => {
|
|
105
|
+
const snap = get(baseState);
|
|
106
|
+
const connection = getActiveConnection(snap);
|
|
54
107
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
);
|
|
108
|
+
return connection ? getActiveAddress(connection) : undefined;
|
|
109
|
+
},
|
|
110
|
+
identity: (get): Identity | undefined => {
|
|
111
|
+
const snap = get(baseState);
|
|
112
|
+
const connection = getActiveConnection(snap);
|
|
59
113
|
|
|
60
|
-
return
|
|
114
|
+
return connection ? getActiveIdentity(connection) : undefined;
|
|
61
115
|
},
|
|
62
116
|
activeBalance: (get): Balance | undefined => {
|
|
63
117
|
const snap = get(baseState);
|
|
118
|
+
const connection = getActiveConnection(snap);
|
|
64
119
|
|
|
65
|
-
if (!
|
|
66
|
-
const connection = snap.connections.get(snap.activeNamespace);
|
|
67
|
-
|
|
68
|
-
if (!connection || !connection.accounts || connection.accounts.length === 0) {
|
|
120
|
+
if (!connection) {
|
|
69
121
|
return undefined;
|
|
70
122
|
}
|
|
71
123
|
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
124
|
+
const activeAddress = getActiveAddress(connection);
|
|
125
|
+
if (!activeAddress || !connection.balances || connection.balances.size === 0) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
75
128
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
!connection.balances ||
|
|
79
|
-
!activeAccount ||
|
|
80
|
-
Object.keys(connection.balances).length === 0
|
|
81
|
-
) {
|
|
129
|
+
const addressBalances = connection.balances.get(activeAddress);
|
|
130
|
+
if (!addressBalances || addressBalances.length === 0) {
|
|
82
131
|
return undefined;
|
|
83
132
|
}
|
|
84
133
|
|
|
85
|
-
|
|
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];
|
|
86
142
|
},
|
|
87
143
|
activeNetwork: (get): AppKitNetwork | undefined => {
|
|
88
144
|
const snap = get(baseState);
|
|
89
|
-
|
|
90
|
-
if (!snap.activeNamespace) return undefined;
|
|
91
|
-
|
|
92
|
-
const connection = snap.connections.get(snap.activeNamespace);
|
|
145
|
+
const connection = getActiveConnection(snap);
|
|
93
146
|
|
|
94
147
|
if (!connection) return undefined;
|
|
95
148
|
|
|
@@ -101,21 +154,46 @@ const derivedState = derive(
|
|
|
101
154
|
},
|
|
102
155
|
activeCaipNetworkId: (get): CaipNetworkId | undefined => {
|
|
103
156
|
const snap = get(baseState);
|
|
157
|
+
const connection = getActiveConnection(snap);
|
|
104
158
|
|
|
105
|
-
|
|
159
|
+
return connection?.caipNetwork;
|
|
160
|
+
},
|
|
161
|
+
accountType: (get): AccountType | undefined => {
|
|
162
|
+
const snap = get(baseState);
|
|
163
|
+
const connection = getActiveConnection(snap);
|
|
106
164
|
|
|
107
|
-
|
|
165
|
+
return connection?.type;
|
|
166
|
+
},
|
|
167
|
+
connection: (get): Connection | undefined => {
|
|
168
|
+
const snap = get(baseState);
|
|
108
169
|
|
|
109
|
-
|
|
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 [];
|
|
110
184
|
|
|
111
|
-
return
|
|
185
|
+
return (
|
|
186
|
+
_connection?.balances
|
|
187
|
+
.get(_activeAddress)
|
|
188
|
+
// Filter out tokens with no quantity
|
|
189
|
+
?.filter(balance => balance?.quantity?.numeric)
|
|
190
|
+
);
|
|
112
191
|
},
|
|
113
192
|
walletInfo: (get): WalletInfo | undefined => {
|
|
114
193
|
const snap = get(baseState);
|
|
194
|
+
const connection = getActiveConnection(snap);
|
|
115
195
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return snap.connections.get(snap.activeNamespace)?.wallet;
|
|
196
|
+
return connection?.wallet;
|
|
119
197
|
}
|
|
120
198
|
},
|
|
121
199
|
{
|
|
@@ -132,28 +210,35 @@ export const ConnectionsController = {
|
|
|
132
210
|
StorageUtil.setActiveNamespace(namespace);
|
|
133
211
|
},
|
|
134
212
|
|
|
135
|
-
|
|
136
|
-
namespace,
|
|
137
|
-
adapter,
|
|
213
|
+
setConnection({
|
|
138
214
|
accounts,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
215
|
+
adapter,
|
|
216
|
+
caipNetwork,
|
|
217
|
+
namespace,
|
|
218
|
+
properties,
|
|
219
|
+
wallet
|
|
142
220
|
}: {
|
|
143
|
-
namespace: ChainNamespace;
|
|
144
|
-
adapter: BlockchainAdapter;
|
|
145
221
|
accounts: CaipAddress[];
|
|
146
|
-
|
|
222
|
+
adapter: BlockchainAdapter;
|
|
223
|
+
caipNetwork: CaipNetworkId;
|
|
224
|
+
namespace: ChainNamespace;
|
|
225
|
+
properties?: ConnectionProperties;
|
|
147
226
|
wallet?: WalletInfo;
|
|
148
|
-
caipNetwork?: CaipNetworkId;
|
|
149
227
|
}) {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
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,
|
|
153
237
|
adapter: ref(adapter),
|
|
154
238
|
accounts,
|
|
155
|
-
|
|
156
|
-
|
|
239
|
+
wallet,
|
|
240
|
+
properties,
|
|
241
|
+
type
|
|
157
242
|
};
|
|
158
243
|
|
|
159
244
|
// Create a new Map to ensure Valtio detects the change
|
|
@@ -177,14 +262,62 @@ export const ConnectionsController = {
|
|
|
177
262
|
updateBalance(namespace: ChainNamespace, address: CaipAddress, balance: Balance) {
|
|
178
263
|
const connection = baseState.connections.get(namespace);
|
|
179
264
|
if (!connection) {
|
|
265
|
+
console.warn(`No connection found for namespace: ${namespace}`);
|
|
266
|
+
|
|
180
267
|
return;
|
|
181
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
|
+
}
|
|
182
276
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
+
}
|
|
188
321
|
},
|
|
189
322
|
|
|
190
323
|
setActiveNetwork(namespace: ChainNamespace, networkId: CaipNetworkId) {
|
|
@@ -213,6 +346,16 @@ export const ConnectionsController = {
|
|
|
213
346
|
);
|
|
214
347
|
},
|
|
215
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
|
+
|
|
216
359
|
async disconnect(namespace: ChainNamespace, isInternal = true) {
|
|
217
360
|
const connection = baseState.connections.get(namespace);
|
|
218
361
|
if (!connection) return;
|
|
@@ -265,27 +408,114 @@ export const ConnectionsController = {
|
|
|
265
408
|
?.adapter.parseUnits(value, decimals);
|
|
266
409
|
},
|
|
267
410
|
|
|
268
|
-
async
|
|
411
|
+
async signMessage(address: CaipAddress, message: string) {
|
|
269
412
|
if (!baseState.activeNamespace) return undefined;
|
|
270
413
|
|
|
414
|
+
const [namespace, chainId, plainAddress] = address.split(':');
|
|
415
|
+
|
|
416
|
+
if (!namespace || namespace !== baseState.activeNamespace || !chainId || !plainAddress) {
|
|
417
|
+
return undefined;
|
|
418
|
+
}
|
|
419
|
+
|
|
271
420
|
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
272
421
|
|
|
273
|
-
if (adapter instanceof EVMAdapter) {
|
|
274
|
-
return adapter.
|
|
422
|
+
if (adapter instanceof EVMAdapter && plainAddress && chainId) {
|
|
423
|
+
return adapter.signMessage(plainAddress, message, chainId);
|
|
275
424
|
}
|
|
276
425
|
|
|
277
426
|
return undefined;
|
|
278
427
|
},
|
|
279
428
|
|
|
280
|
-
async
|
|
429
|
+
async sendTransaction(args: any) {
|
|
281
430
|
if (!baseState.activeNamespace) return undefined;
|
|
282
431
|
|
|
283
432
|
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
433
|
+
if (adapter instanceof EVMAdapter || adapter instanceof SolanaBaseAdapter) {
|
|
434
|
+
return adapter.sendTransaction(args);
|
|
435
|
+
}
|
|
284
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;
|
|
285
444
|
if (adapter instanceof EVMAdapter) {
|
|
286
445
|
return adapter.estimateGas(args);
|
|
287
446
|
}
|
|
288
447
|
|
|
289
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
|
+
});
|
|
290
520
|
}
|
|
291
521
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { subscribeKey as subKey } from 'valtio/
|
|
2
|
-
import { proxy, subscribe as sub } from 'valtio
|
|
1
|
+
import { subscribeKey as subKey } from 'valtio/utils';
|
|
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
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { proxy, subscribe as sub } from 'valtio
|
|
1
|
+
import { proxy, subscribe as sub } from 'valtio';
|
|
2
2
|
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,11 +1,9 @@
|
|
|
1
1
|
import { proxy } from 'valtio';
|
|
2
|
-
import {
|
|
3
|
-
import type { RouterControllerState } from './RouterController';
|
|
4
|
-
import { RouterController } from './RouterController';
|
|
2
|
+
import { RouterController, type RouterControllerState } from './RouterController';
|
|
5
3
|
import { PublicStateController } from './PublicStateController';
|
|
6
4
|
import { EventsController } from './EventsController';
|
|
7
5
|
import { ApiController } from './ApiController';
|
|
8
|
-
import {
|
|
6
|
+
import { ConnectionsController } from './ConnectionsController';
|
|
9
7
|
|
|
10
8
|
// -- Types --------------------------------------------- //
|
|
11
9
|
export interface ModalControllerState {
|
|
@@ -31,11 +29,11 @@ export const ModalController = {
|
|
|
31
29
|
|
|
32
30
|
async open(options?: ModalControllerArguments['open']) {
|
|
33
31
|
await ApiController.state.prefetchPromise;
|
|
34
|
-
const
|
|
32
|
+
const isConnected = ConnectionsController.state.isConnected;
|
|
35
33
|
if (options?.view) {
|
|
36
34
|
RouterController.reset(options.view);
|
|
37
|
-
} else if (
|
|
38
|
-
const isUniversalWallet =
|
|
35
|
+
} else if (isConnected) {
|
|
36
|
+
const isUniversalWallet = !!ConnectionsController.state.connection?.properties?.provider;
|
|
39
37
|
RouterController.reset(isUniversalWallet ? 'Account' : 'AccountDefault');
|
|
40
38
|
} else {
|
|
41
39
|
RouterController.reset('Connect');
|
|
@@ -45,12 +43,12 @@ export const ModalController = {
|
|
|
45
43
|
EventsController.sendEvent({
|
|
46
44
|
type: 'track',
|
|
47
45
|
event: 'MODAL_OPEN',
|
|
48
|
-
properties: { connected }
|
|
46
|
+
properties: { connected: isConnected }
|
|
49
47
|
});
|
|
50
48
|
},
|
|
51
49
|
|
|
52
50
|
close() {
|
|
53
|
-
const connected =
|
|
51
|
+
const connected = ConnectionsController.state.isConnected;
|
|
54
52
|
state.open = false;
|
|
55
53
|
PublicStateController.set({ open: false });
|
|
56
54
|
EventsController.sendEvent({
|