@dynamic-labs/ethereum 3.0.0-alpha.2 → 3.0.0-alpha.21

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.
@@ -1,16 +1,17 @@
1
1
  'use client'
2
2
  import { __awaiter } from '../_virtual/_tslib.js';
3
- import { formatEther } from 'viem';
3
+ import { getAddress, formatEther } from 'viem';
4
4
  import { toAccount } from 'viem/accounts';
5
- import { WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
6
- import { parseEvmNetworks, retryableFn, FALLBACK_UNDEFINED, DynamicError, getOrMapViemChain } from '@dynamic-labs/utils';
5
+ import { parseEvmNetworks, retryableFn, FALLBACK_UNDEFINED, DynamicError } from '@dynamic-labs/utils';
6
+ import { chainsMap, getOrMapViemChain, createViemUiTransaction } from '@dynamic-labs/viem-utils';
7
+ import { WalletConnectorBase, logger } from '@dynamic-labs/wallet-connector-core';
7
8
  import { findEvmNetwork } from './utils/findEvmNetwork.js';
8
9
  import { getNameservice } from './utils/getNameservice.js';
9
10
 
10
11
  class EthWalletConnector extends WalletConnectorBase {
11
12
  getPublicClient() {
12
13
  return __awaiter(this, void 0, void 0, function* () {
13
- var _a, _b, _c;
14
+ var _a, _b;
14
15
  const networkId = (_a = (yield this.getNetwork())) !== null && _a !== void 0 ? _a : 1;
15
16
  if (this.evmNetworks.length === 0) {
16
17
  return undefined;
@@ -21,7 +22,10 @@ class EthWalletConnector extends WalletConnectorBase {
21
22
  solana: [],
22
23
  starknet: undefined,
23
24
  };
24
- return (_c = (_b = this.chainRpcProviders) === null || _b === void 0 ? void 0 : _b.getEvmProviderByChainId(configurations, networkId)) === null || _c === void 0 ? void 0 : _c.provider;
25
+ if (!this.chainRpcProviders)
26
+ return undefined;
27
+ const providers = this.chainRpcProviders.getProviders(configurations);
28
+ return (_b = this.chainRpcProviders.getEvmProviderByChainId(providers, networkId)) === null || _b === void 0 ? void 0 : _b.provider;
25
29
  });
26
30
  }
27
31
  constructor(props) {
@@ -44,7 +48,7 @@ class EthWalletConnector extends WalletConnectorBase {
44
48
  if (!this.supportsNetworkSwitching || !provider) {
45
49
  return Promise.resolve(undefined);
46
50
  }
47
- return retryableFn(provider.getChainId, {
51
+ const chainId = yield retryableFn(provider.getChainId, {
48
52
  fallbackValue: FALLBACK_UNDEFINED,
49
53
  /**
50
54
  * The timeout is set to 1 second because the getChainId method
@@ -54,32 +58,43 @@ class EthWalletConnector extends WalletConnectorBase {
54
58
  */
55
59
  timeoutMs: 1000,
56
60
  });
61
+ this.setActiveChain(chainsMap[chainId]);
62
+ return chainId;
57
63
  });
58
64
  }
59
- getNameService() {
65
+ getNameService(address) {
60
66
  return __awaiter(this, void 0, void 0, function* () {
61
- const [[address], rpcProvider] = yield Promise.all([
62
- this.getConnectedAccounts(),
63
- this.getPublicClient(),
64
- ]);
65
- if (!address || !rpcProvider) {
67
+ const rpcProvider = yield this.getPublicClient();
68
+ if (!rpcProvider) {
66
69
  return;
67
70
  }
68
71
  return getNameservice({ address, rpcProvider });
69
72
  });
70
73
  }
74
+ isTestnet() {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ var _a, _b, _c;
77
+ return (_c = (_b = (_a = (yield this.getPublicClient())) === null || _a === void 0 ? void 0 : _a.chain) === null || _b === void 0 ? void 0 : _b.testnet) !== null && _c !== void 0 ? _c : false;
78
+ });
79
+ }
80
+ parseAddress(address) {
81
+ try {
82
+ // Ensures the address follows the EIP55 format.
83
+ // see: https://eips.ethereum.org/EIPS/eip-55
84
+ return getAddress(address);
85
+ }
86
+ catch (err) {
87
+ logger.debug(`Failed to parse EVM address into EIP55 format: error getting checksum, returning default ${address}`, err);
88
+ }
89
+ return address;
90
+ }
71
91
  getSigner() {
72
92
  return __awaiter(this, void 0, void 0, function* () {
73
93
  return this.getWalletClient();
74
94
  });
75
95
  }
76
- getBalance() {
96
+ getBalance(address) {
77
97
  return __awaiter(this, void 0, void 0, function* () {
78
- // This is equivalent to getSigner().getAddress() but does not crash
79
- // if the signer is not available
80
- const [address] = yield this.getConnectedAccounts();
81
- if (!address)
82
- return;
83
98
  const client = yield this.getPublicClient();
84
99
  const result = yield (client === null || client === void 0 ? void 0 : client.getBalance({
85
100
  address: address,
@@ -119,7 +134,7 @@ class EthWalletConnector extends WalletConnectorBase {
119
134
  return [];
120
135
  const addresses = yield retryableFn(provider.getAddresses, {
121
136
  fallbackValue: [],
122
- timeoutMs: 300,
137
+ timeoutMs: 500,
123
138
  });
124
139
  if (addresses.length) {
125
140
  this.setActiveAccount(addresses[0]);
@@ -139,11 +154,15 @@ class EthWalletConnector extends WalletConnectorBase {
139
154
  if (!this.supportsNetworkSwitching()) {
140
155
  throw new DynamicError('Network switching is not supported');
141
156
  }
142
- yield provider.switchChain(getOrMapViemChain(network));
157
+ const viemChain = getOrMapViemChain(network);
158
+ yield provider.switchChain(viemChain);
143
159
  if (this.key === 'magiceden') {
144
160
  const newChainId = yield provider.getChainId();
145
161
  this.emit('chainChange', { chain: newChainId.toString() });
146
162
  }
163
+ else {
164
+ this.setActiveChain(viemChain);
165
+ }
147
166
  }
148
167
  catch (error) {
149
168
  // we need to check for unrecognized chain error first because it also contains 'rejected' in message
@@ -188,6 +207,22 @@ class EthWalletConnector extends WalletConnectorBase {
188
207
  getActiveAccount() {
189
208
  return this.activeAccount;
190
209
  }
210
+ setActiveChain(chain) {
211
+ this.activeChain = chain;
212
+ }
213
+ getActiveChain() {
214
+ return this.activeChain;
215
+ }
216
+ createUiTransaction(from) {
217
+ return __awaiter(this, void 0, void 0, function* () {
218
+ const walletClient = yield this.getWalletClient();
219
+ const publicClient = yield this.getPublicClient();
220
+ if (!publicClient || !walletClient) {
221
+ throw new DynamicError('No public client available');
222
+ }
223
+ return createViemUiTransaction({ from, publicClient, walletClient });
224
+ });
225
+ }
191
226
  }
192
227
 
193
228
  export { EthWalletConnector };
@@ -5,8 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
 
6
6
  var _tslib = require('../../_virtual/_tslib.cjs');
7
7
  var viem = require('viem');
8
- var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
9
8
  var viemUtils = require('@dynamic-labs/viem-utils');
9
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
10
10
  var EthWalletConnector = require('../EthWalletConnector.cjs');
11
11
  var helpers = require('./helpers.cjs');
12
12
 
@@ -19,7 +19,7 @@ class Coinbase extends EthWalletConnector.EthWalletConnector {
19
19
  this.supportedChains = ['EVM', 'ETH'];
20
20
  this.connectedChain = 'EVM';
21
21
  this.canConnectViaQrCode = false;
22
- this.canConnectViaCustodialService = true;
22
+ this.canConnectViaCustodialService = !this.isInstalledOnBrowser();
23
23
  this.coinbaseProviderOpts = {
24
24
  appLogoUrl: appLogoUrl,
25
25
  appName: appName,