Skip to content

Commit

Permalink
add a new logUserOperationGas function
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed Jun 19, 2024
1 parent 75e56fd commit 0b045e9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
27 changes: 27 additions & 0 deletions modules/4337/src/utils/execution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumberish, Signer, TransactionResponse, ethers } from 'ethers'
import { IEntryPoint } from '../../typechain-types'

export const EIP_DOMAIN = {
EIP712Domain: [
Expand Down Expand Up @@ -137,9 +138,35 @@ export const buildSignatureBytes = (signatures: SafeSignature[]): string => {
export const logGas = async (message: string, tx: Promise<TransactionResponse>, skip?: boolean): Promise<TransactionResponse> => {
return tx.then(async (result) => {
const receipt = await result.wait()
console.log({ receiptLogs: receipt.logs })

if (!receipt?.gasUsed) throw new Error('No gas used in receipt')

if (!skip) console.log(` Used ${receipt.gasUsed} gas for >${message}<`)
return result
})
}

export const logUserOperationGas = async (
message: string,
entryPoint: IEntryPoint,
tx: Promise<TransactionResponse>,
skip?: boolean,
): Promise<TransactionResponse> => {
return tx.then(async (result) => {
const receipt = await result.wait()

const userOperationEvent = await entryPoint.queryFilter(entryPoint.filters.UserOperationEvent(), receipt.blockNumber)
const parsedUserOperationEvent = entryPoint.interface.parseLog(userOperationEvent[0])

if (!receipt?.gasUsed) throw new Error('No gas used in receipt')
if (!parsedUserOperationEvent?.args.actualGasUsed)
throw new Error('No gas used in UserOperationEvent or UserOperationEvent not emitted')

if (!skip) {
console.log(` Used ${parsedUserOperationEvent.args.actualGasUsed} gas (Account or Paymaster) for >${message}<`)
console.log(` Used ${receipt.gasUsed} gas (Transaction) for >${message}<`)
}
return result
})
}
12 changes: 8 additions & 4 deletions modules/4337/test/erc4337/ERC4337ModuleExisting.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { deployments, ethers } from 'hardhat'
import { getTestSafe, getSafe4337Module, getEntryPoint } from '../utils/setup'
import { buildSignatureBytes, signHash, logGas } from '../../src/utils/execution'
import { buildSignatureBytes, signHash, logUserOperationGas } from '../../src/utils/execution'
import {
calculateSafeOperationHash,
buildPackedUserOperationFromSafeUserOperation,
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('Safe4337Module - Existing Safe', () => {
const safeOpHash = calculateSafeOperationHash(await validator.getAddress(), safeOp, await chainId())
const signature = buildSignatureBytes([await signHash(user1, safeOpHash)])
const userOp = buildPackedUserOperationFromSafeUserOperation({ safeOp, signature })
await logGas('Execute UserOp without a prefund payment', entryPoint.handleOps([userOp], relayer))
await logUserOperationGas('Execute UserOp without a prefund payment', entryPoint, entryPoint.handleOps([userOp], relayer))
expect(await ethers.provider.getBalance(await safe.getAddress())).to.be.eq(ethers.parseEther('0'))
})

Expand Down Expand Up @@ -116,7 +116,7 @@ describe('Safe4337Module - Existing Safe', () => {
const safeOpHash = calculateSafeOperationHash(await validator.getAddress(), safeOp, await chainId())
const signature = buildSignatureBytes([await signHash(user1, safeOpHash)])
const userOp = buildPackedUserOperationFromSafeUserOperation({ safeOp, signature })
await logGas('Execute UserOp with fee payment', entryPoint.handleOps([userOp], feeBeneficiary))
await logUserOperationGas('Execute UserOp with fee payment', entryPoint, entryPoint.handleOps([userOp], feeBeneficiary))

// checking that the fee was paid
expect(await ethers.provider.getBalance(feeBeneficiary)).to.be.gt(0)
Expand Down Expand Up @@ -169,7 +169,11 @@ describe('Safe4337Module - Existing Safe', () => {
const safeOpHash = calculateSafeOperationHash(await validator.getAddress(), safeOp, await chainId())
const signature = buildSignatureBytes([await signHash(user1, safeOpHash)])
const userOp = buildPackedUserOperationFromSafeUserOperation({ safeOp, signature })
await logGas('Execute UserOp without fee payment and bubble up error string', entryPoint.handleOps([userOp], relayer))
await logUserOperationGas(
'Execute UserOp without fee payment and bubble up error string',
entryPoint,
entryPoint.handleOps([userOp], relayer),
)
expect(await ethers.provider.getBalance(await safe.getAddress())).to.be.eq(ethers.parseEther('0.5'))
})

Expand Down
12 changes: 8 additions & 4 deletions modules/4337/test/erc4337/ERC4337ModuleNew.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { deployments, ethers } from 'hardhat'
import { getSafe4337Module, getEntryPoint, getFactory, getSafeModuleSetup, getSafeL2Singleton } from '../utils/setup'
import { buildSignatureBytes, logGas } from '../../src/utils/execution'
import { buildSignatureBytes, logUserOperationGas } from '../../src/utils/execution'
import { buildPackedUserOperationFromSafeUserOperation, buildSafeUserOpTransaction, signSafeOp } from '../../src/utils/userOp'
import { chainId } from '../utils/encoding'
import { Safe4337 } from '../../src/utils/safe'
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('Safe4337Module - Newly deployed safe', () => {
safeOp,
signature,
})
await logGas('Execute UserOp without fee payment', entryPoint.handleOps([userOp], user1.address))
await logUserOperationGas('Execute UserOp without fee payment', entryPoint, entryPoint.handleOps([userOp], user1.address))
expect(await ethers.provider.getBalance(safe.address)).to.be.eq(ethers.parseEther('0'))
})

Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Safe4337Module - Newly deployed safe', () => {
safeOp,
signature,
})
await logGas('Execute UserOp with fee payment', entryPoint.handleOps([userOp], feeBeneficiary))
await logUserOperationGas('Execute UserOp with fee payment', entryPoint, entryPoint.handleOps([userOp], feeBeneficiary))

// checking that the fee was paid
expect(await ethers.provider.getBalance(feeBeneficiary)).to.be.gt(0)
Expand Down Expand Up @@ -211,7 +211,11 @@ describe('Safe4337Module - Newly deployed safe', () => {
safeOp,
signature,
})
await logGas('Execute UserOp with fee payment and bubble up error string', entryPoint.handleOps([userOp], user1.address))
await logUserOperationGas(
'Execute UserOp with fee payment and bubble up error string',
entryPoint,
entryPoint.handleOps([userOp], user1.address),
)
expect(await ethers.provider.getBalance(safe.address)).to.be.eq(ethers.parseEther('0.5'))
})

