@injectivelabs/wallet-evm 1.18.22 → 1.18.24

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.
@@ -117,8 +117,38 @@ function getPhantomFromWindow() {
117
117
  }
118
118
 
119
119
  //#endregion
120
- //#region src/strategy/utils/metamask.ts
120
+ //#region src/strategy/utils/keplrEvm.ts
121
121
  const getWindow$3 = () => typeof window === "undefined" ? {} : window;
122
+ async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
123
+ const provider = getKeplrEvmFromWindow();
124
+ if (provider) return provider;
125
+ return listenForKeplrEvmInitialized({ timeout });
126
+ }
127
+ async function listenForKeplrEvmInitialized({ timeout } = { timeout: 3e3 }) {
128
+ return new Promise((resolve) => {
129
+ const handleInitialization = () => {
130
+ resolve(getKeplrEvmFromWindow());
131
+ };
132
+ const $window = getWindow$3();
133
+ $window.addEventListener("keplr#initialized", handleInitialization, { once: true });
134
+ setTimeout(() => {
135
+ $window.removeEventListener("keplr#initialized", handleInitialization);
136
+ resolve(null);
137
+ }, timeout);
138
+ });
139
+ }
140
+ function getKeplrEvmFromWindow() {
141
+ var _keplr, _keplr2, _$window$ethereum;
142
+ const $window = getWindow$3();
143
+ if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof ((_keplr = $window.keplr) === null || _keplr === void 0 ? void 0 : _keplr.ethereum) !== "undefined"))) return;
144
+ if ((_keplr2 = $window.keplr) === null || _keplr2 === void 0 ? void 0 : _keplr2.ethereum) return $window.keplr.ethereum;
145
+ if ((_$window$ethereum = $window.ethereum) === null || _$window$ethereum === void 0 ? void 0 : _$window$ethereum.isKeplr) return $window.ethereum;
146
+ if ($window.providers) return $window.providers.find((p) => p.isKeplr);
147
+ }
148
+
149
+ //#endregion
150
+ //#region src/strategy/utils/metamask.ts
151
+ const getWindow$2 = () => typeof window === "undefined" ? {} : window;
122
152
  async function getMetamaskProvider({ timeout } = { timeout: 3e3 }) {
123
153
  const provider = getMetamaskFromWindow();
124
154
  if (provider) return provider;
@@ -129,7 +159,7 @@ async function listenForMetamaskInitialized({ timeout } = { timeout: 3e3 }) {
129
159
  const handleInitialization = () => {
130
160
  resolve(getMetamaskFromWindow());
131
161
  };
132
- const $window = getWindow$3();
162
+ const $window = getWindow$2();
133
163
  $window.addEventListener("ethereum#initialized", handleInitialization, { once: true });
134
164
  setTimeout(() => {
135
165
  $window.removeEventListener("ethereum#initialized", handleInitialization);
@@ -138,7 +168,7 @@ async function listenForMetamaskInitialized({ timeout } = { timeout: 3e3 }) {
138
168
  });
139
169
  }
140
170
  function getMetamaskFromWindow() {
141
- const $window = getWindow$3();
171
+ const $window = getWindow$2();
142
172
  if (!(typeof window !== "undefined" && typeof $window.ethereum !== "undefined")) return;
143
173
  if ($window.ethereum.isMetaMask) return $window.ethereum;
144
174
  if ($window.providers) return $window.providers.find((p) => p.isMetaMask);
@@ -146,7 +176,7 @@ function getMetamaskFromWindow() {
146
176
 
147
177
  //#endregion
148
178
  //#region src/strategy/utils/trustWallet.ts
149
- const getWindow$2 = () => typeof window === "undefined" ? {} : window;
179
+ const getWindow$1 = () => typeof window === "undefined" ? {} : window;
150
180
  async function getTrustWalletProvider({ timeout } = { timeout: 3e3 }) {
151
181
  const provider = getTrustWalletFromWindow();
152
182
  if (provider) return provider;
@@ -157,7 +187,7 @@ async function listenForTrustWalletInitialized({ timeout } = { timeout: 3e3 }) {
157
187
  const handleInitialization = () => {
158
188
  resolve(getTrustWalletFromWindow());
159
189
  };
160
- const $window = getWindow$2();
190
+ const $window = getWindow$1();
161
191
  $window.addEventListener("trustwallet#initialized", handleInitialization, { once: true });
162
192
  setTimeout(() => {
163
193
  $window.removeEventListener("trustwallet#initialized", handleInitialization);
@@ -166,7 +196,7 @@ async function listenForTrustWalletInitialized({ timeout } = { timeout: 3e3 }) {
166
196
  });
167
197
  }
168
198
  function getTrustWalletFromWindow() {
169
- const $window = getWindow$2();
199
+ const $window = getWindow$1();
170
200
  if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.trustWallet !== "undefined"))) return;
171
201
  if ($window.trustWallet) return $window.trustWallet;
172
202
  if ($window.ethereum.isTrustWallet || $window.ethereum.isTrust) return $window.ethereum;
@@ -185,6 +215,7 @@ const getEvmProvider = async (wallet) => {
185
215
  if (wallet === __injectivelabs_wallet_base.Wallet.Phantom) provider = await getPhantomProvider();
186
216
  if (wallet === __injectivelabs_wallet_base.Wallet.TrustWallet) provider = await getTrustWalletProvider();
187
217
  if (wallet === __injectivelabs_wallet_base.Wallet.OkxWallet) provider = await getOkxWalletProvider();
218
+ if (wallet === __injectivelabs_wallet_base.Wallet.KeplrEvm) provider = await getKeplrEvmProvider();
188
219
  if (!provider) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please install ${(0, __injectivelabs_utils.capitalize)(wallet)} Extension`));
189
220
  return provider;
190
221
  } catch (_unused) {
@@ -195,22 +226,59 @@ const updateEvmNetwork = async (wallet, chainId) => {
195
226
  if (!(0, __injectivelabs_wallet_base.isEvmBrowserWallet)(wallet)) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${(0, __injectivelabs_utils.capitalize)(wallet)} is not supported.`));
196
227
  const provider = await getEvmProvider(wallet);
197
228
  if (!provider) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please install ${(0, __injectivelabs_utils.capitalize)(wallet)} Extension`));
229
+ const chainIdToHex = `0x${chainId.toString(16)}`;
230
+ const TIMEOUT_MS = 3e4;
198
231
  try {
199
- const chainIdToHex = chainId.toString(16);
200
232
  return await Promise.race([provider.request({
201
233
  method: "wallet_switchEthereumChain",
202
- params: [{ chainId: `0x${chainIdToHex}` }]
203
- }), new Promise((resolve) => provider.on("chainChanged", ({ chain }) => {
204
- if ((chain === null || chain === void 0 ? void 0 : chain.id) === chainIdToHex) resolve();
205
- }))]);
206
- } catch (_unused2) {
234
+ params: [{ chainId: chainIdToHex }]
235
+ }), new Promise((resolve, reject) => {
236
+ const handleChainChanged = (newChainId) => {
237
+ if (newChainId.toLowerCase() === chainIdToHex.toLowerCase()) {
238
+ cleanup();
239
+ resolve();
240
+ }
241
+ };
242
+ const timeoutId = setTimeout(() => {
243
+ cleanup();
244
+ reject(/* @__PURE__ */ new Error("Chain switch timed out"));
245
+ }, TIMEOUT_MS);
246
+ const cleanup = () => {
247
+ provider.removeListener("chainChanged", handleChainChanged);
248
+ clearTimeout(timeoutId);
249
+ };
250
+ provider.on("chainChanged", handleChainChanged);
251
+ })]);
252
+ } catch (switchError) {
253
+ var _switchError$code, _switchError$data;
254
+ if (((_switchError$code = switchError === null || switchError === void 0 ? void 0 : switchError.code) !== null && _switchError$code !== void 0 ? _switchError$code : switchError === null || switchError === void 0 || (_switchError$data = switchError.data) === null || _switchError$data === void 0 || (_switchError$data = _switchError$data.originalError) === null || _switchError$data === void 0 ? void 0 : _switchError$data.code) === 4902) {
255
+ const chainConfig = (0, __injectivelabs_wallet_base.getEvmChainConfig)(chainId);
256
+ try {
257
+ var _chainConfig$rpcUrls, _chainConfig$blockExp;
258
+ const rpcUrl = (_chainConfig$rpcUrls = chainConfig.rpcUrls) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.default) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.http) === null || _chainConfig$rpcUrls === void 0 ? void 0 : _chainConfig$rpcUrls[0];
259
+ const explorerUrl = ((_chainConfig$blockExp = chainConfig.blockExplorers) === null || _chainConfig$blockExp === void 0 || (_chainConfig$blockExp = _chainConfig$blockExp.default) === null || _chainConfig$blockExp === void 0 ? void 0 : _chainConfig$blockExp.url) || void 0;
260
+ await provider.request({
261
+ method: "wallet_addEthereumChain",
262
+ params: [{
263
+ chainId: chainIdToHex,
264
+ chainName: chainConfig.name,
265
+ nativeCurrency: chainConfig.nativeCurrency,
266
+ rpcUrls: rpcUrl ? [rpcUrl] : [],
267
+ blockExplorerUrls: explorerUrl ? [explorerUrl] : void 0
268
+ }]
269
+ });
270
+ return;
271
+ } catch (_unused2) {
272
+ throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Failed to add ${chainConfig.name} network to ${(0, __injectivelabs_utils.capitalize)(wallet)}`));
273
+ }
274
+ }
207
275
  throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please update your ${(0, __injectivelabs_utils.capitalize)(wallet)} network`));
208
276
  }
209
277
  };
210
278
 
211
279
  //#endregion
212
280
  //#region src/strategy/utils/rainbow.ts
213
- const getWindow$1 = () => typeof window === "undefined" ? {} : window;
281
+ const getWindow = () => typeof window === "undefined" ? {} : window;
214
282
  async function getRainbowProvider({ timeout } = { timeout: 3e3 }) {
215
283
  const provider = getRainbowWalletFromWindow();
216
284
  if (provider) return provider;
@@ -221,7 +289,7 @@ async function listenForRainbowWalletInitialized({ timeout } = { timeout: 3e3 })
221
289
  const handleInitialization = () => {
222
290
  resolve(getRainbowWalletFromWindow());
223
291
  };
224
- const $window = getWindow$1();
292
+ const $window = getWindow();
225
293
  $window.addEventListener("rainbow#initialized", handleInitialization, { once: true });
226
294
  setTimeout(() => {
227
295
  $window.removeEventListener("rainbow#initialized", handleInitialization);
@@ -230,43 +298,13 @@ async function listenForRainbowWalletInitialized({ timeout } = { timeout: 3e3 })
230
298
  });
231
299
  }
232
300
  function getRainbowWalletFromWindow() {
233
- const $window = getWindow$1();
301
+ const $window = getWindow();
234
302
  if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.rainbow !== "undefined"))) return;
235
303
  if ($window.rainbow) return $window.rainbow;
236
304
  if ($window.ethereum.isRainbow) return $window.ethereum;
237
305
  if ($window.providers) return $window.providers.find((p) => p.isRainbow);
238
306
  }
239
307
 
240
- //#endregion
241
- //#region src/strategy/utils/keplrEvm.ts
242
- const getWindow = () => typeof window === "undefined" ? {} : window;
243
- async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
244
- const provider = getKeplrEvmFromWindow();
245
- if (provider) return provider;
246
- return listenForKeplrEvmInitialized({ timeout });
247
- }
248
- async function listenForKeplrEvmInitialized({ timeout } = { timeout: 3e3 }) {
249
- return new Promise((resolve) => {
250
- const handleInitialization = () => {
251
- resolve(getKeplrEvmFromWindow());
252
- };
253
- const $window = getWindow();
254
- $window.addEventListener("keplr#initialized", handleInitialization, { once: true });
255
- setTimeout(() => {
256
- $window.removeEventListener("keplr#initialized", handleInitialization);
257
- resolve(null);
258
- }, timeout);
259
- });
260
- }
261
- function getKeplrEvmFromWindow() {
262
- var _keplr, _keplr2, _$window$ethereum;
263
- const $window = getWindow();
264
- if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof ((_keplr = $window.keplr) === null || _keplr === void 0 ? void 0 : _keplr.ethereum) !== "undefined"))) return;
265
- if ((_keplr2 = $window.keplr) === null || _keplr2 === void 0 ? void 0 : _keplr2.ethereum) return $window.keplr.ethereum;
266
- if ((_$window$ethereum = $window.ethereum) === null || _$window$ethereum === void 0 ? void 0 : _$window$ethereum.isKeplr) return $window.ethereum;
267
- if ($window.providers) return $window.providers.find((p) => p.isKeplr);
268
- }
269
-
270
308
  //#endregion
271
309
  //#region \0@oxc-project+runtime@0.99.0/helpers/typeof.js
272
310
  function _typeof(o) {
@@ -416,7 +454,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
416
454
  type: __injectivelabs_exceptions.ErrorType.WalletError,
417
455
  contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
418
456
  });
419
- throw new __injectivelabs_exceptions.MetamaskException(new Error(e.message), {
457
+ throw this.EvmWalletException(new Error(e.message), {
420
458
  code: __injectivelabs_exceptions.UnspecifiedErrorCode,
421
459
  type: __injectivelabs_exceptions.ErrorType.WalletError,
422
460
  contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
@@ -509,26 +547,36 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
509
547
  };
510
548
  const TIMEOUT_MS = 3e4;
511
549
  try {
512
- await Promise.race([ethereum.request({
550
+ let timeoutId;
551
+ let handleChainChanged;
552
+ const cleanup = () => {
553
+ if (handleChainChanged) {
554
+ ethereum.removeListener("chainChanged", handleChainChanged);
555
+ handleChainChanged = void 0;
556
+ }
557
+ if (timeoutId !== void 0) {
558
+ clearTimeout(timeoutId);
559
+ timeoutId = void 0;
560
+ }
561
+ };
562
+ const switchRequest = ethereum.request({
513
563
  method: "wallet_switchEthereumChain",
514
564
  params: [{ chainId: chainIdHex }]
515
- }), new Promise((resolve, reject) => {
516
- const handleChainChanged = (newChainId) => {
565
+ }).finally(cleanup);
566
+ const chainChangedWaiter = new Promise((resolve, reject) => {
567
+ handleChainChanged = (newChainId) => {
517
568
  if (newChainId.toLowerCase() === chainIdHex.toLowerCase()) {
518
569
  cleanup();
519
570
  resolve();
520
571
  }
521
572
  };
522
- const timeoutId = setTimeout(() => {
573
+ timeoutId = setTimeout(() => {
523
574
  cleanup();
524
575
  reject(/* @__PURE__ */ new Error("Chain switch timed out"));
525
576
  }, TIMEOUT_MS);
526
- const cleanup = () => {
527
- ethereum.removeListener("chainChanged", handleChainChanged);
528
- clearTimeout(timeoutId);
529
- };
530
577
  ethereum.on("chainChanged", handleChainChanged);
531
- })]);
578
+ });
579
+ await Promise.race([switchRequest, chainChangedWaiter]);
532
580
  } catch (error) {
533
581
  var _code, _data;
534
582
  if (((_code = error.code) !== null && _code !== void 0 ? _code : error === null || error === void 0 || (_data = error.data) === null || _data === void 0 || (_data = _data.originalError) === null || _data === void 0 ? void 0 : _data.code) === 4902) {
@@ -536,6 +584,14 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
536
584
  method: "wallet_addEthereumChain",
537
585
  params: [params]
538
586
  });
587
+ try {
588
+ await ethereum.request({
589
+ method: "wallet_switchEthereumChain",
590
+ params: [{ chainId: chainIdHex }]
591
+ });
592
+ } catch (switchError) {
593
+ console.warn(`Failed to switch to chain ${chainIdHex} after adding it:`, switchError);
594
+ }
539
595
  return;
540
596
  }
541
597
  if (error.message === "Chain switch timed out") throw this.EvmWalletException(/* @__PURE__ */ new Error("Chain switch timed out"), {
package/dist/esm/index.js CHANGED
@@ -117,8 +117,38 @@ function getPhantomFromWindow() {
117
117
  }
118
118
 
119
119
  //#endregion
120
- //#region src/strategy/utils/metamask.ts
120
+ //#region src/strategy/utils/keplrEvm.ts
121
121
  const getWindow$3 = () => typeof window === "undefined" ? {} : window;
122
+ async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
123
+ const provider = getKeplrEvmFromWindow();
124
+ if (provider) return provider;
125
+ return listenForKeplrEvmInitialized({ timeout });
126
+ }
127
+ async function listenForKeplrEvmInitialized({ timeout } = { timeout: 3e3 }) {
128
+ return new Promise((resolve) => {
129
+ const handleInitialization = () => {
130
+ resolve(getKeplrEvmFromWindow());
131
+ };
132
+ const $window = getWindow$3();
133
+ $window.addEventListener("keplr#initialized", handleInitialization, { once: true });
134
+ setTimeout(() => {
135
+ $window.removeEventListener("keplr#initialized", handleInitialization);
136
+ resolve(null);
137
+ }, timeout);
138
+ });
139
+ }
140
+ function getKeplrEvmFromWindow() {
141
+ var _keplr, _keplr2, _$window$ethereum;
142
+ const $window = getWindow$3();
143
+ if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof ((_keplr = $window.keplr) === null || _keplr === void 0 ? void 0 : _keplr.ethereum) !== "undefined"))) return;
144
+ if ((_keplr2 = $window.keplr) === null || _keplr2 === void 0 ? void 0 : _keplr2.ethereum) return $window.keplr.ethereum;
145
+ if ((_$window$ethereum = $window.ethereum) === null || _$window$ethereum === void 0 ? void 0 : _$window$ethereum.isKeplr) return $window.ethereum;
146
+ if ($window.providers) return $window.providers.find((p) => p.isKeplr);
147
+ }
148
+
149
+ //#endregion
150
+ //#region src/strategy/utils/metamask.ts
151
+ const getWindow$2 = () => typeof window === "undefined" ? {} : window;
122
152
  async function getMetamaskProvider({ timeout } = { timeout: 3e3 }) {
123
153
  const provider = getMetamaskFromWindow();
124
154
  if (provider) return provider;
@@ -129,7 +159,7 @@ async function listenForMetamaskInitialized({ timeout } = { timeout: 3e3 }) {
129
159
  const handleInitialization = () => {
130
160
  resolve(getMetamaskFromWindow());
131
161
  };
132
- const $window = getWindow$3();
162
+ const $window = getWindow$2();
133
163
  $window.addEventListener("ethereum#initialized", handleInitialization, { once: true });
134
164
  setTimeout(() => {
135
165
  $window.removeEventListener("ethereum#initialized", handleInitialization);
@@ -138,7 +168,7 @@ async function listenForMetamaskInitialized({ timeout } = { timeout: 3e3 }) {
138
168
  });
139
169
  }
140
170
  function getMetamaskFromWindow() {
141
- const $window = getWindow$3();
171
+ const $window = getWindow$2();
142
172
  if (!(typeof window !== "undefined" && typeof $window.ethereum !== "undefined")) return;
143
173
  if ($window.ethereum.isMetaMask) return $window.ethereum;
144
174
  if ($window.providers) return $window.providers.find((p) => p.isMetaMask);
@@ -146,7 +176,7 @@ function getMetamaskFromWindow() {
146
176
 
147
177
  //#endregion
148
178
  //#region src/strategy/utils/trustWallet.ts
149
- const getWindow$2 = () => typeof window === "undefined" ? {} : window;
179
+ const getWindow$1 = () => typeof window === "undefined" ? {} : window;
150
180
  async function getTrustWalletProvider({ timeout } = { timeout: 3e3 }) {
151
181
  const provider = getTrustWalletFromWindow();
152
182
  if (provider) return provider;
@@ -157,7 +187,7 @@ async function listenForTrustWalletInitialized({ timeout } = { timeout: 3e3 }) {
157
187
  const handleInitialization = () => {
158
188
  resolve(getTrustWalletFromWindow());
159
189
  };
160
- const $window = getWindow$2();
190
+ const $window = getWindow$1();
161
191
  $window.addEventListener("trustwallet#initialized", handleInitialization, { once: true });
162
192
  setTimeout(() => {
163
193
  $window.removeEventListener("trustwallet#initialized", handleInitialization);
@@ -166,7 +196,7 @@ async function listenForTrustWalletInitialized({ timeout } = { timeout: 3e3 }) {
166
196
  });
167
197
  }
168
198
  function getTrustWalletFromWindow() {
169
- const $window = getWindow$2();
199
+ const $window = getWindow$1();
170
200
  if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.trustWallet !== "undefined"))) return;
171
201
  if ($window.trustWallet) return $window.trustWallet;
172
202
  if ($window.ethereum.isTrustWallet || $window.ethereum.isTrust) return $window.ethereum;
@@ -185,6 +215,7 @@ const getEvmProvider = async (wallet) => {
185
215
  if (wallet === Wallet.Phantom) provider = await getPhantomProvider();
186
216
  if (wallet === Wallet.TrustWallet) provider = await getTrustWalletProvider();
187
217
  if (wallet === Wallet.OkxWallet) provider = await getOkxWalletProvider();
218
+ if (wallet === Wallet.KeplrEvm) provider = await getKeplrEvmProvider();
188
219
  if (!provider) throw new WalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} Extension`));
