@injectivelabs/wallet-evm 1.20.2 → 1.20.4
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/dist/cjs/index.cjs +34 -26
- package/dist/cjs/index.d.cts +3 -2
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +35 -28
- package/package.json +6 -6
package/dist/cjs/index.cjs
CHANGED
|
@@ -222,6 +222,14 @@ const getEvmProvider = async (wallet) => {
|
|
|
222
222
|
throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please install ${(0, __injectivelabs_utils.capitalize)(wallet)} Extension`));
|
|
223
223
|
}
|
|
224
224
|
};
|
|
225
|
+
const extractNormalizedErrorCode = (error) => {
|
|
226
|
+
var _normalizedError$data;
|
|
227
|
+
const normalizedError = error && typeof error === "object" ? error : void 0;
|
|
228
|
+
const code = normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code;
|
|
229
|
+
const originalCode = normalizedError === null || normalizedError === void 0 || (_normalizedError$data = normalizedError.data) === null || _normalizedError$data === void 0 || (_normalizedError$data = _normalizedError$data.originalError) === null || _normalizedError$data === void 0 ? void 0 : _normalizedError$data.code;
|
|
230
|
+
const rawCode = (code != null ? Number(code) : void 0) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.InternalError || code === void 0 || code === null ? originalCode !== null && originalCode !== void 0 ? originalCode : code : code;
|
|
231
|
+
return rawCode != null ? Number(rawCode) : NaN;
|
|
232
|
+
};
|
|
225
233
|
const switchEthereumChainWithTimeout = async (provider, chainIdHex, timeoutMs = 3e4) => {
|
|
226
234
|
let timeoutId;
|
|
227
235
|
let handleChainChanged;
|
|
@@ -254,22 +262,20 @@ const switchEthereumChainWithTimeout = async (provider, chainIdHex, timeoutMs =
|
|
|
254
262
|
});
|
|
255
263
|
await Promise.race([switchRequest, chainChangedWaiter]);
|
|
256
264
|
};
|
|
257
|
-
const updateEvmNetwork = async (wallet, chainId) => {
|
|
265
|
+
const updateEvmNetwork = async (wallet, chainId, providerOverride) => {
|
|
258
266
|
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.`));
|
|
259
|
-
const provider = await getEvmProvider(wallet);
|
|
267
|
+
const provider = providerOverride || await getEvmProvider(wallet);
|
|
260
268
|
if (!provider) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please install ${(0, __injectivelabs_utils.capitalize)(wallet)} Extension`));
|
|
261
269
|
const chainIdToHex = `0x${chainId.toString(16)}`;
|
|
262
270
|
const TIMEOUT_MS = 3e4;
|
|
263
271
|
try {
|
|
264
272
|
await switchEthereumChainWithTimeout(provider, chainIdToHex, TIMEOUT_MS);
|
|
265
273
|
} catch (switchError) {
|
|
266
|
-
var
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
const errorCode = !isNaN(parsed) ? parsed : void 0;
|
|
270
|
-
if (errorCode === 4001) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} chain switch was rejected`));
|
|
274
|
+
var _chainConfig$rpcUrls, _chainConfig$blockExp;
|
|
275
|
+
const errorCode = extractNormalizedErrorCode(switchError);
|
|
276
|
+
if (errorCode === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} chain switch was rejected`));
|
|
271
277
|
if (switchError.message === "Chain switch timed out") throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("Chain switch timed out"));
|
|
272
|
-
if (errorCode !==
|
|
278
|
+
if (errorCode !== __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UnrecognizedChain) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Please update your ${(0, __injectivelabs_utils.capitalize)(wallet)} network`));
|
|
273
279
|
const chainConfig = (0, __injectivelabs_wallet_base.getEvmChainConfig)(chainId);
|
|
274
280
|
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];
|
|
275
281
|
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;
|
|
@@ -284,23 +290,25 @@ const updateEvmNetwork = async (wallet, chainId) => {
|
|
|
284
290
|
blockExplorerUrls: explorerUrl ? [explorerUrl] : []
|
|
285
291
|
}]
|
|
286
292
|
});
|
|
287
|
-
} catch (
|
|
293
|
+
} catch (addError) {
|
|
294
|
+
if (extractNormalizedErrorCode(addError) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} add chain was rejected`), {
|
|
295
|
+
code: __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest,
|
|
296
|
+
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
297
|
+
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
298
|
+
});
|
|
288
299
|
throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Failed to add ${chainConfig.name} network to ${(0, __injectivelabs_utils.capitalize)(wallet)}`));
|
|
289
300
|
}
|
|
290
301
|
let currentChainId;
|
|
291
302
|
try {
|
|
292
303
|
currentChainId = await provider.request({ method: "eth_chainId" });
|
|
293
|
-
} catch (
|
|
304
|
+
} catch (_unused2) {
|
|
294
305
|
throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Failed to get current chain ID from ${(0, __injectivelabs_utils.capitalize)(wallet)} wallet`));
|
|
295
306
|
}
|
|
296
307
|
if (typeof currentChainId !== "string" || !currentChainId.startsWith("0x")) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Invalid chain ID response from ${(0, __injectivelabs_utils.capitalize)(wallet)}: ${String(currentChainId)}`));
|
|
297
308
|
if (currentChainId.toLowerCase() !== chainIdToHex.toLowerCase()) try {
|
|
298
309
|
await switchEthereumChainWithTimeout(provider, chainIdToHex, TIMEOUT_MS);
|
|
299
310
|
} catch (postAddError) {
|
|
300
|
-
|
|
301
|
-
const rawCode$1 = (_postAddError$code = postAddError === null || postAddError === void 0 ? void 0 : postAddError.code) !== null && _postAddError$code !== void 0 ? _postAddError$code : postAddError === null || postAddError === void 0 || (_postAddError$data = postAddError.data) === null || _postAddError$data === void 0 || (_postAddError$data = _postAddError$data.originalError) === null || _postAddError$data === void 0 ? void 0 : _postAddError$data.code;
|
|
302
|
-
const parsed$1 = rawCode$1 != null ? Number(rawCode$1) : NaN;
|
|
303
|
-
if ((!isNaN(parsed$1) ? parsed$1 : void 0) === 4001) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} chain switch after add was rejected`));
|
|
311
|
+
if (extractNormalizedErrorCode(postAddError) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(wallet)} chain switch after add was rejected`));
|
|
304
312
|
throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error(`Failed to switch to ${chainConfig.name} network after adding it: ${postAddError.message}`));
|
|
305
313
|
}
|
|
306
314
|
}
|
|
@@ -580,11 +588,8 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
580
588
|
try {
|
|
581
589
|
await switchEthereumChainWithTimeout(ethereum, chainIdHex, TIMEOUT_MS);
|
|
582
590
|
} catch (error) {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
const parsed = rawCode != null ? Number(rawCode) : NaN;
|
|
586
|
-
const errorCode = !isNaN(parsed) ? parsed : void 0;
|
|
587
|
-
if (errorCode === 4001) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} chain switch was rejected`), {
|
|
591
|
+
const errorCode = extractNormalizedErrorCode(error);
|
|
592
|
+
if (errorCode === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} chain switch was rejected`), {
|
|
588
593
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
589
594
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
590
595
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
@@ -594,7 +599,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
594
599
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
595
600
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
596
601
|
});
|
|
597
|
-
if (errorCode !==
|
|
602
|
+
if (errorCode !== __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UnrecognizedChain) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Something went wrong while switching ${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} network`), {
|
|
598
603
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
599
604
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
600
605
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
@@ -604,7 +609,12 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
604
609
|
method: "wallet_addEthereumChain",
|
|
605
610
|
params: [params]
|
|
606
611
|
});
|
|
607
|
-
} catch (
|
|
612
|
+
} catch (addError) {
|
|
613
|
+
if (extractNormalizedErrorCode(addError) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} add chain was rejected`), {
|
|
614
|
+
code: __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest,
|
|
615
|
+
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
616
|
+
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
617
|
+
});
|
|
608
618
|
throw this.EvmWalletException(/* @__PURE__ */ new Error(`Something went wrong while adding ${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} network`), {
|
|
609
619
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
610
620
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
@@ -614,7 +624,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
614
624
|
let currentChainId;
|
|
615
625
|
try {
|
|
616
626
|
currentChainId = await ethereum.request({ method: "eth_chainId" });
|
|
617
|
-
} catch (
|
|
627
|
+
} catch (_unused2) {
|
|
618
628
|
throw this.EvmWalletException(/* @__PURE__ */ new Error(`Failed to get current chain ID from ${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")}`), {
|
|
619
629
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
620
630
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
@@ -629,10 +639,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
629
639
|
if (currentChainId.toLowerCase() !== chainIdHex.toLowerCase()) try {
|
|
630
640
|
await switchEthereumChainWithTimeout(ethereum, chainIdHex, TIMEOUT_MS);
|
|
631
641
|
} catch (postAddError) {
|
|
632
|
-
|
|
633
|
-
const postAddRawCode = (_postAddError$code = postAddError === null || postAddError === void 0 ? void 0 : postAddError.code) !== null && _postAddError$code !== void 0 ? _postAddError$code : postAddError === null || postAddError === void 0 || (_postAddError$data = postAddError.data) === null || _postAddError$data === void 0 || (_postAddError$data = _postAddError$data.originalError) === null || _postAddError$data === void 0 ? void 0 : _postAddError$data.code;
|
|
634
|
-
const postAddParsed = postAddRawCode != null ? Number(postAddRawCode) : NaN;
|
|
635
|
-
if ((!isNaN(postAddParsed) ? postAddParsed : void 0) === 4001) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} chain switch after add was rejected`), {
|
|
642
|
+
if (extractNormalizedErrorCode(postAddError) === __injectivelabs_wallet_base.EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${(0, __injectivelabs_utils.capitalize)(this.wallet || "wallet")} chain switch after add was rejected`), {
|
|
636
643
|
code: __injectivelabs_exceptions.UnspecifiedErrorCode,
|
|
637
644
|
type: __injectivelabs_exceptions.ErrorType.WalletError,
|
|
638
645
|
contextModule: __injectivelabs_wallet_base.WalletAction.GetChainId
|
|
@@ -660,6 +667,7 @@ var EvmWallet = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
|
|
|
660
667
|
|
|
661
668
|
//#endregion
|
|
662
669
|
exports.EvmWalletStrategy = EvmWallet;
|
|
670
|
+
exports.extractNormalizedErrorCode = extractNormalizedErrorCode;
|
|
663
671
|
exports.getBitGetProvider = getBitGetProvider;
|
|
664
672
|
exports.getEvmProvider = getEvmProvider;
|
|
665
673
|
exports.getKeplrEvmProvider = getKeplrEvmProvider;
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -6,8 +6,9 @@ import { AminoSignResponse, DirectSignResponse, TxRaw } from "@injectivelabs/sdk
|
|
|
6
6
|
|
|
7
7
|
//#region src/utils/index.d.ts
|
|
8
8
|
declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
|
|
9
|
+
declare const extractNormalizedErrorCode: (error: unknown) => number;
|
|
9
10
|
declare const switchEthereumChainWithTimeout: (provider: BrowserEip1993Provider, chainIdHex: string, timeoutMs?: number) => Promise<void>;
|
|
10
|
-
declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId) => Promise<void>;
|
|
11
|
+
declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId, providerOverride?: BrowserEip1993Provider) => Promise<void>;
|
|
11
12
|
//#endregion
|
|
12
13
|
//#region src/strategy/utils/rabby.d.ts
|
|
13
14
|
declare function getRabbyProvider({
|
|
@@ -110,4 +111,4 @@ declare class EvmWallet extends BaseConcreteStrategy implements ConcreteWalletSt
|
|
|
110
111
|
private getEthereum;
|
|
111
112
|
}
|
|
112
113
|
//#endregion
|
|
113
|
-
export { EvmWallet as EvmWalletStrategy, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
|
114
|
+
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,8 +6,9 @@ import { AminoSignResponse, DirectSignResponse, TxRaw } from "@injectivelabs/sdk
|
|
|
6
6
|
|
|
7
7
|
//#region src/utils/index.d.ts
|
|
8
8
|
declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
|
|
9
|
+
declare const extractNormalizedErrorCode: (error: unknown) => number;
|
|
9
10
|
declare const switchEthereumChainWithTimeout: (provider: BrowserEip1993Provider, chainIdHex: string, timeoutMs?: number) => Promise<void>;
|
|
10
|
-
declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId) => Promise<void>;
|
|
11
|
+
declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId, providerOverride?: BrowserEip1993Provider) => Promise<void>;
|
|
11
12
|
//#endregion
|
|
12
13
|
//#region src/strategy/utils/rabby.d.ts
|
|
13
14
|
declare function getRabbyProvider({
|
|
@@ -110,4 +111,4 @@ declare class EvmWallet extends BaseConcreteStrategy implements ConcreteWalletSt
|
|
|
110
111
|
private getEthereum;
|
|
111
112
|
}
|
|
112
113
|
//#endregion
|
|
113
|
-
export { EvmWallet as EvmWalletStrategy, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
|
114
|
+
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { capitalize } from "@injectivelabs/utils";
|
|
2
2
|
import { BitGetException, ErrorType, KeplrEvmException, MetamaskException, OkxWalletException, RabbyWalletException, RainbowWalletException, TransactionException, TrustWalletException, UnspecifiedErrorCode, WalletException } from "@injectivelabs/exceptions";
|
|
3
|
-
import { BaseConcreteStrategy, Wallet, WalletAction, WalletDeviceType, WalletEventListener, getEvmChainConfig, getViemPublicClientFromEip1193Provider, isEvmBrowserWallet } from "@injectivelabs/wallet-base";
|
|
3
|
+
import { BaseConcreteStrategy, EvmWalletProviderErrorCode, Wallet, WalletAction, WalletDeviceType, WalletEventListener, getEvmChainConfig, getViemPublicClientFromEip1193Provider, isEvmBrowserWallet } from "@injectivelabs/wallet-base";
|
|
4
4
|
import { TxGrpcApi } from "@injectivelabs/sdk-ts/core/tx";
|
|
5
5
|
import { isServerSide, stringToUint8Array, toUtf8, uint8ArrayToHex } from "@injectivelabs/sdk-ts/utils";
|
|
6
6
|
|
|
@@ -222,6 +222,14 @@ const getEvmProvider = async (wallet) => {
|
|
|
222
222
|
throw new WalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} Extension`));
|
|
223
223
|
}
|
|
224
224
|
};
|
|
225
|
+
const extractNormalizedErrorCode = (error) => {
|
|
226
|
+
var _normalizedError$data;
|
|
227
|
+
const normalizedError = error && typeof error === "object" ? error : void 0;
|
|
228
|
+
const code = normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code;
|
|
229
|
+
const originalCode = normalizedError === null || normalizedError === void 0 || (_normalizedError$data = normalizedError.data) === null || _normalizedError$data === void 0 || (_normalizedError$data = _normalizedError$data.originalError) === null || _normalizedError$data === void 0 ? void 0 : _normalizedError$data.code;
|
|
230
|
+
const rawCode = (code != null ? Number(code) : void 0) === EvmWalletProviderErrorCode.InternalError || code === void 0 || code === null ? originalCode !== null && originalCode !== void 0 ? originalCode : code : code;
|
|
231
|
+
return rawCode != null ? Number(rawCode) : NaN;
|
|
232
|
+
};
|
|
225
233
|
const switchEthereumChainWithTimeout = async (provider, chainIdHex, timeoutMs = 3e4) => {
|
|
226
234
|
let timeoutId;
|
|
227
235
|
let handleChainChanged;
|
|
@@ -254,22 +262,20 @@ const switchEthereumChainWithTimeout = async (provider, chainIdHex, timeoutMs =
|
|
|
254
262
|
});
|
|
255
263
|
await Promise.race([switchRequest, chainChangedWaiter]);
|
|
256
264
|
};
|
|
257
|
-
const updateEvmNetwork = async (wallet, chainId) => {
|
|
265
|
+
const updateEvmNetwork = async (wallet, chainId, providerOverride) => {
|
|
258
266
|
if (!isEvmBrowserWallet(wallet)) throw new WalletException(/* @__PURE__ */ new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`));
|
|
259
|
-
const provider = await getEvmProvider(wallet);
|
|
267
|
+
const provider = providerOverride || await getEvmProvider(wallet);
|
|
260
268
|
if (!provider) throw new WalletException(/* @__PURE__ */ new Error(`Please install ${capitalize(wallet)} Extension`));
|
|
261
269
|
const chainIdToHex = `0x${chainId.toString(16)}`;
|
|
262
270
|
const TIMEOUT_MS = 3e4;
|
|
263
271
|
try {
|
|
264
272
|
await switchEthereumChainWithTimeout(provider, chainIdToHex, TIMEOUT_MS);
|
|
265
273
|
} catch (switchError) {
|
|
266
|
-
var
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
const errorCode = !isNaN(parsed) ? parsed : void 0;
|
|
270
|
-
if (errorCode === 4001) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} chain switch was rejected`));
|
|
274
|
+
var _chainConfig$rpcUrls, _chainConfig$blockExp;
|
|
275
|
+
const errorCode = extractNormalizedErrorCode(switchError);
|
|
276
|
+
if (errorCode === EvmWalletProviderErrorCode.UserRejectedRequest) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} chain switch was rejected`));
|
|
271
277
|
if (switchError.message === "Chain switch timed out") throw new WalletException(/* @__PURE__ */ new Error("Chain switch timed out"));
|
|
272
|
-
if (errorCode !==
|
|
278
|
+
if (errorCode !== EvmWalletProviderErrorCode.UnrecognizedChain) throw new WalletException(/* @__PURE__ */ new Error(`Please update your ${capitalize(wallet)} network`));
|
|
273
279
|
const chainConfig = getEvmChainConfig(chainId);
|
|
274
280
|
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];
|
|
275
281
|
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;
|
|
@@ -284,23 +290,25 @@ const updateEvmNetwork = async (wallet, chainId) => {
|
|
|
284
290
|
blockExplorerUrls: explorerUrl ? [explorerUrl] : []
|
|
285
291
|
}]
|
|
286
292
|
});
|
|
287
|
-
} catch (
|
|
293
|
+
} catch (addError) {
|
|
294
|
+
if (extractNormalizedErrorCode(addError) === EvmWalletProviderErrorCode.UserRejectedRequest) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} add chain was rejected`), {
|
|
295
|
+
code: EvmWalletProviderErrorCode.UserRejectedRequest,
|
|
296
|
+
type: ErrorType.WalletError,
|
|
297
|
+
contextModule: WalletAction.GetChainId
|
|
298
|
+
});
|
|
288
299
|
throw new WalletException(/* @__PURE__ */ new Error(`Failed to add ${chainConfig.name} network to ${capitalize(wallet)}`));
|
|
289
300
|
}
|
|
290
301
|
let currentChainId;
|
|
291
302
|
try {
|
|
292
303
|
currentChainId = await provider.request({ method: "eth_chainId" });
|
|
293
|
-
} catch (
|
|
304
|
+
} catch (_unused2) {
|
|
294
305
|
throw new WalletException(/* @__PURE__ */ new Error(`Failed to get current chain ID from ${capitalize(wallet)} wallet`));
|
|
295
306
|
}
|
|
296
307
|
if (typeof currentChainId !== "string" || !currentChainId.startsWith("0x")) throw new WalletException(/* @__PURE__ */ new Error(`Invalid chain ID response from ${capitalize(wallet)}: ${String(currentChainId)}`));
|
|
297
308
|
if (currentChainId.toLowerCase() !== chainIdToHex.toLowerCase()) try {
|
|
298
309
|
await switchEthereumChainWithTimeout(provider, chainIdToHex, TIMEOUT_MS);
|
|
299
310
|
} catch (postAddError) {
|
|
300
|
-
|
|
301
|
-
const rawCode$1 = (_postAddError$code = postAddError === null || postAddError === void 0 ? void 0 : postAddError.code) !== null && _postAddError$code !== void 0 ? _postAddError$code : postAddError === null || postAddError === void 0 || (_postAddError$data = postAddError.data) === null || _postAddError$data === void 0 || (_postAddError$data = _postAddError$data.originalError) === null || _postAddError$data === void 0 ? void 0 : _postAddError$data.code;
|
|
302
|
-
const parsed$1 = rawCode$1 != null ? Number(rawCode$1) : NaN;
|
|
303
|
-
if ((!isNaN(parsed$1) ? parsed$1 : void 0) === 4001) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} chain switch after add was rejected`));
|
|
311
|
+
if (extractNormalizedErrorCode(postAddError) === EvmWalletProviderErrorCode.UserRejectedRequest) throw new WalletException(/* @__PURE__ */ new Error(`${capitalize(wallet)} chain switch after add was rejected`));
|
|
304
312
|
throw new WalletException(/* @__PURE__ */ new Error(`Failed to switch to ${chainConfig.name} network after adding it: ${postAddError.message}`));
|
|
305
313
|
}
|
|
306
314
|
}
|
|
@@ -580,11 +588,8 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
580
588
|
try {
|
|
581
589
|
await switchEthereumChainWithTimeout(ethereum, chainIdHex, TIMEOUT_MS);
|
|
582
590
|
} catch (error) {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
const parsed = rawCode != null ? Number(rawCode) : NaN;
|
|
586
|
-
const errorCode = !isNaN(parsed) ? parsed : void 0;
|
|
587
|
-
if (errorCode === 4001) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} chain switch was rejected`), {
|
|
591
|
+
const errorCode = extractNormalizedErrorCode(error);
|
|
592
|
+
if (errorCode === EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} chain switch was rejected`), {
|
|
588
593
|
code: UnspecifiedErrorCode,
|
|
589
594
|
type: ErrorType.WalletError,
|
|
590
595
|
contextModule: WalletAction.GetChainId
|
|
@@ -594,7 +599,7 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
594
599
|
type: ErrorType.WalletError,
|
|
595
600
|
contextModule: WalletAction.GetChainId
|
|
596
601
|
});
|
|
597
|
-
if (errorCode !==
|
|
602
|
+
if (errorCode !== EvmWalletProviderErrorCode.UnrecognizedChain) throw this.EvmWalletException(/* @__PURE__ */ new Error(`Something went wrong while switching ${capitalize(this.wallet || "wallet")} network`), {
|
|
598
603
|
code: UnspecifiedErrorCode,
|
|
599
604
|
type: ErrorType.WalletError,
|
|
600
605
|
contextModule: WalletAction.GetChainId
|
|
@@ -604,7 +609,12 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
604
609
|
method: "wallet_addEthereumChain",
|
|
605
610
|
params: [params]
|
|
606
611
|
});
|
|
607
|
-
} catch (
|
|
612
|
+
} catch (addError) {
|
|
613
|
+
if (extractNormalizedErrorCode(addError) === EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} add chain was rejected`), {
|
|
614
|
+
code: EvmWalletProviderErrorCode.UserRejectedRequest,
|
|
615
|
+
type: ErrorType.WalletError,
|
|
616
|
+
contextModule: WalletAction.GetChainId
|
|
617
|
+
});
|
|
608
618
|
throw this.EvmWalletException(/* @__PURE__ */ new Error(`Something went wrong while adding ${capitalize(this.wallet || "wallet")} network`), {
|
|
609
619
|
code: UnspecifiedErrorCode,
|
|
610
620
|
type: ErrorType.WalletError,
|
|
@@ -614,7 +624,7 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
614
624
|
let currentChainId;
|
|
615
625
|
try {
|
|
616
626
|
currentChainId = await ethereum.request({ method: "eth_chainId" });
|
|
617
|
-
} catch (
|
|
627
|
+
} catch (_unused2) {
|
|
618
628
|
throw this.EvmWalletException(/* @__PURE__ */ new Error(`Failed to get current chain ID from ${capitalize(this.wallet || "wallet")}`), {
|
|
619
629
|
code: UnspecifiedErrorCode,
|
|
620
630
|
type: ErrorType.WalletError,
|
|
@@ -629,10 +639,7 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
629
639
|
if (currentChainId.toLowerCase() !== chainIdHex.toLowerCase()) try {
|
|
630
640
|
await switchEthereumChainWithTimeout(ethereum, chainIdHex, TIMEOUT_MS);
|
|
631
641
|
} catch (postAddError) {
|
|
632
|
-
|
|
633
|
-
const postAddRawCode = (_postAddError$code = postAddError === null || postAddError === void 0 ? void 0 : postAddError.code) !== null && _postAddError$code !== void 0 ? _postAddError$code : postAddError === null || postAddError === void 0 || (_postAddError$data = postAddError.data) === null || _postAddError$data === void 0 || (_postAddError$data = _postAddError$data.originalError) === null || _postAddError$data === void 0 ? void 0 : _postAddError$data.code;
|
|
634
|
-
const postAddParsed = postAddRawCode != null ? Number(postAddRawCode) : NaN;
|
|
635
|
-
if ((!isNaN(postAddParsed) ? postAddParsed : void 0) === 4001) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} chain switch after add was rejected`), {
|
|
642
|
+
if (extractNormalizedErrorCode(postAddError) === EvmWalletProviderErrorCode.UserRejectedRequest) throw this.EvmWalletException(/* @__PURE__ */ new Error(`${capitalize(this.wallet || "wallet")} chain switch after add was rejected`), {
|
|
636
643
|
code: UnspecifiedErrorCode,
|
|
637
644
|
type: ErrorType.WalletError,
|
|
638
645
|
contextModule: WalletAction.GetChainId
|
|
@@ -659,4 +666,4 @@ var EvmWallet = class extends BaseConcreteStrategy {
|
|
|
659
666
|
};
|
|
660
667
|
|
|
661
668
|
//#endregion
|
|
662
|
-
export { EvmWallet as EvmWalletStrategy, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
|
669
|
+
export { EvmWallet as EvmWalletStrategy, extractNormalizedErrorCode, getBitGetProvider, getEvmProvider, getKeplrEvmProvider, getMetamaskProvider, getOkxWalletProvider, getPhantomProvider, getRabbyProvider, getRainbowProvider, getTrustWalletProvider, switchEthereumChainWithTimeout, updateEvmNetwork };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@injectivelabs/wallet-evm",
|
|
3
|
-
"version": "1.20.
|
|
3
|
+
"version": "1.20.4",
|
|
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/
|
|
46
|
-
"@injectivelabs/utils": "1.20.
|
|
47
|
-
"@injectivelabs/
|
|
48
|
-
"@injectivelabs/
|
|
49
|
-
"@injectivelabs/
|
|
45
|
+
"@injectivelabs/sdk-ts": "1.20.4",
|
|
46
|
+
"@injectivelabs/utils": "1.20.4",
|
|
47
|
+
"@injectivelabs/ts-types": "1.20.4",
|
|
48
|
+
"@injectivelabs/exceptions": "1.20.4",
|
|
49
|
+
"@injectivelabs/wallet-base": "1.20.4"
|
|
50
50
|
},
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|