@tetherto/wdk-wallet-evm 1.0.0-beta.18 → 1.0.0-beta.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tetherto/wdk-wallet-evm",
3
- "version": "1.0.0-beta.18",
3
+ "version": "1.0.0-beta.19",
4
4
  "description": "A simple package to manage BIP-32 wallets for evm blockchains.",
5
5
  "keywords": [
6
6
  "wdk",
@@ -81,7 +81,7 @@ import FailoverProvider from '@tetherto/wdk-failover-provider'
81
81
 
82
82
  /**
83
83
  * @typedef {Object} EvmWalletConfig
84
- * @property {string | Eip1193Provider | Array<string | Eip1193Provider>} [provider] - The url of the rpc provider, or an instance of a class that implements eip-1193. It's also possible to provide an array of urls or EIP 1193 providers instead. In such case, connection errors will cause the wallet to automatically fallback on the next provider in the list.
84
+ * @property {string | Provider | Eip1193Provider | Array<string | Provider | Eip1193Provider>} [provider] - The url of the rpc provider, an already-built ethers provider (e.g. a `JsonRpcProvider` or a failover wrapper), or an instance of a class that implements eip-1193. It's also possible to provide an array of these instead. In such case, connection errors will cause the wallet to automatically fallback on the next provider in the list. An already-built provider is reused as-is, which lets a manager share a single provider across all the accounts it creates.
85
85
  * @property {number} [retries] - If set and if 'provider' is a list of urls or EIP 1193 providers, the number of additional retry attempts after the initial call fails. Total attempts = `1 + retries`. For example, `retries: 3` with 4 providers will try each provider once before throwing. If `retries` exceeds the number of providers, the failover will loop back and retry already-failed providers in round-robin order. Default: 3.
86
86
  * @property {number} [chainId] - The chain ID of the network. When provided, skips automatic chain ID detection from the provider.
87
87
  * @property {number | bigint} [transferMaxFee] - The maximum fee amount for transfer operations.
@@ -116,31 +116,67 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
116
116
  * @protected
117
117
  * @type {Provider | undefined}
118
118
  */
119
- this._provider = undefined
119
+ this._provider = WalletAccountReadOnlyEvm._buildProvider(config)
120
+ }
121
+
122
+ /**
123
+ * Whether a value is an EIP-1193 provider (e.g. a browser wallet).
124
+ *
125
+ * @protected
126
+ * @param {string | Eip1193Provider | Provider} value - The value to inspect.
127
+ * @returns {boolean} True if the value is an EIP-1193 provider.
128
+ */
129
+ static _isEip1193Provider (value) {
130
+ return typeof value.request === 'function'
131
+ }
120
132
 
