|
1 | 1 | import { ethers } from "ethers";
|
2 |
| -import { Client, Presets } from "userop"; |
| 2 | +import { |
| 3 | + BundlerJsonRpcProvider, |
| 4 | + Client, |
| 5 | + Presets, |
| 6 | + UserOperationBuilder, |
| 7 | +} from "userop"; |
3 | 8 | import { errorCodes } from "../src/errors";
|
4 | 9 | import { TestAccount } from "../src/testAccount";
|
5 | 10 | import config from "../config";
|
6 | 11 |
|
7 | 12 | describe("During the verification phase", () => {
|
| 13 | + const provider = new BundlerJsonRpcProvider(config.nodeUrl).setBundlerRpc( |
| 14 | + config.bundlerUrl |
| 15 | + ); |
8 | 16 | let client: Client;
|
9 | 17 | let acc: TestAccount;
|
10 | 18 | beforeAll(async () => {
|
@@ -59,49 +67,126 @@ describe("During the verification phase", () => {
|
59 | 67 | });
|
60 | 68 | });
|
61 | 69 |
|
62 |
| - describe("With state overrides", () => { |
63 |
| - test("Sender with zero funds can successfully estimate UserOperation gas", async () => { |
64 |
| - expect.assertions(4); |
| 70 | + describe("With no gas fees", () => { |
| 71 | + test("Sender with funds can estimate gas and send", async () => { |
| 72 | + const signer = new ethers.Wallet(config.signingKey); |
| 73 | + const fundedAcc = await Presets.Builder.SimpleAccount.init( |
| 74 | + signer, |
| 75 | + config.nodeUrl, |
| 76 | + { |
| 77 | + overrideBundlerRpc: config.bundlerUrl, |
| 78 | + } |
| 79 | + ); |
| 80 | + const op = await client.buildUserOperation( |
| 81 | + fundedAcc.execute( |
| 82 | + ethers.constants.AddressZero, |
| 83 | + ethers.constants.Zero, |
| 84 | + "0x" |
| 85 | + ) |
| 86 | + ); |
| 87 | + |
| 88 | + const builderWithEstimate = new UserOperationBuilder() |
| 89 | + .useDefaults({ |
| 90 | + ...op, |
| 91 | + maxFeePerGas: 0, |
| 92 | + maxPriorityFeePerGas: 0, |
| 93 | + }) |
| 94 | + .useMiddleware(Presets.Middleware.estimateUserOperationGas(provider)); |
| 95 | + const opWithEstimate = await client.buildUserOperation( |
| 96 | + builderWithEstimate |
| 97 | + ); |
| 98 | + expect(ethers.BigNumber.from(opWithEstimate.maxFeePerGas).isZero()).toBe( |
| 99 | + true |
| 100 | + ); |
| 101 | + expect( |
| 102 | + ethers.BigNumber.from(opWithEstimate.maxPriorityFeePerGas).isZero() |
| 103 | + ).toBe(true); |
| 104 | + |
| 105 | + const builderWithGasPrice = new UserOperationBuilder() |
| 106 | + .useDefaults(opWithEstimate) |
| 107 | + .useMiddleware(Presets.Middleware.getGasPrice(provider)) |
| 108 | + .useMiddleware(Presets.Middleware.signUserOpHash(signer)); |
| 109 | + const response = await client.sendUserOperation(builderWithGasPrice); |
| 110 | + const event = await response.wait(); |
| 111 | + expect(event?.args.success).toBe(true); |
| 112 | + }); |
| 113 | + |
| 114 | + test("Sender with zero funds can estimate gas but cannot send", async () => { |
| 115 | + expect.assertions(3); |
| 116 | + const signer = new ethers.Wallet(ethers.utils.randomBytes(32)); |
65 | 117 | const randAcc = await Presets.Builder.SimpleAccount.init(
|
66 |
| - new ethers.Wallet(ethers.utils.randomBytes(32)), |
| 118 | + signer, |
67 | 119 | config.nodeUrl,
|
68 | 120 | {
|
69 | 121 | overrideBundlerRpc: config.bundlerUrl,
|
70 | 122 | }
|
71 | 123 | );
|
72 |
| - randAcc |
73 |
| - .setPreVerificationGas(0) |
74 |
| - .setVerificationGasLimit(0) |
75 |
| - .setCallGasLimit(0); |
| 124 | + const op = await client.buildUserOperation( |
| 125 | + randAcc.execute( |
| 126 | + ethers.constants.AddressZero, |
| 127 | + ethers.constants.Zero, |
| 128 | + "0x" |
| 129 | + ) |
| 130 | + ); |
| 131 | + |
| 132 | + const builderWithEstimate = new UserOperationBuilder() |
| 133 | + .useDefaults({ |
| 134 | + ...op, |
| 135 | + maxFeePerGas: 0, |
| 136 | + maxPriorityFeePerGas: 0, |
| 137 | + }) |
| 138 | + .useMiddleware(Presets.Middleware.estimateUserOperationGas(provider)); |
| 139 | + const opWithEstimate = await client.buildUserOperation( |
| 140 | + builderWithEstimate |
| 141 | + ); |
| 142 | + expect(ethers.BigNumber.from(opWithEstimate.maxFeePerGas).isZero()).toBe( |
| 143 | + true |
| 144 | + ); |
| 145 | + expect( |
| 146 | + ethers.BigNumber.from(opWithEstimate.maxPriorityFeePerGas).isZero() |
| 147 | + ).toBe(true); |
76 | 148 |
|
77 | 149 | try {
|
78 |
| - await client.sendUserOperation( |
79 |
| - randAcc.execute(randAcc.getSender(), ethers.constants.Zero, "0x") |
80 |
| - ); |
| 150 | + const builderWithGasPrice = new UserOperationBuilder() |
| 151 | + .useDefaults(opWithEstimate) |
| 152 | + .useMiddleware(Presets.Middleware.getGasPrice(provider)) |
| 153 | + .useMiddleware(Presets.Middleware.signUserOpHash(signer)); |
| 154 | + await client.sendUserOperation(builderWithGasPrice); |
81 | 155 | } catch (error: any) {
|
82 | 156 | expect(error?.error.code).toBe(errorCodes.rejectedByEpOrAccount);
|
83 | 157 | }
|
| 158 | + }); |
| 159 | + }); |
84 | 160 |
|
85 |
| - await client.sendUserOperation( |
86 |
| - randAcc.execute(randAcc.getSender(), ethers.constants.Zero, "0x"), |
| 161 | + describe("With state overrides", () => { |
| 162 | + test("New sender will fail estimation if it uses its actual balance", async () => { |
| 163 | + expect.assertions(1); |
| 164 | + const randAcc = await Presets.Builder.SimpleAccount.init( |
| 165 | + new ethers.Wallet(ethers.utils.randomBytes(32)), |
| 166 | + config.nodeUrl, |
87 | 167 | {
|
88 |
| - dryRun: true, |
89 |
| - stateOverrides: { |
90 |
| - [randAcc.getSender()]: { |
91 |
| - balance: ethers.constants.MaxUint256.toHexString(), |
92 |
| - }, |
93 |
| - }, |
94 |
| - onBuild(op) { |
95 |
| - expect(ethers.BigNumber.from(op.preVerificationGas).gte(0)).toBe( |
96 |
| - true |
97 |
| - ); |
98 |
| - expect(ethers.BigNumber.from(op.verificationGasLimit).gte(0)).toBe( |
99 |
| - true |
100 |
| - ); |
101 |
| - expect(ethers.BigNumber.from(op.callGasLimit).gte(0)).toBe(true); |
102 |
| - }, |
| 168 | + overrideBundlerRpc: config.bundlerUrl, |
103 | 169 | }
|
104 | 170 | );
|
| 171 | + |
| 172 | + try { |
| 173 | + await client.buildUserOperation( |
| 174 | + randAcc.execute( |
| 175 | + ethers.constants.AddressZero, |
| 176 | + ethers.constants.Zero, |
| 177 | + "0x" |
| 178 | + ), |
| 179 | + { |
| 180 | + [randAcc.getSender()]: { |
| 181 | + balance: ethers.utils.hexValue( |
| 182 | + await provider.getBalance(randAcc.getSender()) |
| 183 | + ), |
| 184 | + }, |
| 185 | + } |
| 186 | + ); |
| 187 | + } catch (error: any) { |
| 188 | + expect(error?.error.code).toBe(errorCodes.rejectedByEpOrAccount); |
| 189 | + } |
105 | 190 | });
|
106 | 191 | });
|
107 | 192 | });
|
0 commit comments