@reown/appkit-core-react-native 0.0.0-feat-onramp-20250409135734 → 0.0.0-feat-multichain-20250513150920
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 +11 -11
- package/lib/commonjs/controllers/ApiController.js.map +1 -1
- package/lib/commonjs/controllers/BlockchainApiController.js +102 -14
- package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionController.js +7 -1
- package/lib/commonjs/controllers/ConnectionController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionsController.js +156 -0
- package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
- package/lib/commonjs/controllers/ConnectorController.js +1 -0
- package/lib/commonjs/controllers/ConnectorController.js.map +1 -1
- package/lib/commonjs/controllers/OptionsController.js +3 -3
- package/lib/commonjs/controllers/OptionsController.js.map +1 -1
- package/lib/commonjs/controllers/RouterController.js.map +1 -1
- package/lib/commonjs/controllers/SwapController.js +13 -5
- 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 +2 -2
- package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
- package/lib/commonjs/index.js +3 -3
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/AssetUtil.js +5 -6
- package/lib/commonjs/utils/AssetUtil.js.map +1 -1
- package/lib/commonjs/utils/ConstantsUtil.js +11 -5
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
- package/lib/commonjs/utils/StorageUtil.js +67 -0
- package/lib/commonjs/utils/StorageUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapApiUtil.js +9 -4
- package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
- package/lib/module/controllers/ApiController.js +11 -11
- package/lib/module/controllers/ApiController.js.map +1 -1
- package/lib/module/controllers/BlockchainApiController.js +102 -14
- package/lib/module/controllers/BlockchainApiController.js.map +1 -1
- package/lib/module/controllers/ConnectionController.js +7 -1
- package/lib/module/controllers/ConnectionController.js.map +1 -1
- package/lib/module/controllers/ConnectionsController.js +151 -0
- package/lib/module/controllers/ConnectionsController.js.map +1 -0
- package/lib/module/controllers/ConnectorController.js +1 -0
- package/lib/module/controllers/ConnectorController.js.map +1 -1
- package/lib/module/controllers/OptionsController.js +3 -3
- package/lib/module/controllers/OptionsController.js.map +1 -1
- package/lib/module/controllers/RouterController.js.map +1 -1
- package/lib/module/controllers/SwapController.js +13 -5
- 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 +2 -2
- package/lib/module/controllers/TransactionsController.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/AssetUtil.js +5 -6
- package/lib/module/utils/AssetUtil.js.map +1 -1
- package/lib/module/utils/ConstantsUtil.js +11 -5
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/CoreHelperUtil.js.map +1 -1
- package/lib/module/utils/StorageUtil.js +67 -0
- package/lib/module/utils/StorageUtil.js.map +1 -1
- package/lib/module/utils/SwapApiUtil.js +9 -4
- package/lib/module/utils/SwapApiUtil.js.map +1 -1
- package/lib/typescript/controllers/AccountController.d.ts +2 -2
- package/lib/typescript/controllers/AccountController.d.ts.map +1 -1
- package/lib/typescript/controllers/ApiController.d.ts +1 -1
- package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts +14 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionController.d.ts +2 -0
- package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionsController.d.ts +41 -0
- package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
- package/lib/typescript/controllers/ConnectorController.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/OptionsController.d.ts +3 -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 +2 -1
- package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
- 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/index.d.ts +1 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/AssetUtil.d.ts +2 -2
- package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
- package/lib/typescript/utils/ConstantsUtil.d.ts +6 -1
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/CoreHelperUtil.d.ts +2 -2
- package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
- package/lib/typescript/utils/NetworkUtil.d.ts +1 -1
- package/lib/typescript/utils/NetworkUtil.d.ts.map +1 -1
- package/lib/typescript/utils/StorageUtil.d.ts +13 -1
- package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
- package/lib/typescript/utils/TypeUtil.d.ts +3 -14
- package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/controllers/AccountController.ts +2 -2
- package/src/controllers/ApiController.ts +12 -7
- package/src/controllers/BlockchainApiController.ts +133 -13
- package/src/controllers/ConnectionController.ts +9 -1
- package/src/controllers/ConnectionsController.ts +235 -0
- package/src/controllers/ConnectorController.ts +1 -0
- package/src/controllers/NetworkController.ts +1 -1
- package/src/controllers/OptionsController.ts +7 -6
- package/src/controllers/PublicStateController.ts +1 -1
- package/src/controllers/RouterController.ts +2 -1
- package/src/controllers/SwapController.ts +14 -5
- package/src/controllers/ThemeController.ts +11 -2
- package/src/controllers/TransactionsController.ts +2 -2
- package/src/index.ts +5 -1
- package/src/utils/AssetUtil.ts +5 -7
- package/src/utils/ConstantsUtil.ts +12 -5
- package/src/utils/CoreHelperUtil.ts +8 -3
- package/src/utils/NetworkUtil.ts +1 -1
- package/src/utils/StorageUtil.ts +82 -1
- package/src/utils/SwapApiUtil.ts +10 -4
- package/src/utils/TypeUtil.ts +5 -19
- package/lib/commonjs/utils/ConnectionUtil.js +0 -33
- package/lib/commonjs/utils/ConnectionUtil.js.map +0 -1
- package/lib/module/utils/ConnectionUtil.js +0 -27
- package/lib/module/utils/ConnectionUtil.js.map +0 -1
- package/lib/typescript/utils/ConnectionUtil.d.ts +0 -4
- package/lib/typescript/utils/ConnectionUtil.d.ts.map +0 -1
- package/src/utils/ConnectionUtil.ts +0 -27
|
@@ -37,6 +37,9 @@ import type {
|
|
|
37
37
|
import { OptionsController } from './OptionsController';
|
|
38
38
|
import { ConstantsUtil } from '../utils/ConstantsUtil';
|
|
39
39
|
import { ApiUtil } from '../utils/ApiUtil';
|
|
40
|
+
import type { CaipNetworkId } from '@reown/appkit-common-react-native';
|
|
41
|
+
import { ConnectionsController } from './ConnectionsController';
|
|
42
|
+
import { SnackController } from './SnackController';
|
|
40
43
|
|
|
41
44
|
// -- Helpers ------------------------------------------- //
|
|
42
45
|
const baseUrl = CoreHelperUtil.getBlockchainApiUrl();
|
|
@@ -57,19 +60,54 @@ const getHeaders = () => {
|
|
|
57
60
|
export interface BlockchainApiControllerState {
|
|
58
61
|
clientId: string | null;
|
|
59
62
|
api: FetchUtil;
|
|
63
|
+
supportedChains: { http: CaipNetworkId[]; ws: CaipNetworkId[] };
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
// -- State --------------------------------------------- //
|
|
63
67
|
const state = proxy<BlockchainApiControllerState>({
|
|
64
68
|
clientId: null,
|
|
65
69
|
api: new FetchUtil({ baseUrl }),
|
|
70
|
+
supportedChains: { http: [], ws: [] }
|
|
66
71
|
});
|
|
67
72
|
|
|
68
73
|
// -- Controller ---------------------------------------- //
|
|
69
74
|
export const BlockchainApiController = {
|
|
70
75
|
state,
|
|
71
76
|
|
|
72
|
-
|
|
77
|
+
async isNetworkSupported(networkId?: CaipNetworkId) {
|
|
78
|
+
if (!networkId) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
if (!state.supportedChains.http.length) {
|
|
83
|
+
await BlockchainApiController.getSupportedNetworks();
|
|
84
|
+
}
|
|
85
|
+
} catch (e) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return state.supportedChains.http.includes(networkId);
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
async getSupportedNetworks() {
|
|
93
|
+
const supportedChains = await state.api.get<BlockchainApiControllerState['supportedChains']>({
|
|
94
|
+
path: 'v1/supported-chains'
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
state.supportedChains = supportedChains!;
|
|
98
|
+
|
|
99
|
+
return supportedChains;
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
async fetchIdentity({ address }: BlockchainApiIdentityRequest) {
|
|
103
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
104
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
if (!isSupported) {
|
|
108
|
+
return { avatar: '', name: '' };
|
|
109
|
+
}
|
|
110
|
+
|
|
73
111
|
return state.api.get<BlockchainApiIdentityResponse>({
|
|
74
112
|
path: `/v1/identity/${address}`,
|
|
75
113
|
params: {
|
|
@@ -79,29 +117,48 @@ export const BlockchainApiController = {
|
|
|
79
117
|
});
|
|
80
118
|
},
|
|
81
119
|
|
|
82
|
-
fetchTransactions({
|
|
120
|
+
async fetchTransactions({
|
|
83
121
|
account,
|
|
84
122
|
projectId,
|
|
85
123
|
cursor,
|
|
86
124
|
onramp,
|
|
87
125
|
signal,
|
|
88
|
-
cache
|
|
126
|
+
cache,
|
|
127
|
+
chainId
|
|
89
128
|
}: BlockchainApiTransactionsRequest) {
|
|
90
|
-
|
|
129
|
+
const _chainId = chainId ?? ConnectionsController.state.activeCaipNetworkId;
|
|
130
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(_chainId);
|
|
131
|
+
|
|
132
|
+
if (!isSupported) {
|
|
133
|
+
return { data: [], next: undefined };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const response = await state.api.get<BlockchainApiTransactionsResponse>({
|
|
91
137
|
path: `/v1/account/${account}/history`,
|
|
92
138
|
headers: getHeaders(),
|
|
93
139
|
params: {
|
|
94
140
|
projectId,
|
|
95
141
|
cursor,
|
|
96
|
-
onramp
|
|
142
|
+
onramp,
|
|
143
|
+
chainId: _chainId
|
|
97
144
|
},
|
|
98
145
|
signal,
|
|
99
146
|
cache
|
|
100
147
|
});
|
|
148
|
+
|
|
149
|
+
return response;
|
|
101
150
|
},
|
|
102
151
|
|
|
103
|
-
fetchTokenPrice({ projectId, addresses }: BlockchainApiTokenPriceRequest) {
|
|
104
|
-
|
|
152
|
+
async fetchTokenPrice({ projectId, addresses }: BlockchainApiTokenPriceRequest) {
|
|
153
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
154
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
if (!isSupported) {
|
|
158
|
+
return { fungibles: [] };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const response = await state.api.post<BlockchainApiTokenPriceResponse>({
|
|
105
162
|
path: '/v1/fungible/price',
|
|
106
163
|
body: {
|
|
107
164
|
projectId,
|
|
@@ -110,9 +167,23 @@ export const BlockchainApiController = {
|
|
|
110
167
|
},
|
|
111
168
|
headers: getHeaders()
|
|
112
169
|
});
|
|
170
|
+
|
|
171
|
+
return response;
|
|
113
172
|
},
|
|
114
173
|
|
|
115
|
-
fetchSwapAllowance({
|
|
174
|
+
async fetchSwapAllowance({
|
|
175
|
+
projectId,
|
|
176
|
+
tokenAddress,
|
|
177
|
+
userAddress
|
|
178
|
+
}: BlockchainApiSwapAllowanceRequest) {
|
|
179
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
180
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
if (!isSupported) {
|
|
184
|
+
return { allowance: '0' };
|
|
185
|
+
}
|
|
186
|
+
|
|
116
187
|
return state.api.get<BlockchainApiSwapAllowanceResponse>({
|
|
117
188
|
path: `/v1/convert/allowance`,
|
|
118
189
|
params: {
|
|
@@ -124,7 +195,15 @@ export const BlockchainApiController = {
|
|
|
124
195
|
});
|
|
125
196
|
},
|
|
126
197
|
|
|
127
|
-
fetchGasPrice({ projectId, chainId }: BlockchainApiGasPriceRequest) {
|
|
198
|
+
async fetchGasPrice({ projectId, chainId }: BlockchainApiGasPriceRequest) {
|
|
199
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
200
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
if (!isSupported) {
|
|
204
|
+
throw new Error('Network not supported for Gas Price');
|
|
205
|
+
}
|
|
206
|
+
|
|
128
207
|
return state.api.get<BlockchainApiGasPriceResponse>({
|
|
129
208
|
path: `/v1/convert/gas-price`,
|
|
130
209
|
headers: getHeaders(),
|
|
@@ -135,7 +214,7 @@ export const BlockchainApiController = {
|
|
|
135
214
|
});
|
|
136
215
|
},
|
|
137
216
|
|
|
138
|
-
fetchSwapQuote({
|
|
217
|
+
async fetchSwapQuote({
|
|
139
218
|
projectId,
|
|
140
219
|
amount,
|
|
141
220
|
userAddress,
|
|
@@ -143,6 +222,14 @@ export const BlockchainApiController = {
|
|
|
143
222
|
to,
|
|
144
223
|
gasPrice
|
|
145
224
|
}: BlockchainApiSwapQuoteRequest) {
|
|
225
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
226
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
if (!isSupported) {
|
|
230
|
+
return { quotes: [] };
|
|
231
|
+
}
|
|
232
|
+
|
|
146
233
|
return state.api.get<BlockchainApiSwapQuoteResponse>({
|
|
147
234
|
path: `/v1/convert/quotes`,
|
|
148
235
|
headers: getHeaders(),
|
|
@@ -157,7 +244,15 @@ export const BlockchainApiController = {
|
|
|
157
244
|
});
|
|
158
245
|
},
|
|
159
246
|
|
|
160
|
-
fetchSwapTokens({ projectId, chainId }: BlockchainApiSwapTokensRequest) {
|
|
247
|
+
async fetchSwapTokens({ projectId, chainId }: BlockchainApiSwapTokensRequest) {
|
|
248
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
249
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
if (!isSupported) {
|
|
253
|
+
return { tokens: [] };
|
|
254
|
+
}
|
|
255
|
+
|
|
161
256
|
return state.api.get<BlockchainApiSwapTokensResponse>({
|
|
162
257
|
path: `/v1/convert/tokens`,
|
|
163
258
|
headers: getHeaders(),
|
|
@@ -168,13 +263,21 @@ export const BlockchainApiController = {
|
|
|
168
263
|
});
|
|
169
264
|
},
|
|
170
265
|
|
|
171
|
-
generateSwapCalldata({
|
|
266
|
+
async generateSwapCalldata({
|
|
172
267
|
amount,
|
|
173
268
|
from,
|
|
174
269
|
projectId,
|
|
175
270
|
to,
|
|
176
271
|
userAddress
|
|
177
272
|
}: BlockchainApiGenerateSwapCalldataRequest) {
|
|
273
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
274
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
if (!isSupported) {
|
|
278
|
+
throw new Error('Network not supported for Swaps');
|
|
279
|
+
}
|
|
280
|
+
|
|
178
281
|
return state.api.post<BlockchainApiGenerateSwapCalldataResponse>({
|
|
179
282
|
path: '/v1/convert/build-transaction',
|
|
180
283
|
headers: getHeaders(),
|
|
@@ -191,12 +294,20 @@ export const BlockchainApiController = {
|
|
|
191
294
|
});
|
|
192
295
|
},
|
|
193
296
|
|
|
194
|
-
generateApproveCalldata({
|
|
297
|
+
async generateApproveCalldata({
|
|
195
298
|
from,
|
|
196
299
|
projectId,
|
|
197
300
|
to,
|
|
198
301
|
userAddress
|
|
199
302
|
}: BlockchainApiGenerateApproveCalldataRequest) {
|
|
303
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
304
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
if (!isSupported) {
|
|
308
|
+
throw new Error('Network not supported for Swaps');
|
|
309
|
+
}
|
|
310
|
+
|
|
200
311
|
return state.api.get<BlockchainApiGenerateApproveCalldataResponse>({
|
|
201
312
|
path: `/v1/convert/build-approve`,
|
|
202
313
|
headers: getHeaders(),
|
|
@@ -210,6 +321,15 @@ export const BlockchainApiController = {
|
|
|
210
321
|
},
|
|
211
322
|
|
|
212
323
|
async getBalance(address: string, chainId?: string, forceUpdate?: string) {
|
|
324
|
+
const isSupported = await BlockchainApiController.isNetworkSupported(
|
|
325
|
+
ConnectionsController.state.activeCaipNetworkId
|
|
326
|
+
);
|
|
327
|
+
if (!isSupported) {
|
|
328
|
+
SnackController.showError('Token Balance Unavailable');
|
|
329
|
+
|
|
330
|
+
return { balances: [] };
|
|
331
|
+
}
|
|
332
|
+
|
|
213
333
|
return state.api.get<BlockchainApiBalanceResponse>({
|
|
214
334
|
path: `/v1/account/${address}/balance`,
|
|
215
335
|
headers: getHeaders(),
|
|
@@ -120,6 +120,15 @@ export const ConnectionController = {
|
|
|
120
120
|
state.pressedWallet = undefined;
|
|
121
121
|
},
|
|
122
122
|
|
|
123
|
+
setWcPromise(wcPromise: ConnectionControllerState['wcPromise']) {
|
|
124
|
+
state.wcPromise = wcPromise;
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
setWcUri(wcUri: ConnectionControllerState['wcUri']) {
|
|
128
|
+
state.wcUri = wcUri;
|
|
129
|
+
state.wcPairingExpiry = CoreHelperUtil.getPairingExpiry();
|
|
130
|
+
},
|
|
131
|
+
|
|
123
132
|
setRecentWallets(wallets: ConnectionControllerState['recentWallets']) {
|
|
124
133
|
state.recentWallets = wallets;
|
|
125
134
|
},
|
|
@@ -192,7 +201,6 @@ export const ConnectionController = {
|
|
|
192
201
|
},
|
|
193
202
|
|
|
194
203
|
async disconnect() {
|
|
195
|
-
await this._getClient().disconnect();
|
|
196
204
|
this.resetWcConnection();
|
|
197
205
|
// remove transactions
|
|
198
206
|
// RouterController.reset('Connect');
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { proxy, ref } from 'valtio';
|
|
2
|
+
import { derive } from 'valtio/utils';
|
|
3
|
+
import type {
|
|
4
|
+
AppKitNetwork,
|
|
5
|
+
BlockchainAdapter,
|
|
6
|
+
CaipAddress,
|
|
7
|
+
CaipNetworkId,
|
|
8
|
+
ChainNamespace,
|
|
9
|
+
GetBalanceResponse,
|
|
10
|
+
WalletInfo
|
|
11
|
+
} from '@reown/appkit-common-react-native';
|
|
12
|
+
import { StorageUtil } from '../utils/StorageUtil';
|
|
13
|
+
|
|
14
|
+
// -- Types --------------------------------------------- //
|
|
15
|
+
type Balance = GetBalanceResponse;
|
|
16
|
+
|
|
17
|
+
//TODO: balance could be elsewhere
|
|
18
|
+
interface Connection {
|
|
19
|
+
accounts: CaipAddress[];
|
|
20
|
+
balances: Record<CaipAddress, Balance>; //TODO: make this an array of balances
|
|
21
|
+
adapter: BlockchainAdapter;
|
|
22
|
+
chains: CaipNetworkId[];
|
|
23
|
+
activeChain: CaipNetworkId;
|
|
24
|
+
wallet?: WalletInfo;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ConnectionsControllerState {
|
|
28
|
+
activeNamespace?: ChainNamespace;
|
|
29
|
+
connections: Record<string, Connection>;
|
|
30
|
+
networks: AppKitNetwork[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// -- State --------------------------------------------- //
|
|
34
|
+
const baseState = proxy<ConnectionsControllerState>({
|
|
35
|
+
activeNamespace: undefined,
|
|
36
|
+
connections: {},
|
|
37
|
+
networks: []
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const derivedState = derive(
|
|
41
|
+
{
|
|
42
|
+
activeAddress: (get): CaipAddress | undefined => {
|
|
43
|
+
const snap = get(baseState);
|
|
44
|
+
|
|
45
|
+
if (!snap.activeNamespace) return undefined;
|
|
46
|
+
|
|
47
|
+
const connection = snap.connections[snap.activeNamespace];
|
|
48
|
+
|
|
49
|
+
if (!connection || !connection.accounts || connection.accounts.length === 0) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//TODO: what happens if there are several accounts on the same chain?
|
|
54
|
+
const activeAccount = connection.accounts.find(account =>
|
|
55
|
+
account.startsWith(connection.activeChain)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return activeAccount;
|
|
59
|
+
},
|
|
60
|
+
activeBalance: (get): Balance | undefined => {
|
|
61
|
+
const snap = get(baseState);
|
|
62
|
+
|
|
63
|
+
if (!snap.activeNamespace) return undefined;
|
|
64
|
+
const connection = snap.connections[snap.activeNamespace];
|
|
65
|
+
|
|
66
|
+
if (!connection || !connection.accounts || connection.accounts.length === 0) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const activeAccount = connection.accounts.find(account =>
|
|
71
|
+
account.startsWith(connection.activeChain)
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
!connection ||
|
|
76
|
+
!connection.balances ||
|
|
77
|
+
!activeAccount ||
|
|
78
|
+
Object.keys(connection.balances).length === 0
|
|
79
|
+
) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return connection.balances[activeAccount];
|
|
84
|
+
},
|
|
85
|
+
activeNetwork: (get): AppKitNetwork | undefined => {
|
|
86
|
+
const snap = get(baseState);
|
|
87
|
+
|
|
88
|
+
if (!snap.activeNamespace) return undefined;
|
|
89
|
+
|
|
90
|
+
const connection = snap.connections[snap.activeNamespace];
|
|
91
|
+
|
|
92
|
+
if (!connection) return undefined;
|
|
93
|
+
|
|
94
|
+
return snap.networks.find(
|
|
95
|
+
network =>
|
|
96
|
+
(network.chainNamespace ?? 'eip155') === snap.activeNamespace &&
|
|
97
|
+
network.id?.toString() === connection.activeChain?.split(':')[1]
|
|
98
|
+
);
|
|
99
|
+
},
|
|
100
|
+
activeCaipNetworkId: (get): CaipNetworkId | undefined => {
|
|
101
|
+
const snap = get(baseState);
|
|
102
|
+
|
|
103
|
+
if (!snap.activeNamespace) return undefined;
|
|
104
|
+
|
|
105
|
+
const connection = snap.connections[snap.activeNamespace];
|
|
106
|
+
|
|
107
|
+
if (!connection) return undefined;
|
|
108
|
+
|
|
109
|
+
return connection.activeChain;
|
|
110
|
+
},
|
|
111
|
+
walletInfo: (get): WalletInfo | undefined => {
|
|
112
|
+
const snap = get(baseState);
|
|
113
|
+
|
|
114
|
+
if (!snap.activeNamespace) return undefined;
|
|
115
|
+
|
|
116
|
+
return snap.connections[snap.activeNamespace]?.wallet;
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
proxy: baseState // Link derived proxy to the base state proxy
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
// -- Controller ---------------------------------------- //
|
|
125
|
+
export const ConnectionsController = {
|
|
126
|
+
state: derivedState,
|
|
127
|
+
|
|
128
|
+
setActiveNamespace(namespace?: ChainNamespace) {
|
|
129
|
+
baseState.activeNamespace = namespace;
|
|
130
|
+
StorageUtil.setActiveNamespace(namespace);
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
storeConnection({
|
|
134
|
+
namespace,
|
|
135
|
+
adapter,
|
|
136
|
+
accounts,
|
|
137
|
+
chains,
|
|
138
|
+
wallet,
|
|
139
|
+
activeChain
|
|
140
|
+
}: {
|
|
141
|
+
namespace: string;
|
|
142
|
+
adapter: BlockchainAdapter;
|
|
143
|
+
accounts: CaipAddress[];
|
|
144
|
+
chains: CaipNetworkId[];
|
|
145
|
+
wallet?: WalletInfo;
|
|
146
|
+
activeChain?: CaipNetworkId;
|
|
147
|
+
}) {
|
|
148
|
+
baseState.connections[namespace] = {
|
|
149
|
+
balances: {},
|
|
150
|
+
activeChain: activeChain ?? chains[0]!,
|
|
151
|
+
adapter: ref(adapter),
|
|
152
|
+
accounts,
|
|
153
|
+
chains,
|
|
154
|
+
wallet
|
|
155
|
+
};
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
updateAccounts(namespace: string, accounts: CaipAddress[]) {
|
|
159
|
+
const connection = baseState.connections[namespace];
|
|
160
|
+
if (!connection) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
connection.accounts = accounts;
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
updateBalance(namespace: string, address: CaipAddress, balance: Balance) {
|
|
167
|
+
const connection = baseState.connections[namespace];
|
|
168
|
+
if (!connection) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
connection.balances[address] = balance;
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
setActiveChain(namespace: string, chain: CaipNetworkId) {
|
|
175
|
+
const connection = baseState.connections[namespace];
|
|
176
|
+
|
|
177
|
+
if (!connection) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
connection.activeChain = chain;
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
setNetworks(networks: AppKitNetwork[]) {
|
|
185
|
+
baseState.networks = networks;
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
getConnectedNetworks() {
|
|
189
|
+
return baseState.networks.filter(
|
|
190
|
+
network => baseState.connections[network.chainNamespace ?? 'eip155']
|
|
191
|
+
);
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
async disconnect(namespace: string, isInternal = true) {
|
|
195
|
+
const connection = baseState.connections[namespace];
|
|
196
|
+
if (!connection) return;
|
|
197
|
+
|
|
198
|
+
// Get the current connector from the adapter
|
|
199
|
+
const connector = connection.adapter.connector;
|
|
200
|
+
if (!connector) return;
|
|
201
|
+
|
|
202
|
+
// Find all namespaces that use the same connector
|
|
203
|
+
const namespacesUsingConnector = Object.keys(baseState.connections).filter(
|
|
204
|
+
ns => baseState.connections[ns]?.adapter.connector === connector
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
// Unsubscribe all event listeners from the adapter
|
|
208
|
+
namespacesUsingConnector.forEach(ns => {
|
|
209
|
+
const _connection = baseState.connections[ns];
|
|
210
|
+
if (_connection?.adapter) {
|
|
211
|
+
_connection.adapter.removeAllListeners();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Disconnect the adapter
|
|
216
|
+
if (isInternal) {
|
|
217
|
+
await connection.adapter.disconnect();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Remove all namespaces that used this connector
|
|
221
|
+
namespacesUsingConnector.forEach(ns => {
|
|
222
|
+
delete baseState.connections[ns];
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// Remove activeNamespace if it is in the list of namespaces using the connector
|
|
226
|
+
if (
|
|
227
|
+
baseState.activeNamespace &&
|
|
228
|
+
(baseState.activeNamespace === namespace ||
|
|
229
|
+
namespacesUsingConnector.includes(baseState.activeNamespace))
|
|
230
|
+
) {
|
|
231
|
+
baseState.activeNamespace = undefined;
|
|
232
|
+
StorageUtil.setActiveNamespace(undefined);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
@@ -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';
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { proxy, ref } from 'valtio';
|
|
2
|
+
import type { Tokens } from '@reown/appkit-common-react-native';
|
|
2
3
|
import type {
|
|
3
4
|
CustomWallet,
|
|
4
5
|
Features,
|
|
5
6
|
Metadata,
|
|
6
7
|
ProjectId,
|
|
7
8
|
SdkType,
|
|
8
|
-
SdkVersion
|
|
9
|
-
Tokens
|
|
9
|
+
SdkVersion
|
|
10
10
|
} from '../utils/TypeUtil';
|
|
11
|
+
|
|
11
12
|
import { ConstantsUtil } from '../utils/ConstantsUtil';
|
|
12
13
|
|
|
13
14
|
// -- Types --------------------------------------------- //
|
|
@@ -17,7 +18,7 @@ export interface ClipboardClient {
|
|
|
17
18
|
|
|
18
19
|
export interface OptionsControllerState {
|
|
19
20
|
projectId: ProjectId;
|
|
20
|
-
|
|
21
|
+
clipboardClient?: ClipboardClient;
|
|
21
22
|
includeWalletIds?: string[];
|
|
22
23
|
excludeWalletIds?: string[];
|
|
23
24
|
featuredWalletIds?: string[];
|
|
@@ -47,7 +48,7 @@ export const OptionsController = {
|
|
|
47
48
|
state,
|
|
48
49
|
|
|
49
50
|
setClipboardClient(client: ClipboardClient) {
|
|
50
|
-
state.
|
|
51
|
+
state.clipboardClient = ref(client);
|
|
51
52
|
},
|
|
52
53
|
|
|
53
54
|
setProjectId(projectId: OptionsControllerState['projectId']) {
|
|
@@ -103,11 +104,11 @@ export const OptionsController = {
|
|
|
103
104
|
},
|
|
104
105
|
|
|
105
106
|
isClipboardAvailable() {
|
|
106
|
-
return !!state.
|
|
107
|
+
return !!state.clipboardClient;
|
|
107
108
|
},
|
|
108
109
|
|
|
109
110
|
copyToClipboard(value: string) {
|
|
110
|
-
const client = state.
|
|
111
|
+
const client = state.clipboardClient;
|
|
111
112
|
if (client) {
|
|
112
113
|
client?.setString(value);
|
|
113
114
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { proxy, subscribe as sub } from 'valtio';
|
|
2
2
|
import { subscribeKey as subKey } from 'valtio/utils';
|
|
3
|
-
import type { CaipNetworkId } from '
|
|
3
|
+
import type { CaipNetworkId } from '@reown/appkit-common-react-native';
|
|
4
4
|
|
|
5
5
|
// -- Types --------------------------------------------- //
|
|
6
6
|
export interface PublicStateControllerState {
|
|
@@ -17,6 +17,7 @@ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
|
|
|
17
17
|
import { ConnectionController } from './ConnectionController';
|
|
18
18
|
import { TransactionsController } from './TransactionsController';
|
|
19
19
|
import { EventsController } from './EventsController';
|
|
20
|
+
import { ConnectionsController } from './ConnectionsController';
|
|
20
21
|
|
|
21
22
|
// -- Constants ---------------------------------------- //
|
|
22
23
|
export const INITIAL_GAS_LIMIT = 150000;
|
|
@@ -158,9 +159,17 @@ export const SwapController = {
|
|
|
158
159
|
},
|
|
159
160
|
|
|
160
161
|
getParams() {
|
|
161
|
-
const
|
|
162
|
-
const address = CoreHelperUtil.getPlainAddress(
|
|
163
|
-
|
|
162
|
+
const { activeAddress, activeNamespace, activeNetwork } = ConnectionsController.state;
|
|
163
|
+
const address = CoreHelperUtil.getPlainAddress(activeAddress);
|
|
164
|
+
|
|
165
|
+
if (!activeNamespace || !activeNetwork) {
|
|
166
|
+
throw new Error('No active namespace or network found to swap the tokens from.');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const networkAddress = `${activeNetwork.caipNetworkId ?? 'eip155:1'}:${
|
|
170
|
+
ConstantsUtil.NATIVE_TOKEN_ADDRESS[activeNamespace]
|
|
171
|
+
}`;
|
|
172
|
+
|
|
164
173
|
const type = ConnectorController.state.connectedConnector;
|
|
165
174
|
|
|
166
175
|
if (!address) {
|
|
@@ -178,7 +187,7 @@ export const SwapController = {
|
|
|
178
187
|
return {
|
|
179
188
|
networkAddress,
|
|
180
189
|
fromAddress: address,
|
|
181
|
-
fromCaipAddress:
|
|
190
|
+
fromCaipAddress: activeAddress,
|
|
182
191
|
sourceTokenAddress: state.sourceToken?.address,
|
|
183
192
|
toTokenAddress: state.toToken?.address,
|
|
184
193
|
toTokenAmount: state.toTokenAmount,
|
|
@@ -189,7 +198,7 @@ export const SwapController = {
|
|
|
189
198
|
invalidSourceToken,
|
|
190
199
|
invalidSourceTokenAmount,
|
|
191
200
|
availableToSwap:
|
|
192
|
-
|
|
201
|
+
activeAddress && !invalidToToken && !invalidSourceToken && !invalidSourceTokenAmount,
|
|
193
202
|
isAuthConnector: type === 'AUTH'
|
|
194
203
|
};
|
|
195
204
|
},
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { Appearance } from 'react-native';
|
|
1
2
|
import { proxy, subscribe as sub } from 'valtio';
|
|
2
3
|
import type { ThemeMode, ThemeVariables } from '@reown/appkit-common-react-native';
|
|
3
4
|
|
|
4
5
|
// -- Types --------------------------------------------- //
|
|
5
6
|
export interface ThemeControllerState {
|
|
6
7
|
themeMode?: ThemeMode;
|
|
7
|
-
themeVariables
|
|
8
|
+
themeVariables?: ThemeVariables;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
// -- State --------------------------------------------- //
|
|
@@ -22,10 +23,18 @@ export const ThemeController = {
|
|
|
22
23
|
},
|
|
23
24
|
|
|
24
25
|
setThemeMode(themeMode: ThemeControllerState['themeMode']) {
|
|
25
|
-
|
|
26
|
+
if (!themeMode) {
|
|
27
|
+
state.themeMode = Appearance.getColorScheme() as ThemeMode;
|
|
28
|
+
} else {
|
|
29
|
+
state.themeMode = themeMode;
|
|
30
|
+
}
|
|
26
31
|
},
|
|
27
32
|
|
|
28
33
|
setThemeVariables(themeVariables: ThemeControllerState['themeVariables']) {
|
|
34
|
+
if (!themeVariables) {
|
|
35
|
+
state.themeVariables = {};
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
state.themeVariables = { ...state.themeVariables, ...themeVariables };
|
|
30
39
|
}
|
|
31
40
|
};
|