From 54302b69b54a084e10e84c80c47b3a44d31f7e1d Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Wed, 22 Nov 2023 11:16:50 +0100 Subject: [PATCH] Refactor E2E Test to Wait For User Operation Execution (#164) This PR makes a small refactor to the E2E test to make it wait for user operation execution based on sender and nonce instead of side effects. This makes the tests more robust and (IMO at least) more self-explanatory. --- 4337/test/e2e/LocalBundler.spec.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/4337/test/e2e/LocalBundler.spec.ts b/4337/test/e2e/LocalBundler.spec.ts index d5e924203..0a0158cbb 100644 --- a/4337/test/e2e/LocalBundler.spec.ts +++ b/4337/test/e2e/LocalBundler.spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai' import { deployments, ethers, network } from 'hardhat' import { buildSignatureBytes } from '../../src/utils/execution' -import { buildUserOperationFromSafeUserOperation, buildSafeUserOpTransaction, signSafeOp } from '../../src/utils/userOp' +import { buildUserOperationFromSafeUserOperation, buildSafeUserOpTransaction, signSafeOp, UserOperation } from '../../src/utils/userOp' import { chainId } from '../utils/encoding' import { MultiProvider4337, Safe4337 } from '../../src/utils/safe' @@ -75,11 +75,14 @@ describe('E2E - Local Bundler', () => { return new MultiProvider4337(url, ethers.provider) } - const waitFor = async (name: string, predicate: () => Promise, timeout = 10_000) => { + const waitForUserOp = async ({ sender, nonce }: Pick, timeout = 10_000) => { + const { address: entryPointAddress } = await deployments.get('EntryPoint') + const entryPoint = await ethers.getContractAt('INonceManager', entryPointAddress) const start = performance.now() - while (!(await predicate())) { + const key = BigInt(nonce) >> 64n + while ((await entryPoint.getNonce(sender, key)) <= BigInt(nonce)) { if (performance.now() - start > timeout) { - throw new Error(`timeout waiting for ${name}`) + throw new Error(`timeout waiting for user operation execution`) } await new Promise((resolve) => setTimeout(resolve, 10)) } @@ -112,8 +115,9 @@ describe('E2E - Local Bundler', () => { await bundler.sendUserOperation(userOp, await entryPoint.getAddress()) - await waitFor('user operation to execute', async () => (await token.balanceOf(safe.address)) == 0n) + await waitForUserOp(userOp) expect(ethers.dataLength(await ethers.provider.getCode(safe.address))).to.not.equal(0) + expect(await token.balanceOf(safe.address)).to.equal(0) expect(await ethers.provider.getBalance(safe.address)).to.be.lessThan(ethers.parseEther('0.5')) }) @@ -149,7 +153,8 @@ describe('E2E - Local Bundler', () => { await bundler.sendUserOperation(userOp, await entryPoint.getAddress()) - await waitFor('user operation to execute', async () => (await token.balanceOf(safe.address)) == 0n) + await waitForUserOp(userOp) + expect(await token.balanceOf(safe.address)).to.equal(0n) expect(await ethers.provider.getBalance(safe.address)).to.be.lessThan(ethers.parseEther('0.5')) }) })