@tetherto/wdk-wallet-evm-erc-4337 1.0.0-beta.13 → 1.0.0-beta.15
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/README.md +27 -0
- package/index.js +1 -0
- package/package.json +2 -2
- package/src/wallet-account-evm-erc-4337.js +41 -116
- package/src/wallet-account-read-only-evm-erc-4337.js +35 -12
- package/types/index.d.ts +1 -0
- package/types/src/wallet-account-evm-erc-4337.d.ts +1 -13
- package/types/src/wallet-account-read-only-evm-erc-4337.d.ts +31 -12
package/README.md
CHANGED
|
@@ -53,8 +53,35 @@ wallet.dispose()
|
|
|
53
53
|
- **ERC-20 Token Support**: Query balances and transfer tokens via UserOperations
|
|
54
54
|
- **Message Signing**: Sign and verify messages with the underlying EOA key
|
|
55
55
|
- **Per-Call Config Overrides**: Switch gas payment modes on a per-transaction basis
|
|
56
|
+
- **Parallel Nonce Lanes**: Send independent operations concurrently via ERC-4337 two-dimensional nonces (`parallel: true` or a reusable `nonceKey`)
|
|
56
57
|
- **Secure Memory Disposal**: Clear private keys from memory when done
|
|
57
58
|
|
|
59
|
+
## Concurrent / Parallel Sends (nonce lanes)
|
|
60
|
+
|
|
61
|
+
By default every send uses the account's key-0 nonce, so it is sequential — a second send fired before the first is mined would collide on the nonce. To send independent operations concurrently, put each in its own **nonce lane** (an ERC-4337 two-dimensional nonce: a 192-bit `key` with its own sequence). Ops in different keys have no ordering constraint and validate independently, so they can be submitted at the same time.
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
// parallel: true → each send gets a fresh random lane (sequence 0). Fire them together:
|
|
65
|
+
const [a, b] = await Promise.all([
|
|
66
|
+
account.sendTransaction({ to: '0x...', value: 0n }, { parallel: true }),
|
|
67
|
+
account.sendTransaction({ to: '0x...', value: 0n }, { parallel: true })
|
|
68
|
+
])
|
|
69
|
+
|
|
70
|
+
// nonceKey: a string is a reusable named lane (same label resumes the same lane across sessions);
|
|
71
|
+
// a bigint is used as the raw uint192 key. Same key = ordered; different keys = parallel.
|
|
72
|
+
await account.sendTransaction(tx, { nonceKey: 'payments' })
|
|
73
|
+
await account.sendTransaction(tx, { nonceKey: 1n })
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Both options can also be set at construction (`new WalletManagerEvmErc4337(seed, { ..., parallel: true })`) and overridden per call. Precedence: `nonceKey` > `parallel` > default (key 0).
|
|
77
|
+
|
|
78
|
+
**Notes:**
|
|
79
|
+
- **Deploy the account first.** A Safe smart account is deployed by its first UserOperation (which carries the init code). Concurrent sends from an **undeployed** account can't each carry the init code, so let one send land (deploying the account) before firing parallel lanes.
|
|
80
|
+
- **Requires a bundler that accepts parallel keys** (Pimlico and Candide both do). There is no SDK-side lane limit; respect your bundler's cap (e.g. Pimlico allows up to 100 parallel).
|
|
81
|
+
- **Per-sender mempool cap (≈4).** Per [ERC-7562](https://eips.ethereum.org/EIPS/eip-7562), a standard bundler mempool holds at most **4** in-flight UserOperations from a single sender at once (`SAME_SENDER_MEMPOOL_COUNT = 4`). Firing more than 4 lanes concurrently may be rejected until earlier ops mine. This is a mempool validation limit, separate from parallel-key support, and can be raised by the bundler operator if needed.
|
|
82
|
+
- `parallel: true` mints a **new** EntryPoint nonce slot per send (a one-time gas cost per lane; permanent state). For repeated parallel workloads, reuse a fixed set of lanes with `nonceKey` labels instead of a fresh key every time.
|
|
83
|
+
- A single lane is still **sequential**. Two concurrent sends sharing the same `nonceKey` collide on the sequence: the second is typically still **accepted by the bundler and returns a hash, yet can never mine** — its hash never resolves to a receipt and no error is thrown (a silent phantom hash). To run operations concurrently, give each its **own lane** (`parallel: true`, or distinct `nonceKey`s). If operations must share a lane, either **await each send's receipt before firing the next**, or **batch them into a single UserOperation** by passing an array to `sendTransaction([tx1, tx2, ...])` — batched calls execute atomically, in order, under one nonce.
|
|
84
|
+
|
|
58
85
|
## Compatibility
|
|
59
86
|
|
|
60
87
|
- **Ethereum Mainnet** and testnets (Sepolia)
|
package/index.js
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
/** @typedef {import('@tetherto/wdk-wallet-evm').KeyPair} KeyPair */
|
|
23
23
|
/** @typedef {import('./src/wallet-account-read-only-evm-erc-4337.js').EvmErc4337Transaction} EvmErc4337Transaction */
|
|
24
24
|
/** @typedef {import('./src/wallet-account-read-only-evm-erc-4337.js').EvmErc4337GasOverrides} EvmErc4337GasOverrides */
|
|
25
|
+
/** @typedef {import('./src/wallet-account-read-only-evm-erc-4337.js').Nonce} Nonce */
|
|
25
26
|
/** @typedef {import('@tetherto/wdk-wallet-evm').TransactionResult} TransactionResult */
|
|
26
27
|
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferOptions} TransferOptions */
|
|
27
28
|
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tetherto/wdk-wallet-evm-erc-4337",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.15",
|
|
4
4
|
"description": "A simple package to manage BIP-32 wallets for evm blockchains, which implement the erc-4337 standard and its account abstraction features",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"wdk",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@pimlico/mock-paymaster": "0.0.4",
|
|
43
43
|
"concurrently": "9.2.0",
|
|
44
44
|
"cross-env": "7.0.3",
|
|
45
|
-
"hardhat": "3.
|
|
45
|
+
"hardhat": "3.12.0",
|
|
46
46
|
"jest": "29.7.0",
|
|
47
47
|
"prool": "0.0.23",
|
|
48
48
|
"standard": "17.1.2",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
'use strict'
|
|
16
16
|
|
|
17
|
-
import { Contract } from 'ethers'
|
|
17
|
+
import { Contract, hexlify, keccak256, randomBytes, toUtf8Bytes } from 'ethers'
|
|
18
18
|
|
|
19
19
|
import { WalletAccountEvm } from '@tetherto/wdk-wallet-evm'
|
|
20
20
|
|
|
@@ -53,26 +53,11 @@ import WalletAccountReadOnlyEvmErc4337, { FEE_TOLERANCE_COEFFICIENT } from './wa
|
|
|
53
53
|
|
|
54
54
|
const QUOTE_MAX_AGE_MS = 2 * 60 * 1_000
|
|
55
55
|
|
|
56
|
-
const
|
|
56
|
+
const NONCE_KEY_SHIFT = 64n
|
|
57
57
|
|
|
58
|
-
const
|
|
58
|
+
const MAX_UINT192 = (1n << 192n) - 1n
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
* Races a promise against a timeout, rejecting if it does not settle in time.
|
|
62
|
-
* The timer is always cleared so a pending timeout never keeps the event loop alive.
|
|
63
|
-
*
|
|
64
|
-
* @template T
|
|
65
|
-
* @param {Promise<T>} promise - The promise to bound.
|
|
66
|
-
* @param {number} ms - The timeout in milliseconds.
|
|
67
|
-
* @returns {Promise<T>} The promise's result, or a rejection if it times out.
|
|
68
|
-
*/
|
|
69
|
-
const withTimeout = (promise, ms) => {
|
|
70
|
-
let timer
|
|
71
|
-
const timeout = new Promise((_resolve, reject) => {
|
|
72
|
-
timer = setTimeout(() => reject(new Error('Timed out reading the on-chain account nonce.')), ms)
|
|
73
|
-
})
|
|
74
|
-
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer))
|
|
75
|
-
}
|
|
60
|
+
const USDT_MAINNET_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
|
|
76
61
|
|
|
77
62
|
/** @implements {IWalletAccount<UserOperationV7>} */
|
|
78
63
|
export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc4337 {
|
|
@@ -106,12 +91,6 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
106
91
|
* @type {Map<string, TransactionQuote>}
|
|
107
92
|
*/
|
|
108
93
|
this._quoteCache = new Map()
|
|
109
|
-
|
|
110
|
-
/** @private */
|
|
111
|
-
this._reservedNonces = new Set()
|
|
112
|
-
|
|
113
|
-
/** @private */
|
|
114
|
-
this._nonceLock = Promise.resolve()
|
|
115
94
|
}
|
|
116
95
|
|
|
117
96
|
/**
|
|
@@ -182,16 +161,16 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
182
161
|
this._validateConfig(mergedConfig)
|
|
183
162
|
}
|
|
184
163
|
|
|
185
|
-
const
|
|
164
|
+
const prepared = await this._prepareForSend(tx, [tx], mergedConfig)
|
|
186
165
|
|
|
187
|
-
const fee =
|
|
166
|
+
const fee = prepared.fee
|
|
188
167
|
|
|
189
168
|
const { isSponsored, transactionMaxFee } = mergedConfig
|
|
190
169
|
if (!isSponsored && transactionMaxFee !== undefined && fee > transactionMaxFee) {
|
|
191
170
|
throw new Error('Exceeded maximum fee cost for transaction operation.')
|
|
192
171
|
}
|
|
193
172
|
|
|
194
|
-
const { userOp } = await this._signUserOperation([tx], { config: mergedConfig, cachedBuild:
|
|
173
|
+
const { userOp } = await this._signUserOperation([tx], { config: mergedConfig, cachedBuild: prepared })
|
|
195
174
|
|
|
196
175
|
this._quoteCache.clear()
|
|
197
176
|
|
|
@@ -303,6 +282,7 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
303
282
|
* @param {Partial<EvmErc4337WalletPaymasterTokenConfig | EvmErc4337WalletSponsorshipPolicyConfig | EvmErc4337WalletNativeCoinsConfig>} [config] - If set, overrides the given configuration options.
|
|
304
283
|
* @returns {Promise<TransactionResult>} The transaction's result.
|
|
305
284
|
* @throws {Error} If the transaction is not sponsored, and the transaction's cost surpasses the transaction max. fee option.
|
|
285
|
+
* @throws {Error} If `nonceKey` is a bigint outside the uint192 range (0 to 2^192 - 1).
|
|
306
286
|
*/
|
|
307
287
|
async sendTransaction (tx, config) {
|
|
308
288
|
const mergedConfig = { ...this._config, ...config }
|
|
@@ -324,17 +304,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
324
304
|
|
|
325
305
|
const { isSponsored, transactionMaxFee } = mergedConfig
|
|
326
306
|
if (!isSponsored && transactionMaxFee !== undefined && prepared.fee > transactionMaxFee) {
|
|
327
|
-
this._releaseNonce(prepared.userOp?.nonce)
|
|
328
307
|
throw new Error('Exceeded maximum fee cost for transaction operation.')
|
|
329
308
|
}
|
|
330
309
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
return { hash, fee: prepared.fee }
|
|
334
|
-
} catch (error) {
|
|
335
|
-
this._maybeReleaseNonceOnRejection(error, prepared.userOp?.nonce)
|
|
336
|
-
throw error
|
|
337
|
-
}
|
|
310
|
+
const hash = await this._sendUserOperation(txs, { config: mergedConfig, cachedBuild: prepared })
|
|
311
|
+
return { hash, fee: prepared.fee }
|
|
338
312
|
}
|
|
339
313
|
|
|
340
314
|
/**
|
|
@@ -347,6 +321,7 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
347
321
|
* @param {EvmErc4337GasOverrides} [txOverrides] - If set, applies these UserOperationV7 gas/fee overrides to the underlying transaction.
|
|
348
322
|
* @returns {Promise<TransferResult>} The transfer's result.
|
|
349
323
|
* @throws {Error} If the transaction is not sponsored, and the transfer's cost surpasses the transfer max. fee option.
|
|
324
|
+
* @throws {Error} If `nonceKey` is a bigint outside the uint192 range (0 to 2^192 - 1).
|
|
350
325
|
*/
|
|
351
326
|
async transfer (options, config, txOverrides) {
|
|
352
327
|
const mergedConfig = { ...this._config, ...config }
|
|
@@ -364,17 +339,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
364
339
|
const prepared = await this._prepareForSend(tx, txs, mergedConfig)
|
|
365
340
|
|
|
366
341
|
if (!isSponsored && transferMaxFee !== undefined && prepared.fee >= transferMaxFee) {
|
|
367
|
-
this._releaseNonce(prepared.userOp?.nonce)
|
|
368
342
|
throw new Error('Exceeded maximum fee cost for transfer operation.')
|
|
369
343
|
}
|
|
370
344
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
return { hash, fee: prepared.fee }
|
|
374
|
-
} catch (error) {
|
|
375
|
-
this._maybeReleaseNonceOnRejection(error, prepared.userOp?.nonce)
|
|
376
|
-
throw error
|
|
377
|
-
}
|
|
345
|
+
const hash = await this._sendUserOperation(txs, { config: mergedConfig, cachedBuild: prepared })
|
|
346
|
+
return { hash, fee: prepared.fee }
|
|
378
347
|
}
|
|
379
348
|
|
|
380
349
|
/**
|
|
@@ -397,43 +366,30 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
397
366
|
this._ownerAccount.dispose()
|
|
398
367
|
}
|
|
399
368
|
|
|
400
|
-
/** @private */
|
|
401
|
-
async _resolveQuote (tx, config) {
|
|
402
|
-
let cached = this._consumeCachedQuote(tx)
|
|
403
|
-
|
|
404
|
-
if (cached?.userOp) {
|
|
405
|
-
const onChainNonce = await fetchAccountNonce(this._provider, cached.smartAccount.entrypointAddress, cached.smartAccount.accountAddress)
|
|
406
|
-
if (cached.userOp.nonce !== onChainNonce) cached = undefined
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
if (!cached) {
|
|
410
|
-
await this.quoteSendTransaction(tx, config)
|
|
411
|
-
cached = this._consumeCachedQuote(tx)
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
return cached
|
|
415
|
-
}
|
|
416
|
-
|
|
417
369
|
/** @private */
|
|
418
370
|
async _prepareForSend (tx, txs, config) {
|
|
419
|
-
const
|
|
371
|
+
const nonce = await this._resolveNonce(config)
|
|
420
372
|
|
|
421
|
-
|
|
373
|
+
if (nonce === undefined) {
|
|
422
374
|
const cached = this._consumeCachedQuote(tx)
|
|
423
|
-
if (cached?.userOp
|
|
424
|
-
|
|
375
|
+
if (cached?.userOp) {
|
|
376
|
+
const onChainNonce = await fetchAccountNonce(this._provider, cached.smartAccount.entrypointAddress, cached.smartAccount.accountAddress)
|
|
377
|
+
if (cached.userOp.nonce === onChainNonce) {
|
|
378
|
+
return cached
|
|
379
|
+
}
|
|
425
380
|
}
|
|
426
|
-
return await this._buildAtNonce(txs, allocatedNonce, config)
|
|
427
|
-
} catch (error) {
|
|
428
|
-
this._releaseNonce(allocatedNonce)
|
|
429
|
-
throw error
|
|
430
381
|
}
|
|
382
|
+
|
|
383
|
+
return await this._buildAtNonce(txs, nonce, config)
|
|
431
384
|
}
|
|
432
385
|
|
|
433
386
|
/** @private */
|
|
434
|
-
async _buildAtNonce (txs,
|
|
387
|
+
async _buildAtNonce (txs, nonce, config) {
|
|
435
388
|
const calls = WalletAccountReadOnlyEvmErc4337._toMetaTransactions(txs)
|
|
436
|
-
const txOverrides = {
|
|
389
|
+
const txOverrides = {
|
|
390
|
+
...WalletAccountReadOnlyEvmErc4337._extractGasOverrides(txs[0]),
|
|
391
|
+
...(nonce !== undefined ? { nonce } : {})
|
|
392
|
+
}
|
|
437
393
|
|
|
438
394
|
const { userOp, smartAccount, chainId, tokenQuote } = await this._buildUserOperation(calls, config, txOverrides)
|
|
439
395
|
|
|
@@ -445,56 +401,25 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
445
401
|
}
|
|
446
402
|
|
|
447
403
|
/** @private */
|
|
448
|
-
async
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
)
|
|
459
|
-
|
|
460
|
-
for (const reserved of this._reservedNonces) {
|
|
461
|
-
if (reserved < onChainNonce) this._reservedNonces.delete(reserved)
|
|
404
|
+
async _resolveNonce (config) {
|
|
405
|
+
if (config.nonceKey !== undefined && config.nonceKey !== null) {
|
|
406
|
+
let key
|
|
407
|
+
if (typeof config.nonceKey === 'string') {
|
|
408
|
+
key = BigInt(keccak256(toUtf8Bytes(config.nonceKey))) & MAX_UINT192
|
|
409
|
+
} else {
|
|
410
|
+
key = BigInt(config.nonceKey)
|
|
411
|
+
if (key < 0n || key > MAX_UINT192) {
|
|
412
|
+
throw new Error('nonceKey must be within the uint192 range (0 to 2^192 - 1).')
|
|
413
|
+
}
|
|
462
414
|
}
|
|
463
|
-
|
|
464
|
-
let candidate = onChainNonce
|
|
465
|
-
while (this._reservedNonces.has(candidate)) candidate += 1n
|
|
466
|
-
this._reservedNonces.add(candidate)
|
|
467
|
-
|
|
468
|
-
return candidate
|
|
469
|
-
} finally {
|
|
470
|
-
release()
|
|
415
|
+
return await fetchAccountNonce(this._provider, ENTRYPOINT_V7, this._address, key)
|
|
471
416
|
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/** @private */
|
|
475
|
-
_releaseNonce (nonce) {
|
|
476
|
-
if (nonce !== undefined && nonce !== null) this._reservedNonces.delete(nonce)
|
|
477
|
-
}
|
|
478
417
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
if (WalletAccountEvmErc4337._isPreAcceptanceError(error)) {
|
|
482
|
-
this._releaseNonce(nonce)
|
|
418
|
+
if (config.parallel) {
|
|
419
|
+
return BigInt(hexlify(randomBytes(24))) << NONCE_KEY_SHIFT
|
|
483
420
|
}
|
|
484
|
-
}
|
|
485
421
|
|
|
486
|
-
|
|
487
|
-
static _isPreAcceptanceError (error) {
|
|
488
|
-
if (error instanceof AbstractionKitError) {
|
|
489
|
-
const message = `${error.message ?? ''} ${error.cause?.message ?? ''}`.toLowerCase()
|
|
490
|
-
return [
|
|
491
|
-
'aa10', 'aa13', 'aa14', 'aa21', 'aa22', 'aa23', 'aa24', 'aa25', 'aa26',
|
|
492
|
-
'aa31', 'aa32', 'aa33', 'aa34', 'aa40', 'aa41', 'aa50', 'aa51',
|
|
493
|
-
'nonce', 'already known', 'replacement underpriced', 'underpriced',
|
|
494
|
-
'fee too low', 'sender already constructed'
|
|
495
|
-
].some(marker => message.includes(marker))
|
|
496
|
-
}
|
|
497
|
-
return typeof error?.message === 'string' && error.message.includes('Not enough funds')
|
|
422
|
+
return undefined
|
|
498
423
|
}
|
|
499
424
|
|
|
500
425
|
/** @private */
|
|
@@ -84,18 +84,27 @@ export const FEE_TOLERANCE_COEFFICIENT = 120n
|
|
|
84
84
|
*/
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
|
-
* Gas-related UserOperationV7 overrides
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* gas price (fee pair) or AbstractionKit's gas estimation (gas limits).
|
|
87
|
+
* Gas-related UserOperationV7 overrides. Numeric fields accept `number` or `bigint`; numbers are
|
|
88
|
+
* coerced to `bigint` by `_extractGasOverrides` before the operation is built. All fields are
|
|
89
|
+
* optional; absent fields fall back to the bundler-fetched gas price (fee pair) or the bundler's
|
|
90
|
+
* gas estimation (gas limits).
|
|
92
91
|
*
|
|
93
92
|
* @typedef {Object} EvmErc4337GasOverrides
|
|
94
|
-
* @property {bigint} [callGasLimit] - Override for the UserOperation's call gas limit.
|
|
95
|
-
* @property {bigint} [verificationGasLimit] - Override for the UserOperation's verification gas limit.
|
|
96
|
-
* @property {bigint} [preVerificationGas] - Override for the UserOperation's pre-verification gas.
|
|
97
|
-
* @property {bigint} [maxFeePerGas] - Override for the UserOperation's max fee per gas (EIP-1559 cap).
|
|
98
|
-
* @property {bigint} [maxPriorityFeePerGas] - Override for the UserOperation's max priority fee per gas.
|
|
93
|
+
* @property {number | bigint} [callGasLimit] - Override for the UserOperation's call gas limit.
|
|
94
|
+
* @property {number | bigint} [verificationGasLimit] - Override for the UserOperation's verification gas limit.
|
|
95
|
+
* @property {number | bigint} [preVerificationGas] - Override for the UserOperation's pre-verification gas.
|
|
96
|
+
* @property {number | bigint} [maxFeePerGas] - Override for the UserOperation's max fee per gas (EIP-1559 cap).
|
|
97
|
+
* @property {number | bigint} [maxPriorityFeePerGas] - Override for the UserOperation's max priority fee per gas.
|
|
98
|
+
*/
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A single explicit UserOperationV7 `nonce`, combined with `EvmErc4337GasOverrides` for the build
|
|
102
|
+
* step to place the operation in a specific two-dimensional nonce lane. The `nonce` is derived
|
|
103
|
+
* internally from the account's `parallel`/`nonceKey` configuration, never from user-supplied
|
|
104
|
+
* transaction fields.
|
|
105
|
+
*
|
|
106
|
+
* @typedef {Object} Nonce
|
|
107
|
+
* @property {number | bigint} [nonce] - Full 256-bit UserOperation nonce (`key << 64 | sequence`) placing the op in a specific lane. Omitted for default (key-0) sends, in which case the account fetches the current on-chain nonce.
|
|
99
108
|
*/
|
|
100
109
|
|
|
101
110
|
/**
|
|
@@ -114,6 +123,8 @@ export const FEE_TOLERANCE_COEFFICIENT = 120n
|
|
|
114
123
|
* @property {string} bundlerUrl - The url of the bundler service.
|
|
115
124
|
* @property {string} safeModulesVersion - Version of the Safe 4337 module set to deploy with the account (e.g. "0.3.0"). Determines the module addresses used in init code.
|
|
116
125
|
* @property {OnChainIdentifier | string} [onChainIdentifier] - Optional on-chain identifier. Appends a 50-byte project marker to every UserOperation callData. Pass a string to reuse it as the project name, or a full object for more control.
|
|
126
|
+
* @property {boolean} [parallel] - When true, each send is placed in a fresh, independent nonce lane (a random 192-bit key at sequence 0) so concurrent or back-to-back sends don't collide on the nonce. Ordering between such sends is not guaranteed and each consumes a new EntryPoint nonce slot. Ignored when `nonceKey` is set. Overridable per call.
|
|
127
|
+
* @property {number | bigint | string} [nonceKey] - Send in an explicit nonce lane. A string is hashed to a deterministic key — a reusable named lane that resumes the same sequence across sessions; a number or bigint is used as the raw uint192 key and must be within the uint192 range (0 to 2^192 - 1), otherwise the send throws (pass a bigint or string for keys above 2^53). Sends sharing a key are ordered sequentially; different keys run in parallel. Overridable per call.
|
|
117
128
|
*/
|
|
118
129
|
|
|
119
130
|
/**
|
|
@@ -305,6 +316,7 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
|
|
|
305
316
|
* @param {Partial<EvmErc4337WalletPaymasterTokenConfig | EvmErc4337WalletSponsorshipPolicyConfig | EvmErc4337WalletNativeCoinsConfig>} [config] - If set, overrides the given configuration options.
|
|
306
317
|
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
307
318
|
* @throws {ConfigurationError} If the override `config` is invalid or has missing required fields.
|
|
319
|
+
* @throws {ConfigurationError} If, in token mode, the configured `paymasterAddress` does not match the paymaster address returned by the paymaster RPC. This guards against the auto-generated ERC-20 approval targeting an unexpected paymaster contract.
|
|
308
320
|
* @throws {Error} If the token paymaster reports AA50 (account does not hold the paymaster token).
|
|
309
321
|
*/
|
|
310
322
|
async quoteSendTransaction (tx, config) {
|
|
@@ -338,6 +350,7 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
|
|
|
338
350
|
* @param {EvmErc4337GasOverrides} [txOverrides] - If set, applies these UserOperationV7 gas/fee overrides to the underlying transaction.
|
|
339
351
|
* @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
|
|
340
352
|
* @throws {ConfigurationError} If the override `config` is invalid or has missing required fields.
|
|
353
|
+
* @throws {ConfigurationError} If, in token mode, the configured `paymasterAddress` does not match the paymaster address returned by the paymaster RPC. This guards against the auto-generated ERC-20 approval targeting an unexpected paymaster contract.
|
|
341
354
|
* @throws {Error} If the token paymaster reports AA50 (account does not hold the paymaster token).
|
|
342
355
|
*/
|
|
343
356
|
async quoteTransfer (options, config, txOverrides) {
|
|
@@ -620,7 +633,7 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
|
|
|
620
633
|
* @protected
|
|
621
634
|
* @param {MetaTransaction[]} calls - The meta-transactions to include in the UserOperation.
|
|
622
635
|
* @param {Omit<EvmErc4337WalletConfig, 'transferMaxFee' | 'transactionMaxFee'>} config - The wallet configuration.
|
|
623
|
-
* @param {EvmErc4337GasOverrides} [txOverrides] - Optional UserOperationV7 gas overrides extracted from the input transaction(s)
|
|
636
|
+
* @param {EvmErc4337GasOverrides & Nonce} [txOverrides] - Optional UserOperationV7 gas overrides extracted from the input transaction(s), plus an optional explicit lane `nonce`.
|
|
624
637
|
* @returns {Promise<BuiltUserOperation>} The built operation, signing context, and (in token mode) the paymaster quote.
|
|
625
638
|
*/
|
|
626
639
|
async _buildUserOperation (calls, config, txOverrides = {}) {
|
|
@@ -764,6 +777,16 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
|
|
|
764
777
|
paymasterOverrides
|
|
765
778
|
)
|
|
766
779
|
|
|
767
|
-
|
|
780
|
+
const sponsoredOp = result.userOperation
|
|
781
|
+
|
|
782
|
+
if (mode === PaymasterMode.TOKEN && sponsoredOp.paymaster &&
|
|
783
|
+
sponsoredOp.paymaster.toLowerCase() !== config.paymasterAddress.toLowerCase()) {
|
|
784
|
+
throw new ConfigurationError(
|
|
785
|
+
`paymasterAddress mismatch: configured ${config.paymasterAddress} but RPC ${config.paymasterUrl} returned ${sponsoredOp.paymaster}. ` +
|
|
786
|
+
'The generated ERC-20 approval would target an unexpected paymaster.'
|
|
787
|
+
)
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
return { userOp: sponsoredOp, tokenQuote: result.tokenQuote }
|
|
768
791
|
}
|
|
769
792
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export type FeeRates = import("@tetherto/wdk-wallet-evm").FeeRates;
|
|
|
8
8
|
export type KeyPair = import("@tetherto/wdk-wallet-evm").KeyPair;
|
|
9
9
|
export type EvmErc4337Transaction = import("./src/wallet-account-read-only-evm-erc-4337.js").EvmErc4337Transaction;
|
|
10
10
|
export type EvmErc4337GasOverrides = import("./src/wallet-account-read-only-evm-erc-4337.js").EvmErc4337GasOverrides;
|
|
11
|
+
export type Nonce = import("./src/wallet-account-read-only-evm-erc-4337.js").Nonce;
|
|
11
12
|
export type TransactionResult = import("@tetherto/wdk-wallet-evm").TransactionResult;
|
|
12
13
|
export type TransferOptions = import("@tetherto/wdk-wallet-evm").TransferOptions;
|
|
13
14
|
export type TransferResult = import("@tetherto/wdk-wallet-evm").TransferResult;
|
|
@@ -19,10 +19,6 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
19
19
|
private _ownerAccount;
|
|
20
20
|
/** @private */
|
|
21
21
|
private _quoteCache;
|
|
22
|
-
/** @private */
|
|
23
|
-
private _reservedNonces;
|
|
24
|
-
/** @private */
|
|
25
|
-
private _nonceLock;
|
|
26
22
|
/**
|
|
27
23
|
* The derivation path's index of this account.
|
|
28
24
|
*
|
|
@@ -131,19 +127,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
131
127
|
*/
|
|
132
128
|
dispose(): void;
|
|
133
129
|
/** @private */
|
|
134
|
-
private _resolveQuote;
|
|
135
|
-
/** @private */
|
|
136
130
|
private _prepareForSend;
|
|
137
131
|
/** @private */
|
|
138
132
|
private _buildAtNonce;
|
|
139
133
|
/** @private */
|
|
140
|
-
private
|
|
141
|
-
/** @private */
|
|
142
|
-
private _releaseNonce;
|
|
143
|
-
/** @private */
|
|
144
|
-
private _maybeReleaseNonceOnRejection;
|
|
145
|
-
/** @private */
|
|
146
|
-
private static _isPreAcceptanceError;
|
|
134
|
+
private _resolveNonce;
|
|
147
135
|
/** @private */
|
|
148
136
|
private static _getTxKey;
|
|
149
137
|
/** @private */
|
|
@@ -211,10 +211,10 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
|
|
|
211
211
|
* @protected
|
|
212
212
|
* @param {MetaTransaction[]} calls - The meta-transactions to include in the UserOperation.
|
|
213
213
|
* @param {Omit<EvmErc4337WalletConfig, 'transferMaxFee'>} config - The wallet configuration.
|
|
214
|
-
* @param {EvmErc4337GasOverrides} [txOverrides] - Optional UserOperationV7 gas overrides extracted from the input transaction(s)
|
|
214
|
+
* @param {EvmErc4337GasOverrides & Nonce} [txOverrides] - Optional UserOperationV7 gas overrides extracted from the input transaction(s), plus an optional explicit lane `nonce`.
|
|
215
215
|
* @returns {Promise<BuiltUserOperation>} The built operation, signing context, and (in token mode) the paymaster quote.
|
|
216
216
|
*/
|
|
217
|
-
protected _buildUserOperation(calls: import('abstractionkit').MetaTransaction[], config: Omit<EvmErc4337WalletConfig, "transferMaxFee" | "transactionMaxFee">, txOverrides?: EvmErc4337GasOverrides): Promise<BuiltUserOperation>;
|
|
217
|
+
protected _buildUserOperation(calls: import('abstractionkit').MetaTransaction[], config: Omit<EvmErc4337WalletConfig, "transferMaxFee" | "transactionMaxFee">, txOverrides?: EvmErc4337GasOverrides & Nonce): Promise<BuiltUserOperation>;
|
|
218
218
|
/**
|
|
219
219
|
* Extracts the optional UserOperationV7 gas overrides from a single transaction.
|
|
220
220
|
*
|
|
@@ -285,33 +285,44 @@ export type EvmErc4337Transaction = {
|
|
|
285
285
|
maxPriorityFeePerGas?: number | bigint;
|
|
286
286
|
};
|
|
287
287
|
/**
|
|
288
|
-
* Gas-related UserOperationV7 overrides
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* gas price (fee pair) or AbstractionKit's gas estimation (gas limits).
|
|
288
|
+
* Gas-related UserOperationV7 overrides. Numeric fields accept `number` or `bigint`; numbers are
|
|
289
|
+
* coerced to `bigint` by `_extractGasOverrides` before the operation is built. All fields are
|
|
290
|
+
* optional; absent fields fall back to the bundler-fetched gas price (fee pair) or the bundler's
|
|
291
|
+
* gas estimation (gas limits).
|
|
293
292
|
*/
|
|
294
293
|
export type EvmErc4337GasOverrides = {
|
|
295
294
|
/**
|
|
296
295
|
* - Override for the UserOperation's call gas limit.
|
|
297
296
|
*/
|
|
298
|
-
callGasLimit?: bigint;
|
|
297
|
+
callGasLimit?: number | bigint;
|
|
299
298
|
/**
|
|
300
299
|
* - Override for the UserOperation's verification gas limit.
|
|
301
300
|
*/
|
|
302
|
-
verificationGasLimit?: bigint;
|
|
301
|
+
verificationGasLimit?: number | bigint;
|
|
303
302
|
/**
|
|
304
303
|
* - Override for the UserOperation's pre-verification gas.
|
|
305
304
|
*/
|
|
306
|
-
preVerificationGas?: bigint;
|
|
305
|
+
preVerificationGas?: number | bigint;
|
|
307
306
|
/**
|
|
308
307
|
* - Override for the UserOperation's max fee per gas (EIP-1559 cap).
|
|
309
308
|
*/
|
|
310
|
-
maxFeePerGas?: bigint;
|
|
309
|
+
maxFeePerGas?: number | bigint;
|
|
311
310
|
/**
|
|
312
311
|
* - Override for the UserOperation's max priority fee per gas.
|
|
313
312
|
*/
|
|
314
|
-
maxPriorityFeePerGas?: bigint;
|
|
313
|
+
maxPriorityFeePerGas?: number | bigint;
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* A single explicit UserOperationV7 `nonce`, combined with `EvmErc4337GasOverrides` for the build
|
|
317
|
+
* step to place the operation in a specific two-dimensional nonce lane. The `nonce` is derived
|
|
318
|
+
* internally from the account's `parallel`/`nonceKey` configuration, never from user-supplied
|
|
319
|
+
* transaction fields.
|
|
320
|
+
*/
|
|
321
|
+
export type Nonce = {
|
|
322
|
+
/**
|
|
323
|
+
* - Full 256-bit UserOperation nonce (`key << 64 | sequence`) placing the op in a specific lane. Omitted for default (key-0) sends, in which case the account fetches the current on-chain nonce.
|
|
324
|
+
*/
|
|
325
|
+
nonce?: number | bigint;
|
|
315
326
|
};
|
|
316
327
|
export type BuiltUserOperation = {
|
|
317
328
|
/**
|
|
@@ -378,6 +389,14 @@ export type EvmErc4337WalletCommonConfig = {
|
|
|
378
389
|
* - Optional on-chain identifier. Appends a 50-byte project marker to every UserOperation callData. Pass a string to reuse it as the project name, or a full object for more control.
|
|
379
390
|
*/
|
|
380
391
|
onChainIdentifier?: OnChainIdentifier | string;
|
|
392
|
+
/**
|
|
393
|
+
* - When true, each send is placed in a fresh, independent nonce lane (a random 192-bit key at sequence 0) so concurrent or back-to-back sends don't collide on the nonce. Ordering between such sends is not guaranteed and each consumes a new EntryPoint nonce slot. Ignored when `nonceKey` is set. Overridable per call.
|
|
394
|
+
*/
|
|
395
|
+
parallel?: boolean;
|
|
396
|
+
/**
|
|
397
|
+
* - Send in an explicit nonce lane. A string is hashed to a deterministic key — a reusable named lane that resumes the same sequence across sessions; a number or bigint is used as the raw uint192 key and must be within the uint192 range (0 to 2^192 - 1), otherwise the send throws (pass a bigint or string for keys above 2^53). Sends sharing a key are ordered sequentially; different keys run in parallel. Overridable per call.
|
|
398
|
+
*/
|
|
399
|
+
nonceKey?: number | bigint | string;
|
|
381
400
|
};
|
|
382
401
|
export type EvmErc4337WalletPaymasterTokenConfig = {
|
|
383
402
|
/**
|