@tetherto/wdk-wallet-evm 1.0.0-beta.2 → 1.0.0-beta.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.
- package/AGENTS.md +50 -0
- package/README.md +171 -218
- package/bare.js +3 -3
- package/{hardhat.config.cjs → hardhat.config.js} +4 -3
- package/index.js +24 -1
- package/package.json +31 -14
- package/src/memory-safe/hd-node-wallet.js +5 -4
- package/src/memory-safe/signing-key.js +4 -3
- package/src/multicall.js +75 -0
- package/src/signers/bare.js +20 -0
- package/src/signers/index.js +18 -0
- package/src/signers/private-key-signer-evm.js +166 -0
- package/src/signers/seed-signer-evm.js +185 -0
- package/src/signers/signer-evm.js +69 -0
- package/src/wallet-account-evm.js +307 -68
- package/src/wallet-account-read-only-evm.js +509 -33
- package/src/wallet-manager-evm.js +142 -40
- package/types/index.d.ts +20 -1
- package/types/src/signers/index.d.ts +4 -0
- package/types/src/signers/private-key-signer-evm.d.ts +106 -0
- package/types/src/signers/seed-signer-evm.d.ts +126 -0
- package/types/src/signers/signer-evm.d.ts +41 -0
- package/types/src/wallet-account-evm.d.ts +166 -32
- package/types/src/wallet-account-read-only-evm.d.ts +260 -14
- package/types/src/wallet-manager-evm.d.ts +79 -17
|
@@ -14,41 +14,101 @@
|
|
|
14
14
|
|
|
15
15
|
'use strict'
|
|
16
16
|
|
|
17
|
-
import { WalletAccountReadOnly } from '@tetherto/wdk-wallet'
|
|
17
|
+
import { WalletAccountReadOnly, NoSuchElementError, ProviderRequiredError, ValueError } from '@tetherto/wdk-wallet'
|
|
18
18
|
|
|
19
|
-
import { BrowserProvider, Contract, JsonRpcProvider } from 'ethers'
|
|
19
|
+
import { BrowserProvider, Contract, Interface, isError, isHexString, JsonRpcProvider, Network, Signature, toQuantity, verifyMessage, verifyTypedData } from 'ethers'
|
|
20
|
+
|
|
21
|
+
import { multicall } from './multicall.js'
|
|
22
|
+
|
|
23
|
+
import FailoverProvider from '@tetherto/wdk-failover-provider'
|
|
20
24
|
|
|
21
25
|
/** @typedef {import('ethers').Provider} Provider */
|
|
22
26
|
/** @typedef {import('ethers').Eip1193Provider} Eip1193Provider */
|
|
27
|
+
/** @typedef {import('ethers').TypedDataDomain} TypedDataDomain */
|
|
28
|
+
/** @typedef {import('ethers').TypedDataField} TypedDataField */
|
|
29
|
+
/** @typedef {import('ethers').AuthorizationLike} AuthorizationLike */
|
|
30
|
+
/** @typedef {import('ethers').BlobLike} BlobLike */
|
|
23
31
|
/** @typedef {import('ethers').TransactionReceipt} EvmTransactionReceipt */
|
|
32
|
+
/** @typedef {import('ethers').TransactionResponse} EvmTransactionResponse */
|
|
33
|
+
/** @typedef {import('ethers').TransactionRequest} EvmTransactionRequest */
|
|
24
34
|
|
|
25
35
|
/** @typedef {import('@tetherto/wdk-wallet').TransactionResult} TransactionResult */
|
|
26
|
-
/** @typedef {import('@tetherto/wdk-wallet').TransferOptions} TransferOptions */
|
|
27
36
|
/** @typedef {import('@tetherto/wdk-wallet').TransferResult} TransferResult */
|
|
37
|
+
/** @typedef {import('@tetherto/wdk-wallet').TransactionReceipt} TransactionReceipt */
|
|
38
|
+
/** @typedef {import('@tetherto/wdk-wallet').WaitForTransactionOptions} WaitForTransactionOptions */
|
|
39
|
+
/** @typedef {import('@tetherto/wdk-wallet').TransferOptions} TransferOptions */
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The EVM-specific fields added to a normalized transaction receipt.
|
|
43
|
+
*
|
|
44
|
+
* @typedef {Object} EvmTransactionDetails
|
|
45
|
+
* @property {number} confirmations - The number of confirmations (0 while pending or dropped).
|
|
46
|
+
* @property {EvmTransactionReceipt | null} receipt - The native ethers receipt, or null while the transaction is pending or dropped.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @typedef {Object} TypedData
|
|
51
|
+
* @property {TypedDataDomain} domain - The domain separator.
|
|
52
|
+
* @property {Record<string, TypedDataField[]>} types - The type definitions.
|
|
53
|
+
* @property {Record<string, unknown>} message - The message data.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @typedef {Object} DelegationInfo
|
|
58
|
+
* @property {boolean} isDelegated - Whether the account has an active ERC-7702 delegation.
|
|
59
|
+
* @property {string | null} delegateAddress - The address of the delegate contract, or null if not delegated.
|
|
60
|
+
*/
|
|
28
61
|
|
|
29
62
|
/**
|
|
30
63
|
* @typedef {Object} EvmTransaction
|
|
31
|
-
* @property {string} to - The transaction's recipient.
|
|
64
|
+
* @property {string | null} [to] - The transaction's recipient. Omit or pass null to deploy a contract.
|
|
32
65
|
* @property {number | bigint} value - The amount of ethers to send to the recipient (in weis).
|
|
33
66
|
* @property {string} [data] - The transaction's data in hex format.
|
|
34
67
|
* @property {number | bigint} [gasLimit] - The maximum amount of gas this transaction is permitted to use.
|
|
35
68
|
* @property {number | bigint} [gasPrice] - The price (in wei) per unit of gas this transaction will pay.
|
|
36
69
|
* @property {number | bigint} [maxFeePerGas] - The maximum price (in wei) per unit of gas this transaction will pay for the combined [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) block's base fee and this transaction's priority fee.
|
|
37
70
|
* @property {number | bigint} [maxPriorityFeePerGas] - The price (in wei) per unit of gas this transaction will allow in addition to the [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) block's base fee to bribe miners into giving this transaction priority. This is included in the maxFeePerGas, so this will not affect the total maximum cost set with maxFeePerGas.
|
|
71
|
+
* @property {number} [type] - The transaction type (e.g. 4 for ERC-7702).
|
|
72
|
+
* @property {number} [nonce] - The transaction nonce.
|
|
73
|
+
* @property {number | bigint} [chainId] - The chain ID of the network.
|
|
74
|
+
* @property {number | bigint} [maxFeePerBlobGas] - The maximum price (in wei) per unit of blob gas this transaction will pay for [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) blob data. Required for type 3 (blob) transactions.
|
|
75
|
+
* @property {BlobLike[]} [blobs] - The blobs of an [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) type 3 transaction.
|
|
76
|
+
* @property {string[]} [blobVersionedHashes] - The versioned hashes of the blobs of an [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) type 3 transaction.
|
|
77
|
+
* @property {AuthorizationLike[]} [authorizationList] - An optional list of ERC-7702 signed authorizations for type 4 transactions.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The gas and fee fields of an evm transaction that can be set on transfer and approve options.
|
|
82
|
+
*
|
|
83
|
+
* @typedef {Pick<EvmTransaction, 'gasLimit' | 'gasPrice' | 'maxFeePerGas' | 'maxPriorityFeePerGas'>} EvmGasOverrides
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The options of a token transfer, extended with the optional gas overrides and ERC-7702 authorizations of an evm
|
|
88
|
+
* transaction.
|
|
89
|
+
*
|
|
90
|
+
* @typedef {TransferOptions & EvmGasOverrides & Pick<EvmTransaction, 'authorizationList'>} EvmTransferOptions
|
|
38
91
|
*/
|
|
39
92
|
|
|
40
93
|
/**
|
|
41
94
|
* @typedef {Object} EvmWalletConfig
|
|
42
|
-
* @property {string | Eip1193Provider} [provider] - The url of the rpc provider, or an instance of a class that implements eip-1193.
|
|
95
|
+
* @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.
|
|
96
|
+
* @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.
|
|
97
|
+
* @property {number} [chainId] - The chain ID of the network. When provided, skips automatic chain ID detection from the provider.
|
|
43
98
|
* @property {number | bigint} [transferMaxFee] - The maximum fee amount for transfer operations.
|
|
99
|
+
* @property {number | bigint} [transactionMaxFee] - The maximum fee amount for sendTransaction and signTransaction operations.
|
|
44
100
|
*/
|
|
45
101
|
|
|
102
|
+
const DELEGATION_DESIGNATOR_PREFIX = '0xef0100'
|
|
103
|
+
|
|
104
|
+
const DELEGATION_DESIGNATOR_LENGTH = 48
|
|
105
|
+
|
|
46
106
|
export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
47
107
|
/**
|
|
48
108
|
* Creates a new evm read-only wallet account.
|
|
49
109
|
*
|
|
50
110
|
* @param {string} address - The account's address.
|
|
51
|
-
* @param {Omit<EvmWalletConfig, 'transferMaxFee'>} [config] - The configuration object.
|
|
111
|
+
* @param {Omit<EvmWalletConfig, 'transferMaxFee' | 'transactionMaxFee'>} [config] - The configuration object.
|
|
52
112
|
*/
|
|
53
113
|
constructor (address, config = { }) {
|
|
54
114
|
super(address)
|
|
@@ -57,33 +117,100 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
57
117
|
* The read-only wallet account configuration.
|
|
58
118
|
*
|
|
59
119
|
* @protected
|
|
60
|
-
* @type {Omit<EvmWalletConfig, 'transferMaxFee'>}
|
|
120
|
+
* @type {Omit<EvmWalletConfig, 'transferMaxFee' | 'transactionMaxFee'>}
|
|
61
121
|
*/
|
|
62
122
|
this._config = config
|
|
63
123
|
|
|
64
|
-
|
|
124
|
+
/**
|
|
125
|
+
* An ethers provider to interact with a node of the blockchain.
|
|
126
|
+
*
|
|
127
|
+
* @protected
|
|
128
|
+
* @type {Provider | undefined}
|
|
129
|
+
*/
|
|
130
|
+
this._provider = WalletAccountReadOnlyEvm._buildProvider(config)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Whether a value is an EIP-1193 provider (e.g. a browser wallet).
|
|
135
|
+
*
|
|
136
|
+
* @protected
|
|
137
|
+
* @param {string | Eip1193Provider | Provider} value - The value to inspect.
|
|
138
|
+
* @returns {boolean} True if the value is an EIP-1193 provider.
|
|
139
|
+
*/
|
|
140
|
+
static _isEip1193Provider (value) {
|
|
141
|
+
return typeof value.request === 'function'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Builds an ethers provider from the wallet configuration:
|
|
146
|
+
* - a url string -> a new `JsonRpcProvider`
|
|
147
|
+
* - an already-built ethers provider (or failover wrapper) -> reused as-is
|
|
148
|
+
* - anything else (EIP-1193 / browser wallet) -> wrapped in a `BrowserProvider`
|
|
149
|
+
* - an array of the above -> a `FailoverProvider` across each entry
|
|
150
|
+
*
|
|
151
|
+
* @protected
|
|
152
|
+
* @param {Omit<EvmWalletConfig, 'transferMaxFee' | 'transactionMaxFee'>} [config] - The configuration object.
|
|
153
|
+
* @returns {Provider | undefined} The provider, or undefined if none is configured.
|
|
154
|
+
*/
|
|
155
|
+
static _buildProvider (config = {}) {
|
|
156
|
+
const { provider, retries = 3 } = config
|
|
157
|
+
const network = config.chainId ? Network.from(config.chainId) : undefined
|
|
158
|
+
const providerOpts = config.chainId ? { staticNetwork: true } : undefined
|
|
159
|
+
|
|
160
|
+
const toOption = (entry) => {
|
|
161
|
+
if (typeof entry === 'string') {
|
|
162
|
+
return new JsonRpcProvider(entry, network, providerOpts)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (WalletAccountReadOnlyEvm._isEip1193Provider(entry)) {
|
|
166
|
+
return new BrowserProvider(entry)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return entry
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (Array.isArray(provider)) {
|
|
173
|
+
if (provider.length === 0) {
|
|
174
|
+
return undefined
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const failoverProvider = new FailoverProvider({ retries })
|
|
178
|
+
|
|
179
|
+
for (const entry of provider) {
|
|
180
|
+
failoverProvider.addProvider(toOption(entry))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return failoverProvider.initialize()
|
|
184
|
+
}
|
|
65
185
|
|
|
66
186
|
if (provider) {
|
|
67
|
-
|
|
68
|
-
* An ethers provider to interact with a node of the blockchain.
|
|
69
|
-
*
|
|
70
|
-
* @protected
|
|
71
|
-
* @type {Provider | undefined}
|
|
72
|
-
*/
|
|
73
|
-
this._provider = typeof provider === 'string'
|
|
74
|
-
? new JsonRpcProvider(provider)
|
|
75
|
-
: new BrowserProvider(provider)
|
|
187
|
+
return toOption(provider)
|
|
76
188
|
}
|
|
189
|
+
|
|
190
|
+
return undefined
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The account's address, or undefined if the account's signer doesn't expose its address
|
|
195
|
+
* synchronously.
|
|
196
|
+
*
|
|
197
|
+
* @deprecated Use {@link getAddress} instead. This property will be removed in an upcoming
|
|
198
|
+
* release: not all signers (e.g. hardware signers) can expose the address synchronously.
|
|
199
|
+
* @type {string | undefined}
|
|
200
|
+
*/
|
|
201
|
+
get address () {
|
|
202
|
+
return this._address
|
|
77
203
|
}
|
|
78
204
|
|
|
79
205
|
/**
|
|
80
206
|
* Returns the account's eth balance.
|
|
81
207
|
*
|
|
82
208
|
* @returns {Promise<bigint>} The eth balance (in weis).
|
|
209
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
83
210
|
*/
|
|
84
211
|
async getBalance () {
|
|
85
212
|
if (!this._provider) {
|
|
86
|
-
throw new
|
|
213
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to retrieve balances.')
|
|
87
214
|
}
|
|
88
215
|
|
|
89
216
|
const address = await this.getAddress()
|
|
@@ -98,10 +225,11 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
98
225
|
*
|
|
99
226
|
* @param {string} tokenAddress - The smart contract address of the token.
|
|
100
227
|
* @returns {Promise<bigint>} The token balance (in base unit).
|
|
228
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
101
229
|
*/
|
|
102
230
|
async getTokenBalance (tokenAddress) {
|
|
103
231
|
if (!this._provider) {
|
|
104
|
-
throw new
|
|
232
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to retrieve token balances.')
|
|
105
233
|
}
|
|
106
234
|
|
|
107
235
|
const address = await this.getAddress()
|
|
@@ -113,36 +241,118 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
113
241
|
return balance
|
|
114
242
|
}
|
|
115
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Returns the account balances for multiple tokens.
|
|
246
|
+
*
|
|
247
|
+
* @param {string[]} tokenAddresses - The smart contract addresses of the tokens.
|
|
248
|
+
* @returns {Promise<Record<string, bigint>>} A mapping of token addresses to their balances (in base units).
|
|
249
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
250
|
+
*/
|
|
251
|
+
async getTokenBalances (tokenAddresses) {
|
|
252
|
+
if (!this._provider) {
|
|
253
|
+
throw new ProviderRequiredError(
|
|
254
|
+
'The wallet must be connected to a provider to retrieve token balances.'
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (tokenAddresses.length === 0) {
|
|
259
|
+
return {}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const address = await this.getAddress()
|
|
263
|
+
const iface = new Interface(['function balanceOf(address owner) view returns (uint256)'])
|
|
264
|
+
const calldata = iface.encodeFunctionData('balanceOf', [address])
|
|
265
|
+
|
|
266
|
+
const calls = tokenAddresses.map(tokenAddress => ({
|
|
267
|
+
to: tokenAddress,
|
|
268
|
+
data: calldata
|
|
269
|
+
}))
|
|
270
|
+
|
|
271
|
+
const results = await multicall(this._provider, calls)
|
|
272
|
+
|
|
273
|
+
return tokenAddresses.reduce((acc, tokenAddress, index) => {
|
|
274
|
+
const result = results[index]
|
|
275
|
+
acc[tokenAddress] = result.status
|
|
276
|
+
? iface.decodeFunctionResult('balanceOf', result.data)[0]
|
|
277
|
+
: 0n
|
|
278
|
+
return acc
|
|
279
|
+
}, {})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Validates that a transaction does not mix fee fields its type doesn't support.
|
|
284
|
+
*
|
|
285
|
+
* @protected
|
|
286
|
+
* @param {EvmTransaction} tx - The transaction to validate.
|
|
287
|
+
* @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
|
|
288
|
+
*/
|
|
289
|
+
static _validateFeeFields (tx) {
|
|
290
|
+
const has1559 = tx.maxFeePerGas !== undefined || tx.maxPriorityFeePerGas !== undefined
|
|
291
|
+
const hasLegacy = tx.gasPrice !== undefined
|
|
292
|
+
const hasBlobs = tx.blobs !== undefined || tx.blobVersionedHashes !== undefined || tx.maxFeePerBlobGas !== undefined
|
|
293
|
+
const hasAuthList = tx.authorizationList !== undefined
|
|
294
|
+
const explicitType = (tx.type !== undefined) ? Number(tx.type) : null
|
|
295
|
+
|
|
296
|
+
if ((explicitType === 2 || (explicitType === null && has1559)) && hasLegacy) {
|
|
297
|
+
throw new ValueError('eip-1559 transaction does not support gasPrice')
|
|
298
|
+
}
|
|
299
|
+
if ((explicitType === 0 || explicitType === 1) && has1559) {
|
|
300
|
+
throw new ValueError('pre-eip-1559 transaction does not support maxFeePerGas/maxPriorityFeePerGas')
|
|
301
|
+
}
|
|
302
|
+
if ((explicitType === 3 || hasBlobs) && hasLegacy) {
|
|
303
|
+
throw new ValueError('blob transaction does not support gasPrice')
|
|
304
|
+
}
|
|
305
|
+
if ((explicitType === 3 || hasBlobs) && tx.maxFeePerBlobGas === undefined) {
|
|
306
|
+
throw new ValueError('maxFeePerBlobGas is required for type 3 transactions')
|
|
307
|
+
}
|
|
308
|
+
if ((explicitType === 4 || (explicitType === null && hasAuthList)) && hasLegacy) {
|
|
309
|
+
throw new ValueError('eip-7702 transaction does not support gasPrice')
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
116
313
|
/**
|
|
117
314
|
* Quotes the costs of a send transaction operation.
|
|
118
315
|
*
|
|
316
|
+
* The transaction is always simulated through gas estimation, so one that would revert is rejected here instead of
|
|
317
|
+
* reaching the signer. A `gasLimit` set on the transaction replaces the estimated gas in the quote, and a `maxFeePerGas`
|
|
318
|
+
* (or `gasPrice`) set on it replaces the fee rate fetched from the provider, so the quote is the transaction's maximum
|
|
319
|
+
* cost as it will be sent.
|
|
320
|
+
*
|
|
119
321
|
* @param {EvmTransaction} tx - The transaction.
|
|
120
322
|
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
323
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
324
|
+
* @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
|
|
325
|
+
* @throws {Error} If the simulation of the transaction reverts, as an ethers error with code `CALL_EXCEPTION`.
|
|
121
326
|
*/
|
|
122
327
|
async quoteSendTransaction (tx) {
|
|
123
328
|
if (!this._provider) {
|
|
124
|
-
throw new
|
|
329
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to quote send transaction operations.')
|
|
125
330
|
}
|
|
126
331
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
332
|
+
WalletAccountReadOnlyEvm._validateFeeFields(tx)
|
|
333
|
+
|
|
334
|
+
const from = await this.getAddress()
|
|
335
|
+
|
|
336
|
+
const estimatedGas = await this._estimateGas({ from, ...tx })
|
|
131
337
|
|
|
132
|
-
const
|
|
338
|
+
const gas = tx.gasLimit ?? estimatedGas
|
|
339
|
+
const feeRate = tx.maxFeePerGas ?? tx.gasPrice ?? await this._getFeeRate()
|
|
133
340
|
|
|
134
|
-
return { fee: gas *
|
|
341
|
+
return { fee: BigInt(gas) * BigInt(feeRate) }
|
|
135
342
|
}
|
|
136
343
|
|
|
137
344
|
/**
|
|
138
345
|
* Quotes the costs of a transfer operation.
|
|
139
346
|
*
|
|
140
|
-
* @param {
|
|
347
|
+
* @param {EvmTransferOptions} options - The transfer's options, including any gas overrides to carry onto the transaction.
|
|
141
348
|
* @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
|
|
349
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
350
|
+
* @throws {ValueError} If the options mix gas override fields that the transaction's type doesn't support.
|
|
351
|
+
* @throws {Error} If the simulation of the transfer reverts, as an ethers error with code `CALL_EXCEPTION`.
|
|
142
352
|
*/
|
|
143
353
|
async quoteTransfer (options) {
|
|
144
354
|
if (!this._provider) {
|
|
145
|
-
throw new
|
|
355
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to quote transfer operations.')
|
|
146
356
|
}
|
|
147
357
|
|
|
148
358
|
const tx = await WalletAccountReadOnlyEvm._getTransferTransaction(options)
|
|
@@ -155,26 +365,250 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
155
365
|
/**
|
|
156
366
|
* Returns a transaction's receipt.
|
|
157
367
|
*
|
|
368
|
+
* @deprecated Use {@link getTransaction} instead, which returns a normalized, finality-based receipt. The raw ethers receipt remains available on its `receipt` property.
|
|
158
369
|
* @param {string} hash - The transaction's hash.
|
|
159
370
|
* @returns {Promise<EvmTransactionReceipt | null>} – The receipt, or null if the transaction has not been included in a block yet.
|
|
371
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
160
372
|
*/
|
|
161
373
|
async getTransactionReceipt (hash) {
|
|
162
374
|
if (!this._provider) {
|
|
163
|
-
throw new
|
|
375
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to fetch transaction receipts.')
|
|
164
376
|
}
|
|
165
377
|
|
|
166
378
|
return await this._provider.getTransactionReceipt(hash)
|
|
167
379
|
}
|
|
168
380
|
|
|
381
|
+
/**
|
|
382
|
+
* Returns a normalized, finality-based receipt for a transaction.
|
|
383
|
+
*
|
|
384
|
+
* @param {string} hash - The transaction's hash.
|
|
385
|
+
* @returns {Promise<TransactionReceipt & EvmTransactionDetails>} The normalized receipt.
|
|
386
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
387
|
+
* @throws {ValueError} If the hash is not a valid transaction hash.
|
|
388
|
+
* @throws {NoSuchElementError} If no transaction has been found for the given hash.
|
|
389
|
+
*/
|
|
390
|
+
async getTransaction (hash) {
|
|
391
|
+
if (!this._provider) {
|
|
392
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to fetch transactions.')
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (!isHexString(hash, 32)) {
|
|
396
|
+
throw new ValueError(`Invalid transaction hash: '${hash}'.`)
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const [transaction, receipt] = await Promise.all([
|
|
400
|
+
this._provider.getTransaction(hash),
|
|
401
|
+
this._provider.getTransactionReceipt(hash)
|
|
402
|
+
])
|
|
403
|
+
|
|
404
|
+
if (!transaction && !receipt) {
|
|
405
|
+
throw new NoSuchElementError(`No transaction found for '${hash}'.`)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (!receipt) {
|
|
409
|
+
const dropped = transaction
|
|
410
|
+
? await this._isReplaced(transaction)
|
|
411
|
+
: false
|
|
412
|
+
|
|
413
|
+
return {
|
|
414
|
+
hash,
|
|
415
|
+
finality: dropped ? 'dropped' : 'pending',
|
|
416
|
+
confirmations: 0,
|
|
417
|
+
receipt: null
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const confirmations = await receipt.confirmations()
|
|
422
|
+
const finalized = await this._isFinalized(receipt.blockNumber)
|
|
423
|
+
|
|
424
|
+
return {
|
|
425
|
+
hash,
|
|
426
|
+
finality: finalized ? 'final' : 'confirmed',
|
|
427
|
+
success: receipt.status === 1,
|
|
428
|
+
block: receipt.blockNumber,
|
|
429
|
+
fee: receipt.fee,
|
|
430
|
+
confirmations,
|
|
431
|
+
receipt
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Blocks until a transaction reaches a terminal state (the requested finality target or `dropped`), or times out.
|
|
437
|
+
*
|
|
438
|
+
* @param {string} hash - The transaction's hash.
|
|
439
|
+
* @param {WaitForTransactionOptions} [options] - The wait options.
|
|
440
|
+
* @returns {Promise<TransactionReceipt & EvmTransactionDetails>} The terminal receipt: the finality target reached (inspect `success` to tell success from revert), or `dropped`.
|
|
441
|
+
* @throws {TimeoutError} If the target is not reached before the timeout.
|
|
442
|
+
*/
|
|
443
|
+
async waitForTransaction (hash, options = {}) {
|
|
444
|
+
return await super.waitForTransaction(hash, options)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Returns whether a block is at or below the chain's `finalized` block. Chains that don't support the tag are treated as not finalized.
|
|
449
|
+
*
|
|
450
|
+
* @protected
|
|
451
|
+
* @param {number} blockNumber - The block number to check.
|
|
452
|
+
* @returns {Promise<boolean>} True if the block is finalized.
|
|
453
|
+
*/
|
|
454
|
+
async _isFinalized (blockNumber) {
|
|
455
|
+
try {
|
|
456
|
+
const finalizedBlock = await this._provider.getBlock('finalized')
|
|
457
|
+
return !!finalizedBlock && blockNumber <= finalizedBlock.number
|
|
458
|
+
} catch (error) {
|
|
459
|
+
// Treat an unsupported 'finalized' block tag as not-finalized; let real failures propagate.
|
|
460
|
+
const rpcCode = error?.info?.error?.code
|
|
461
|
+
|
|
462
|
+
if (isError(error, 'UNSUPPORTED_OPERATION') || rpcCode === -32601 || rpcCode === -32602) {
|
|
463
|
+
return false
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
throw error
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Returns whether an unmined transaction has been replaced, i.e. its sender's mined nonce has already advanced past the transaction's nonce.
|
|
472
|
+
*
|
|
473
|
+
* @protected
|
|
474
|
+
* @param {EvmTransactionResponse} transaction - The unmined transaction.
|
|
475
|
+
* @returns {Promise<boolean>} True if the transaction's nonce slot is already taken.
|
|
476
|
+
*/
|
|
477
|
+
async _isReplaced (transaction) {
|
|
478
|
+
const accountNonce = await this._provider.getTransactionCount(transaction.from, 'latest')
|
|
479
|
+
return accountNonce > transaction.nonce
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Overrides the base default to allow for slower EVM inclusion and confirmation.
|
|
484
|
+
*
|
|
485
|
+
* @type {number}
|
|
486
|
+
*/
|
|
487
|
+
get defaultWaitTimeout () {
|
|
488
|
+
return 120000
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Returns the current allowance for the given token and spender.
|
|
493
|
+
* @param {string} token The token's address.
|
|
494
|
+
* @param {string} spender The spender's address.
|
|
495
|
+
* @returns {Promise<bigint>} The allowance.
|
|
496
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
497
|
+
*/
|
|
498
|
+
async getAllowance (token, spender) {
|
|
499
|
+
if (!this._provider) {
|
|
500
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to retrieve allowances.')
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const address = await this.getAddress()
|
|
504
|
+
const abi = ['function allowance(address owner, address spender) view returns (uint256)']
|
|
505
|
+
const contract = new Contract(token, abi, this._provider)
|
|
506
|
+
const allowance = await contract.allowance(address, spender)
|
|
507
|
+
return allowance
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Verifies a message's signature.
|
|
512
|
+
*
|
|
513
|
+
* @param {string} message - The original message.
|
|
514
|
+
* @param {string} signature - The signature to verify.
|
|
515
|
+
* @returns {Promise<boolean>} True if the signature is valid.
|
|
516
|
+
*/
|
|
517
|
+
async verify (message, signature) {
|
|
518
|
+
const address = await verifyMessage(message, signature)
|
|
519
|
+
const accountAddress = await this.getAddress()
|
|
520
|
+
|
|
521
|
+
return address.toLowerCase() === accountAddress.toLowerCase()
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Verifies a typed data signature.
|
|
526
|
+
*
|
|
527
|
+
* @param {TypedData} typedData - The typed data to verify.
|
|
528
|
+
* @param {string} signature - The signature to verify.
|
|
529
|
+
* @returns {Promise<boolean>} True if the signature is valid.
|
|
530
|
+
*/
|
|
531
|
+
async verifyTypedData (typedData, signature) {
|
|
532
|
+
const { domain, types, message } = typedData
|
|
533
|
+
const address = verifyTypedData(domain, types, message, signature)
|
|
534
|
+
const accountAddress = await this.getAddress()
|
|
535
|
+
|
|
536
|
+
return address.toLowerCase() === accountAddress.toLowerCase()
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Checks if this account has an active ERC-7702 delegation.
|
|
541
|
+
*
|
|
542
|
+
* @returns {Promise<DelegationInfo>} The delegation info.
|
|
543
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
544
|
+
*/
|
|
545
|
+
async getDelegation () {
|
|
546
|
+
if (!this._provider) {
|
|
547
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to check delegation.')
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const address = await this.getAddress()
|
|
551
|
+
const code = await this._provider.send('eth_getCode', [address, 'latest'])
|
|
552
|
+
|
|
553
|
+
if (
|
|
554
|
+
code &&
|
|
555
|
+
code.toLowerCase().startsWith(DELEGATION_DESIGNATOR_PREFIX) &&
|
|
556
|
+
code.length === DELEGATION_DESIGNATOR_LENGTH
|
|
557
|
+
) {
|
|
558
|
+
const delegateAddress = '0x' + code.slice(DELEGATION_DESIGNATOR_PREFIX.length)
|
|
559
|
+
|
|
560
|
+
return {
|
|
561
|
+
isDelegated: true,
|
|
562
|
+
delegateAddress
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
return {
|
|
567
|
+
isDelegated: false,
|
|
568
|
+
delegateAddress: null
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Estimates the gas of a transaction by simulating it against the connected provider, using the authorization-list
|
|
574
|
+
* aware estimation for ERC-7702 transactions.
|
|
575
|
+
*
|
|
576
|
+
* @protected
|
|
577
|
+
* @param {EvmTransactionRequest} tx - The transaction to simulate, including its `from` address.
|
|
578
|
+
* @returns {Promise<bigint>} The gas units the simulated transaction consumed.
|
|
579
|
+
*/
|
|
580
|
+
async _estimateGas (tx) {
|
|
581
|
+
return tx.authorizationList
|
|
582
|
+
? await this._estimateGasWithAuthList(tx)
|
|
583
|
+
: await this._provider.estimateGas(tx)
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Extracts the gas and fee overrides set on transfer or approve options.
|
|
588
|
+
*
|
|
589
|
+
* @protected
|
|
590
|
+
* @param {EvmGasOverrides} options - The options to read the overrides from.
|
|
591
|
+
* @returns {EvmGasOverrides} Only the gas and fee fields that are set on the options.
|
|
592
|
+
*/
|
|
593
|
+
static _getGasOverrides (options) {
|
|
594
|
+
const overrides = {}
|
|
595
|
+
|
|
596
|
+
for (const field of ['gasLimit', 'gasPrice', 'maxFeePerGas', 'maxPriorityFeePerGas']) {
|
|
597
|
+
if (options[field] !== undefined) overrides[field] = options[field]
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return overrides
|
|
601
|
+
}
|
|
602
|
+
|
|
169
603
|
/**
|
|
170
604
|
* Returns an evm transaction to execute the given token transfer.
|
|
171
605
|
*
|
|
172
606
|
* @protected
|
|
173
|
-
* @param {
|
|
174
|
-
* @returns {Promise<EvmTransaction>} The evm transaction.
|
|
607
|
+
* @param {EvmTransferOptions} options - The transfer's options, including any gas overrides and ERC-7702 authorizations to carry onto the transaction.
|
|
608
|
+
* @returns {Promise<EvmTransaction>} The ERC-20 transfer call as an evm transaction, with the options' gas overrides and authorizations applied.
|
|
175
609
|
*/
|
|
176
610
|
static async _getTransferTransaction (options) {
|
|
177
|
-
const { token, recipient, amount } = options
|
|
611
|
+
const { token, recipient, amount, authorizationList } = options
|
|
178
612
|
|
|
179
613
|
const abi = ['function transfer(address to, uint256 amount) returns (bool)']
|
|
180
614
|
|
|
@@ -183,9 +617,51 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
183
617
|
const tx = {
|
|
184
618
|
to: token,
|
|
185
619
|
value: 0,
|
|
186
|
-
data: contract.interface.encodeFunctionData('transfer', [recipient, amount])
|
|
620
|
+
data: contract.interface.encodeFunctionData('transfer', [recipient, amount]),
|
|
621
|
+
...WalletAccountReadOnlyEvm._getGasOverrides(options),
|
|
622
|
+
authorizationList
|
|
187
623
|
}
|
|
188
624
|
|
|
189
625
|
return tx
|
|
190
626
|
}
|
|
627
|
+
|
|
628
|
+
/** @private */
|
|
629
|
+
async _estimateGasWithAuthList ({ from, to, value, data, authorizationList }) {
|
|
630
|
+
const formatAuth = (auth) => {
|
|
631
|
+
const { address, nonce, chainId } = auth
|
|
632
|
+
|
|
633
|
+
const signature = auth.signature instanceof Signature
|
|
634
|
+
? auth.signature
|
|
635
|
+
: Signature.from(auth.signature)
|
|
636
|
+
|
|
637
|
+
return {
|
|
638
|
+
address,
|
|
639
|
+
nonce: toQuantity(nonce),
|
|
640
|
+
chainId: toQuantity(chainId),
|
|
641
|
+
r: toQuantity(signature.r),
|
|
642
|
+
s: toQuantity(signature.s),
|
|
643
|
+
yParity: toQuantity(signature.yParity)
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const rpcTx = {
|
|
648
|
+
from,
|
|
649
|
+
to,
|
|
650
|
+
value: toQuantity(value),
|
|
651
|
+
data: data ?? '0x',
|
|
652
|
+
type: '0x04',
|
|
653
|
+
authorizationList: authorizationList.map(formatAuth)
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
const result = await this._provider.send('eth_estimateGas', [rpcTx])
|
|
657
|
+
|
|
658
|
+
return BigInt(result)
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** @private */
|
|
662
|
+
async _getFeeRate () {
|
|
663
|
+
const { maxFeePerGas, gasPrice } = await this._provider.getFeeData()
|
|
664
|
+
|
|
665
|
+
return maxFeePerGas || gasPrice
|
|
666
|
+
}
|
|
191
667
|
}
|