Skip to content

Commit

Permalink
added custom factory, exec for zerodev
Browse files Browse the repository at this point in the history
  • Loading branch information
drortirosh committed Apr 8, 2024
1 parent 66ab9b4 commit eb8bf79
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
60 changes: 47 additions & 13 deletions gascalc/7-zerodev-kernel-lite.gas.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
import { GasCheckCollector, GasChecker } from './GasChecker'
import { createAccountOwner } from '../test/testutils'
import { ethers } from 'hardhat'

import { readFileSync } from 'fs'
import { BigNumberish } from 'ethers'
import { defaultAbiCoder, Interface } from 'ethers/lib/utils'
import { log } from 'console'
// TODO: NOTE: Must be executed separately as otherwise test will reuse SimpleAccount
context.only('simple account', function () {
this.timeout(60000)
const g = new GasChecker()

const kernelDir = __dirname + '/../../zerodev-kernel'
const accounts = JSON.parse(readFileSync(kernelDir + '/deployed.txt', 'ascii'))
const zkLite0 = accounts.address0
const zkLite1 = accounts.address1

const kernelLiteFactory = JSON.parse(readFileSync(kernelDir + '/deployments//localhost/KernelFactory.json', 'ascii')).address
const ECDSAValidator = JSON.parse(readFileSync(kernelDir + '/deployments//localhost/ECDSAValidator.json', 'ascii')).address
const kernelLiteECDSA = JSON.parse(readFileSync(kernelDir + '/deployments//localhost/KernelLiteECDSA.json', 'ascii')).address
const kernelFunctions = new Interface([
'function createAccount(address,bytes,uint256)',
'function execute(address,uint256,bytes,uint8)',
'function executeBatch((address,uint256,bytes)[])',
'function initialize(address,bytes)'
])
const factoryInfo = async (owner: string, salt: string): Promise<any> => {
const initData = kernelFunctions.encodeFunctionData(
'initialize', [ECDSAValidator, owner])

console.log('create factoryinfo for ', owner, 'salt=', salt)
const ret = {
factory: kernelLiteFactory,
// salt "0" was already created..
factoryData: kernelFunctions.encodeFunctionData('createAccount', [kernelLiteECDSA, initData, salt])
}
console.log('factoryInfo= ', ret)
return ret
}

const execInfo = (target: string, value: BigNumberish, data: string): string =>
kernelFunctions.encodeFunctionData('execute', [target, value, data, 0])

// deployed by 'hardhat deploy' command in Zerodev repo fork
const zkLite0 = '0xbA1ee907417BA5B9D77E7Eb53F0666972113b406'
const zkLite1 = '0x56842B386eAd36dA47A9b1f7dE5d9b83161Cbe86'

before(async function () {
await GasCheckCollector.init()
Expand All @@ -21,9 +53,9 @@ context.only('simple account', function () {
GasCheckCollector.inst.setContractName(zkLite0, 'ERC1967Proxy')
GasCheckCollector.inst.setContractName(zkLite1, 'ERC1967Proxy')
// todo: read this from deployed
GasCheckCollector.inst.setContractName('0x0aB1A1Dfb7FAa89aCE93c489120F5a8b602B1a99', 'ECDSAValidator')
GasCheckCollector.inst.setContractName('0x3fb14a424e5beD8E6ba066913fDA7882Aa2E1A58', 'KernelLiteECDSA')
GasCheckCollector.inst.setContractName('0xB668C3f3D2155d8b37F9a6483d79570FC8659c52', 'KernelFactory')
GasCheckCollector.inst.setContractName(ECDSAValidator, 'ECDSAValidator')
GasCheckCollector.inst.setContractName(kernelLiteECDSA, 'KernelLiteECDSA')
GasCheckCollector.inst.setContractName(kernelLiteFactory, 'KernelFactory')

await ethers.provider.getSigner().sendTransaction({ to: zkLite0, value: 1e18.toString() })
await ethers.provider.getSigner().sendTransaction({ to: zkLite1, value: 1e18.toString() })
Expand All @@ -33,16 +65,18 @@ context.only('simple account', function () {
await g.addTestRow({
title: 'zd-kernel-lite',
count: 1,
factoryInfo,
execInfo,
skipAccountCreation: true,
appendZerodevMode: true,
diffLastGas: false
})
await g.addTestRow({
title: 'zd-kernel-lite - diff from previous',
count: 2,
skipAccountCreation: true,
appendZerodevMode: true,
diffLastGas: true
})
// await g.addTestRow({
// title: 'zd-kernel-lite - diff from previous',
// count: 2,
// skipAccountCreation: true,
// appendZerodevMode: true,
// diffLastGas: true
// })
})
})
22 changes: 20 additions & 2 deletions gascalc/GasChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface GasTestInfo {
paymaster: string
skipAccountCreation: boolean
appendZerodevMode: boolean
factoryInfo?: (owner: string, salt: string) => Promise<{ factory: string, factoryData: string }>
execInfo?: (target: string, value: BigNumberish, data: string) => string
count: number
// address, or 'random' or 'self' (for account itself)
dest: string
Expand Down Expand Up @@ -206,7 +208,19 @@ export class GasChecker {
let accountEst: number = 0
const userOps = await Promise.all(range(info.count)
.map(index => Object.entries(this.accounts)[index])
.map(async ([account, accountOwner]) => {
.map(async ([account, accountOwner], index) => {
let initCode: string | undefined

if (params.factoryInfo != null) {
const f = await params.factoryInfo(accountOwner.address, index.toString())
const ret = await provider.call({ to: f.factory, data: f.factoryData })
account = defaultAbiCoder.decode(['address'], ret)[0]
// if ((await getBalance(account)).eq(0)) {
console.log('replenish initcode account', account)
await ethersSigner.sendTransaction({ to: account, value: parseEther('1') })
// }
initCode = hexConcat([f.factory, f.factoryData])
}
const paymaster = info.paymaster

let { dest, destValue, destCallData } = info
Expand All @@ -221,7 +235,9 @@ export class GasChecker {
await ethersSigner.sendTransaction({ to: dest, value: 1 })
}
}
const accountExecFromEntryPoint = this.accountExec(dest, destValue, destCallData)
const accountExecFromEntryPoint = params.execInfo != null
? params.execInfo(dest, destValue, destCallData)
: this.accountExec(dest, destValue, destCallData)

// remove the "dest" from the key to the saved estimations
// so we have a single estimation per method.
Expand All @@ -239,8 +255,10 @@ export class GasChecker {
}
// console.debug('== account est=', accountEst.toString())
accountEst = est.accountEst

const op = await fillAndSign({
sender: account,
initCode,
callData: accountExecFromEntryPoint,
maxPriorityFeePerGas: info.gasPrice,
maxFeePerGas: info.gasPrice,
Expand Down

0 comments on commit eb8bf79

Please sign in to comment.