Skip to content

Commit

Permalink
Refactor E2E Test to Wait For User Operation Execution (#164)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nlordell authored Nov 22, 2023
1 parent 307a3b4 commit 54302b6
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions 4337/test/e2e/LocalBundler.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -75,11 +75,14 @@ describe('E2E - Local Bundler', () => {
return new MultiProvider4337(url, ethers.provider)
}

const waitFor = async (name: string, predicate: () => Promise<boolean>, timeout = 10_000) => {
const waitForUserOp = async ({ sender, nonce }: Pick<UserOperation, 'sender' | 'nonce'>, 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))
}
Expand Down Expand Up @@ -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'))
})

Expand Down Expand Up @@ -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'))
})
})

0 comments on commit 54302b6

Please sign in to comment.