-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathwrite.test.ts
759 lines (659 loc) · 23.5 KB
/
write.test.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
import {
http,
type Hex,
createPublicClient,
createWalletClient,
encodeFunctionData,
getContract,
parseAbi
} from "viem"
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { arbitrumSepolia, polygonAmoy, taikoHekla } from "viem/chains"
import { beforeAll, describe, expect, test } from "vitest"
import {
type BiconomySmartAccountV2,
DEFAULT_ENTRYPOINT_ADDRESS,
ERC20_ABI,
type TransferOwnershipCompatibleModule,
createSmartAccountClient,
getCustomChain,
percentage
} from "../../src/account"
import { ECDSAModuleAbi } from "../../src/account/abi/ECDSAModule"
import { EntryPointAbi } from "../../src/account/abi/EntryPointAbi"
import { DEFAULT_ECDSA_OWNERSHIP_MODULE } from "../../src/modules"
import { PaymasterMode } from "../../src/paymaster"
import { testOnlyOnOptimism } from "../setupFiles"
import {
checkBalance,
getBundlerUrl,
getConfig,
nonZeroBalance,
topUp
} from "../utils"
describe("Account:Write", async () => {
const nonceOptions = { nonceKey: Date.now() + 10 }
const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e"
const token = "0x747A4168DB14F57871fa8cda8B5455D8C2a8e90a"
const {
chain,
chainId,
privateKey,
privateKeyTwo,
bundlerUrl,
paymasterUrl
} = getConfig()
const account = privateKeyToAccount(`0x${privateKey}`)
const accountTwo = privateKeyToAccount(`0x${privateKeyTwo}`)
const sender = account.address
const recipient = accountTwo.address
const publicClient = createPublicClient({
chain,
transport: http()
})
let [smartAccount, smartAccountTwo]: BiconomySmartAccountV2[] = []
let [smartAccountAddress, smartAccountAddressTwo]: Hex[] = []
const [walletClient, walletClientTwo] = [
createWalletClient({
account,
chain,
transport: http()
}),
createWalletClient({
account: accountTwo,
chain,
transport: http()
})
]
const firstOwner = account.address
const newOwner = accountTwo.address
let _smartAccount = await createSmartAccountClient({
signer: walletClient,
paymasterUrl,
bundlerUrl
// accountAddress: "0xe6dBb5C8696d2E0f90B875cbb6ef26E3bBa575AC"
})
beforeAll(async () => {
;[smartAccount, smartAccountTwo] = await Promise.all(
[walletClient, walletClientTwo].map((client) =>
createSmartAccountClient({
chainId,
signer: client,
bundlerUrl,
paymasterUrl
})
)
)
;[smartAccountAddress, smartAccountAddressTwo] = await Promise.all(
[smartAccount, smartAccountTwo].map((account) =>
account.getAccountAddress()
)
)
})
test.skip("should test the nonce on arbSepolia", async () => {
const chain = arbitrumSepolia
const account = privateKeyToAccount(`0x${privateKey}`)
const signer = createWalletClient({ account, chain, transport: http() })
const smartAccount = await createSmartAccountClient({
signer,
bundlerUrl: getBundlerUrl(chain.id)
})
const address = await smartAccount.getAccountAddress()
await nonZeroBalance(address)
const nonceBefore = await smartAccount.getNonce()
const balanceOfRecipient = await checkBalance(recipient, undefined, chain)
const { wait } = await smartAccount.sendTransaction(
{
to: recipient,
value: 1n
},
{
nonceOptions
}
)
const result = await wait()
const newBalanceOfRecipient = await checkBalance(
recipient,
undefined,
chain
)
const nonceAfter = await smartAccount.getNonce()
expect(result?.receipt?.transactionHash).toBeTruthy()
expect(result.success).toBe("true")
expect(newBalanceOfRecipient).toBeGreaterThan(balanceOfRecipient)
expect(nonceAfter - nonceBefore).toBe(1n)
}, 10000)
test("should send some native token to recipient via the entrypoint", async () => {
const balanceOfRecipient = await checkBalance(recipient)
// biome-ignore lint/style/useConst: <explanation>
let userOp = await smartAccount.buildUserOp([
{
to: recipient,
value: 1n
}
])
userOp.signature = undefined
const signedUserOp = await smartAccount.signUserOp(userOp)
const entrypointContract = getContract({
address: DEFAULT_ENTRYPOINT_ADDRESS,
abi: EntryPointAbi,
client: { public: publicClient, wallet: walletClient }
})
const hash = await entrypointContract.write.handleOps([
[
{
sender: signedUserOp.sender as Hex,
nonce: BigInt(signedUserOp.nonce ?? 0),
callGasLimit: BigInt(signedUserOp.callGasLimit ?? 0),
verificationGasLimit: BigInt(signedUserOp.verificationGasLimit ?? 0),
preVerificationGas: BigInt(signedUserOp.preVerificationGas ?? 0),
maxFeePerGas: BigInt(signedUserOp.maxFeePerGas ?? 0),
maxPriorityFeePerGas: BigInt(signedUserOp.maxPriorityFeePerGas ?? 0),
initCode: signedUserOp.initCode as Hex,
callData: signedUserOp.callData as Hex,
paymasterAndData: signedUserOp.paymasterAndData as Hex,
signature: signedUserOp.signature as Hex
}
],
sender
])
const { status, transactionHash } =
await publicClient.waitForTransactionReceipt({ hash })
expect(status).toBe("success")
expect(transactionHash).toBeTruthy()
const balanceOfRecipientAfter = await checkBalance(recipient)
expect(balanceOfRecipientAfter - balanceOfRecipient).toBe(1n)
}, 50000)
test("should deploy a smart account with native token balance", async () => {
const newPrivateKey = generatePrivateKey()
const newAccount = privateKeyToAccount(newPrivateKey)
const newViemWallet = createWalletClient({
account: newAccount,
chain,
transport: http()
})
const newSmartAccount = await createSmartAccountClient({
signer: newViemWallet,
paymasterUrl,
bundlerUrl
})
const newSmartAccountAddress = await newSmartAccount.getAccountAddress()
// Setup:
await topUp(newSmartAccountAddress, 3000000000000000000n)
const balanceCheck = await checkBalance(newSmartAccountAddress)
// Test:
const { wait } = await newSmartAccount.deploy()
const { success } = await wait()
const byteCode = await publicClient.getCode({ address: newSmartAccountAddress })
expect(success).toBe("true")
expect(byteCode).toBeTruthy()
const balanceCheckAfter = await checkBalance(newSmartAccountAddress)
}, 60000)
test.concurrent(
"should add a custom chain",
async () => {
const customChain = getCustomChain(
"Amoy",
polygonAmoy.id,
polygonAmoy.rpcUrls.default.http[0],
polygonAmoy.blockExplorers.default.url
)
const accountOne = privateKeyToAccount(`0x${privateKey}`)
const walletClientWithCustomChain = createWalletClient({
account: accountOne,
chain: customChain,
transport: http()
})
const smartAccountCustomChain = await createSmartAccountClient({
signer: walletClientWithCustomChain,
bundlerUrl,
customChain
})
expect(smartAccountCustomChain.rpcProvider.transport.url).toBe(
"https://rpc-amoy.polygon.technology"
)
expect(walletClientWithCustomChain.chain).toEqual(customChain)
const { wait } = await smartAccountCustomChain.sendTransaction(
{
to: recipient,
value: 1n
},
{ nonceOptions }
)
const { success, receipt } = await wait()
expect(receipt).toBeTruthy()
expect(success).toBe("true")
},
80000
)
testOnlyOnOptimism(
"should send some native token to a recipient on optimism",
async () => {
const balanceOfRecipient = await checkBalance(recipient)
const { wait } = await smartAccount.sendTransaction(
{
to: recipient,
value: 1n
},
{ nonceOptions, simulationType: "validation_and_execution" }
)
const result = await wait()
const newBalanceOfRecipient = await checkBalance(recipient)
expect(result?.receipt?.transactionHash).toBeTruthy()
expect(result.success).toBe("true")
expect(newBalanceOfRecipient).toBeGreaterThan(balanceOfRecipient)
},
50000
)
testOnlyOnOptimism(
"should create a smart account with paymaster with an api key on optimism",
async () => {
const paymaster = smartAccount.paymaster
expect(paymaster).not.toBeNull()
expect(paymaster).not.toBeUndefined()
}
)
test("should withdraw erc20 balances", async () => {
await nonZeroBalance(smartAccountAddress, token)
const tokenBalanceOfSABefore = await checkBalance(
smartAccountAddress,
token
)
const tokenBalanceOfRecipientBefore = await checkBalance(sender, token)
const { wait } = await smartAccount.withdraw(
[{ address: token, amount: 1n, recipient: sender }],
undefined,
{
nonceOptions,
paymasterServiceData: {
mode: PaymasterMode.SPONSORED
}
}
)
const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()
expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
expect(transactionHash).toBeTruthy()
const tokenBalanceOfSAAfter = await checkBalance(smartAccountAddress, token)
const tokenBalanceOfRecipientAfter = await checkBalance(sender, token)
expect(tokenBalanceOfSAAfter - tokenBalanceOfSABefore).toBe(-1n)
expect(tokenBalanceOfRecipientAfter - tokenBalanceOfRecipientBefore).toBe(
1n
)
}, 60000)
test("should mint an NFT and pay with ERC20 - with token", async () => {
const encodedCall = encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [recipient]
})
const transaction = {
to: nftAddress, // NFT address
data: encodedCall
}
const balance = await checkBalance(recipient, nftAddress)
const nativeBalanceBefore = await checkBalance(smartAccountAddress)
const tokenBalanceBefore = await checkBalance(smartAccountAddress, token)
const { wait } = await smartAccount.sendTransaction([transaction], {
nonceOptions,
paymasterServiceData: {
mode: PaymasterMode.ERC20,
preferredToken: token
}
})
const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()
expect(transactionHash).toBeTruthy()
expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
const nativeBalanceAfter = await checkBalance(smartAccountAddress)
expect(nativeBalanceAfter).toEqual(nativeBalanceBefore)
const tokenBalanceAfter = await checkBalance(smartAccountAddress, token)
expect(tokenBalanceAfter).toBeLessThan(tokenBalanceBefore)
const newBalance = await checkBalance(recipient, nftAddress)
expect(newBalance - balance).toBe(1n)
}, 60000)
test("should mint an NFT and pay with ERC20 - with token selection and no maxApproval", async () => {
const encodedCall = encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [recipient]
})
const transaction = {
to: nftAddress, // NFT address
data: encodedCall
}
const feeQuotesResponse = await smartAccount.getTokenFees(transaction, {
paymasterServiceData: {
mode: PaymasterMode.ERC20,
preferredToken: token
}
})
const selectedFeeQuote = feeQuotesResponse.feeQuotes?.[0]
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const spender = feeQuotesResponse.tokenPaymasterAddress!
const contract = getContract({
address: token,
abi: parseAbi(ERC20_ABI),
client: publicClient
})
const balance = await checkBalance(recipient, nftAddress)
const nativeBalanceBefore = await checkBalance(smartAccountAddress)
const tokenBalanceBefore = await checkBalance(smartAccountAddress, token)
const { wait } = await smartAccount.sendTransaction(transaction, {
nonceOptions,
paymasterServiceData: {
mode: PaymasterMode.ERC20,
feeQuote: selectedFeeQuote,
spender: feeQuotesResponse.tokenPaymasterAddress
}
})
const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()
expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
expect(transactionHash).toBeTruthy()
const nativeBalanceAfter = await checkBalance(smartAccountAddress)
expect(nativeBalanceAfter).toEqual(nativeBalanceBefore)
const tokenBalanceAfter = await checkBalance(smartAccountAddress, token)
expect(tokenBalanceAfter).toBeLessThan(tokenBalanceBefore)
const newBalance = await checkBalance(recipient, nftAddress)
expect(newBalance - balance).toBe(1n)
}, 60000)
test("should increment user op verificationGasLimit by 50%. Paymaster OFF", async () => {
const transaction = {
to: recipient,
data: "0x"
}
const userOpWithNoOffset = await smartAccount.buildUserOp([transaction])
const userOpWithOffset = await smartAccount.buildUserOp([transaction], {
gasOffset: {
verificationGasLimitOffsetPct: 50 // 50% increase
}
})
const difference = Math.round(
Number(userOpWithOffset.verificationGasLimit) -
Number(userOpWithNoOffset.verificationGasLimit)
)
const percentageValue = Math.round(
percentage(difference, Number(userOpWithNoOffset.verificationGasLimit))
)
expect(percentageValue).toBe(50)
}, 60000)
test("should increment user op gas values. Paymaster OFF", async () => {
const transaction = {
to: recipient,
data: "0x"
}
const userOpWithNoOffset = await smartAccount.buildUserOp([transaction])
const userOpWithOffset = await smartAccount.buildUserOp([transaction], {
gasOffset: {
verificationGasLimitOffsetPct: 50, // 50% increase
preVerificationGasOffsetPct: 100 // 100% increase
}
})
const vglDifference = Math.round(
Number(userOpWithOffset.verificationGasLimit) -
Number(userOpWithNoOffset.verificationGasLimit)
)
const cgllDifference = Math.round(
Number(userOpWithOffset.callGasLimit) -
Number(userOpWithNoOffset.callGasLimit)
)
const pvgDifference = Math.round(
Number(userOpWithOffset.preVerificationGas) -
Number(userOpWithNoOffset.preVerificationGas)
)
const vglPercentageValue = Math.round(
percentage(vglDifference, Number(userOpWithNoOffset.verificationGasLimit))
)
const cglPercentageValue = Math.round(
percentage(cgllDifference, Number(userOpWithNoOffset.callGasLimit))
)
const pvgPercentageValue = Math.round(
percentage(pvgDifference, Number(userOpWithNoOffset.preVerificationGas))
)
expect(vglPercentageValue).toBe(50)
expect(cglPercentageValue).toBe(0)
expect(pvgPercentageValue).toBe(100)
}, 60000)
test("should increment user op gas values. Paymaster ON", async () => {
const transaction = {
to: recipient,
data: "0x"
}
const userOpWithNoOffset = await smartAccount.buildUserOp([transaction], {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
gasOffset: {
verificationGasLimitOffsetPct: 0 // no increment but provided to avoid paymaster gas calculation (just for testing purposes)
}
}) // Passing gasOffset to avoid paymaster gas calculation
const userOpWithOffset = await smartAccount.buildUserOp([transaction], {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
gasOffset: {
verificationGasLimitOffsetPct: 13.2, // 13.2% increase
preVerificationGasOffsetPct: 81 // 81% increase
}
})
const vglDifference = Math.round(
Number(userOpWithOffset.verificationGasLimit) -
Number(userOpWithNoOffset.verificationGasLimit)
)
const cgllDifference = Math.round(
Number(userOpWithOffset.callGasLimit) -
Number(userOpWithNoOffset.callGasLimit)
)
const pvgDifference = Math.round(
Number(userOpWithOffset.preVerificationGas) -
Number(userOpWithNoOffset.preVerificationGas)
)
const vglPercentageValue = Math.round(
percentage(vglDifference, Number(userOpWithNoOffset.verificationGasLimit))
)
const cglPercentageValue = Math.round(
percentage(cgllDifference, Number(userOpWithNoOffset.callGasLimit))
)
const pvgPercentageValue = Math.round(
percentage(pvgDifference, Number(userOpWithNoOffset.preVerificationGas))
)
expect(vglPercentageValue).toBe(13)
expect(cglPercentageValue).toBe(0)
expect(pvgPercentageValue).toBe(81)
}, 60000)
test("should throw if percentage given is bigger than 100. Paymaster ON", async () => {
const transaction = {
to: recipient,
data: "0x"
}
const userOpWithNoOffset = await smartAccount.buildUserOp([transaction], {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
gasOffset: {
verificationGasLimitOffsetPct: 0 // no increment, just for testing purposes
}
}) // Passing gasOffset to avoid paymaster gas calculation
const userOpWithOffset = smartAccount.buildUserOp([transaction], {
paymasterServiceData: { mode: PaymasterMode.SPONSORED },
gasOffset: {
verificationGasLimitOffsetPct: 110 // 110% increase (not allowed)
}
})
expect(userOpWithOffset).rejects.toThrowError(
"The percentage value should be between 1 and 100."
)
}, 60000)
test("should increment user op gas with no paymaster using sendTransaction", async () => {
const encodedCall = encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [recipient]
})
const transaction = {
to: nftAddress, // NFT address
data: encodedCall
}
const { wait } = await smartAccount.sendTransaction(transaction, {
nonceOptions,
gasOffset: {
verificationGasLimitOffsetPct: 10, // 10% increase
preVerificationGasOffsetPct: 20, // 20% increase
maxFeePerGasOffsetPct: 30, // 30% increase
callGasLimitOffsetPct: 40, // 40% increase
maxPriorityFeePerGasOffsetPct: 50 // 50% increase
}
})
const {
receipt: { transactionHash },
userOpHash,
success
} = await wait()
expect(userOpHash).toBeTruthy()
expect(success).toBe("true")
}, 60000)
test.skip("should transfer ownership of smart account to accountTwo", async () => {
const signerOfAccount = walletClient.account.address
const ownerOfAccount = await publicClient.readContract({
address: DEFAULT_ECDSA_OWNERSHIP_MODULE,
abi: ECDSAModuleAbi,
functionName: "getOwner",
args: [await _smartAccount.getAccountAddress()]
})
expect(ownerOfAccount).toBe(signerOfAccount)
const response = await _smartAccount.transferOwnership(
newOwner,
DEFAULT_ECDSA_OWNERSHIP_MODULE as TransferOwnershipCompatibleModule,
{
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
}
)
const receipt = await response.wait()
expect(receipt.success).toBe("true")
}, 50000)
test.skip("should revert transfer ownership with signer that is not the owner", async () => {
_smartAccount = await createSmartAccountClient({
signer: walletClient,
paymasterUrl,
bundlerUrl,
accountAddress: smartAccountAddress
})
const signerOfAccount = walletClient.account.address
const ownerOfAccount = await publicClient.readContract({
address: DEFAULT_ECDSA_OWNERSHIP_MODULE,
abi: ECDSAModuleAbi,
functionName: "getOwner",
args: [await _smartAccount.getAccountAddress()]
})
expect(ownerOfAccount).not.toBe(signerOfAccount)
expect(
_smartAccount.transferOwnership(
newOwner,
DEFAULT_ECDSA_OWNERSHIP_MODULE as TransferOwnershipCompatibleModule,
{
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
}
)
).rejects.toThrowError()
}, 50000)
test.skip("send an user op with the new owner", async () => {
_smartAccount = await createSmartAccountClient({
signer: walletClientTwo,
paymasterUrl,
bundlerUrl,
accountAddress: smartAccountAddress
})
const currentSmartAccountInstanceSigner = await _smartAccount
.getSigner()
.getAddress()
expect(currentSmartAccountInstanceSigner).toBe(newOwner)
const tx = {
to: nftAddress,
data: encodeFunctionData({
abi: parseAbi(["function safeMint(address _to)"]),
functionName: "safeMint",
args: [smartAccountAddressTwo]
})
}
const { wait } = await _smartAccount.sendTransaction(tx, {
nonceOptions,
paymasterServiceData: { mode: PaymasterMode.SPONSORED }
})
const response = await wait()
expect(response.success).toBe("true")
}, 50000)
test.concurrent.skip(
"should check taiko network",
async () => {
const { privateKey, privateKeyTwo } = getConfig();
const customChain = taikoHekla
const chainId = customChain.id;
const bundlerUrl = getBundlerUrl(chainId);
const account = privateKeyToAccount(`0x${privateKey}`);
const recipientAccount = privateKeyToAccount(`0x${privateKeyTwo}`);
const walletClientWithCustomChain = createWalletClient({
account,
chain: customChain,
transport: http(),
})
const publicClient = createPublicClient({
chain: customChain,
transport: http(),
})
const smartAccount = await createSmartAccountClient({
signer: walletClientWithCustomChain,
bundlerUrl,
customChain,
})
const smartAccountAddress: Hex = await smartAccount.getAddress();
const [balance] = await smartAccount.getBalances();
if (balance.amount <= 1n) console.warn("Insufficient balance in smart account")
const recipientBalanceBefore = await checkBalance(recipientAccount.address, undefined, customChain);
const userOp = await smartAccount.buildUserOp([
{
to: recipientAccount.address,
value: 1n
}
])
userOp.signature = undefined
const signedUserOp = await smartAccount.signUserOp(userOp)
const entrypointContract = getContract({
address: DEFAULT_ENTRYPOINT_ADDRESS,
abi: EntryPointAbi,
client: { public: publicClient, wallet: walletClientWithCustomChain }
})
const hash = await entrypointContract.write.handleOps([
[
{
sender: signedUserOp.sender as Hex,
nonce: BigInt(signedUserOp.nonce ?? 0),
callGasLimit: BigInt(signedUserOp.callGasLimit ?? 0),
verificationGasLimit: BigInt(signedUserOp.verificationGasLimit ?? 0),
preVerificationGas: BigInt(signedUserOp.preVerificationGas ?? 0),
maxFeePerGas: BigInt(signedUserOp.maxFeePerGas ?? 0),
maxPriorityFeePerGas: BigInt(signedUserOp.maxPriorityFeePerGas ?? 0),
initCode: signedUserOp.initCode as Hex,
callData: signedUserOp.callData as Hex,
paymasterAndData: signedUserOp.paymasterAndData as Hex,
signature: signedUserOp.signature as Hex
}
],
account.address
])
const { status, transactionHash } =
await publicClient.waitForTransactionReceipt({ hash })
const recipientBalanceAfter = await checkBalance(recipientAccount.address, undefined, customChain);
expect(status).toBe("success")
expect(transactionHash).toBeTruthy()
expect(recipientBalanceAfter).toBe(recipientBalanceBefore + 1n)
},
70000
)
})