Expand Down
17 changes: 13 additions & 4 deletions modules/4337/test/erc4337/ReferenceEntryPoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deployments, ethers } from 'hardhat'
import { time } from '@nomicfoundation/hardhat-network-helpers'
import { EventLog, Log } from 'ethers'
import { getEntryPoint, getFactory, getSafeModuleSetup } from '../utils/setup'
import { buildSignatureBytes, logGas } from '../../src/utils/execution'
import { buildSignatureBytes, logUserOperationGas } from '../../src/utils/execution'
import {
buildSafeUserOpTransaction,
buildPackedUserOperationFromSafeUserOperation,
Expand Down Expand Up @@ -78,7 +78,11 @@ describe('Safe4337Module - Reference EntryPoint', () => {
}),
)

const transaction = await logGas('Execute UserOps with reference EntryPoint', entryPoint.handleOps(userOps, await relayer.getAddress()))
const transaction = await logUserOperationGas(
'Execute UserOps with reference EntryPoint',
entryPoint,
entryPoint.handleOps(userOps, await relayer.getAddress()),
)
const receipt = await transaction.wait()

const transfers = ethers.parseEther('0.1') * BigInt(userOps.length)
Expand Down Expand Up @@ -132,7 +136,11 @@ describe('Safe4337Module - Reference EntryPoint', () => {
.withArgs(0, 'AA22 expired or not due')
await time.increaseTo(validAfter + 1)

const transaction = await logGas('Execute UserOps with reference EntryPoint', entryPoint.handleOps(userOps, await relayer.getAddress()))
const transaction = await logUserOperationGas(
'Execute UserOps with reference EntryPoint',
entryPoint,
entryPoint.handleOps(userOps, await relayer.getAddress()),
)
const receipt = await transaction.wait()

const transfers = ethers.parseEther('0.1') * BigInt(userOps.length)
Expand Down Expand Up @@ -191,8 +199,9 @@ describe('Safe4337Module - Reference EntryPoint', () => {
signature,
})

const transaction = await logGas(
const transaction = await logUserOperationGas(
'Execute UserOps with reference EntryPoint',
entryPoint,
entryPoint.handleOps([userOp], await relayer.getAddress()),
)
const receipt = await transaction.wait()
Expand Down
6 changes: 3 additions & 3 deletions modules/4337/test/erc4337/Safe4337Mock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { deployments, ethers } from 'hardhat'
import { getEntryPoint, get4337TestSafe } from '../utils/setup'
import { buildSignatureBytes, signHash, logGas } from '../../src/utils/execution'
import { buildSignatureBytes, signHash, logUserOperationGas } from '../../src/utils/execution'
import {
buildSafeUserOp,
buildSafeUserOpTransaction,
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('Safe4337Mock', () => {
const safeOpHash = calculateSafeOperationHash(await validator.getAddress(), safeOp, await chainId())
const signature = buildSignatureBytes([await signHash(user1, safeOpHash)])
const userOp = buildPackedUserOperationFromSafeUserOperation({ safeOp, signature })
await logGas('Execute UserOp without fee payment', entryPoint.handleOps([userOp], user1.address))
await logUserOperationGas('Execute UserOp without fee payment', entryPoint, entryPoint.handleOps([userOp], user1.address))
expect(await ethers.provider.getBalance(await safe.getAddress())).to.be.eq(ethers.parseEther('0.5'))
})

Expand All @@ -72,7 +72,7 @@ describe('Safe4337Mock', () => {
const safeOpHash = calculateSafeOperationHash(await validator.getAddress(), safeOp, await chainId())
const signature = buildSignatureBytes([await signHash(user1, safeOpHash)])
const userOp = buildPackedUserOperationFromSafeUserOperation({ safeOp, signature })
await logGas('Execute UserOp with fee payment', entryPoint.handleOps([userOp], feeBeneficiary))
await logUserOperationGas('Execute UserOp with fee payment', entryPoint, entryPoint.handleOps([userOp], feeBeneficiary))

// checking that the fee was paid
expect(await ethers.provider.getBalance(feeBeneficiary)).to.be.gt(0)
Expand Down
28 changes: 20 additions & 8 deletions modules/4337/test/gas/Gas.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { deployments, ethers } from 'hardhat'
import { getSafe4337Module, getEntryPoint, getFactory, getSafeModuleSetup, getSafeL2Singleton } from '../utils/setup'
import { buildSignatureBytes, logGas } from '../../src/utils/execution'
import { buildSignatureBytes, logUserOperationGas } from '../../src/utils/execution'
import { buildPackedUserOperationFromSafeUserOperation, buildSafeUserOpTransaction, signSafeOp } from '../../src/utils/userOp'
import { chainId } from '../utils/encoding'
import { Safe4337 } from '../../src/utils/safe'
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('Gas Metering', () => {
signature,
})

await logGas('Safe with 4337 Module Deployment', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas('Safe with 4337 Module Deployment', entryPoint, entryPoint.handleOps([userOp], user.address))

expect(ethers.dataLength(await ethers.provider.getCode(safe.address))).to.not.equal(0)
})
Expand Down Expand Up @@ -108,7 +108,11 @@ describe('Gas Metering', () => {
signature,
})

await logGas('Safe with 4337 Module Deployment + Native Transfer', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas(
'Safe with 4337 Module Deployment + Native Transfer',
entryPoint,
entryPoint.handleOps([userOp], user.address),
)

const recipientBalAfter = await ethers.provider.getBalance(receiver)
expect(ethers.dataLength(await ethers.provider.getCode(safe.address))).to.not.equal(0)
Expand Down Expand Up @@ -143,7 +147,7 @@ describe('Gas Metering', () => {
signature,
})

await logGas('Safe with 4337 Module Native Transfer', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas('Safe with 4337 Module Native Transfer', entryPoint, entryPoint.handleOps([userOp], user.address))

expect(await ethers.provider.getBalance(receiver)).to.equal(amount)
})
Expand Down Expand Up @@ -182,7 +186,11 @@ describe('Gas Metering', () => {
signature,
})

await logGas('Safe with 4337 Module Deployment + ERC20 Transfer', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas(
'Safe with 4337 Module Deployment + ERC20 Transfer',
entryPoint,
entryPoint.handleOps([userOp], user.address),
)
expect(await erc20Token.balanceOf(safe.address)).to.equal(0)
expect(ethers.dataLength(await ethers.provider.getCode(safe.address))).to.not.equal(0)
})
Expand Down Expand Up @@ -215,7 +223,11 @@ describe('Gas Metering', () => {
})

expect(await erc721Token.balanceOf(safe.address)).to.equal(0)
await logGas('Safe with 4337 Module Deployment + ERC721 Transfer', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas(
'Safe with 4337 Module Deployment + ERC721 Transfer',
entryPoint,
entryPoint.handleOps([userOp], user.address),
)
expect(await erc721Token.balanceOf(safe.address)).to.equal(1)
expect(await erc721Token.ownerOf(tokenID)).to.equal(safe.address)
expect(ethers.dataLength(await ethers.provider.getCode(safe.address))).to.not.equal(0)
Expand Down Expand Up @@ -254,7 +266,7 @@ describe('Gas Metering', () => {
signature,
})

await logGas('Safe with 4337 Module ERC20 Transfer', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas('Safe with 4337 Module ERC20 Transfer', entryPoint, entryPoint.handleOps([userOp], user.address))

expect(await erc20Token.balanceOf(safe.address)).to.equal(0)
})
Expand Down Expand Up @@ -289,7 +301,7 @@ describe('Gas Metering', () => {
})

expect(await erc721Token.balanceOf(safe.address)).to.equal(0)
await logGas('Safe with 4337 Module ERC721 Transfer', entryPoint.handleOps([userOp], user.address))
await logUserOperationGas('Safe with 4337 Module ERC721 Transfer', entryPoint, entryPoint.handleOps([userOp], user.address))
expect(await erc721Token.balanceOf(safe.address)).to.equal(1)
expect(await erc721Token.ownerOf(tokenID)).to.equal(safe.address)
})
Expand Down

0 comments on commit 0b045e9

Please sign in to comment.