189
220
  return provider;
190
221
  } catch (_unused) {
@@ -195,22 +226,59 @@ const updateEvmNetwork = async (wallet, chainId) => {
195
226
  if (!isEvmBrowserWallet(wallet)) throw new WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`));
196
227
  const provider = await getEvmProvider(wallet);
197
228
  if (!provider) throw new WalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} Extension`));
229
+ const chainIdToHex = `0x${chainId.toString(16)}`;
230
+ const TIMEOUT_MS = 3e4;
198
231
  try {
199
- const chainIdToHex = chainId.toString(16);
200
232
  return await Promise.race([provider.request({
201
233
  method: "wallet_switchEthereumChain",
202
- params: [{ chainId: `0x${chainIdToHex}` }]
203
- }), new Promise((resolve) => provider.on("chainChanged", ({ chain }) => {
204
- if ((chain === null || chain === void 0 ? void 0 : chain.id) === chainIdToHex) resolve();
205
- }))]);
206
- } catch (_unused2) {
234
+ params: [{ chainId: chainIdToHex }]
235
+ }), new Promise((resolve, reject) => {
236
+ const handleChainChanged = (newChainId) => {
237
+ if (newChainId.toLowerCase() === chainIdToHex.toLowerCase()) {
238
+ cleanup();
239
+ resolve();
240
+ }
241
+ };
242
+ const timeoutId = setTimeout(() => {
243
+ cleanup();
244
+ reject(/* @__PURE__ */ new Error("Chain switch timed out"));
245
+ }, TIMEOUT_MS);
246
+ const cleanup = () => {
247
+ provider.removeListener("chainChanged", handleChainChanged);
248
+ clearTimeout(timeoutId);
249
+ };
250
+ provider.on("chainChanged", handleChainChanged);
251
+ })]);
252
+ } catch (switchError) {
253
+ var _switchError$code, _switchError$data;
254
+ if (((_switchError$code = switchError === null || switchError === void 0 ? void 0 : switchError.code) !== null && _switchError$code !== void 0 ? _switchError$code : switchError === null || switchError === void 0 || (_switchError$data = switchError.data) === null || _switchError$data === void 0 || (_switchError$data = _switchError$data.originalError) === null || _switchError$data === void 0 ? void 0 : _switchError$data.code) === 4902) {
255
+ const chainConfig = getEvmChainConfig(chainId);
256
+ try {
257
+ var _chainConfig$rpcUrls, _chainConfig$blockExp;
258
+ const rpcUrl = (_chainConfig$rpcUrls = chainConfig.rpcUrls) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.default) === null || _chainConfig$rpcUrls === void 0 || (_chainConfig$rpcUrls = _chainConfig$rpcUrls.http) === null || _chainConfig$rpcUrls === void 0 ? void 0 : _chainConfig$rpcUrls[0];
259
+ const explorerUrl = ((_chainConfig$blockExp = chainConfig.blockExplorers) === null || _chainConfig$blockExp === void 0 || (_chainConfig$blockExp = _chainConfig$blockExp.default) === null || _chainConfig$blockExp === void 0 ? void 0 : _chainConfig$blockExp.url) || void 0;
260
+ await provider.request({
261
+ method: "wallet_addEthereumChain",
262
+ params: [{
263
+ chainId: chainIdToHex,
264
+ chainName: chainConfig.name,
265
+ nativeCurrency: chainConfig.nativeCurrency,
266
+ rpcUrls: rpcUrl ? [rpcUrl] : [],
267
+ blockExplorerUrls: explorerUrl ? [explorerUrl] : void 0
268
+ }]
269
+ });
270
+ return;
271
+ } catch (_unused2) {
272
+ throw new WalletException(/* @__PURE__ */ new Error(`Failed to add ${chainConfig.name} network to ${capitalize(wallet)}`));
273
+ }
274
+ }
207
275
  throw new WalletException(/* @__PURE__ */ new Error(`Please update your ${capitalize(wallet)} network`));
