-
Notifications
You must be signed in to change notification settings - Fork 173
/
Safe4337Pack.ts
655 lines (560 loc) · 22.8 KB
/
Safe4337Pack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import { ethers } from 'ethers'
import semverSatisfies from 'semver/functions/satisfies'
import Safe, {
EthSafeSignature,
EthersAdapter,
SigningMethod,
encodeMultiSendData,
getMultiSendContract
} from '@safe-global/protocol-kit'
import { RelayKitBasePack } from '@safe-global/relay-kit/RelayKitBasePack'
import { MetaTransactionData, OperationType, SafeSignature } from '@safe-global/safe-core-sdk-types'
import {
getAddModulesLibDeployment,
getSafe4337ModuleDeployment
} from '@safe-global/safe-modules-deployments'
import SafeOperation from './SafeOperation'
import {
EstimateFeeProps,
Safe4337CreateTransactionProps,
Safe4337ExecutableProps,
Safe4337InitOptions,
Safe4337Options,
SafeUserOperation,
UserOperation,
UserOperationReceipt,
UserOperationWithPayload,
paymasterOptions
} from './types'
import { EIP712_SAFE_OPERATION_TYPE, INTERFACES, RPC_4337_CALLS } from './constants'
import { getEip1193Provider, getEip4337BundlerProvider, userOperationToHexValues } from './utils'
import { PimlicoFeeEstimator } from './estimators/PimlicoFeeEstimator'
const DEFAULT_SAFE_VERSION = '1.4.1'
const DEFAULT_SAFE_MODULES_VERSION = '0.2.0'
const MAX_ERC20_AMOUNT_TO_APPROVE =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn
/**
* Safe4337Pack class that extends RelayKitBasePack.
* This class provides an implementation of the ERC-4337 that enables Safe accounts to wrk with UserOperations.
* It allows to create, sign and execute transactions using the Safe 4337 Module.
*
* @class
* @link https://github.com/safe-global/safe-modules/blob/main/modules/4337/contracts/Safe4337Module.sol
* @link https://eips.ethereum.org/EIPS/eip-4337
*/
export class Safe4337Pack extends RelayKitBasePack<{
EstimateFeeProps: EstimateFeeProps
EstimateFeeResult: SafeOperation
CreateTransactionProps: Safe4337CreateTransactionProps
CreateTransactionResult: SafeOperation
ExecuteTransactionProps: Safe4337ExecutableProps
ExecuteTransactionResult: string
}> {
#BUNDLER_URL: string
#ENTRYPOINT_ADDRESS: string
#SAFE_4337_MODULE_ADDRESS: string = '0x'
#bundlerClient: ethers.JsonRpcProvider
#publicClient: ethers.JsonRpcProvider
#paymasterOptions?: paymasterOptions
/**
* Creates an instance of the Safe4337Pack.
*
* @param {Safe4337Options} options - The initialization parameters.
*/
constructor({
protocolKit,
bundlerClient,
publicClient,
bundlerUrl,
paymasterOptions,
entryPointAddress,
safe4337ModuleAddress
}: Safe4337Options) {
super(protocolKit)
this.#BUNDLER_URL = bundlerUrl
this.#bundlerClient = bundlerClient
this.#publicClient = publicClient
this.#paymasterOptions = paymasterOptions
this.#ENTRYPOINT_ADDRESS = entryPointAddress
this.#SAFE_4337_MODULE_ADDRESS = safe4337ModuleAddress
}
/**
* Initializes a Safe4337Pack class.
* This method creates the protocolKit instance based on the input parameters.
* When the Safe address is provided, it will use the existing Safe.
* When the Safe address is not provided, it will use the predictedSafe feature with the provided owners and threshold.
* It will use the correct contract addresses for the fallbackHandler and the module and will add the data to enable the 4337 module.
*
* @param {Safe4337InitOptions} initOptions - The initialization parameters.
* @return {Promise<Safe4337Pack>} The Promise object that will be resolved into an instance of Safe4337Pack.
*/
static async init(initOptions: Safe4337InitOptions): Promise<Safe4337Pack> {
const { ethersAdapter, options, bundlerUrl, rpcUrl, customContracts, paymasterOptions } =
initOptions
let protocolKit: Safe
const bundlerClient = getEip4337BundlerProvider(bundlerUrl)
const publicClient = getEip1193Provider(rpcUrl)
const chainId = await bundlerClient.send(RPC_4337_CALLS.CHAIN_ID, [])
let addModulesLibAddress = customContracts?.addModulesLibAddress
const network = parseInt(chainId, 16).toString()
if (!addModulesLibAddress) {
const addModulesDeployment = getAddModulesLibDeployment({
released: true,
version: initOptions.safeModulesVersion || DEFAULT_SAFE_MODULES_VERSION,
network
})
addModulesLibAddress = addModulesDeployment?.networkAddresses[network]
}
let safe4337ModuleAddress = customContracts?.safe4337ModuleAddress
if (!safe4337ModuleAddress) {
const safe4337ModuleDeployment = getSafe4337ModuleDeployment({
released: true,
version: initOptions.safeModulesVersion || DEFAULT_SAFE_MODULES_VERSION,
network
})
safe4337ModuleAddress = safe4337ModuleDeployment?.networkAddresses[network]
}
if (!addModulesLibAddress || !safe4337ModuleAddress) {
throw new Error(
`Safe4337Module and/or AddModulesLib not available for chain ${network} and modules version ${DEFAULT_SAFE_MODULES_VERSION}`
)
}
// Existing Safe
if ('safeAddress' in options) {
protocolKit = await Safe.create({
ethAdapter: ethersAdapter,
safeAddress: options.safeAddress
})
const safeVersion = await protocolKit.getContractVersion()
const isSafeVersion4337Compatible = semverSatisfies(safeVersion, '>=1.4.1')
if (!isSafeVersion4337Compatible) {
throw new Error(
`Incompatibility detected: The current Safe Account version (${safeVersion}) is not supported. EIP-4337 requires the Safe to use at least v1.4.1.`
)
}
const safeModules = (await protocolKit.getModules()) as string[]
const is4337ModulePresent = safeModules.some((module) => module === safe4337ModuleAddress)
if (!is4337ModulePresent) {
throw new Error(
`Incompatibility detected: The EIP-4337 module is not enabled in the provided Safe Account. Enable this module (address: ${safe4337ModuleAddress}) to add compatibility.`
)
}
const safeFallbackhandler = await protocolKit.getFallbackHandler()
const is4337FallbackhandlerPresent = safeFallbackhandler === safe4337ModuleAddress
if (!is4337FallbackhandlerPresent) {
throw new Error(
`Incompatibility detected: The EIP-4337 fallbackhandler is not attached to the Safe Account. Attach this fallbackhandler (address: ${safe4337ModuleAddress}) to ensure compatibility.`
)
}
} else {
// New Safe will be created based on the provided configuration when bundling a new UserOperation
if (!options.owners || !options.threshold) {
throw new Error('Owners and threshold are required to deploy a new Safe')
}
let deploymentTo = addModulesLibAddress
let deploymentData = INTERFACES.encodeFunctionData('enableModules', [[safe4337ModuleAddress]])
const { isSponsored, paymasterTokenAddress } = paymasterOptions || {}
const isApproveTransactionRequired =
!!paymasterOptions && !isSponsored && !!paymasterTokenAddress
if (isApproveTransactionRequired) {
const { paymasterAddress, amountToApprove = MAX_ERC20_AMOUNT_TO_APPROVE } = paymasterOptions
const enable4337ModulesTransaction = {
to: addModulesLibAddress,
value: '0',
data: INTERFACES.encodeFunctionData('enableModules', [[safe4337ModuleAddress]]),
operation: OperationType.DelegateCall // DelegateCall required for enabling the 4337 module
}
const approveToPaymasterTransaction = {
to: paymasterTokenAddress,
data: INTERFACES.encodeFunctionData('approve', [paymasterAddress, amountToApprove]),
value: '0',
operation: OperationType.Call // Call for approve
}
const setupBatch = [enable4337ModulesTransaction, approveToPaymasterTransaction]
const batchData = INTERFACES.encodeFunctionData('multiSend', [
encodeMultiSendData(setupBatch)
])
const multiSendContract = await getMultiSendContract({
ethAdapter: ethersAdapter,
safeVersion: options.safeVersion || DEFAULT_SAFE_VERSION
})
deploymentTo = await multiSendContract.getAddress()
deploymentData = batchData
}
protocolKit = await Safe.create({
ethAdapter: ethersAdapter,
predictedSafe: {
safeDeploymentConfig: {
safeVersion: options.safeVersion || DEFAULT_SAFE_VERSION,
saltNonce: options.saltNonce || undefined
},
safeAccountConfig: {
owners: options.owners,
threshold: options.threshold,
to: deploymentTo,
data: deploymentData,
fallbackHandler: safe4337ModuleAddress,
paymentToken: ethers.ZeroAddress,
payment: 0,
paymentReceiver: ethers.ZeroAddress
}
}
})
}
let supportedEntryPoints
if (!customContracts?.entryPointAddress) {
supportedEntryPoints = await bundlerClient.send(RPC_4337_CALLS.SUPPORTED_ENTRY_POINTS, [])
if (!supportedEntryPoints.length) {
throw new Error('No entrypoint provided or available through the bundler')
}
}
return new Safe4337Pack({
protocolKit,
bundlerClient,
publicClient,
paymasterOptions,
bundlerUrl,
entryPointAddress: customContracts?.entryPointAddress || supportedEntryPoints[0],
safe4337ModuleAddress
})
}
/**
* Estimates gas for the SafeOperation.
*
* @param {EstimateFeeProps} props - The parameters for the gas estimation.
* @param {SafeOperation} props.safeOperation - The SafeOperation to estimate the gas.
* @param {IFeeEstimator} props.feeEstimator - The function to estimate the gas.
* @return {Promise<SafeOperation>} The Promise object that will be resolved into the gas estimation.
*/
async getEstimateFee({
safeOperation,
feeEstimator = new PimlicoFeeEstimator()
}: EstimateFeeProps): Promise<SafeOperation> {
const userOperation = safeOperation.toUserOperation()
const setupEstimationData = await feeEstimator?.setupEstimation?.({
bundlerUrl: this.#BUNDLER_URL,
entryPoint: this.#ENTRYPOINT_ADDRESS,
userOperation: safeOperation.toUserOperation()
})
if (setupEstimationData) {
safeOperation.addEstimations(setupEstimationData)
}
const estimateUserOperationGas = await this.#bundlerClient.send(
RPC_4337_CALLS.ESTIMATE_USER_OPERATION_GAS,
[userOperationToHexValues(userOperation), this.#ENTRYPOINT_ADDRESS]
)
if (estimateUserOperationGas) {
safeOperation.addEstimations({
preVerificationGas: BigInt(estimateUserOperationGas.preVerificationGas),
verificationGasLimit: BigInt(estimateUserOperationGas.verificationGasLimit),
callGasLimit: BigInt(estimateUserOperationGas.callGasLimit)
})
}
const adjustEstimationData = await feeEstimator?.adjustEstimation?.({
bundlerUrl: this.#BUNDLER_URL,
entryPoint: this.#ENTRYPOINT_ADDRESS,
userOperation: safeOperation.toUserOperation()
})
if (adjustEstimationData) {
safeOperation.addEstimations(adjustEstimationData)
}
if (this.#paymasterOptions?.isSponsored) {
if (!this.#paymasterOptions.paymasterUrl) {
throw new Error('No paymaster url provided for a sponsored transaction')
}
const paymasterEstimation = await feeEstimator?.getPaymasterEstimation?.({
userOperation: safeOperation.toUserOperation(),
paymasterUrl: this.#paymasterOptions.paymasterUrl,
entryPoint: this.#ENTRYPOINT_ADDRESS,
sponsorshipPolicyId: this.#paymasterOptions.sponsorshipPolicyId
})
safeOperation.data.paymasterAndData =
paymasterEstimation?.paymasterAndData || safeOperation.data.paymasterAndData
if (paymasterEstimation) {
safeOperation.addEstimations(paymasterEstimation)
}
}
return safeOperation
}
/**
* Creates a relayed transaction based on the provided parameters.
*
* @param {MetaTransactionData[]} transactions - The transactions to batch in a SafeOperation.
* @param options - Optional configuration options for the transaction creation.
* @return {Promise<SafeOperation>} The Promise object will resolve a SafeOperation.
*/
async createTransaction({
transactions,
options = {}
}: Safe4337CreateTransactionProps): Promise<SafeOperation> {
const safeAddress = await this.protocolKit.getAddress()
const nonce = await this.#getAccountNonce(safeAddress)
const { amountToApprove, validUntil, validAfter, feeEstimator } = options
if (amountToApprove) {
if (!this.#paymasterOptions || !this.#paymasterOptions.paymasterTokenAddress) {
throw new Error('Paymaster must be initialized')
}
const paymasterAddress = this.#paymasterOptions.paymasterAddress
const paymasterTokenAddress = this.#paymasterOptions.paymasterTokenAddress
const approveToPaymasterTransaction = {
to: paymasterTokenAddress,
data: INTERFACES.encodeFunctionData('approve', [paymasterAddress, amountToApprove]),
value: '0',
operation: OperationType.Call // Call for approve
}
transactions.push(approveToPaymasterTransaction)
}
const isBatch = transactions.length > 1
const multiSendAddress = await this.protocolKit.getMultiSendAddress()
const callData = isBatch
? this.#encodeExecuteUserOpCallData({
to: multiSendAddress,
value: '0',
data: this.#encodeMultiSendCallData(transactions),
operation: OperationType.DelegateCall
})
: this.#encodeExecuteUserOpCallData(transactions[0])
const paymasterAndData = this.#paymasterOptions?.paymasterAddress || '0x'
const userOperation: UserOperation = {
sender: safeAddress,
nonce: nonce,
initCode: '0x',
callData,
callGasLimit: 1n,
verificationGasLimit: 1n,
preVerificationGas: 1n,
maxFeePerGas: 1n,
maxPriorityFeePerGas: 1n,
paymasterAndData,
signature: '0x'
}
const isSafeDeployed = await this.protocolKit.isSafeDeployed()
if (!isSafeDeployed) {
userOperation.initCode = await this.protocolKit.getInitCode()
}
const safeOperation = new SafeOperation(userOperation, {
entryPoint: this.#ENTRYPOINT_ADDRESS,
validUntil,
validAfter
})
return await this.getEstimateFee({
safeOperation,
feeEstimator
})
}
/**
* Signs a safe operation.
*
* @param {SafeOperation} safeOperation - The SafeOperation to sign.
* @param {SigningMethod} signingMethod - The signing method to use.
* @return {Promise<SafeOperation>} The Promise object will resolve to the signed SafeOperation.
*/
async signSafeOperation(
safeOperation: SafeOperation,
signingMethod: SigningMethod = SigningMethod.ETH_SIGN_TYPED_DATA_V4
): Promise<SafeOperation> {
const owners = await this.protocolKit.getOwners()
const signerAddress = await this.protocolKit.getEthAdapter().getSignerAddress()
if (!signerAddress) {
throw new Error('EthAdapter must be initialized with a signer to use this method')
}
const addressIsOwner = owners.some(
(owner: string) => signerAddress && owner.toLowerCase() === signerAddress.toLowerCase()
)
if (!addressIsOwner) {
throw new Error('UserOperations can only be signed by Safe owners')
}
let signature: SafeSignature
if (
signingMethod === SigningMethod.ETH_SIGN_TYPED_DATA_V4 ||
signingMethod === SigningMethod.ETH_SIGN_TYPED_DATA_V3 ||
signingMethod === SigningMethod.ETH_SIGN_TYPED_DATA
) {
signature = await this.#signTypedData(safeOperation.data)
} else {
const chainId = await this.protocolKit.getEthAdapter().getChainId()
const safeOpHash = this.#getSafeUserOperationHash(safeOperation.data, chainId)
signature = await this.protocolKit.signHash(safeOpHash)
}
const signedSafeOperation = new SafeOperation(safeOperation.toUserOperation(), {
entryPoint: this.#ENTRYPOINT_ADDRESS,
validUntil: safeOperation.data.validUntil,
validAfter: safeOperation.data.validAfter
})
signedSafeOperation.signatures.forEach((signature: SafeSignature) => {
signedSafeOperation.addSignature(signature)
})
signedSafeOperation.addSignature(signature)
return signedSafeOperation
}
/**
* Executes the relay transaction.
*
* @param {SafeOperation} safeOperation - The SafeOperation to execute.
* @return {Promise<string>} The user operation hash.
*/
async executeTransaction({
executable: safeOperation
}: Safe4337ExecutableProps): Promise<string> {
const userOperation = safeOperation.toUserOperation()
return this.sendUserOperation(userOperation)
}
/**
* Return a UserOperation based on a hash (userOpHash) returned by eth_sendUserOperation
*
* @param {string} userOpHash - The hash of the user operation to fetch. Returned from the sendUserOperation method
* @returns {UserOperation} - null in case the UserOperation is not yet included in a block, or a full UserOperation, with the addition of entryPoint, blockNumber, blockHash and transactionHash
*/
async getUserOperationByHash(userOpHash: string): Promise<UserOperationWithPayload> {
const userOperation = await this.#bundlerClient.send(
RPC_4337_CALLS.GET_USER_OPERATION_BY_HASH,
[userOpHash]
)
return userOperation
}
/**
* Return a UserOperation receipt based on a hash (userOpHash) returned by eth_sendUserOperation
*
* @param {string} userOpHash - The hash of the user operation to fetch. Returned from the sendUserOperation method
* @returns {UserOperationReceipt} - null in case the UserOperation is not yet included in a block, or UserOperationReceipt object
*/
async getUserOperationReceipt(userOpHash: string): Promise<UserOperationReceipt | null> {
const userOperationReceipt = await this.#bundlerClient.send(
RPC_4337_CALLS.GET_USER_OPERATION_RECEIPT,
[userOpHash]
)
return userOperationReceipt
}
/**
* Returns an array of the entryPoint addresses supported by the client.
* The first element of the array SHOULD be the entryPoint addressed preferred by the client.
*
* @returns {string[]} - The supported entry points.
*/
async getSupportedEntryPoints(): Promise<string[]> {
const supportedEntryPoints = await this.#bundlerClient.send(
RPC_4337_CALLS.SUPPORTED_ENTRY_POINTS,
[]
)
return supportedEntryPoints
}
/**
* Returns EIP-155 Chain ID.
*
* @returns {string} - The chain id.
*/
async getChainId(): Promise<string> {
const chainId = await this.#bundlerClient.send(RPC_4337_CALLS.CHAIN_ID, [])
return chainId
}
/**
* Gets the safe user operation hash.
*
* @param {SafeUserOperation} safeUserOperation - The SafeUserOperation.
* @param {bigint} chainId - The chain id.
* @return {string} The hash of the safe operation.
*/
#getSafeUserOperationHash(safeUserOperation: SafeUserOperation, chainId: bigint): string {
return ethers.TypedDataEncoder.hash(
{
chainId,
verifyingContract: this.#SAFE_4337_MODULE_ADDRESS
},
EIP712_SAFE_OPERATION_TYPE,
safeUserOperation
)
}
/**
* Send the UserOperation to the bundler.
*
* @param {UserOperation} userOpWithSignature - The signed UserOperation to send to the bundler.
* @return {Promise<string>} The hash.
*/
async sendUserOperation(userOpWithSignature: UserOperation): Promise<string> {
return await this.#bundlerClient.send(RPC_4337_CALLS.SEND_USER_OPERATION, [
userOperationToHexValues(userOpWithSignature),
this.#ENTRYPOINT_ADDRESS
])
}
/**
* Signs typed data.
* This is currently only EthersAdapter compatible (Reflected in the init() props). If I want to make it compatible with any EthAdapter I need to either:
* - Add a SafeOp type to the protocol-kit (createSafeOperation, signSafeOperation, etc)
* - Allow to pass the data types (SafeOp, SafeMessage, SafeTx) to the signTypedData method and refactor the protocol-kit to allow any kind of data signing from outside (Currently only SafeTx and SafeMessage)
*
* @param {SafeUserOperation} safeUserOperation - Safe user operation to sign.
* @return {Promise<SafeSignature>} The SafeSignature object containing the data and the signatures.
*/
async #signTypedData(safeUserOperation: SafeUserOperation): Promise<SafeSignature> {
const ethAdapter = this.protocolKit.getEthAdapter() as EthersAdapter
const signer = ethAdapter.getSigner() as ethers.Signer
const chainId = await ethAdapter.getChainId()
const signerAddress = await signer.getAddress()
const signature = await signer.signTypedData(
{
chainId,
verifyingContract: this.#SAFE_4337_MODULE_ADDRESS
},
EIP712_SAFE_OPERATION_TYPE,
{
...safeUserOperation,
nonce: ethers.toBeHex(safeUserOperation.nonce),
validAfter: ethers.toBeHex(safeUserOperation.validAfter),
validUntil: ethers.toBeHex(safeUserOperation.validUntil),
maxFeePerGas: ethers.toBeHex(safeUserOperation.maxFeePerGas),
maxPriorityFeePerGas: ethers.toBeHex(safeUserOperation.maxPriorityFeePerGas)
}
)
return new EthSafeSignature(signerAddress, signature)
}
/**
* Gets account nonce from the bundler.
*
* @param {string} sender - Account address for which the nonce is to be fetched.
* @returns {Promise<string>} The Promise object will resolve to the account nonce.
*/
async #getAccountNonce(sender: string): Promise<string> {
const abi = [
{
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'key', type: 'uint192' }
],
name: 'getNonce',
outputs: [{ name: 'nonce', type: 'uint256' }],
stateMutability: 'view',
type: 'function'
}
]
const contract = new ethers.Contract(this.#ENTRYPOINT_ADDRESS || '0x', abi, this.#publicClient)
const newNonce = await contract.getNonce(sender, BigInt(0))
return newNonce.toString()
}
/**
* Encode the UserOperation execution from a transaction.
*
* @param {MetaTransactionData} transaction - The transaction data to encode.
* @return {string} The encoded call data string.
*/
#encodeExecuteUserOpCallData(transaction: MetaTransactionData): string {
return INTERFACES.encodeFunctionData('executeUserOp', [
transaction.to,
transaction.value,
transaction.data,
transaction.operation || OperationType.Call
])
}
/**
* Encodes multi-send data from transactions batch.
*
* @param {MetaTransactionData[]} transactions - an array of transaction to to be encoded.
* @return {string} The encoded data string.
*/
#encodeMultiSendCallData(transactions: MetaTransactionData[]): string {
return INTERFACES.encodeFunctionData('multiSend', [
encodeMultiSendData(
transactions.map((tx) => ({ ...tx, operation: tx.operation ?? OperationType.Call }))
)
])
}
}