133
+ /**
134
+ * Builds an ethers provider from the wallet configuration:
135
+ * - a url string -> a new `JsonRpcProvider`
136
+ * - an already-built ethers provider (or failover wrapper) -> reused as-is
137
+ * - anything else (EIP-1193 / browser wallet) -> wrapped in a `BrowserProvider`
138
+ * - an array of the above -> a `FailoverProvider` across each entry
139
+ *
140
+ * @protected
141
+ * @param {Omit<EvmWalletConfig, 'transferMaxFee' | 'transactionMaxFee'>} [config] - The configuration object.
142
+ * @returns {Provider | undefined} The provider, or undefined if none is configured.
143
+ */
144
+ static _buildProvider (config = {}) {
121
145
  const { provider, retries = 3 } = config
122
146
  const network = config.chainId ? Network.from(config.chainId) : undefined
123
147
  const providerOpts = config.chainId ? { staticNetwork: true } : undefined
124
148
 
149
+ const toOption = (entry) => {
150
+ if (typeof entry === 'string') {
151
+ return new JsonRpcProvider(entry, network, providerOpts)
152
+ }
153
+
154
+ if (WalletAccountReadOnlyEvm._isEip1193Provider(entry)) {
155
+ return new BrowserProvider(entry)
156
+ }
157
+
158
+ return entry
159
+ }
160
+
125
161
  if (Array.isArray(provider)) {
126
- if (provider.length > 0) {
127
- const failoverProvider = new FailoverProvider({ retries })
162
+ if (provider.length === 0) {
163
+ return undefined
164
+ }
128
165
 
129
- for (const entry of provider) {
130
- const option = typeof entry === 'string'
131
- ? new JsonRpcProvider(entry, network, providerOpts)
132
- : new BrowserProvider(entry)
133
- failoverProvider.addProvider(option)
134
- }
166
+ const failoverProvider = new FailoverProvider({ retries })
135
167
 
136
- this._provider = failoverProvider.initialize()
168
+ for (const entry of provider) {
169
+ failoverProvider.addProvider(toOption(entry))
137
170
  }
138
- } else if (provider) {
139
- this._provider =
140
- typeof provider === 'string'
141
- ? new JsonRpcProvider(provider, network, providerOpts)
142
- : new BrowserProvider(provider)
171
+
172
+ return failoverProvider.initialize()
143
173
  }
174
+
175
+ if (provider) {
176
+ return toOption(provider)
177
+ }
178
+
179
+ return undefined
144
180
  }
145
181
 
146
182
  /**
@@ -16,10 +16,6 @@
16
16
 
17
17
  import WalletManager, { InvalidSignerError, ProviderRequiredError } from '@tetherto/wdk-wallet'
18
18
 
19
- import { BrowserProvider, JsonRpcProvider } from 'ethers'
20
-
21
- import FailoverProvider from '@tetherto/wdk-failover-provider'
22
-
23
19
  import WalletAccountEvm from './wallet-account-evm.js'
24
20
  import SeedSignerEvm from './signers/seed-signer-evm.js'
25
21
 
@@ -80,34 +76,13 @@ export default class WalletManagerEvm extends WalletManager {
80
76
  this._config = config
81
77
 
82
78
  /**
83
- * An ethers provider to interact with a node of the blockchain.
79
+ * An ethers provider to interact with a node of the blockchain. Shared with every account
80
+ * this manager creates, so two accounts never open two clients for the same endpoint.
84
81
  *
85
82
  * @protected
86
83
  * @type {Provider | undefined}
87
84
  */
88
- this._provider = undefined
89
-
90
- const { provider, retries = 3 } = config
91
-
92
- if (Array.isArray(provider)) {
93
- if (provider.length > 0) {
94
- const failoverProvider = new FailoverProvider({ retries })
95
-
96
- for (const entry of provider) {
97
- const option = typeof entry === 'string'
98
- ? new JsonRpcProvider(entry)
99
- : new BrowserProvider(entry)
100
- failoverProvider.addProvider(option)
101
- }
102
-
103
- this._provider = failoverProvider.initialize()
104
- }
105
- } else if (provider) {
106
- this._provider =
107
- typeof provider === 'string'
108
- ? new JsonRpcProvider(provider)
109
- : new BrowserProvider(provider)
110
- }
85
+ this._provider = WalletAccountEvm._buildProvider(config)
111
86
  }
112
87
 
113
88
  /**
@@ -143,7 +118,7 @@ export default class WalletManagerEvm extends WalletManager {
143
118
  const accountSigner = signer.isDerivable
144
119
  ? await signer.derive(signer.path.split('/').slice(-3).join('/'))
145
120
  : signer
146
- const account = new WalletAccountEvm(accountSigner, this._config)
121
+ const account = new WalletAccountEvm(accountSigner, this._accountConfig())
147
122
  this._accounts[key] = account
148
123
  return account
149
124
  }
@@ -170,11 +145,22 @@ export default class WalletManagerEvm extends WalletManager {
170
145
  }
171
146
  const signer = this.getSigner(signerName)
172
147
  const childSigner = await signer.derive(path)
173
- const account = new WalletAccountEvm(childSigner, this._config)
148
+ const account = new WalletAccountEvm(childSigner, this._accountConfig())
174
149
  this._accounts[key] = account
175
150
  return account
176
151
  }
177
152
 
153
+ /**
154
+ * Builds the account config, injecting the manager's shared provider so accounts reuse
155
+ * it instead of opening their own client.
156
+ *
157
+ * @private
158
+ * @returns {EvmWalletConfig} The account configuration.
159
+ */
160
+ _accountConfig () {
161
+ return { ...this._config, provider: this._provider }
162
+ }
163
+
178
164
  /**
179
165
  * Returns the current fee rates.
180
166
  *