208
276
  }
209
277
  };
210
278
 
211
279
  //#endregion
212
280
  //#region src/strategy/utils/rainbow.ts
213
- const getWindow$1 = () => typeof window === "undefined" ? {} : window;
281
+ const getWindow = () => typeof window === "undefined" ? {} : window;
214
282
  async function getRainbowProvider({ timeout } = { timeout: 3e3 }) {
215
283
  const provider = getRainbowWalletFromWindow();
216
284
  if (provider) return provider;
@@ -221,7 +289,7 @@ async function listenForRainbowWalletInitialized({ timeout } = { timeout: 3e3 })
221
289
  const handleInitialization = () => {
222
290
  resolve(getRainbowWalletFromWindow());
223
291
  };
224
- const $window = getWindow$1();
292
+ const $window = getWindow();
225
293
  $window.addEventListener("rainbow#initialized", handleInitialization, { once: true });
226
294
  setTimeout(() => {
227
295
  $window.removeEventListener("rainbow#initialized", handleInitialization);
@@ -230,43 +298,13 @@ async function listenForRainbowWalletInitialized({ timeout } = { timeout: 3e3 })
230
298
  });
231
299
  }
232
300
  function getRainbowWalletFromWindow() {
233
- const $window = getWindow$1();
301
+ const $window = getWindow();
234
302
  if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof $window.rainbow !== "undefined"))) return;
