@injectivelabs/wallet-evm 1.16.32 → 1.16.34
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/strategy/strategy.d.ts +1 -0
- package/dist/cjs/strategy/strategy.js +86 -46
- package/dist/cjs/utils/index.d.ts +0 -15
- package/dist/cjs/utils/index.js +1 -34
- package/dist/esm/strategy/strategy.d.ts +1 -0
- package/dist/esm/strategy/strategy.js +42 -2
- package/dist/esm/utils/index.d.ts +0 -15
- package/dist/esm/utils/index.js +0 -32
- package/package.json +8 -8
|
@@ -38,5 +38,6 @@ export declare class EvmWallet extends BaseConcreteStrategy implements ConcreteW
|
|
|
38
38
|
onChainIdChanged(callback: (chain: string) => void): Promise<void>;
|
|
39
39
|
onAccountChange(callback: (account: AccountAddress | string[]) => void): Promise<void>;
|
|
40
40
|
getEip1193Provider(): Promise<Eip1193Provider>;
|
|
41
|
+
addEvmNetwork(chainId: EvmChainId): Promise<void>;
|
|
41
42
|
private getEthereum;
|
|
42
43
|
}
|
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EvmWallet = void 0;
|
|
4
4
|
const utils_1 = require("@injectivelabs/utils");
|
|
5
|
-
const wallet_base_1 = require("@injectivelabs/wallet-base");
|
|
6
5
|
const sdk_ts_1 = require("@injectivelabs/sdk-ts");
|
|
7
|
-
const
|
|
6
|
+
const wallet_base_1 = require("@injectivelabs/wallet-base");
|
|
8
7
|
const exceptions_1 = require("@injectivelabs/exceptions");
|
|
9
8
|
const index_js_1 = require("./utils/index.js");
|
|
10
|
-
class EvmWallet extends
|
|
9
|
+
class EvmWallet extends wallet_base_1.BaseConcreteStrategy {
|
|
11
10
|
wallet;
|
|
12
11
|
evmProviders = {};
|
|
13
12
|
constructor(args) {
|
|
@@ -19,26 +18,26 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
19
18
|
window.addEventListener('eip6963:announceProvider', (announcement) => {
|
|
20
19
|
const event = announcement;
|
|
21
20
|
const walletName = event.detail.info.name.toLowerCase();
|
|
22
|
-
if (walletName ===
|
|
23
|
-
this.evmProviders[
|
|
21
|
+
if (walletName === wallet_base_1.Wallet.Metamask.toLowerCase()) {
|
|
22
|
+
this.evmProviders[wallet_base_1.Wallet.Metamask] = event.detail.provider;
|
|
24
23
|
}
|
|
25
|
-
if (walletName ===
|
|
26
|
-
this.evmProviders[
|
|
24
|
+
if (walletName === wallet_base_1.Wallet.Rabby.toLowerCase()) {
|
|
25
|
+
this.evmProviders[wallet_base_1.Wallet.Rabby] = event.detail.provider;
|
|
27
26
|
}
|
|
28
|
-
if (walletName ===
|
|
29
|
-
this.evmProviders[
|
|
27
|
+
if (walletName === wallet_base_1.Wallet.Rainbow.toLowerCase()) {
|
|
28
|
+
this.evmProviders[wallet_base_1.Wallet.Rainbow] = event.detail.provider;
|
|
30
29
|
}
|
|
31
|
-
if (walletName ===
|
|
32
|
-
this.evmProviders[
|
|
30
|
+
if (walletName === wallet_base_1.Wallet.Phantom.toLowerCase()) {
|
|
31
|
+
this.evmProviders[wallet_base_1.Wallet.Phantom] = event.detail.provider;
|
|
33
32
|
}
|
|
34
|
-
if (walletName ===
|
|
35
|
-
this.evmProviders[
|
|
33
|
+
if (walletName === wallet_base_1.Wallet.OkxWallet.toLowerCase()) {
|
|
34
|
+
this.evmProviders[wallet_base_1.Wallet.OkxWallet] = event.detail.provider;
|
|
36
35
|
}
|
|
37
|
-
if (walletName ===
|
|
38
|
-
this.evmProviders[
|
|
36
|
+
if (walletName === wallet_base_1.Wallet.BitGet.toLowerCase()) {
|
|
37
|
+
this.evmProviders[wallet_base_1.Wallet.BitGet] = event.detail.provider;
|
|
39
38
|
}
|
|
40
|
-
if (walletName ===
|
|
41
|
-
this.evmProviders[
|
|
39
|
+
if (walletName === wallet_base_1.Wallet.TrustWallet.toLowerCase()) {
|
|
40
|
+
this.evmProviders[wallet_base_1.Wallet.TrustWallet] = event.detail.provider;
|
|
42
41
|
}
|
|
43
42
|
});
|
|
44
43
|
window.dispatchEvent(new Event('eip6963:requestProvider'));
|
|
@@ -46,40 +45,40 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
46
45
|
this.wallet = args.wallet;
|
|
47
46
|
}
|
|
48
47
|
EvmWalletException(error, context) {
|
|
49
|
-
if (this.wallet ===
|
|
48
|
+
if (this.wallet === wallet_base_1.Wallet.Metamask) {
|
|
50
49
|
return new exceptions_1.MetamaskException(error, context);
|
|
51
50
|
}
|
|
52
|
-
if (this.wallet ===
|
|
51
|
+
if (this.wallet === wallet_base_1.Wallet.BitGet) {
|
|
53
52
|
return new exceptions_1.BitGetException(error, context);
|
|
54
53
|
}
|
|
55
|
-
if (this.wallet ===
|
|
54
|
+
if (this.wallet === wallet_base_1.Wallet.OkxWallet) {
|
|
56
55
|
return new exceptions_1.OkxWalletException(error, context);
|
|
57
56
|
}
|
|
58
|
-
if (this.wallet ===
|
|
57
|
+
if (this.wallet === wallet_base_1.Wallet.Phantom) {
|
|
59
58
|
return new exceptions_1.MetamaskException(error, context);
|
|
60
59
|
}
|
|
61
|
-
if (this.wallet ===
|
|
60
|
+
if (this.wallet === wallet_base_1.Wallet.TrustWallet) {
|
|
62
61
|
return new exceptions_1.TrustWalletException(error, context);
|
|
63
62
|
}
|
|
64
|
-
if (this.wallet ===
|
|
63
|
+
if (this.wallet === wallet_base_1.Wallet.Rainbow) {
|
|
65
64
|
return new exceptions_1.RainbowWalletException(error, context);
|
|
66
65
|
}
|
|
67
66
|
return new exceptions_1.WalletException(error, context);
|
|
68
67
|
}
|
|
69
68
|
async getWalletDeviceType() {
|
|
70
|
-
return Promise.resolve(
|
|
69
|
+
return Promise.resolve(wallet_base_1.WalletDeviceType.Browser);
|
|
71
70
|
}
|
|
72
71
|
async enable() {
|
|
73
72
|
return Promise.resolve(true);
|
|
74
73
|
}
|
|
75
74
|
async disconnect() {
|
|
76
|
-
if (this.listeners[
|
|
75
|
+
if (this.listeners[wallet_base_1.WalletEventListener.AccountChange]) {
|
|
77
76
|
const ethereum = await this.getEthereum();
|
|
78
|
-
ethereum.removeListener('accountsChanged', this.listeners[
|
|
77
|
+
ethereum.removeListener('accountsChanged', this.listeners[wallet_base_1.WalletEventListener.AccountChange]);
|
|
79
78
|
}
|
|
80
|
-
if (this.listeners[
|
|
79
|
+
if (this.listeners[wallet_base_1.WalletEventListener.ChainIdChange]) {
|
|
81
80
|
const ethereum = await this.getEthereum();
|
|
82
|
-
ethereum.removeListener('chainChanged', this.listeners[
|
|
81
|
+
ethereum.removeListener('chainChanged', this.listeners[wallet_base_1.WalletEventListener.ChainIdChange]);
|
|
83
82
|
}
|
|
84
83
|
this.listeners = {};
|
|
85
84
|
}
|
|
@@ -94,7 +93,7 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
94
93
|
throw this.EvmWalletException(new Error(e.message), {
|
|
95
94
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
96
95
|
type: exceptions_1.ErrorType.WalletError,
|
|
97
|
-
contextModule:
|
|
96
|
+
contextModule: wallet_base_1.WalletAction.GetAccounts,
|
|
98
97
|
});
|
|
99
98
|
}
|
|
100
99
|
}
|
|
@@ -113,7 +112,7 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
113
112
|
throw this.EvmWalletException(new Error(e.message), {
|
|
114
113
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
115
114
|
type: exceptions_1.ErrorType.WalletError,
|
|
116
|
-
contextModule:
|
|
115
|
+
contextModule: wallet_base_1.WalletAction.SendEvmTransaction,
|
|
117
116
|
});
|
|
118
117
|
}
|
|
119
118
|
}
|
|
@@ -146,13 +145,13 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
146
145
|
throw new exceptions_1.MetamaskException(new Error('Please connect directly with Ledger to sign this transaction'), {
|
|
147
146
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
148
147
|
type: exceptions_1.ErrorType.WalletError,
|
|
149
|
-
contextModule:
|
|
148
|
+
contextModule: wallet_base_1.WalletAction.SignTransaction,
|
|
150
149
|
});
|
|
151
150
|
}
|
|
152
151
|
throw new exceptions_1.MetamaskException(new Error(e.message), {
|
|
153
152
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
154
153
|
type: exceptions_1.ErrorType.WalletError,
|
|
155
|
-
contextModule:
|
|
154
|
+
contextModule: wallet_base_1.WalletAction.SignTransaction,
|
|
156
155
|
});
|
|
157
156
|
}
|
|
158
157
|
}
|
|
@@ -160,14 +159,14 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
160
159
|
throw new exceptions_1.WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
|
|
161
160
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
162
161
|
type: exceptions_1.ErrorType.WalletError,
|
|
163
|
-
contextModule:
|
|
162
|
+
contextModule: wallet_base_1.WalletAction.SignTransaction,
|
|
164
163
|
});
|
|
165
164
|
}
|
|
166
165
|
async signCosmosTransaction(_transaction) {
|
|
167
166
|
throw new exceptions_1.WalletException(new Error('This wallet does not support signing Cosmos transactions'), {
|
|
168
167
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
169
168
|
type: exceptions_1.ErrorType.WalletError,
|
|
170
|
-
contextModule:
|
|
169
|
+
contextModule: wallet_base_1.WalletAction.SignTransaction,
|
|
171
170
|
});
|
|
172
171
|
}
|
|
173
172
|
async signArbitrary(signer, data) {
|
|
@@ -183,7 +182,7 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
183
182
|
throw this.EvmWalletException(new Error(e.message), {
|
|
184
183
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
185
184
|
type: exceptions_1.ErrorType.WalletError,
|
|
186
|
-
contextModule:
|
|
185
|
+
contextModule: wallet_base_1.WalletAction.SignArbitrary,
|
|
187
186
|
});
|
|
188
187
|
}
|
|
189
188
|
}
|
|
@@ -196,7 +195,7 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
196
195
|
throw this.EvmWalletException(new Error(e.message), {
|
|
197
196
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
198
197
|
type: exceptions_1.ErrorType.WalletError,
|
|
199
|
-
contextModule:
|
|
198
|
+
contextModule: wallet_base_1.WalletAction.GetChainId,
|
|
200
199
|
});
|
|
201
200
|
}
|
|
202
201
|
}
|
|
@@ -227,45 +226,86 @@ class EvmWallet extends wallet_base_2.BaseConcreteStrategy {
|
|
|
227
226
|
async onChainIdChanged(callback) {
|
|
228
227
|
const ethereum = await this.getEthereum();
|
|
229
228
|
this.listeners = {
|
|
230
|
-
[
|
|
229
|
+
[wallet_base_1.WalletEventListener.ChainIdChange]: callback,
|
|
231
230
|
};
|
|
232
231
|
ethereum.on('chainChanged', callback);
|
|
233
232
|
}
|
|
234
233
|
async onAccountChange(callback) {
|
|
235
234
|
const ethereum = await this.getEthereum();
|
|
236
235
|
this.listeners = {
|
|
237
|
-
[
|
|
236
|
+
[wallet_base_1.WalletEventListener.AccountChange]: callback,
|
|
238
237
|
};
|
|
239
238
|
ethereum.on('accountsChanged', callback);
|
|
240
239
|
}
|
|
241
240
|
async getEip1193Provider() {
|
|
242
241
|
return this.getEthereum();
|
|
243
242
|
}
|
|
243
|
+
async addEvmNetwork(chainId) {
|
|
244
|
+
const ethereum = await this.getEthereum();
|
|
245
|
+
const chainIdToHex = chainId.toString(16);
|
|
246
|
+
const chain = (0, wallet_base_1.getEvmChainConfig)(chainId);
|
|
247
|
+
const params = {
|
|
248
|
+
chainId: `0x${chainIdToHex}`,
|
|
249
|
+
chainName: chain.name,
|
|
250
|
+
rpcUrls: [...(chain.rpcUrls?.default?.http || [])],
|
|
251
|
+
blockExplorerUrls: chain.blockExplorers?.default?.url
|
|
252
|
+
? [chain.blockExplorers.default.url]
|
|
253
|
+
: [],
|
|
254
|
+
nativeCurrency: chain.nativeCurrency,
|
|
255
|
+
};
|
|
256
|
+
try {
|
|
257
|
+
await Promise.race([
|
|
258
|
+
ethereum.request({
|
|
259
|
+
method: 'wallet_switchEthereumChain',
|
|
260
|
+
params: [{ chainId: `0x${chainIdToHex}` }],
|
|
261
|
+
}),
|
|
262
|
+
new Promise((resolve) => ethereum.on('chainChanged', ({ chain }) => {
|
|
263
|
+
if (chain?.id === chainIdToHex) {
|
|
264
|
+
resolve();
|
|
265
|
+
}
|
|
266
|
+
})),
|
|
267
|
+
]);
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
if (error.code === 4902) {
|
|
271
|
+
await ethereum.request({
|
|
272
|
+
method: 'wallet_addEthereumChain',
|
|
273
|
+
params: [params],
|
|
274
|
+
});
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
throw this.EvmWalletException(new Error(`Something went wrong while adding ${(0, utils_1.capitalize)(this.wallet || 'wallet')} network`), {
|
|
278
|
+
code: exceptions_1.UnspecifiedErrorCode,
|
|
279
|
+
type: exceptions_1.ErrorType.WalletError,
|
|
280
|
+
contextModule: wallet_base_1.WalletAction.GetChainId,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
244
284
|
async getEthereum() {
|
|
245
285
|
const evmProvider = this.evmProviders[this.wallet];
|
|
246
286
|
if (evmProvider) {
|
|
247
287
|
return evmProvider;
|
|
248
288
|
}
|
|
249
|
-
const backUpProvider = this.wallet ===
|
|
289
|
+
const backUpProvider = this.wallet === wallet_base_1.Wallet.Metamask
|
|
250
290
|
? await (0, index_js_1.getMetamaskProvider)()
|
|
251
|
-
: this.wallet ===
|
|
291
|
+
: this.wallet === wallet_base_1.Wallet.Rabby
|
|
252
292
|
? await (0, index_js_1.getRabbyProvider)()
|
|
253
|
-
: this.wallet ===
|
|
293
|
+
: this.wallet === wallet_base_1.Wallet.Phantom
|
|
254
294
|
? await (0, index_js_1.getPhantomProvider)()
|
|
255
|
-
: this.wallet ===
|
|
295
|
+
: this.wallet === wallet_base_1.Wallet.BitGet
|
|
256
296
|
? await (0, index_js_1.getBitGetProvider)()
|
|
257
|
-
: this.wallet ===
|
|
297
|
+
: this.wallet === wallet_base_1.Wallet.OkxWallet
|
|
258
298
|
? await (0, index_js_1.getOkxWalletProvider)()
|
|
259
|
-
: this.wallet ===
|
|
299
|
+
: this.wallet === wallet_base_1.Wallet.TrustWallet
|
|
260
300
|
? await (0, index_js_1.getTrustWalletProvider)()
|
|
261
|
-
: this.wallet ===
|
|
301
|
+
: this.wallet === wallet_base_1.Wallet.Rainbow
|
|
262
302
|
? await (0, index_js_1.getRainbowProvider)()
|
|
263
303
|
: undefined;
|
|
264
304
|
if (!backUpProvider) {
|
|
265
305
|
throw this.EvmWalletException(new Error(`Please install the ${this.wallet} wallet extension.`), {
|
|
266
306
|
code: exceptions_1.UnspecifiedErrorCode,
|
|
267
307
|
type: exceptions_1.ErrorType.WalletNotInstalledError,
|
|
268
|
-
contextModule:
|
|
308
|
+
contextModule: wallet_base_1.WalletAction.GetAccounts,
|
|
269
309
|
});
|
|
270
310
|
}
|
|
271
311
|
return backUpProvider;
|
|
@@ -3,18 +3,3 @@ import type { EvmChainId } from '@injectivelabs/ts-types';
|
|
|
3
3
|
import type { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
|
|
4
4
|
export declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
|
|
5
5
|
export declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId) => Promise<unknown>;
|
|
6
|
-
export declare const addEvmNetworkToWallet: ({ wallet, chainId, params, }: {
|
|
7
|
-
wallet: Wallet;
|
|
8
|
-
chainId: EvmChainId;
|
|
9
|
-
params: {
|
|
10
|
-
rpcUrls: string[];
|
|
11
|
-
chainName: string;
|
|
12
|
-
blockExplorerUrls: string[];
|
|
13
|
-
chainId: string;
|
|
14
|
-
nativeCurrency: {
|
|
15
|
-
name: string;
|
|
16
|
-
symbol: string;
|
|
17
|
-
decimals: number;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
}) => Promise<void>;
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.updateEvmNetwork = exports.getEvmProvider = void 0;
|
|
4
4
|
const utils_1 = require("@injectivelabs/utils");
|
|
5
5
|
const exceptions_1 = require("@injectivelabs/exceptions");
|
|
6
6
|
const wallet_base_1 = require("@injectivelabs/wallet-base");
|
|
@@ -71,36 +71,3 @@ const updateEvmNetwork = async (wallet, chainId) => {
|
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
73
|
exports.updateEvmNetwork = updateEvmNetwork;
|
|
74
|
-
const addEvmNetworkToWallet = async ({ wallet, chainId, params, }) => {
|
|
75
|
-
if (!(0, wallet_base_1.isEvmBrowserWallet)(wallet)) {
|
|
76
|
-
throw new exceptions_1.WalletException(new Error(`Evm Wallet for ${(0, utils_1.capitalize)(wallet)} is not supported.`));
|
|
77
|
-
}
|
|
78
|
-
const provider = (await (0, exports.getEvmProvider)(wallet));
|
|
79
|
-
if (!provider) {
|
|
80
|
-
throw new exceptions_1.WalletException(new Error(`Please install ${(0, utils_1.capitalize)(wallet)} Extension`));
|
|
81
|
-
}
|
|
82
|
-
const chainIdToHex = chainId.toString(16);
|
|
83
|
-
try {
|
|
84
|
-
await Promise.race([
|
|
85
|
-
provider.request({
|
|
86
|
-
method: 'wallet_switchEthereumChain',
|
|
87
|
-
params: [{ chainId: `0x${chainIdToHex}` }],
|
|
88
|
-
}),
|
|
89
|
-
new Promise((resolve) => provider.on('chainChanged', ({ chain }) => {
|
|
90
|
-
if (chain?.id === chainIdToHex) {
|
|
91
|
-
resolve();
|
|
92
|
-
}
|
|
93
|
-
})),
|
|
94
|
-
]);
|
|
95
|
-
}
|
|
96
|
-
catch (error) {
|
|
97
|
-
if (error.code === 4902) {
|
|
98
|
-
await provider?.request({
|
|
99
|
-
method: 'wallet_addEthereumChain',
|
|
100
|
-
params: [params],
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
throw new exceptions_1.WalletException(new Error(`Something went wrong while adding ${(0, utils_1.capitalize)(wallet)} network`));
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
exports.addEvmNetworkToWallet = addEvmNetworkToWallet;
|
|
@@ -38,5 +38,6 @@ export declare class EvmWallet extends BaseConcreteStrategy implements ConcreteW
|
|
|
38
38
|
onChainIdChanged(callback: (chain: string) => void): Promise<void>;
|
|
39
39
|
onAccountChange(callback: (account: AccountAddress | string[]) => void): Promise<void>;
|
|
40
40
|
getEip1193Provider(): Promise<Eip1193Provider>;
|
|
41
|
+
addEvmNetwork(chainId: EvmChainId): Promise<void>;
|
|
41
42
|
private getEthereum;
|
|
42
43
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { sleep, capitalize } from '@injectivelabs/utils';
|
|
2
|
-
import { isEvmBrowserWallet } from '@injectivelabs/wallet-base';
|
|
3
2
|
import { toUtf8, TxGrpcApi, isServerSide } from '@injectivelabs/sdk-ts';
|
|
4
|
-
import { Wallet, WalletAction, WalletDeviceType, WalletEventListener, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
|
|
3
|
+
import { Wallet, WalletAction, WalletDeviceType, getEvmChainConfig, isEvmBrowserWallet, WalletEventListener, BaseConcreteStrategy, } from '@injectivelabs/wallet-base';
|
|
5
4
|
import { ErrorType, WalletException, BitGetException, MetamaskException, OkxWalletException, UnspecifiedErrorCode, TransactionException, TrustWalletException, RainbowWalletException, } from '@injectivelabs/exceptions';
|
|
6
5
|
import { getRabbyProvider, getBitGetProvider, getPhantomProvider, getRainbowProvider, getMetamaskProvider, getOkxWalletProvider, getTrustWalletProvider, } from './utils/index.js';
|
|
7
6
|
export class EvmWallet extends BaseConcreteStrategy {
|
|
@@ -238,6 +237,47 @@ export class EvmWallet extends BaseConcreteStrategy {
|
|
|
238
237
|
async getEip1193Provider() {
|
|
239
238
|
return this.getEthereum();
|
|
240
239
|
}
|
|
240
|
+
async addEvmNetwork(chainId) {
|
|
241
|
+
const ethereum = await this.getEthereum();
|
|
242
|
+
const chainIdToHex = chainId.toString(16);
|
|
243
|
+
const chain = getEvmChainConfig(chainId);
|
|
244
|
+
const params = {
|
|
245
|
+
chainId: `0x${chainIdToHex}`,
|
|
246
|
+
chainName: chain.name,
|
|
247
|
+
rpcUrls: [...(chain.rpcUrls?.default?.http || [])],
|
|
248
|
+
blockExplorerUrls: chain.blockExplorers?.default?.url
|
|
249
|
+
? [chain.blockExplorers.default.url]
|
|
250
|
+
: [],
|
|
251
|
+
nativeCurrency: chain.nativeCurrency,
|
|
252
|
+
};
|
|
253
|
+
try {
|
|
254
|
+
await Promise.race([
|
|
255
|
+
ethereum.request({
|
|
256
|
+
method: 'wallet_switchEthereumChain',
|
|
257
|
+
params: [{ chainId: `0x${chainIdToHex}` }],
|
|
258
|
+
}),
|
|
259
|
+
new Promise((resolve) => ethereum.on('chainChanged', ({ chain }) => {
|
|
260
|
+
if (chain?.id === chainIdToHex) {
|
|
261
|
+
resolve();
|
|
262
|
+
}
|
|
263
|
+
})),
|
|
264
|
+
]);
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
if (error.code === 4902) {
|
|
268
|
+
await ethereum.request({
|
|
269
|
+
method: 'wallet_addEthereumChain',
|
|
270
|
+
params: [params],
|
|
271
|
+
});
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
throw this.EvmWalletException(new Error(`Something went wrong while adding ${capitalize(this.wallet || 'wallet')} network`), {
|
|
275
|
+
code: UnspecifiedErrorCode,
|
|
276
|
+
type: ErrorType.WalletError,
|
|
277
|
+
contextModule: WalletAction.GetChainId,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
241
281
|
async getEthereum() {
|
|
242
282
|
const evmProvider = this.evmProviders[this.wallet];
|
|
243
283
|
if (evmProvider) {
|
|
@@ -3,18 +3,3 @@ import type { EvmChainId } from '@injectivelabs/ts-types';
|
|
|
3
3
|
import type { BrowserEip1993Provider } from '@injectivelabs/wallet-base';
|
|
4
4
|
export declare const getEvmProvider: (wallet: Wallet) => Promise<BrowserEip1993Provider>;
|
|
5
5
|
export declare const updateEvmNetwork: (wallet: Wallet, chainId: EvmChainId) => Promise<unknown>;
|
|
6
|
-
export declare const addEvmNetworkToWallet: ({ wallet, chainId, params, }: {
|
|
7
|
-
wallet: Wallet;
|
|
8
|
-
chainId: EvmChainId;
|
|
9
|
-
params: {
|
|
10
|
-
rpcUrls: string[];
|
|
11
|
-
chainName: string;
|
|
12
|
-
blockExplorerUrls: string[];
|
|
13
|
-
chainId: string;
|
|
14
|
-
nativeCurrency: {
|
|
15
|
-
name: string;
|
|
16
|
-
symbol: string;
|
|
17
|
-
decimals: number;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
}) => Promise<void>;
|
package/dist/esm/utils/index.js
CHANGED
|
@@ -66,35 +66,3 @@ export const updateEvmNetwork = async (wallet, chainId) => {
|
|
|
66
66
|
throw new WalletException(new Error(`Please update your ${capitalize(wallet)} network`));
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
|
-
export const addEvmNetworkToWallet = async ({ wallet, chainId, params, }) => {
|
|
70
|
-
if (!isEvmBrowserWallet(wallet)) {
|
|
71
|
-
throw new WalletException(new Error(`Evm Wallet for ${capitalize(wallet)} is not supported.`));
|
|
72
|
-
}
|
|
73
|
-
const provider = (await getEvmProvider(wallet));
|
|
74
|
-
if (!provider) {
|
|
75
|
-
throw new WalletException(new Error(`Please install ${capitalize(wallet)} Extension`));
|
|
76
|
-
}
|
|
77
|
-
const chainIdToHex = chainId.toString(16);
|
|
78
|
-
try {
|
|
79
|
-
await Promise.race([
|
|
80
|
-
provider.request({
|
|
81
|
-
method: 'wallet_switchEthereumChain',
|
|
82
|
-
params: [{ chainId: `0x${chainIdToHex}` }],
|
|
83
|
-
}),
|
|
84
|
-
new Promise((resolve) => provider.on('chainChanged', ({ chain }) => {
|
|
85
|
-
if (chain?.id === chainIdToHex) {
|
|
86
|
-
resolve();
|
|
87
|
-
}
|
|
88
|
-
})),
|
|
89
|
-
]);
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
if (error.code === 4902) {
|
|
93
|
-
await provider?.request({
|
|
94
|
-
method: 'wallet_addEthereumChain',
|
|
95
|
-
params: [params],
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
throw new WalletException(new Error(`Something went wrong while adding ${capitalize(wallet)} network`));
|
|
99
|
-
}
|
|
100
|
-
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@injectivelabs/wallet-evm",
|
|
3
3
|
"description": "EVM wallet strategies for use with @injectivelabs/wallet-core.",
|
|
4
|
-
"version": "1.16.
|
|
4
|
+
"version": "1.16.34",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"author": {
|
|
@@ -56,17 +56,17 @@
|
|
|
56
56
|
"start": "node dist/index.js"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@injectivelabs/exceptions": "1.16.
|
|
60
|
-
"@injectivelabs/networks": "1.16.
|
|
61
|
-
"@injectivelabs/sdk-ts": "1.16.
|
|
62
|
-
"@injectivelabs/ts-types": "1.16.
|
|
63
|
-
"@injectivelabs/utils": "1.16.
|
|
64
|
-
"@injectivelabs/wallet-base": "1.16.
|
|
59
|
+
"@injectivelabs/exceptions": "1.16.34",
|
|
60
|
+
"@injectivelabs/networks": "1.16.34",
|
|
61
|
+
"@injectivelabs/sdk-ts": "1.16.34",
|
|
62
|
+
"@injectivelabs/ts-types": "1.16.34",
|
|
63
|
+
"@injectivelabs/utils": "1.16.34",
|
|
64
|
+
"@injectivelabs/wallet-base": "1.16.34"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"shx": "^0.3.3"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "a77a9cea69fd19e201c3c5ebf14860c7b54cc76e",
|
|
70
70
|
"typedoc": {
|
|
71
71
|
"entryPoint": "./src/index.ts",
|
|
72
72
|
"readmeFile": "./README.md",
|