@sodax/wallet-sdk-react 0.0.1-rc.2
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/LICENSE +21 -0
- package/README.md +233 -0
- package/dist/index.cjs +1589 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15970 -0
- package/dist/index.d.ts +15970 -0
- package/dist/index.mjs +1513 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +74 -0
- package/src/SodaxWalletProvider.tsx +55 -0
- package/src/actions/getXChainType.ts +10 -0
- package/src/actions/getXService.ts +25 -0
- package/src/actions/index.ts +2 -0
- package/src/assets/wallets/hana.svg +6 -0
- package/src/assets/wallets/havah.svg +76 -0
- package/src/assets/wallets/keplr.svg +30 -0
- package/src/assets/wallets/metamask.svg +60 -0
- package/src/assets/wallets/phantom.svg +4 -0
- package/src/assets/wallets/sui.svg +20 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/xChains.ts +164 -0
- package/src/core/XConnector.ts +54 -0
- package/src/core/XService.ts +91 -0
- package/src/core/index.ts +2 -0
- package/src/hooks/index.ts +11 -0
- package/src/hooks/useEthereumChainId.ts +36 -0
- package/src/hooks/useEvmSwitchChain.ts +92 -0
- package/src/hooks/useWalletProvider.ts +149 -0
- package/src/hooks/useWalletProviderOptions.ts +51 -0
- package/src/hooks/useXAccount.ts +51 -0
- package/src/hooks/useXAccounts.ts +56 -0
- package/src/hooks/useXBalances.ts +66 -0
- package/src/hooks/useXConnect.ts +119 -0
- package/src/hooks/useXConnection.ts +72 -0
- package/src/hooks/useXConnectors.ts +56 -0
- package/src/hooks/useXDisconnect.ts +65 -0
- package/src/hooks/useXService.ts +8 -0
- package/src/index.ts +18 -0
- package/src/types/index.ts +44 -0
- package/src/useXWagmiStore.tsx +164 -0
- package/src/utils/index.ts +33 -0
- package/src/xchains/evm/EvmXConnector.ts +27 -0
- package/src/xchains/evm/EvmXService.ts +189 -0
- package/src/xchains/evm/index.ts +2 -0
- package/src/xchains/icon/IconHanaXConnector.ts +39 -0
- package/src/xchains/icon/IconXService.ts +115 -0
- package/src/xchains/icon/iconex/index.tsx +46 -0
- package/src/xchains/icon/index.ts +2 -0
- package/src/xchains/injective/InjectiveKelprXConnector.ts +37 -0
- package/src/xchains/injective/InjectiveMetamaskXConnector.ts +40 -0
- package/src/xchains/injective/InjectiveXService.ts +71 -0
- package/src/xchains/injective/index.ts +4 -0
- package/src/xchains/injective/utils.ts +17 -0
- package/src/xchains/solana/SolanaXConnector.ts +26 -0
- package/src/xchains/solana/SolanaXService.ts +50 -0
- package/src/xchains/solana/hooks/useAnchorProvider.tsx +9 -0
- package/src/xchains/solana/index.ts +2 -0
- package/src/xchains/stellar/CustomSorobanServer.ts +93 -0
- package/src/xchains/stellar/StellarWalletsKitXConnector.ts +45 -0
- package/src/xchains/stellar/StellarXService.ts +61 -0
- package/src/xchains/stellar/index.tsx +2 -0
- package/src/xchains/stellar/useStellarXConnectors.ts +30 -0
- package/src/xchains/stellar/utils.ts +49 -0
- package/src/xchains/sui/SuiXConnector.ts +28 -0
- package/src/xchains/sui/SuiXService.ts +56 -0
- package/src/xchains/sui/index.ts +2 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1589 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var viem = require('viem');
|
|
4
|
+
var actions = require('wagmi/actions');
|
|
5
|
+
var wagmi = require('wagmi');
|
|
6
|
+
var chains = require('wagmi/chains');
|
|
7
|
+
var types = require('@sodax/types');
|
|
8
|
+
var iconSdkJs = require('icon-sdk-js');
|
|
9
|
+
var networks = require('@injectivelabs/networks');
|
|
10
|
+
var sdkTs = require('@injectivelabs/sdk-ts');
|
|
11
|
+
var tsTypes = require('@injectivelabs/ts-types');
|
|
12
|
+
var walletEvm = require('@injectivelabs/wallet-evm');
|
|
13
|
+
var walletCore = require('@injectivelabs/wallet-core');
|
|
14
|
+
var walletBase = require('@injectivelabs/wallet-base');
|
|
15
|
+
var walletCosmos = require('@injectivelabs/wallet-cosmos');
|
|
16
|
+
var web3_js = require('@solana/web3.js');
|
|
17
|
+
var splToken = require('@solana/spl-token');
|
|
18
|
+
var stellarWalletsKit = require('@creit.tech/stellar-wallets-kit');
|
|
19
|
+
var StellarSdk = require('@stellar/stellar-sdk');
|
|
20
|
+
var React3 = require('react');
|
|
21
|
+
var dappKit = require('@mysten/dapp-kit');
|
|
22
|
+
var walletAdapterReact = require('@solana/wallet-adapter-react');
|
|
23
|
+
var zustand = require('zustand');
|
|
24
|
+
var middleware = require('zustand/middleware');
|
|
25
|
+
var immer = require('zustand/middleware/immer');
|
|
26
|
+
var anchor = require('@coral-xyz/anchor');
|
|
27
|
+
var reactQuery = require('@tanstack/react-query');
|
|
28
|
+
var chains$1 = require('viem/chains');
|
|
29
|
+
var walletSdkCore = require('@sodax/wallet-sdk-core');
|
|
30
|
+
var client = require('@mysten/sui/client');
|
|
31
|
+
var walletAdapterWallets = require('@solana/wallet-adapter-wallets');
|
|
32
|
+
|
|
33
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
34
|
+
|
|
35
|
+
function _interopNamespace(e) {
|
|
36
|
+
if (e && e.__esModule) return e;
|
|
37
|
+
var n = Object.create(null);
|
|
38
|
+
if (e) {
|
|
39
|
+
Object.keys(e).forEach(function (k) {
|
|
40
|
+
if (k !== 'default') {
|
|
41
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
42
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: function () { return e[k]; }
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
n.default = e;
|
|
50
|
+
return Object.freeze(n);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var StellarSdk__namespace = /*#__PURE__*/_interopNamespace(StellarSdk);
|
|
54
|
+
var React3__default = /*#__PURE__*/_interopDefault(React3);
|
|
55
|
+
|
|
56
|
+
// src/constants/xChains.ts
|
|
57
|
+
var icon = {
|
|
58
|
+
id: 1,
|
|
59
|
+
name: "ICON",
|
|
60
|
+
xChainId: "0x1.icon",
|
|
61
|
+
xChainType: "ICON",
|
|
62
|
+
testnet: false
|
|
63
|
+
};
|
|
64
|
+
var avalanche = {
|
|
65
|
+
id: 43114,
|
|
66
|
+
name: "Avalanche",
|
|
67
|
+
xChainId: "0xa86a.avax",
|
|
68
|
+
xChainType: "EVM",
|
|
69
|
+
testnet: false
|
|
70
|
+
};
|
|
71
|
+
var bsc = {
|
|
72
|
+
id: 56,
|
|
73
|
+
name: "BNB Chain",
|
|
74
|
+
xChainId: "0x38.bsc",
|
|
75
|
+
xChainType: "EVM",
|
|
76
|
+
testnet: false
|
|
77
|
+
};
|
|
78
|
+
var arbitrum = {
|
|
79
|
+
id: 42161,
|
|
80
|
+
name: "Arbitrum",
|
|
81
|
+
xChainId: "0xa4b1.arbitrum",
|
|
82
|
+
xChainType: "EVM",
|
|
83
|
+
testnet: false
|
|
84
|
+
};
|
|
85
|
+
var base = {
|
|
86
|
+
id: 8453,
|
|
87
|
+
name: "Base",
|
|
88
|
+
xChainId: "0x2105.base",
|
|
89
|
+
xChainType: "EVM",
|
|
90
|
+
testnet: false
|
|
91
|
+
};
|
|
92
|
+
var injective = {
|
|
93
|
+
id: "injective-1",
|
|
94
|
+
name: "Injective",
|
|
95
|
+
xChainId: "injective-1",
|
|
96
|
+
xChainType: "INJECTIVE",
|
|
97
|
+
testnet: false
|
|
98
|
+
};
|
|
99
|
+
var stellar = {
|
|
100
|
+
id: "stellar",
|
|
101
|
+
name: "Stellar",
|
|
102
|
+
xChainId: "stellar",
|
|
103
|
+
xChainType: "STELLAR",
|
|
104
|
+
testnet: false
|
|
105
|
+
};
|
|
106
|
+
var sui = {
|
|
107
|
+
id: "sui",
|
|
108
|
+
name: "Sui",
|
|
109
|
+
xChainId: "sui",
|
|
110
|
+
xChainType: "SUI",
|
|
111
|
+
testnet: false
|
|
112
|
+
};
|
|
113
|
+
var solana = {
|
|
114
|
+
id: "solana",
|
|
115
|
+
name: "Solana",
|
|
116
|
+
xChainId: "solana",
|
|
117
|
+
xChainType: "SOLANA",
|
|
118
|
+
testnet: false
|
|
119
|
+
};
|
|
120
|
+
var optimism = {
|
|
121
|
+
id: 10,
|
|
122
|
+
name: "Optimism",
|
|
123
|
+
xChainId: "0xa.optimism",
|
|
124
|
+
xChainType: "EVM",
|
|
125
|
+
testnet: false
|
|
126
|
+
};
|
|
127
|
+
var sonic = {
|
|
128
|
+
id: 146,
|
|
129
|
+
name: "Sonic",
|
|
130
|
+
xChainId: "sonic",
|
|
131
|
+
xChainType: "EVM",
|
|
132
|
+
testnet: false
|
|
133
|
+
};
|
|
134
|
+
var polygon = {
|
|
135
|
+
id: 137,
|
|
136
|
+
name: "Polygon",
|
|
137
|
+
xChainId: "0x89.polygon",
|
|
138
|
+
xChainType: "EVM",
|
|
139
|
+
testnet: false
|
|
140
|
+
};
|
|
141
|
+
var nibiru = {
|
|
142
|
+
id: 6900,
|
|
143
|
+
name: "Nibiru",
|
|
144
|
+
xChainId: "nibiru",
|
|
145
|
+
xChainType: "EVM",
|
|
146
|
+
testnet: false
|
|
147
|
+
};
|
|
148
|
+
var hyper = {
|
|
149
|
+
id: 999,
|
|
150
|
+
name: "HyperEVM",
|
|
151
|
+
xChainId: "hyper",
|
|
152
|
+
xChainType: "EVM",
|
|
153
|
+
testnet: false
|
|
154
|
+
};
|
|
155
|
+
var lightlink = {
|
|
156
|
+
id: 1890,
|
|
157
|
+
name: "Lightlink",
|
|
158
|
+
xChainId: "lightlink",
|
|
159
|
+
xChainType: "EVM",
|
|
160
|
+
testnet: false
|
|
161
|
+
};
|
|
162
|
+
var xChainMap = {
|
|
163
|
+
"0x1.icon": icon,
|
|
164
|
+
"0xa4b1.arbitrum": arbitrum,
|
|
165
|
+
"0xa86a.avax": avalanche,
|
|
166
|
+
"0x38.bsc": bsc,
|
|
167
|
+
"0x2105.base": base,
|
|
168
|
+
"0xa.optimism": optimism,
|
|
169
|
+
"injective-1": injective,
|
|
170
|
+
stellar,
|
|
171
|
+
sui,
|
|
172
|
+
solana,
|
|
173
|
+
sonic,
|
|
174
|
+
"0x89.polygon": polygon,
|
|
175
|
+
nibiru,
|
|
176
|
+
hyper,
|
|
177
|
+
lightlink
|
|
178
|
+
};
|
|
179
|
+
var xChains = Object.values(xChainMap);
|
|
180
|
+
|
|
181
|
+
// src/actions/getXChainType.ts
|
|
182
|
+
function getXChainType(xChainId) {
|
|
183
|
+
if (!xChainId) {
|
|
184
|
+
return void 0;
|
|
185
|
+
}
|
|
186
|
+
return xChainMap[xChainId].xChainType;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/actions/getXService.ts
|
|
190
|
+
function getXService(xChainType) {
|
|
191
|
+
switch (xChainType) {
|
|
192
|
+
case "EVM":
|
|
193
|
+
return EvmXService.getInstance();
|
|
194
|
+
case "SUI":
|
|
195
|
+
return SuiXService.getInstance();
|
|
196
|
+
case "SOLANA":
|
|
197
|
+
return SolanaXService.getInstance();
|
|
198
|
+
case "ICON":
|
|
199
|
+
return IconXService.getInstance();
|
|
200
|
+
case "INJECTIVE":
|
|
201
|
+
return InjectiveXService.getInstance();
|
|
202
|
+
case "STELLAR":
|
|
203
|
+
return StellarXService.getInstance();
|
|
204
|
+
default:
|
|
205
|
+
throw new Error(`Unsupported chain type: ${xChainType}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// src/core/XService.ts
|
|
210
|
+
var XService = class {
|
|
211
|
+
constructor(xChainType) {
|
|
212
|
+
/** Available wallet connectors for this chain */
|
|
213
|
+
this.xConnectors = [];
|
|
214
|
+
this.xChainType = xChainType;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Gets the balance of a specific token for an address
|
|
218
|
+
* @param address The wallet address to check
|
|
219
|
+
* @param xToken The token to get the balance for
|
|
220
|
+
* @param xChainId The chain ID to query
|
|
221
|
+
* @returns Promise resolving to the token balance as a bigint
|
|
222
|
+
*/
|
|
223
|
+
async getBalance(address, xToken, xChainId) {
|
|
224
|
+
return 0n;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Gets balances for multiple tokens for an address
|
|
228
|
+
* @param address The wallet address to check
|
|
229
|
+
* @param xTokens Array of tokens to get balances for
|
|
230
|
+
* @param xChainId The chain ID to query
|
|
231
|
+
* @returns Promise resolving to object mapping token addresses to balances
|
|
232
|
+
*/
|
|
233
|
+
async getBalances(address, xTokens, xChainId) {
|
|
234
|
+
if (!address) return {};
|
|
235
|
+
const balancePromises = xTokens.map(async (xToken) => {
|
|
236
|
+
const balance = await this.getBalance(address, xToken, xChainId);
|
|
237
|
+
return { address: xToken.address, balance };
|
|
238
|
+
});
|
|
239
|
+
const balances = await Promise.all(balancePromises);
|
|
240
|
+
return balances.reduce((acc, { address: address2, balance }) => {
|
|
241
|
+
acc[address2] = balance;
|
|
242
|
+
return acc;
|
|
243
|
+
}, {});
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Gets all available connectors for this chain
|
|
247
|
+
*/
|
|
248
|
+
getXConnectors() {
|
|
249
|
+
return this.xConnectors;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Sets the available connectors for this chain
|
|
253
|
+
*/
|
|
254
|
+
setXConnectors(xConnectors) {
|
|
255
|
+
this.xConnectors = xConnectors;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Gets a specific connector by its ID
|
|
259
|
+
* @param xConnectorId The connector ID to look up
|
|
260
|
+
* @returns The matching connector or undefined if not found
|
|
261
|
+
*/
|
|
262
|
+
getXConnectorById(xConnectorId) {
|
|
263
|
+
return this.getXConnectors().find((xConnector) => xConnector.id === xConnectorId);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// src/core/XConnector.ts
|
|
268
|
+
var XConnector = class {
|
|
269
|
+
constructor(xChainType, name, id) {
|
|
270
|
+
this.xChainType = xChainType;
|
|
271
|
+
this.name = name;
|
|
272
|
+
this._id = id;
|
|
273
|
+
}
|
|
274
|
+
/** Get the unique identifier for this connector */
|
|
275
|
+
get id() {
|
|
276
|
+
return this._id;
|
|
277
|
+
}
|
|
278
|
+
/** Get the optional icon URL for this wallet provider */
|
|
279
|
+
get icon() {
|
|
280
|
+
return this._icon;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/utils/index.ts
|
|
285
|
+
var isNativeToken = (xToken) => {
|
|
286
|
+
const nativeAddresses = [
|
|
287
|
+
"cx0000000000000000000000000000000000000000",
|
|
288
|
+
"0x0000000000000000000000000000000000000000",
|
|
289
|
+
"inj",
|
|
290
|
+
"0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",
|
|
291
|
+
"hx0000000000000000000000000000000000000000",
|
|
292
|
+
"11111111111111111111111111111111",
|
|
293
|
+
// solana
|
|
294
|
+
"CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA"
|
|
295
|
+
// stellar,
|
|
296
|
+
];
|
|
297
|
+
return nativeAddresses.includes(xToken.address);
|
|
298
|
+
};
|
|
299
|
+
var getWagmiChainId = (xChainId) => {
|
|
300
|
+
const xChainMap2 = {
|
|
301
|
+
"0xa869.fuji": 43113,
|
|
302
|
+
"sonic-blaze": 57054,
|
|
303
|
+
sonic: 146,
|
|
304
|
+
"0xa86a.avax": 43114,
|
|
305
|
+
"0x38.bsc": 56,
|
|
306
|
+
"0xa4b1.arbitrum": 42161,
|
|
307
|
+
"0x2105.base": 8453,
|
|
308
|
+
"0xa.optimism": 10,
|
|
309
|
+
"0x89.polygon": 137,
|
|
310
|
+
hyper: 999,
|
|
311
|
+
lightlink: 1890
|
|
312
|
+
};
|
|
313
|
+
return xChainMap2[xChainId] ?? 0;
|
|
314
|
+
};
|
|
315
|
+
var hyper2 = /* @__PURE__ */ viem.defineChain({
|
|
316
|
+
id: 999,
|
|
317
|
+
name: "HyperEVM",
|
|
318
|
+
nativeCurrency: {
|
|
319
|
+
decimals: 18,
|
|
320
|
+
name: "HYPE",
|
|
321
|
+
symbol: "HYPE"
|
|
322
|
+
},
|
|
323
|
+
rpcUrls: {
|
|
324
|
+
default: { http: ["https://rpc.hyperliquid.xyz/evm"] }
|
|
325
|
+
},
|
|
326
|
+
blockExplorers: {
|
|
327
|
+
default: {
|
|
328
|
+
name: "HyperEVMScan",
|
|
329
|
+
url: "https://hyperevmscan.io/"
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
contracts: {
|
|
333
|
+
multicall3: {
|
|
334
|
+
address: "0xcA11bde05977b3631167028862bE2a173976CA11",
|
|
335
|
+
blockCreated: 13051
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
var evmChainMap = {
|
|
340
|
+
[types.AVALANCHE_MAINNET_CHAIN_ID]: chains.avalanche,
|
|
341
|
+
[types.ARBITRUM_MAINNET_CHAIN_ID]: chains.arbitrum,
|
|
342
|
+
[types.BASE_MAINNET_CHAIN_ID]: chains.base,
|
|
343
|
+
[types.BSC_MAINNET_CHAIN_ID]: chains.bsc,
|
|
344
|
+
[types.SONIC_MAINNET_CHAIN_ID]: chains.sonic,
|
|
345
|
+
[types.OPTIMISM_MAINNET_CHAIN_ID]: chains.optimism,
|
|
346
|
+
[types.POLYGON_MAINNET_CHAIN_ID]: chains.polygon,
|
|
347
|
+
[types.NIBIRU_MAINNET_CHAIN_ID]: chains.nibiru,
|
|
348
|
+
[types.HYPEREVM_MAINNET_CHAIN_ID]: hyper2,
|
|
349
|
+
[types.LIGHTLINK_MAINNET_CHAIN_ID]: chains.lightlinkPhoenix
|
|
350
|
+
};
|
|
351
|
+
var getWagmiConfig = (chains$1) => {
|
|
352
|
+
const mappedChains = chains$1.map((chain) => evmChainMap[chain]);
|
|
353
|
+
const finalChains = mappedChains.length > 0 ? mappedChains : [chains.mainnet];
|
|
354
|
+
const transports = finalChains.reduce(
|
|
355
|
+
(acc, chain) => {
|
|
356
|
+
acc[chain.id] = wagmi.http();
|
|
357
|
+
return acc;
|
|
358
|
+
},
|
|
359
|
+
{}
|
|
360
|
+
);
|
|
361
|
+
return wagmi.createConfig({
|
|
362
|
+
chains: finalChains,
|
|
363
|
+
transports
|
|
364
|
+
// ssr: true,
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
var EvmXService = class _EvmXService extends XService {
|
|
368
|
+
constructor() {
|
|
369
|
+
super("EVM");
|
|
370
|
+
}
|
|
371
|
+
getXConnectors() {
|
|
372
|
+
return [];
|
|
373
|
+
}
|
|
374
|
+
static getInstance() {
|
|
375
|
+
if (!_EvmXService.instance) {
|
|
376
|
+
_EvmXService.instance = new _EvmXService();
|
|
377
|
+
}
|
|
378
|
+
return _EvmXService.instance;
|
|
379
|
+
}
|
|
380
|
+
setConfig(config) {
|
|
381
|
+
this.config = config;
|
|
382
|
+
}
|
|
383
|
+
getPublicClient(chainId) {
|
|
384
|
+
if (!this.config) {
|
|
385
|
+
throw new Error("EvmXService: config is not initialized yet");
|
|
386
|
+
}
|
|
387
|
+
return actions.getPublicClient(getWagmiConfig(this.config.chains), { chainId });
|
|
388
|
+
}
|
|
389
|
+
async getWalletClient(chainId) {
|
|
390
|
+
if (!this.config) {
|
|
391
|
+
throw new Error("EvmXService: config is not initialized yet");
|
|
392
|
+
}
|
|
393
|
+
return await actions.getWalletClient(getWagmiConfig(this.config.chains), { chainId });
|
|
394
|
+
}
|
|
395
|
+
async getBalance(address, xToken, xChainId) {
|
|
396
|
+
if (!address) return 0n;
|
|
397
|
+
const chainId = getWagmiChainId(xChainId);
|
|
398
|
+
if (isNativeToken(xToken)) {
|
|
399
|
+
const balance = await this.getPublicClient(chainId)?.getBalance({ address });
|
|
400
|
+
return balance || 0n;
|
|
401
|
+
}
|
|
402
|
+
throw new Error(`Unsupported token: ${xToken.symbol}`);
|
|
403
|
+
}
|
|
404
|
+
async getBalances(address, xTokens, xChainId) {
|
|
405
|
+
if (!address) return {};
|
|
406
|
+
const balancePromises = xTokens.filter((xToken) => isNativeToken(xToken)).map(async (xToken) => {
|
|
407
|
+
const balance = await this.getBalance(address, xToken, xChainId);
|
|
408
|
+
return { symbol: xToken.symbol, address: xToken.address, balance };
|
|
409
|
+
});
|
|
410
|
+
const balances = await Promise.all(balancePromises);
|
|
411
|
+
const tokenMap = balances.reduce((map, { address: address2, balance }) => {
|
|
412
|
+
if (balance) map[address2] = balance;
|
|
413
|
+
return map;
|
|
414
|
+
}, {});
|
|
415
|
+
const nonNativeXTokens = xTokens.filter((xToken) => !isNativeToken(xToken));
|
|
416
|
+
const result = await this.getPublicClient(getWagmiChainId(xChainId))?.multicall({
|
|
417
|
+
contracts: nonNativeXTokens.map((token) => ({
|
|
418
|
+
abi: viem.erc20Abi,
|
|
419
|
+
address: token.address,
|
|
420
|
+
functionName: "balanceOf",
|
|
421
|
+
args: [address],
|
|
422
|
+
chainId: getWagmiChainId(xChainId)
|
|
423
|
+
}))
|
|
424
|
+
});
|
|
425
|
+
return nonNativeXTokens.map((token, index) => ({
|
|
426
|
+
symbol: token.symbol,
|
|
427
|
+
address: token.address,
|
|
428
|
+
balance: result?.[index]?.result?.toString() || "0"
|
|
429
|
+
})).reduce((acc, balance) => {
|
|
430
|
+
acc[balance.address] = balance.balance;
|
|
431
|
+
return acc;
|
|
432
|
+
}, tokenMap);
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// src/xchains/evm/EvmXConnector.ts
|
|
437
|
+
var EvmXConnector = class extends XConnector {
|
|
438
|
+
constructor(connector) {
|
|
439
|
+
super("EVM", connector.name, connector.id);
|
|
440
|
+
this.connector = connector;
|
|
441
|
+
}
|
|
442
|
+
async connect() {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
async disconnect() {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
get id() {
|
|
449
|
+
return this.connector.id;
|
|
450
|
+
}
|
|
451
|
+
get icon() {
|
|
452
|
+
return this.connector.icon;
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
var CHAIN_INFO = {
|
|
456
|
+
[1 /* MAINNET */]: {
|
|
457
|
+
APIEndpoint: "https://ctz.solidwallet.io/api/v3"}
|
|
458
|
+
};
|
|
459
|
+
var IconXService = class _IconXService extends XService {
|
|
460
|
+
constructor() {
|
|
461
|
+
super("ICON");
|
|
462
|
+
this.iconService = new iconSdkJs.IconService(
|
|
463
|
+
new iconSdkJs.IconService.HttpProvider(CHAIN_INFO[1 /* MAINNET */].APIEndpoint)
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
static getInstance() {
|
|
467
|
+
if (!_IconXService.instance) {
|
|
468
|
+
_IconXService.instance = new _IconXService();
|
|
469
|
+
}
|
|
470
|
+
return _IconXService.instance;
|
|
471
|
+
}
|
|
472
|
+
async getAggregateData(requireSuccess, calls) {
|
|
473
|
+
const rawTx = new iconSdkJs.Builder.CallBuilder().to("cxa4aa9185e23558cff990f494c1fd2845f6cbf741").method("tryAggregate").params({ requireSuccess: iconSdkJs.Converter.toHex(requireSuccess ? 1 : 0), calls }).build();
|
|
474
|
+
try {
|
|
475
|
+
const result = await this.iconService.call(rawTx).execute();
|
|
476
|
+
const aggs = result["returnData"];
|
|
477
|
+
const data = aggs.map((agg) => {
|
|
478
|
+
if (agg["success"] === "0x0") {
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
return agg["returnData"];
|
|
482
|
+
});
|
|
483
|
+
return data;
|
|
484
|
+
} catch (err) {
|
|
485
|
+
console.error(err);
|
|
486
|
+
return Array(calls.length).fill(null);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
async getBalances(address, xTokens, xChainId) {
|
|
490
|
+
if (!address) return {};
|
|
491
|
+
const balances = {};
|
|
492
|
+
const nativeXToken = xTokens.find((xToken) => isNativeToken(xToken));
|
|
493
|
+
const nonNativeXTokens = xTokens.filter((xToken) => !isNativeToken(xToken));
|
|
494
|
+
if (nativeXToken) {
|
|
495
|
+
const balance = await this.iconService.getBalance(address).execute();
|
|
496
|
+
balances[nativeXToken.address] = BigInt(balance.toFixed());
|
|
497
|
+
}
|
|
498
|
+
const cds = nonNativeXTokens.map((token) => {
|
|
499
|
+
return {
|
|
500
|
+
target: token.address,
|
|
501
|
+
method: "balanceOf",
|
|
502
|
+
params: [address]
|
|
503
|
+
};
|
|
504
|
+
});
|
|
505
|
+
const data = await this.getAggregateData(
|
|
506
|
+
false,
|
|
507
|
+
cds.filter((cd) => cd.target.startsWith("cx"))
|
|
508
|
+
);
|
|
509
|
+
return nonNativeXTokens.reduce((agg, token, idx) => {
|
|
510
|
+
const balance = data[idx];
|
|
511
|
+
balances[token.address] = BigInt(balance);
|
|
512
|
+
return agg;
|
|
513
|
+
}, balances);
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// src/xchains/icon/iconex/index.tsx
|
|
518
|
+
var ICONEX_RELAY_RESPONSE = "ICONEX_RELAY_RESPONSE";
|
|
519
|
+
var ICONEX_RELAY_REQUEST = "ICONEX_RELAY_REQUEST";
|
|
520
|
+
var request = (event) => {
|
|
521
|
+
return new Promise((resolve, reject) => {
|
|
522
|
+
const handler = (evt) => {
|
|
523
|
+
window.removeEventListener(ICONEX_RELAY_RESPONSE, handler);
|
|
524
|
+
resolve(evt.detail);
|
|
525
|
+
};
|
|
526
|
+
window.addEventListener(ICONEX_RELAY_RESPONSE, handler);
|
|
527
|
+
window.dispatchEvent(
|
|
528
|
+
new CustomEvent(ICONEX_RELAY_REQUEST, {
|
|
529
|
+
detail: event
|
|
530
|
+
})
|
|
531
|
+
);
|
|
532
|
+
});
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// src/xchains/icon/IconHanaXConnector.ts
|
|
536
|
+
var IconHanaXConnector = class extends XConnector {
|
|
537
|
+
constructor() {
|
|
538
|
+
super("ICON", "Hana Wallet", "hana");
|
|
539
|
+
}
|
|
540
|
+
async connect() {
|
|
541
|
+
const { hanaWallet } = window;
|
|
542
|
+
if (window && !hanaWallet && !hanaWallet?.isAvailable) {
|
|
543
|
+
window.open("https://chromewebstore.google.com/detail/hana-wallet/jfdlamikmbghhapbgfoogdffldioobgl", "_blank");
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
const detail = await request({
|
|
547
|
+
type: "REQUEST_ADDRESS" /* REQUEST_ADDRESS */
|
|
548
|
+
});
|
|
549
|
+
if (detail?.type === "RESPONSE_ADDRESS" /* RESPONSE_ADDRESS */) {
|
|
550
|
+
return {
|
|
551
|
+
address: detail?.payload,
|
|
552
|
+
xChainType: this.xChainType
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
return void 0;
|
|
556
|
+
}
|
|
557
|
+
async disconnect() {
|
|
558
|
+
console.log("HanaIconXConnector disconnected");
|
|
559
|
+
}
|
|
560
|
+
get icon() {
|
|
561
|
+
return "https://raw.githubusercontent.com/balancednetwork/icons/master/wallets/hana.svg";
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
var InjectiveXService = class _InjectiveXService extends XService {
|
|
565
|
+
constructor() {
|
|
566
|
+
super("INJECTIVE");
|
|
567
|
+
const endpoints = networks.getNetworkEndpoints(networks.Network.Mainnet);
|
|
568
|
+
this.walletStrategy = new walletCore.BaseWalletStrategy({
|
|
569
|
+
chainId: tsTypes.ChainId.Mainnet,
|
|
570
|
+
strategies: {
|
|
571
|
+
[walletBase.Wallet.Metamask]: new walletEvm.EvmWalletStrategy({
|
|
572
|
+
chainId: tsTypes.ChainId.Mainnet,
|
|
573
|
+
wallet: walletBase.Wallet.Metamask,
|
|
574
|
+
evmOptions: {
|
|
575
|
+
evmChainId: tsTypes.EvmChainId.Mainnet,
|
|
576
|
+
rpcUrl: chains.mainnet.rpcUrls.default.http[0]
|
|
577
|
+
}
|
|
578
|
+
})
|
|
579
|
+
// [Wallet.Keplr]: new CosmosWalletStrategy({
|
|
580
|
+
// chainId: InjectiveChainId.Mainnet,
|
|
581
|
+
// wallet: Wallet.Keplr,
|
|
582
|
+
// }),
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
this.indexerGrpcAccountPortfolioApi = new sdkTs.IndexerGrpcAccountPortfolioApi(endpoints.indexer);
|
|
586
|
+
this.chainGrpcWasmApi = new sdkTs.ChainGrpcWasmApi(endpoints.grpc);
|
|
587
|
+
this.msgBroadcaster = new walletCore.MsgBroadcaster({
|
|
588
|
+
walletStrategy: this.walletStrategy,
|
|
589
|
+
network: networks.Network.Mainnet,
|
|
590
|
+
endpoints
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
static getInstance() {
|
|
594
|
+
if (!_InjectiveXService.instance) {
|
|
595
|
+
_InjectiveXService.instance = new _InjectiveXService();
|
|
596
|
+
}
|
|
597
|
+
return _InjectiveXService.instance;
|
|
598
|
+
}
|
|
599
|
+
async getBalance(address, xToken) {
|
|
600
|
+
if (!address) return 0n;
|
|
601
|
+
const portfolio = await this.indexerGrpcAccountPortfolioApi.fetchAccountPortfolioBalances(address);
|
|
602
|
+
const xTokenAddress = xToken.address;
|
|
603
|
+
const balance = portfolio.bankBalancesList.find((_balance) => _balance.denom === xTokenAddress);
|
|
604
|
+
if (balance) {
|
|
605
|
+
return BigInt(balance.amount);
|
|
606
|
+
}
|
|
607
|
+
return 0n;
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
var InjectiveKelprXConnector = class extends XConnector {
|
|
611
|
+
constructor() {
|
|
612
|
+
super("INJECTIVE", "Keplr", "keplr");
|
|
613
|
+
}
|
|
614
|
+
getXService() {
|
|
615
|
+
return InjectiveXService.getInstance();
|
|
616
|
+
}
|
|
617
|
+
async connect() {
|
|
618
|
+
if (!walletCosmos.isCosmosWalletInstalled(walletBase.Wallet.Keplr)) {
|
|
619
|
+
window.open("https://chrome.google.com/webstore/detail/keplr/dmkamcknogkgcdfhhbddcghachkejeap?hl=en", "_blank");
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
this.getXService().walletStrategy.setWallet(walletBase.Wallet.Keplr);
|
|
623
|
+
const addresses = await this.getXService().walletStrategy.getAddresses();
|
|
624
|
+
return {
|
|
625
|
+
address: addresses?.[0],
|
|
626
|
+
xChainType: this.xChainType
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
async disconnect() {
|
|
630
|
+
}
|
|
631
|
+
get icon() {
|
|
632
|
+
return "https://raw.githubusercontent.com/balancednetwork/icons/master/wallets/keplr.svg";
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
var InjectiveMetamaskXConnector = class extends XConnector {
|
|
636
|
+
constructor() {
|
|
637
|
+
super("INJECTIVE", "MetaMask", "metamask");
|
|
638
|
+
}
|
|
639
|
+
getXService() {
|
|
640
|
+
return InjectiveXService.getInstance();
|
|
641
|
+
}
|
|
642
|
+
async connect() {
|
|
643
|
+
if (!walletBase.isEvmBrowserWallet(walletBase.Wallet.Metamask)) {
|
|
644
|
+
window.open("https://chromewebstore.google.com/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en", "_blank");
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
this.getXService().walletStrategy.setWallet(walletBase.Wallet.Metamask);
|
|
648
|
+
const addresses = await this.getXService().walletStrategy.getAddresses();
|
|
649
|
+
const injectiveAddresses = addresses.map(sdkTs.getInjectiveAddress);
|
|
650
|
+
return {
|
|
651
|
+
address: injectiveAddresses?.[0],
|
|
652
|
+
xChainType: this.xChainType
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
async disconnect() {
|
|
656
|
+
await this.getXService().walletStrategy.disconnect();
|
|
657
|
+
}
|
|
658
|
+
get icon() {
|
|
659
|
+
return "https://raw.githubusercontent.com/balancednetwork/icons/master/wallets/metamask.svg";
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
// src/xchains/injective/utils.ts
|
|
664
|
+
var switchEthereumChain = async (chainId) => {
|
|
665
|
+
const metamaskProvider = window.ethereum;
|
|
666
|
+
await Promise.race([
|
|
667
|
+
metamaskProvider.request({
|
|
668
|
+
method: "wallet_switchEthereumChain",
|
|
669
|
+
params: [{ chainId: `0x${chainId}` }]
|
|
670
|
+
}),
|
|
671
|
+
new Promise(
|
|
672
|
+
(resolve) => metamaskProvider.on("change", ({ chain }) => {
|
|
673
|
+
if (chain?.id === chainId) {
|
|
674
|
+
resolve();
|
|
675
|
+
}
|
|
676
|
+
})
|
|
677
|
+
)
|
|
678
|
+
]);
|
|
679
|
+
};
|
|
680
|
+
var SolanaXService = class _SolanaXService extends XService {
|
|
681
|
+
constructor() {
|
|
682
|
+
super("SOLANA");
|
|
683
|
+
}
|
|
684
|
+
static getInstance() {
|
|
685
|
+
if (!_SolanaXService.instance) {
|
|
686
|
+
_SolanaXService.instance = new _SolanaXService();
|
|
687
|
+
}
|
|
688
|
+
return _SolanaXService.instance;
|
|
689
|
+
}
|
|
690
|
+
async getBalance(address, xToken) {
|
|
691
|
+
if (!address) return BigInt(0);
|
|
692
|
+
const connection = this.connection;
|
|
693
|
+
if (!connection) {
|
|
694
|
+
throw new Error("Connection is not initialized");
|
|
695
|
+
}
|
|
696
|
+
try {
|
|
697
|
+
if (isNativeToken(xToken)) {
|
|
698
|
+
const newBalance = await connection.getBalance(new web3_js.PublicKey(address));
|
|
699
|
+
return BigInt(newBalance);
|
|
700
|
+
}
|
|
701
|
+
const tokenAccountPubkey = splToken.getAssociatedTokenAddressSync(new web3_js.PublicKey(xToken.address), new web3_js.PublicKey(address));
|
|
702
|
+
const tokenAccount = await splToken.getAccount(connection, tokenAccountPubkey);
|
|
703
|
+
return BigInt(tokenAccount.amount);
|
|
704
|
+
} catch (e) {
|
|
705
|
+
console.log("error", e);
|
|
706
|
+
}
|
|
707
|
+
return BigInt(0);
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
// src/xchains/solana/SolanaXConnector.ts
|
|
712
|
+
var SolanaXConnector = class extends XConnector {
|
|
713
|
+
constructor(wallet) {
|
|
714
|
+
super("SOLANA", wallet?.adapter.name, wallet?.adapter.name);
|
|
715
|
+
this.wallet = wallet;
|
|
716
|
+
}
|
|
717
|
+
getXService() {
|
|
718
|
+
return SolanaXService.getInstance();
|
|
719
|
+
}
|
|
720
|
+
async connect() {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
async disconnect() {
|
|
724
|
+
}
|
|
725
|
+
get icon() {
|
|
726
|
+
return this.wallet?.adapter.icon;
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
var CustomSorobanServer = class extends StellarSdk.SorobanRpc.Server {
|
|
730
|
+
constructor(serverUrl, customHeaders) {
|
|
731
|
+
super(serverUrl, {
|
|
732
|
+
allowHttp: true
|
|
733
|
+
});
|
|
734
|
+
this.customHeaders = customHeaders;
|
|
735
|
+
}
|
|
736
|
+
async simulateTransaction(tx) {
|
|
737
|
+
const requestOptions = {
|
|
738
|
+
method: "POST",
|
|
739
|
+
headers: {
|
|
740
|
+
"Content-Type": "application/json",
|
|
741
|
+
...this.customHeaders
|
|
742
|
+
},
|
|
743
|
+
body: JSON.stringify({
|
|
744
|
+
id: 1,
|
|
745
|
+
jsonrpc: "2.0",
|
|
746
|
+
method: "simulateTransaction",
|
|
747
|
+
params: {
|
|
748
|
+
transaction: tx.toXDR()
|
|
749
|
+
}
|
|
750
|
+
})
|
|
751
|
+
};
|
|
752
|
+
const response = await fetch(`${this.serverURL}`, requestOptions);
|
|
753
|
+
if (!response.ok) {
|
|
754
|
+
throw new Error(`HTTP error simulating TX! status: ${response.status}`);
|
|
755
|
+
}
|
|
756
|
+
return response.json().then((json) => json.result);
|
|
757
|
+
}
|
|
758
|
+
async sendTransaction(tx) {
|
|
759
|
+
const requestOptions = {
|
|
760
|
+
method: "POST",
|
|
761
|
+
headers: {
|
|
762
|
+
"Content-Type": "application/json",
|
|
763
|
+
...this.customHeaders
|
|
764
|
+
},
|
|
765
|
+
body: JSON.stringify({
|
|
766
|
+
id: 1,
|
|
767
|
+
jsonrpc: "2.0",
|
|
768
|
+
method: "sendTransaction",
|
|
769
|
+
params: {
|
|
770
|
+
transaction: tx.toXDR()
|
|
771
|
+
}
|
|
772
|
+
})
|
|
773
|
+
};
|
|
774
|
+
const response = await fetch(`${this.serverURL}`, requestOptions);
|
|
775
|
+
if (!response.ok) {
|
|
776
|
+
throw new Error(`HTTP error submitting TX! status: ${response.status}`);
|
|
777
|
+
}
|
|
778
|
+
return response.json().then((json) => json.result);
|
|
779
|
+
}
|
|
780
|
+
async getTransaction(hash) {
|
|
781
|
+
const requestOptions = {
|
|
782
|
+
method: "POST",
|
|
783
|
+
headers: {
|
|
784
|
+
"Content-Type": "application/json",
|
|
785
|
+
...this.customHeaders
|
|
786
|
+
},
|
|
787
|
+
body: JSON.stringify({
|
|
788
|
+
id: 1,
|
|
789
|
+
jsonrpc: "2.0",
|
|
790
|
+
method: "getTransaction",
|
|
791
|
+
params: { hash }
|
|
792
|
+
})
|
|
793
|
+
};
|
|
794
|
+
const response = await fetch(`${this.serverURL}`, requestOptions);
|
|
795
|
+
if (!response.ok) {
|
|
796
|
+
throw new Error(`HTTP error getting TX! status: ${response.status}`);
|
|
797
|
+
}
|
|
798
|
+
return response.json().then((json) => json.result);
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
var CustomSorobanServer_default = CustomSorobanServer;
|
|
802
|
+
var accountToScVal = (account) => new StellarSdk.Address(account).toScVal();
|
|
803
|
+
var simulateTx = async (tx, server) => {
|
|
804
|
+
const response = await server.simulateTransaction(tx);
|
|
805
|
+
if (response !== void 0) {
|
|
806
|
+
return response;
|
|
807
|
+
}
|
|
808
|
+
throw new Error("cannot simulate transaction");
|
|
809
|
+
};
|
|
810
|
+
var getTokenBalance = async (address, tokenId, txBuilder, server) => {
|
|
811
|
+
const params = [accountToScVal(address)];
|
|
812
|
+
const contract = new StellarSdk.Contract(tokenId);
|
|
813
|
+
const tx = txBuilder.addOperation(contract.call("balance", ...params)).setTimeout(StellarSdk.TimeoutInfinite).build();
|
|
814
|
+
const result = await simulateTx(tx, server);
|
|
815
|
+
return result.results ? StellarSdk.scValToBigInt(StellarSdk.xdr.ScVal.fromXDR(result.results[0].xdr, "base64")) : 0n;
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
// src/xchains/stellar/StellarXService.ts
|
|
819
|
+
var StellarXService = class _StellarXService extends XService {
|
|
820
|
+
constructor() {
|
|
821
|
+
super("STELLAR");
|
|
822
|
+
this.walletsKit = new stellarWalletsKit.StellarWalletsKit({
|
|
823
|
+
network: stellarWalletsKit.WalletNetwork.PUBLIC,
|
|
824
|
+
selectedWalletId: stellarWalletsKit.FREIGHTER_ID,
|
|
825
|
+
modules: stellarWalletsKit.allowAllModules()
|
|
826
|
+
});
|
|
827
|
+
this.server = new StellarSdk__namespace.Horizon.Server("https://horizon.stellar.org", { allowHttp: true });
|
|
828
|
+
this.sorobanServer = new CustomSorobanServer_default("https://rpc.ankr.com/stellar_soroban", {});
|
|
829
|
+
}
|
|
830
|
+
static getInstance() {
|
|
831
|
+
if (!_StellarXService.instance) {
|
|
832
|
+
_StellarXService.instance = new _StellarXService();
|
|
833
|
+
}
|
|
834
|
+
return _StellarXService.instance;
|
|
835
|
+
}
|
|
836
|
+
async getBalance(address, xToken) {
|
|
837
|
+
if (!address) return BigInt(0);
|
|
838
|
+
const stellarAccount = await this.server.loadAccount(address);
|
|
839
|
+
if (xToken.symbol === "XLM") {
|
|
840
|
+
const xlmBalance = stellarAccount.balances.find((balance) => balance.asset_type === "native");
|
|
841
|
+
if (xlmBalance) {
|
|
842
|
+
return BigInt(xlmBalance.balance.replace(".", ""));
|
|
843
|
+
}
|
|
844
|
+
} else {
|
|
845
|
+
try {
|
|
846
|
+
const txBuilder = new StellarSdk__namespace.TransactionBuilder(stellarAccount, {
|
|
847
|
+
fee: StellarSdk__namespace.BASE_FEE,
|
|
848
|
+
networkPassphrase: StellarSdk__namespace.Networks.PUBLIC
|
|
849
|
+
});
|
|
850
|
+
const balance = await getTokenBalance(address, xToken.address, txBuilder, this.sorobanServer);
|
|
851
|
+
return balance;
|
|
852
|
+
} catch (e) {
|
|
853
|
+
console.error(`Error while fetching token on Stellar: ${xToken.symbol}, Error: ${e}`);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return BigInt(0);
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
|
|
860
|
+
// src/xchains/stellar/StellarWalletsKitXConnector.ts
|
|
861
|
+
var StellarWalletsKitXConnector = class extends XConnector {
|
|
862
|
+
constructor(wallet) {
|
|
863
|
+
super("STELLAR", wallet.name, wallet.id);
|
|
864
|
+
this._wallet = wallet;
|
|
865
|
+
}
|
|
866
|
+
getXService() {
|
|
867
|
+
return StellarXService.getInstance();
|
|
868
|
+
}
|
|
869
|
+
async connect() {
|
|
870
|
+
const kit = this.getXService().walletsKit;
|
|
871
|
+
if (!this._wallet) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
if (!this._wallet.isAvailable && this._wallet.url) {
|
|
875
|
+
window.open(this._wallet.url, "_blank");
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
kit.setWallet(this._wallet.id);
|
|
879
|
+
const { address } = await kit.getAddress();
|
|
880
|
+
return {
|
|
881
|
+
address,
|
|
882
|
+
xChainType: this.xChainType
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
async disconnect() {
|
|
886
|
+
}
|
|
887
|
+
get icon() {
|
|
888
|
+
return this._wallet.icon;
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
// src/xchains/sui/SuiXService.ts
|
|
893
|
+
var SuiXService = class _SuiXService extends XService {
|
|
894
|
+
// TODO: define suiAccount type
|
|
895
|
+
constructor() {
|
|
896
|
+
super("SUI");
|
|
897
|
+
}
|
|
898
|
+
static getInstance() {
|
|
899
|
+
if (!_SuiXService.instance) {
|
|
900
|
+
_SuiXService.instance = new _SuiXService();
|
|
901
|
+
}
|
|
902
|
+
return _SuiXService.instance;
|
|
903
|
+
}
|
|
904
|
+
// getBalance is not used because getBalances uses getAllBalances which returns all balances
|
|
905
|
+
async getBalances(address, xTokens) {
|
|
906
|
+
if (!address) return {};
|
|
907
|
+
try {
|
|
908
|
+
const allBalances = await this.suiClient.getAllBalances({
|
|
909
|
+
owner: address
|
|
910
|
+
});
|
|
911
|
+
const tokenMap = xTokens.reduce((map, xToken) => {
|
|
912
|
+
let coinType = isNativeToken(xToken) ? "0x2::sui::SUI" : xToken.address;
|
|
913
|
+
if (coinType === "0x03917a812fe4a6d6bc779c5ab53f8a80ba741f8af04121193fc44e0f662e2ceb::balanced_dollar::BALANCED_DOLLAR") {
|
|
914
|
+
coinType = "0x3917a812fe4a6d6bc779c5ab53f8a80ba741f8af04121193fc44e0f662e2ceb::balanced_dollar::BALANCED_DOLLAR";
|
|
915
|
+
}
|
|
916
|
+
const balance = allBalances.find((b) => b.coinType === coinType);
|
|
917
|
+
if (balance) map[xToken.address] = balance.totalBalance;
|
|
918
|
+
return map;
|
|
919
|
+
}, {});
|
|
920
|
+
return tokenMap;
|
|
921
|
+
} catch (e) {
|
|
922
|
+
console.log("error", e);
|
|
923
|
+
return {};
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
// src/xchains/sui/SuiXConnector.ts
|
|
929
|
+
var SuiXConnector = class extends XConnector {
|
|
930
|
+
constructor(wallet) {
|
|
931
|
+
super("SUI", wallet?.name, wallet?.name);
|
|
932
|
+
this.wallet = wallet;
|
|
933
|
+
}
|
|
934
|
+
getXService() {
|
|
935
|
+
return SuiXService.getInstance();
|
|
936
|
+
}
|
|
937
|
+
async connect() {
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
async disconnect() {
|
|
941
|
+
}
|
|
942
|
+
get icon() {
|
|
943
|
+
return this.wallet?.icon;
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
function useAnchorProvider() {
|
|
947
|
+
const { connection } = walletAdapterReact.useConnection();
|
|
948
|
+
const wallet = walletAdapterReact.useWallet();
|
|
949
|
+
return new anchor.AnchorProvider(connection, wallet, { commitment: "confirmed" });
|
|
950
|
+
}
|
|
951
|
+
var useXWagmiStore = zustand.create()(
|
|
952
|
+
middleware.persist(
|
|
953
|
+
immer.immer((set, get) => ({
|
|
954
|
+
xServices: {},
|
|
955
|
+
xConnections: {},
|
|
956
|
+
setXConnection: (xChainType, xConnection) => {
|
|
957
|
+
set((state) => {
|
|
958
|
+
state.xConnections[xChainType] = xConnection;
|
|
959
|
+
});
|
|
960
|
+
},
|
|
961
|
+
unsetXConnection: (xChainType) => {
|
|
962
|
+
set((state) => {
|
|
963
|
+
delete state.xConnections[xChainType];
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
})),
|
|
967
|
+
{
|
|
968
|
+
name: "xwagmi-store",
|
|
969
|
+
storage: middleware.createJSONStorage(() => localStorage),
|
|
970
|
+
partialize: (state) => ({ xConnections: state.xConnections }),
|
|
971
|
+
// TODO: better way to handle rehydration of xConnections?
|
|
972
|
+
onRehydrateStorage: (state) => {
|
|
973
|
+
console.log("hydration starts");
|
|
974
|
+
return (state2, error) => {
|
|
975
|
+
if (state2?.xConnections) {
|
|
976
|
+
console.log("rehydrating xConnections", state2.xConnections);
|
|
977
|
+
Object.entries(state2.xConnections).forEach(([xChainType, xConnection]) => {
|
|
978
|
+
const xConnector = getXService(xChainType).getXConnectorById(xConnection.xConnectorId);
|
|
979
|
+
xConnector?.connect();
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
if (error) {
|
|
983
|
+
console.log("an error happened during hydration", error);
|
|
984
|
+
} else {
|
|
985
|
+
console.log("hydration finished");
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
)
|
|
991
|
+
);
|
|
992
|
+
var initXServices = (config) => {
|
|
993
|
+
const xServices = {};
|
|
994
|
+
Object.keys(config).forEach((key) => {
|
|
995
|
+
const xChainType = key;
|
|
996
|
+
switch (xChainType) {
|
|
997
|
+
case "EVM":
|
|
998
|
+
if (config[xChainType]) {
|
|
999
|
+
xServices[xChainType] = EvmXService.getInstance();
|
|
1000
|
+
xServices[xChainType].setXConnectors([]);
|
|
1001
|
+
xServices[xChainType].setConfig(config[xChainType]);
|
|
1002
|
+
}
|
|
1003
|
+
break;
|
|
1004
|
+
case "INJECTIVE":
|
|
1005
|
+
xServices[xChainType] = InjectiveXService.getInstance();
|
|
1006
|
+
xServices[xChainType].setXConnectors([new InjectiveMetamaskXConnector()]);
|
|
1007
|
+
break;
|
|
1008
|
+
case "STELLAR":
|
|
1009
|
+
xServices[xChainType] = StellarXService.getInstance();
|
|
1010
|
+
xServices[xChainType].setXConnectors([]);
|
|
1011
|
+
break;
|
|
1012
|
+
case "SUI":
|
|
1013
|
+
xServices[xChainType] = SuiXService.getInstance();
|
|
1014
|
+
xServices[xChainType].setXConnectors([]);
|
|
1015
|
+
break;
|
|
1016
|
+
case "SOLANA":
|
|
1017
|
+
xServices[xChainType] = SolanaXService.getInstance();
|
|
1018
|
+
xServices[xChainType].setXConnectors([]);
|
|
1019
|
+
break;
|
|
1020
|
+
case "ICON":
|
|
1021
|
+
xServices[xChainType] = IconXService.getInstance();
|
|
1022
|
+
xServices[xChainType].setXConnectors([new IconHanaXConnector()]);
|
|
1023
|
+
break;
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
return xServices;
|
|
1027
|
+
};
|
|
1028
|
+
var initXWagmiStore = (config) => {
|
|
1029
|
+
useXWagmiStore.setState({
|
|
1030
|
+
xServices: initXServices(config)
|
|
1031
|
+
});
|
|
1032
|
+
};
|
|
1033
|
+
var InitXWagmiStore = () => {
|
|
1034
|
+
const suiClient = dappKit.useSuiClient();
|
|
1035
|
+
React3.useEffect(() => {
|
|
1036
|
+
if (suiClient) {
|
|
1037
|
+
SuiXService.getInstance().suiClient = suiClient;
|
|
1038
|
+
}
|
|
1039
|
+
}, [suiClient]);
|
|
1040
|
+
const { currentWallet: suiWallet } = dappKit.useCurrentWallet();
|
|
1041
|
+
React3.useEffect(() => {
|
|
1042
|
+
if (suiWallet) {
|
|
1043
|
+
SuiXService.getInstance().suiWallet = suiWallet;
|
|
1044
|
+
}
|
|
1045
|
+
}, [suiWallet]);
|
|
1046
|
+
const suiAccount = dappKit.useCurrentAccount();
|
|
1047
|
+
React3.useEffect(() => {
|
|
1048
|
+
if (suiAccount) {
|
|
1049
|
+
SuiXService.getInstance().suiAccount = suiAccount;
|
|
1050
|
+
}
|
|
1051
|
+
}, [suiAccount]);
|
|
1052
|
+
const { connection: solanaConnection } = walletAdapterReact.useConnection();
|
|
1053
|
+
const solanaWallet = walletAdapterReact.useWallet();
|
|
1054
|
+
const solanaProvider = useAnchorProvider();
|
|
1055
|
+
React3.useEffect(() => {
|
|
1056
|
+
if (solanaConnection) {
|
|
1057
|
+
SolanaXService.getInstance().connection = solanaConnection;
|
|
1058
|
+
}
|
|
1059
|
+
}, [solanaConnection]);
|
|
1060
|
+
React3.useEffect(() => {
|
|
1061
|
+
if (solanaWallet) {
|
|
1062
|
+
SolanaXService.getInstance().wallet = solanaWallet;
|
|
1063
|
+
}
|
|
1064
|
+
}, [solanaWallet]);
|
|
1065
|
+
React3.useEffect(() => {
|
|
1066
|
+
if (solanaProvider) {
|
|
1067
|
+
SolanaXService.getInstance().provider = solanaProvider;
|
|
1068
|
+
}
|
|
1069
|
+
}, [solanaProvider]);
|
|
1070
|
+
return /* @__PURE__ */ React3__default.default.createElement(React3__default.default.Fragment, null);
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
// src/hooks/useXConnection.ts
|
|
1074
|
+
function useXConnection(xChainType) {
|
|
1075
|
+
const xConnection = useXWagmiStore((state) => xChainType ? state.xConnections?.[xChainType] : void 0);
|
|
1076
|
+
const evmConnections = wagmi.useConnections();
|
|
1077
|
+
const { address: evmAddress } = wagmi.useAccount();
|
|
1078
|
+
const suiAccount = dappKit.useCurrentAccount();
|
|
1079
|
+
const suiCurrentWallet = dappKit.useCurrentWallet();
|
|
1080
|
+
const solanaWallet = walletAdapterReact.useWallet();
|
|
1081
|
+
const xConnection2 = React3.useMemo(() => {
|
|
1082
|
+
if (!xChainType) {
|
|
1083
|
+
return void 0;
|
|
1084
|
+
}
|
|
1085
|
+
switch (xChainType) {
|
|
1086
|
+
case "EVM":
|
|
1087
|
+
return {
|
|
1088
|
+
xAccount: { address: evmAddress, xChainType },
|
|
1089
|
+
xConnectorId: evmConnections?.[0]?.connector.id
|
|
1090
|
+
};
|
|
1091
|
+
case "SUI":
|
|
1092
|
+
if (suiCurrentWallet.currentWallet && suiCurrentWallet.connectionStatus === "connected") {
|
|
1093
|
+
return {
|
|
1094
|
+
xAccount: { address: suiAccount?.address, xChainType },
|
|
1095
|
+
xConnectorId: suiCurrentWallet.currentWallet.name
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
return void 0;
|
|
1099
|
+
case "SOLANA":
|
|
1100
|
+
if (solanaWallet.connected) {
|
|
1101
|
+
return {
|
|
1102
|
+
xAccount: { address: solanaWallet.publicKey?.toString(), xChainType },
|
|
1103
|
+
xConnectorId: `${solanaWallet.wallet?.adapter.name}`
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
return void 0;
|
|
1107
|
+
default:
|
|
1108
|
+
return xConnection;
|
|
1109
|
+
}
|
|
1110
|
+
}, [xChainType, xConnection, evmAddress, suiAccount, evmConnections, suiCurrentWallet, solanaWallet]);
|
|
1111
|
+
return xConnection2;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
// src/hooks/useXAccount.ts
|
|
1115
|
+
function isChainType(chainIdentifier) {
|
|
1116
|
+
return ["ICON", "EVM", "INJECTIVE", "SUI", "STELLAR", "SOLANA"].includes(chainIdentifier);
|
|
1117
|
+
}
|
|
1118
|
+
function useXAccount(chainIdentifier) {
|
|
1119
|
+
const resolvedChainType = chainIdentifier ? isChainType(chainIdentifier) ? chainIdentifier : getXChainType(chainIdentifier) : void 0;
|
|
1120
|
+
const xConnection = useXConnection(resolvedChainType);
|
|
1121
|
+
const xAccount = React3.useMemo(() => {
|
|
1122
|
+
if (!resolvedChainType) {
|
|
1123
|
+
return {
|
|
1124
|
+
address: void 0,
|
|
1125
|
+
xChainType: void 0
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
return xConnection?.xAccount || { address: void 0, xChainType: resolvedChainType };
|
|
1129
|
+
}, [resolvedChainType, xConnection]);
|
|
1130
|
+
return xAccount;
|
|
1131
|
+
}
|
|
1132
|
+
function useXAccounts() {
|
|
1133
|
+
const xChainTypes = useXWagmiStore((state) => Object.keys(state.xServices));
|
|
1134
|
+
const xConnections = useXWagmiStore((state) => state.xConnections);
|
|
1135
|
+
const { address: evmAddress } = wagmi.useAccount();
|
|
1136
|
+
const suiAccount = dappKit.useCurrentAccount();
|
|
1137
|
+
const solanaWallet = walletAdapterReact.useWallet();
|
|
1138
|
+
const xAccounts = React3.useMemo(() => {
|
|
1139
|
+
const result = {};
|
|
1140
|
+
for (const xChainType of xChainTypes) {
|
|
1141
|
+
const xConnection = xConnections[xChainType];
|
|
1142
|
+
if (xConnection?.xAccount) {
|
|
1143
|
+
result[xChainType] = xConnection.xAccount;
|
|
1144
|
+
} else {
|
|
1145
|
+
result[xChainType] = {
|
|
1146
|
+
address: void 0,
|
|
1147
|
+
xChainType
|
|
1148
|
+
};
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
if (evmAddress) {
|
|
1152
|
+
result["EVM"] = {
|
|
1153
|
+
address: evmAddress,
|
|
1154
|
+
xChainType: "EVM"
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
if (suiAccount) {
|
|
1158
|
+
result["SUI"] = {
|
|
1159
|
+
address: suiAccount.address,
|
|
1160
|
+
xChainType: "SUI"
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
if (solanaWallet.publicKey) {
|
|
1164
|
+
result["SOLANA"] = {
|
|
1165
|
+
address: solanaWallet.publicKey.toString(),
|
|
1166
|
+
xChainType: "SOLANA"
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
return result;
|
|
1170
|
+
}, [xChainTypes, xConnections, evmAddress, suiAccount, solanaWallet]);
|
|
1171
|
+
return xAccounts;
|
|
1172
|
+
}
|
|
1173
|
+
function useXConnect() {
|
|
1174
|
+
const setXConnection = useXWagmiStore((state) => state.setXConnection);
|
|
1175
|
+
const { connectAsync: evmConnectAsync } = wagmi.useConnect();
|
|
1176
|
+
const { mutateAsync: suiConnectAsync } = dappKit.useConnectWallet();
|
|
1177
|
+
const { select, connect, connected } = walletAdapterReact.useWallet();
|
|
1178
|
+
return reactQuery.useMutation({
|
|
1179
|
+
mutationFn: async (xConnector) => {
|
|
1180
|
+
const xChainType = xConnector.xChainType;
|
|
1181
|
+
let xAccount;
|
|
1182
|
+
switch (xChainType) {
|
|
1183
|
+
case "EVM":
|
|
1184
|
+
await evmConnectAsync({ connector: xConnector.connector });
|
|
1185
|
+
break;
|
|
1186
|
+
case "SUI":
|
|
1187
|
+
await suiConnectAsync({ wallet: xConnector.wallet });
|
|
1188
|
+
break;
|
|
1189
|
+
case "SOLANA": {
|
|
1190
|
+
const walletName = xConnector.wallet.adapter.name;
|
|
1191
|
+
await select(walletName);
|
|
1192
|
+
const adapter = xConnector.wallet.adapter;
|
|
1193
|
+
if (!adapter) throw new Error("No adapter found for Solana wallet");
|
|
1194
|
+
if (walletName === "MetaMask") {
|
|
1195
|
+
await new Promise((resolve, reject) => {
|
|
1196
|
+
const timeout = setTimeout(() => {
|
|
1197
|
+
cleanup();
|
|
1198
|
+
reject(new Error("Wallet connection timeout"));
|
|
1199
|
+
}, 3e4);
|
|
1200
|
+
const handleConnect = () => {
|
|
1201
|
+
cleanup();
|
|
1202
|
+
resolve();
|
|
1203
|
+
};
|
|
1204
|
+
const handleError = (error) => {
|
|
1205
|
+
cleanup();
|
|
1206
|
+
reject(error);
|
|
1207
|
+
};
|
|
1208
|
+
const cleanup = () => {
|
|
1209
|
+
clearTimeout(timeout);
|
|
1210
|
+
adapter.off("connect", handleConnect);
|
|
1211
|
+
adapter.off("error", handleError);
|
|
1212
|
+
};
|
|
1213
|
+
adapter.on("connect", handleConnect);
|
|
1214
|
+
adapter.on("error", handleError);
|
|
1215
|
+
connect().catch((err) => {
|
|
1216
|
+
cleanup();
|
|
1217
|
+
reject(err);
|
|
1218
|
+
});
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
break;
|
|
1222
|
+
}
|
|
1223
|
+
default:
|
|
1224
|
+
xAccount = await xConnector.connect();
|
|
1225
|
+
break;
|
|
1226
|
+
}
|
|
1227
|
+
if (xAccount) {
|
|
1228
|
+
setXConnection(xConnector.xChainType, {
|
|
1229
|
+
xAccount,
|
|
1230
|
+
xConnectorId: xConnector.id
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
return xAccount;
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
var useStellarXConnectors = () => {
|
|
1238
|
+
const xService = useXService("STELLAR");
|
|
1239
|
+
return reactQuery.useQuery({
|
|
1240
|
+
queryKey: ["stellar-wallets", xService],
|
|
1241
|
+
queryFn: async () => {
|
|
1242
|
+
if (!xService) {
|
|
1243
|
+
return [];
|
|
1244
|
+
}
|
|
1245
|
+
const wallets = await xService.walletsKit.getSupportedWallets();
|
|
1246
|
+
return wallets.filter((wallet) => wallet.isAvailable).map((wallet) => new StellarWalletsKitXConnector(wallet));
|
|
1247
|
+
}
|
|
1248
|
+
});
|
|
1249
|
+
};
|
|
1250
|
+
|
|
1251
|
+
// src/hooks/useXService.ts
|
|
1252
|
+
function useXService(xChainType) {
|
|
1253
|
+
const xService = useXWagmiStore((state) => xChainType ? state.xServices[xChainType] : void 0);
|
|
1254
|
+
return xService;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
// src/hooks/useXConnectors.ts
|
|
1258
|
+
function useXConnectors(xChainType) {
|
|
1259
|
+
const xService = useXService(xChainType);
|
|
1260
|
+
const evmConnectors = wagmi.useConnectors();
|
|
1261
|
+
const suiWallets = dappKit.useWallets();
|
|
1262
|
+
const { data: stellarXConnectors } = useStellarXConnectors();
|
|
1263
|
+
const { wallets: solanaWallets } = walletAdapterReact.useWallet();
|
|
1264
|
+
const xConnectors = React3.useMemo(() => {
|
|
1265
|
+
if (!xChainType || !xService) {
|
|
1266
|
+
return [];
|
|
1267
|
+
}
|
|
1268
|
+
switch (xChainType) {
|
|
1269
|
+
case "EVM":
|
|
1270
|
+
return evmConnectors.map((connector) => new EvmXConnector(connector));
|
|
1271
|
+
case "SUI":
|
|
1272
|
+
return suiWallets.map((wallet) => new SuiXConnector(wallet));
|
|
1273
|
+
case "STELLAR":
|
|
1274
|
+
return stellarXConnectors || [];
|
|
1275
|
+
case "SOLANA":
|
|
1276
|
+
return solanaWallets.filter((wallet) => wallet.readyState === "Installed").map((wallet) => new SolanaXConnector(wallet));
|
|
1277
|
+
default:
|
|
1278
|
+
return xService.getXConnectors();
|
|
1279
|
+
}
|
|
1280
|
+
}, [xService, xChainType, evmConnectors, suiWallets, stellarXConnectors, solanaWallets]);
|
|
1281
|
+
return xConnectors;
|
|
1282
|
+
}
|
|
1283
|
+
function useXDisconnect() {
|
|
1284
|
+
const xConnections = useXWagmiStore((state) => state.xConnections);
|
|
1285
|
+
const unsetXConnection = useXWagmiStore((state) => state.unsetXConnection);
|
|
1286
|
+
const { disconnectAsync } = wagmi.useDisconnect();
|
|
1287
|
+
const { mutateAsync: suiDisconnectAsync } = dappKit.useDisconnectWallet();
|
|
1288
|
+
const solanaWallet = walletAdapterReact.useWallet();
|
|
1289
|
+
return React3.useCallback(
|
|
1290
|
+
async (xChainType) => {
|
|
1291
|
+
switch (xChainType) {
|
|
1292
|
+
case "EVM":
|
|
1293
|
+
await disconnectAsync();
|
|
1294
|
+
break;
|
|
1295
|
+
case "SUI":
|
|
1296
|
+
await suiDisconnectAsync();
|
|
1297
|
+
break;
|
|
1298
|
+
case "SOLANA":
|
|
1299
|
+
await solanaWallet.disconnect();
|
|
1300
|
+
break;
|
|
1301
|
+
default: {
|
|
1302
|
+
const xService = getXService(xChainType);
|
|
1303
|
+
const xConnectorId = xConnections[xChainType]?.xConnectorId;
|
|
1304
|
+
const xConnector = xConnectorId ? xService.getXConnectorById(xConnectorId) : void 0;
|
|
1305
|
+
await xConnector?.disconnect();
|
|
1306
|
+
break;
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
unsetXConnection(xChainType);
|
|
1310
|
+
},
|
|
1311
|
+
[xConnections, unsetXConnection, disconnectAsync, suiDisconnectAsync, solanaWallet]
|
|
1312
|
+
);
|
|
1313
|
+
}
|
|
1314
|
+
function useXBalances({
|
|
1315
|
+
xChainId,
|
|
1316
|
+
xTokens,
|
|
1317
|
+
address
|
|
1318
|
+
}) {
|
|
1319
|
+
const xService = useXService(getXChainType(xChainId));
|
|
1320
|
+
return reactQuery.useQuery({
|
|
1321
|
+
queryKey: ["xBalances", xChainId, xTokens.map((x) => x.symbol), address],
|
|
1322
|
+
queryFn: async () => {
|
|
1323
|
+
if (!xService) {
|
|
1324
|
+
return {};
|
|
1325
|
+
}
|
|
1326
|
+
const balances = await xService.getBalances(address, xTokens, xChainId);
|
|
1327
|
+
return balances;
|
|
1328
|
+
},
|
|
1329
|
+
enabled: !!xService,
|
|
1330
|
+
placeholderData: reactQuery.keepPreviousData,
|
|
1331
|
+
refetchInterval: 5e3
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
function useWalletProviderOptions(xChainId) {
|
|
1335
|
+
const xChainType = getXChainType(xChainId);
|
|
1336
|
+
const evmPublicClient = wagmi.usePublicClient({
|
|
1337
|
+
chainId: xChainId ? getWagmiChainId(xChainId) : void 0
|
|
1338
|
+
});
|
|
1339
|
+
const { data: evmWalletClient } = wagmi.useWalletClient({
|
|
1340
|
+
chainId: xChainId ? getWagmiChainId(xChainId) : void 0
|
|
1341
|
+
});
|
|
1342
|
+
const xService = useXService(getXChainType(xChainId));
|
|
1343
|
+
const xAccount = useXAccount(xChainId);
|
|
1344
|
+
return React3.useMemo(() => {
|
|
1345
|
+
switch (xChainType) {
|
|
1346
|
+
case "EVM": {
|
|
1347
|
+
return { walletClient: evmWalletClient, publicClient: evmPublicClient };
|
|
1348
|
+
}
|
|
1349
|
+
case "SUI": {
|
|
1350
|
+
const suiXService = xService;
|
|
1351
|
+
return { client: suiXService.suiClient, wallet: suiXService.suiWallet, account: suiXService.suiAccount };
|
|
1352
|
+
}
|
|
1353
|
+
case "ICON": {
|
|
1354
|
+
return { walletAddress: xAccount.address, rpcUrl: CHAIN_INFO[1 /* MAINNET */].APIEndpoint };
|
|
1355
|
+
}
|
|
1356
|
+
case "INJECTIVE": {
|
|
1357
|
+
const injectiveXService = xService;
|
|
1358
|
+
const endpoints = networks.getNetworkEndpoints(networks.Network.Mainnet);
|
|
1359
|
+
return {
|
|
1360
|
+
walletAddress: xAccount.address,
|
|
1361
|
+
msgBroadcaster: injectiveXService.msgBroadcaster,
|
|
1362
|
+
rpcUrl: endpoints.rpc
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
default:
|
|
1366
|
+
return void 0;
|
|
1367
|
+
}
|
|
1368
|
+
}, [xChainType, evmPublicClient, evmWalletClient, xService, xAccount]);
|
|
1369
|
+
}
|
|
1370
|
+
function useEthereumChainId() {
|
|
1371
|
+
const injectiveXService = useXService("INJECTIVE");
|
|
1372
|
+
const [ethereumChainId, setEthereumChainId] = React3__default.default.useState(null);
|
|
1373
|
+
React3.useEffect(() => {
|
|
1374
|
+
if (!injectiveXService?.walletStrategy?.getWallet()) return;
|
|
1375
|
+
const walletStrategy = injectiveXService.walletStrategy;
|
|
1376
|
+
if (walletStrategy.getWallet() !== walletBase.Wallet.Metamask) return;
|
|
1377
|
+
const getEthereumChainId = async () => {
|
|
1378
|
+
const chainId = await walletStrategy.getEthereumChainId();
|
|
1379
|
+
setEthereumChainId(Number.parseInt(chainId));
|
|
1380
|
+
};
|
|
1381
|
+
getEthereumChainId();
|
|
1382
|
+
walletStrategy.getStrategy().onChainIdChanged(getEthereumChainId);
|
|
1383
|
+
}, [injectiveXService?.walletStrategy]);
|
|
1384
|
+
return ethereumChainId;
|
|
1385
|
+
}
|
|
1386
|
+
var switchEthereumChain2 = async () => {
|
|
1387
|
+
const metamaskProvider = window.ethereum;
|
|
1388
|
+
return await Promise.race([
|
|
1389
|
+
metamaskProvider.request({
|
|
1390
|
+
method: "wallet_switchEthereumChain",
|
|
1391
|
+
params: [{ chainId: "0x1" }]
|
|
1392
|
+
}),
|
|
1393
|
+
new Promise(
|
|
1394
|
+
(resolve) => metamaskProvider.on("change", ({ chain }) => {
|
|
1395
|
+
if (chain?.id === 1) {
|
|
1396
|
+
resolve();
|
|
1397
|
+
}
|
|
1398
|
+
})
|
|
1399
|
+
)
|
|
1400
|
+
]);
|
|
1401
|
+
};
|
|
1402
|
+
var useEvmSwitchChain = (expectedXChainId) => {
|
|
1403
|
+
const xChainType = getXChainType(expectedXChainId);
|
|
1404
|
+
const expectedChainId = xChainMap[expectedXChainId].id;
|
|
1405
|
+
const injectiveXService = useXService("INJECTIVE");
|
|
1406
|
+
const ethereumChainId = useEthereumChainId();
|
|
1407
|
+
const { chainId } = wagmi.useAccount();
|
|
1408
|
+
const isWrongChain = React3.useMemo(() => {
|
|
1409
|
+
return xChainType === "EVM" && chainId !== expectedChainId || xChainType === "INJECTIVE" && injectiveXService && injectiveXService.walletStrategy.getWallet() === walletBase.Wallet.Metamask && ethereumChainId !== chains$1.mainnet.id;
|
|
1410
|
+
}, [xChainType, chainId, expectedChainId, ethereumChainId, injectiveXService]);
|
|
1411
|
+
const { switchChain } = wagmi.useSwitchChain();
|
|
1412
|
+
const handleSwitchChain = React3.useCallback(() => {
|
|
1413
|
+
if (xChainType === "INJECTIVE") {
|
|
1414
|
+
switchEthereumChain2();
|
|
1415
|
+
} else {
|
|
1416
|
+
switchChain({ chainId: expectedChainId });
|
|
1417
|
+
}
|
|
1418
|
+
}, [switchChain, expectedChainId, xChainType]);
|
|
1419
|
+
return React3.useMemo(
|
|
1420
|
+
() => ({
|
|
1421
|
+
isWrongChain,
|
|
1422
|
+
handleSwitchChain
|
|
1423
|
+
}),
|
|
1424
|
+
[isWrongChain, handleSwitchChain]
|
|
1425
|
+
);
|
|
1426
|
+
};
|
|
1427
|
+
function useWalletProvider(spokeChainId) {
|
|
1428
|
+
const xChainType = getXChainType(spokeChainId);
|
|
1429
|
+
const evmPublicClient = wagmi.usePublicClient({
|
|
1430
|
+
chainId: spokeChainId ? getWagmiChainId(spokeChainId) : void 0
|
|
1431
|
+
});
|
|
1432
|
+
const { data: evmWalletClient } = wagmi.useWalletClient({
|
|
1433
|
+
chainId: spokeChainId ? getWagmiChainId(spokeChainId) : void 0
|
|
1434
|
+
});
|
|
1435
|
+
const xService = useXService(getXChainType(spokeChainId));
|
|
1436
|
+
const xAccount = useXAccount(spokeChainId);
|
|
1437
|
+
return React3.useMemo(() => {
|
|
1438
|
+
switch (xChainType) {
|
|
1439
|
+
case "EVM": {
|
|
1440
|
+
if (!evmWalletClient) {
|
|
1441
|
+
return void 0;
|
|
1442
|
+
}
|
|
1443
|
+
if (!evmPublicClient) {
|
|
1444
|
+
return void 0;
|
|
1445
|
+
}
|
|
1446
|
+
return new walletSdkCore.EvmWalletProvider({
|
|
1447
|
+
walletClient: evmWalletClient,
|
|
1448
|
+
publicClient: evmPublicClient
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
case "SUI": {
|
|
1452
|
+
const suiXService = xService;
|
|
1453
|
+
const { client, wallet, account } = {
|
|
1454
|
+
client: suiXService.suiClient,
|
|
1455
|
+
wallet: suiXService.suiWallet,
|
|
1456
|
+
account: suiXService.suiAccount
|
|
1457
|
+
};
|
|
1458
|
+
return new walletSdkCore.SuiWalletProvider({ client, wallet, account });
|
|
1459
|
+
}
|
|
1460
|
+
case "ICON": {
|
|
1461
|
+
const { walletAddress, rpcUrl } = {
|
|
1462
|
+
walletAddress: xAccount.address,
|
|
1463
|
+
rpcUrl: CHAIN_INFO[1 /* MAINNET */].APIEndpoint
|
|
1464
|
+
};
|
|
1465
|
+
return new walletSdkCore.IconWalletProvider({
|
|
1466
|
+
walletAddress,
|
|
1467
|
+
rpcUrl
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
case "INJECTIVE": {
|
|
1471
|
+
const injectiveXService = xService;
|
|
1472
|
+
if (!injectiveXService) {
|
|
1473
|
+
return void 0;
|
|
1474
|
+
}
|
|
1475
|
+
const { walletAddress, msgBroadcaster } = {
|
|
1476
|
+
walletAddress: xAccount.address,
|
|
1477
|
+
msgBroadcaster: injectiveXService.msgBroadcaster
|
|
1478
|
+
};
|
|
1479
|
+
return new walletSdkCore.InjectiveWalletProvider({
|
|
1480
|
+
walletAddress,
|
|
1481
|
+
msgBroadcaster
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
case "STELLAR": {
|
|
1485
|
+
const stellarXService = xService;
|
|
1486
|
+
return new walletSdkCore.StellarWalletProvider({
|
|
1487
|
+
type: "BROWSER_EXTENSION",
|
|
1488
|
+
walletsKit: stellarXService.walletsKit,
|
|
1489
|
+
network: "PUBLIC"
|
|
1490
|
+
});
|
|
1491
|
+
}
|
|
1492
|
+
case "SOLANA": {
|
|
1493
|
+
const solanaXService = xService;
|
|
1494
|
+
if (!solanaXService.wallet) {
|
|
1495
|
+
throw new Error("Wallet is not initialized");
|
|
1496
|
+
}
|
|
1497
|
+
if (!solanaXService.connection) {
|
|
1498
|
+
throw new Error("Connection is not initialized");
|
|
1499
|
+
}
|
|
1500
|
+
return new walletSdkCore.SolanaWalletProvider({
|
|
1501
|
+
wallet: solanaXService.wallet,
|
|
1502
|
+
connection: solanaXService.connection
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
default:
|
|
1506
|
+
return void 0;
|
|
1507
|
+
}
|
|
1508
|
+
}, [xChainType, evmPublicClient, evmWalletClient, xService, xAccount]);
|
|
1509
|
+
}
|
|
1510
|
+
var SodaxWalletProvider = ({ children, config }) => {
|
|
1511
|
+
React3.useEffect(() => {
|
|
1512
|
+
initXWagmiStore(config);
|
|
1513
|
+
}, [config]);
|
|
1514
|
+
const {
|
|
1515
|
+
EVM: { chains },
|
|
1516
|
+
SOLANA: { endpoint }
|
|
1517
|
+
} = config;
|
|
1518
|
+
const wallets = React3.useMemo(() => [new walletAdapterWallets.UnsafeBurnerWalletAdapter()], []);
|
|
1519
|
+
const wagmiConfig = React3.useMemo(() => {
|
|
1520
|
+
return getWagmiConfig(chains);
|
|
1521
|
+
}, [chains]);
|
|
1522
|
+
return /* @__PURE__ */ React3__default.default.createElement(wagmi.WagmiProvider, { config: wagmiConfig }, /* @__PURE__ */ React3__default.default.createElement(dappKit.SuiClientProvider, { networks: { mainnet: { url: client.getFullnodeUrl("mainnet") } }, defaultNetwork: "mainnet" }, /* @__PURE__ */ React3__default.default.createElement(dappKit.WalletProvider, { autoConnect: true }, /* @__PURE__ */ React3__default.default.createElement(walletAdapterReact.ConnectionProvider, { endpoint }, /* @__PURE__ */ React3__default.default.createElement(walletAdapterReact.WalletProvider, { wallets, autoConnect: true }, /* @__PURE__ */ React3__default.default.createElement(InitXWagmiStore, null), children)))));
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// src/types/index.ts
|
|
1526
|
+
var WalletId = /* @__PURE__ */ ((WalletId2) => {
|
|
1527
|
+
WalletId2["METAMASK"] = "metamask";
|
|
1528
|
+
WalletId2["HANA"] = "hana";
|
|
1529
|
+
WalletId2["PHANTOM"] = "phantom";
|
|
1530
|
+
WalletId2["SUI"] = "sui";
|
|
1531
|
+
WalletId2["KEPLR"] = "keplr";
|
|
1532
|
+
return WalletId2;
|
|
1533
|
+
})(WalletId || {});
|
|
1534
|
+
|
|
1535
|
+
exports.EvmXConnector = EvmXConnector;
|
|
1536
|
+
exports.EvmXService = EvmXService;
|
|
1537
|
+
exports.IconHanaXConnector = IconHanaXConnector;
|
|
1538
|
+
exports.IconXService = IconXService;
|
|
1539
|
+
exports.InitXWagmiStore = InitXWagmiStore;
|
|
1540
|
+
exports.InjectiveKelprXConnector = InjectiveKelprXConnector;
|
|
1541
|
+
exports.InjectiveMetamaskXConnector = InjectiveMetamaskXConnector;
|
|
1542
|
+
exports.InjectiveXService = InjectiveXService;
|
|
1543
|
+
exports.SodaxWalletProvider = SodaxWalletProvider;
|
|
1544
|
+
exports.SolanaXConnector = SolanaXConnector;
|
|
1545
|
+
exports.SolanaXService = SolanaXService;
|
|
1546
|
+
exports.StellarWalletsKitXConnector = StellarWalletsKitXConnector;
|
|
1547
|
+
exports.StellarXService = StellarXService;
|
|
1548
|
+
exports.SuiXConnector = SuiXConnector;
|
|
1549
|
+
exports.SuiXService = SuiXService;
|
|
1550
|
+
exports.WalletId = WalletId;
|
|
1551
|
+
exports.XConnector = XConnector;
|
|
1552
|
+
exports.XService = XService;
|
|
1553
|
+
exports.arbitrum = arbitrum;
|
|
1554
|
+
exports.avalanche = avalanche;
|
|
1555
|
+
exports.base = base;
|
|
1556
|
+
exports.bsc = bsc;
|
|
1557
|
+
exports.getWagmiChainId = getWagmiChainId;
|
|
1558
|
+
exports.getXChainType = getXChainType;
|
|
1559
|
+
exports.getXService = getXService;
|
|
1560
|
+
exports.hyper = hyper;
|
|
1561
|
+
exports.icon = icon;
|
|
1562
|
+
exports.initXWagmiStore = initXWagmiStore;
|
|
1563
|
+
exports.injective = injective;
|
|
1564
|
+
exports.isNativeToken = isNativeToken;
|
|
1565
|
+
exports.lightlink = lightlink;
|
|
1566
|
+
exports.nibiru = nibiru;
|
|
1567
|
+
exports.optimism = optimism;
|
|
1568
|
+
exports.polygon = polygon;
|
|
1569
|
+
exports.solana = solana;
|
|
1570
|
+
exports.sonic = sonic;
|
|
1571
|
+
exports.stellar = stellar;
|
|
1572
|
+
exports.sui = sui;
|
|
1573
|
+
exports.switchEthereumChain = switchEthereumChain;
|
|
1574
|
+
exports.useEvmSwitchChain = useEvmSwitchChain;
|
|
1575
|
+
exports.useWalletProvider = useWalletProvider;
|
|
1576
|
+
exports.useWalletProviderOptions = useWalletProviderOptions;
|
|
1577
|
+
exports.useXAccount = useXAccount;
|
|
1578
|
+
exports.useXAccounts = useXAccounts;
|
|
1579
|
+
exports.useXBalances = useXBalances;
|
|
1580
|
+
exports.useXConnect = useXConnect;
|
|
1581
|
+
exports.useXConnection = useXConnection;
|
|
1582
|
+
exports.useXConnectors = useXConnectors;
|
|
1583
|
+
exports.useXDisconnect = useXDisconnect;
|
|
1584
|
+
exports.useXService = useXService;
|
|
1585
|
+
exports.useXWagmiStore = useXWagmiStore;
|
|
1586
|
+
exports.xChainMap = xChainMap;
|
|
1587
|
+
exports.xChains = xChains;
|
|
1588
|
+
//# sourceMappingURL=index.cjs.map
|
|
1589
|
+
//# sourceMappingURL=index.cjs.map
|