235
303
  if ($window.rainbow) return $window.rainbow;
236
304
  if ($window.ethereum.isRainbow) return $window.ethereum;
237
305
  if ($window.providers) return $window.providers.find((p) => p.isRainbow);
238
306
  }
239
307
 
240
- //#endregion
241
- //#region src/strategy/utils/keplrEvm.ts
242
- const getWindow = () => typeof window === "undefined" ? {} : window;
243
- async function getKeplrEvmProvider({ timeout } = { timeout: 3e3 }) {
244
- const provider = getKeplrEvmFromWindow();
245
- if (provider) return provider;
246
- return listenForKeplrEvmInitialized({ timeout });
247
- }
248
- async function listenForKeplrEvmInitialized({ timeout } = { timeout: 3e3 }) {
249
- return new Promise((resolve) => {
250
- const handleInitialization = () => {
251
- resolve(getKeplrEvmFromWindow());
252
- };
253
- const $window = getWindow();
254
- $window.addEventListener("keplr#initialized", handleInitialization, { once: true });
255
- setTimeout(() => {
256
- $window.removeEventListener("keplr#initialized", handleInitialization);
257
- resolve(null);
258
- }, timeout);
259
- });
260
- }
261
- function getKeplrEvmFromWindow() {
262
- var _keplr, _keplr2, _$window$ethereum;
263
- const $window = getWindow();
264
- if (!(typeof window !== "undefined" && (typeof $window.ethereum !== "undefined" || typeof ((_keplr = $window.keplr) === null || _keplr === void 0 ? void 0 : _keplr.ethereum) !== "undefined"))) return;
265
- if ((_keplr2 = $window.keplr) === null || _keplr2 === void 0 ? void 0 : _keplr2.ethereum) return $window.keplr.ethereum;
266
- if ((_$window$ethereum = $window.ethereum) === null || _$window$ethereum === void 0 ? void 0 : _$window$ethereum.isKeplr) return $window.ethereum;
267
- if ($window.providers) return $window.providers.find((p) => p.isKeplr);
268
- }
269
-
270
308
  //#endregion
271
309
  //#region \0@oxc-project+runtime@0.99.0/helpers/typeof.js
272
310
  function _typeof(o) {
@@ -416,7 +454,7 @@ var EvmWallet = class extends BaseConcreteStrategy {
416
454
  type: ErrorType.WalletError,
417
455
  contextModule: WalletAction.SignTransaction
418
456
  });
419
- throw new MetamaskException(new Error(e.message), {
457
+ throw this.EvmWalletException(new Error(e.message), {
420
458
  code: UnspecifiedErrorCode,
421
459
  type: ErrorType.WalletError,
422
460
  contextModule: WalletAction.SignTransaction
@@ -509,26 +547,36 @@ var EvmWallet = class extends BaseConcreteStrategy {
509
547
  };
510
548
  const TIMEOUT_MS = 3e4;
511
549
  try {
512
- await Promise.race([ethereum.request({
550
+ let timeoutId;
551
+ let handleChainChanged;
552
+ const cleanup = () => {
553
+ if (handleChainChanged) {
554
+ ethereum.removeListener("chainChanged", handleChainChanged);
555
+ handleChainChanged = void 0;
556
+ }
557
+ if (timeoutId !== void 0) {
558
+ clearTimeout(timeoutId);
559
+ timeoutId = void 0;
560
+ }
561
+ };
562
+ const switchRequest = ethereum.request({
513
563
  method: "wallet_switchEthereumChain",
514
564
  params: [{ chainId: chainIdHex }]
515
- }), new Promise((resolve, reject) => {
516
- const handleChainChanged = (newChainId) => {
565
+ }).finally(cleanup);
566
+ const chainChangedWaiter = new Promise((resolve, reject) => {
567
+ handleChainChanged = (newChainId) => {
517
568
  if (newChainId.toLowerCase() === chainIdHex.toLowerCase()) {
518
569
  cleanup();
519
570
  resolve();
520
571
  }
521
572
  };
522
- const timeoutId = setTimeout(() => {
573
+ timeoutId = setTimeout(() => {
523
574
  cleanup();
524
575
  reject(/* @__PURE__ */ new Error("Chain switch timed out"));
525
576
  }, TIMEOUT_MS);
526
- const cleanup = () => {
527
- ethereum.removeListener("chainChanged", handleChainChanged);
528
- clearTimeout(timeoutId);
529
- };
530
577
  ethereum.on("chainChanged", handleChainChanged);
531
- })]);
578
+ });
579
+ await Promise.race([switchRequest, chainChangedWaiter]);
532
580
  } catch (error) {
533
581
  var _code, _data;
534
582
  if (((_code = error.code) !== null && _code !== void 0 ? _code : error === null || error === void 0 || (_data = error.data) === null || _data === void 0 || (_data = _data.originalError) === null || _data === void 0 ? void 0 : _data.code) === 4902) {
@@ -536,6 +584,14 @@ var EvmWallet = class extends BaseConcreteStrategy {
536
584
  method: "wallet_addEthereumChain",
537
585
  params: [params]
538
586
  });
587
+ try {
588
+ await ethereum.request({
589
+ method: "wallet_switchEthereumChain",
590
+ params: [{ chainId: chainIdHex }]
591
+ });
592
+ } catch (switchError) {
593
+ console.warn(`Failed to switch to chain ${chainIdHex} after adding it:`, switchError);
594
+ }
539
595
  return;
540
596
  }
541
597
  if (error.message === "Chain switch timed out") throw this.EvmWalletException(/* @__PURE__ */ new Error("Chain switch timed out"), {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@injectivelabs/wallet-evm",
3
- "version": "1.18.22",
3
+ "version": "1.18.24",
4
4
  "description": "EVM wallet strategies for use with @injectivelabs/wallet-core.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -42,11 +42,11 @@
42
42
  "dist"
43
43
  ],
44
44
  "dependencies": {
45
- "@injectivelabs/exceptions": "1.18.22",
46
- "@injectivelabs/sdk-ts": "1.18.22",
47
- "@injectivelabs/ts-types": "1.18.22",
48
- "@injectivelabs/utils": "1.18.22",
49
- "@injectivelabs/wallet-base": "1.18.22"
45
+ "@injectivelabs/exceptions": "1.18.24",
46
+ "@injectivelabs/sdk-ts": "1.18.24",
47
+ "@injectivelabs/ts-types": "1.18.24",
48
+ "@injectivelabs/utils": "1.18.24",
49
+ "@injectivelabs/wallet-base": "1.18.24"
50
50
  },
51
51
  "publishConfig": {
52
52
  "access": "public"