-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,539 additions
and
952 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "@account-abstraction/contracts/core/EntryPointSimulations.sol"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { DeployFunction } from 'hardhat-deploy/types' | ||
|
||
const deploy: DeployFunction = async ({ deployments, getNamedAccounts, network }) => { | ||
if (!network.tags.test) { | ||
return | ||
} | ||
|
||
const { deployer } = await getNamedAccounts() | ||
const { deploy } = deployments | ||
|
||
await deploy('EntryPointSimulations', { | ||
from: deployer, | ||
args: [], | ||
log: true, | ||
deterministicDeployment: true, | ||
}) | ||
} | ||
|
||
export default deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { ethers } from 'ethers' | ||
import { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider' | ||
import { buildPackedUserOperationFromSafeUserOperation, PLACEHOLDER_SIGNATURE, SafeUserOperation } from '../../src/utils/userOp' | ||
import { EntryPointSimulations } from '../../typechain-types' | ||
|
||
export interface GasOverheads { | ||
/** | ||
* fixed overhead for entire handleOp bundle. | ||
*/ | ||
fixed: number | ||
|
||
/** | ||
* per userOp overhead, added on top of the above fixed per-bundle. | ||
*/ | ||
perUserOp: number | ||
|
||
/** | ||
* overhead for userOp word (32 bytes) block | ||
*/ | ||
perUserOpWord: number | ||
|
||
// perCallDataWord: number | ||
|
||
/** | ||
* zero byte cost, for calldata gas cost calculations | ||
*/ | ||
zeroByte: number | ||
|
||
/** | ||
* non-zero byte cost, for calldata gas cost calculations | ||
*/ | ||
nonZeroByte: number | ||
|
||
/** | ||
* expected bundle size, to split per-bundle overhead between all ops. | ||
*/ | ||
bundleSize: number | ||
|
||
/** | ||
* expected length of the userOp signature. | ||
*/ | ||
sigSize: number | ||
} | ||
|
||
/** | ||
* calculate the preVerificationGas of the given UserOperation | ||
* preVerificationGas (by definition) is the cost overhead that can't be calculated on-chain. | ||
* it is based on parameters that are defined by the Ethereum protocol for external transactions. | ||
* @param userOp filled userOp to calculate. The only possible missing fields can be the signature and preVerificationGas itself | ||
* @param overheads gas overheads to use, to override the default values | ||
*/ | ||
export const calcPreVerificationGas = (userOp: SafeUserOperation): number => { | ||
const gasOverheads: GasOverheads = { | ||
fixed: 21000, | ||
perUserOp: 18300, | ||
perUserOpWord: 4, | ||
zeroByte: 4, | ||
nonZeroByte: 16, | ||
bundleSize: 1, | ||
sigSize: 65, | ||
} | ||
const op: SafeUserOperation = { | ||
// dummy values, in case the UserOp is incomplete. | ||
...userOp, | ||
preVerificationGas: 21000, // dummy value, just for calldata cost | ||
} | ||
|
||
const packed = buildPackedUserOperationFromSafeUserOperation({ safeOp: op, signature: PLACEHOLDER_SIGNATURE }) | ||
const encoded = ethers.toBeArray( | ||
ethers.AbiCoder.defaultAbiCoder().encode( | ||
['address', 'uint256', 'bytes', 'bytes', 'bytes32', 'uint256', 'bytes32', 'bytes', 'bytes'], | ||
[ | ||
packed.sender, | ||
packed.nonce, | ||
packed.initCode, | ||
packed.callData, | ||
packed.accountGasLimits, | ||
packed.preVerificationGas, | ||
packed.gasFees, | ||
packed.paymasterAndData, | ||
packed.signature, | ||
], | ||
), | ||
) | ||
const lengthInWord = (encoded.length + 31) / 32 | ||
const callDataCost = encoded.map((x) => (x === 0 ? gasOverheads.zeroByte : gasOverheads.nonZeroByte)).reduce((sum, x) => sum + x) | ||
const ret = Math.round( | ||
callDataCost + gasOverheads.fixed / gasOverheads.bundleSize + gasOverheads.perUserOp + gasOverheads.perUserOpWord * lengthInWord, | ||
) | ||
return ret | ||
} | ||
|
||
export interface EstimateUserOpGasResult { | ||
/** | ||
* the preVerification gas used by this UserOperation. | ||
*/ | ||
preVerificationGas: bigint | ||
/** | ||
* gas used for validation of this UserOperation, including account creation | ||
*/ | ||
verificationGasLimit: bigint | ||
|
||
/** | ||
* (possibly future timestamp) after which this UserOperation is valid | ||
*/ | ||
validAfter?: bigint | ||
|
||
/** | ||
* the deadline after which this UserOperation is invalid (not a gas estimation parameter, but returned by validation | ||
*/ | ||
validUntil?: bigint | ||
/** | ||
* estimated cost of calling the account with the given callData | ||
*/ | ||
callGasLimit: bigint | ||
} | ||
|
||
export type ExecutionResultStructOutput = [ | ||
preOpGas: ethers.BigNumberish, | ||
paid: ethers.BigNumberish, | ||
accountValidationData: ethers.BigNumberish, | ||
paymasterValidationData: ethers.BigNumberish, | ||
targetSuccess: boolean, | ||
targetResult: string, | ||
] & { | ||
preOpGas: ethers.BigNumberish | ||
paid: ethers.BigNumberish | ||
accountValidationData: ethers.BigNumberish | ||
paymasterValidationData: ethers.BigNumberish | ||
targetSuccess: boolean | ||
targetResult: string | ||
} | ||
|
||
export const calcVerificationGasAndCallGasLimit = (userOperation: SafeUserOperation, executionResult: ExecutionResultStructOutput) => { | ||
const verificationGasLimit = ((BigInt(executionResult.preOpGas) - BigInt(userOperation.preVerificationGas)) * 3n) / 2n | ||
|
||
const gasPrice = BigInt(userOperation.maxFeePerGas) | ||
|
||
const calculatedCallGasLimit = BigInt(executionResult.paid) / gasPrice - BigInt(executionResult.preOpGas) | ||
|
||
const callGasLimit = (calculatedCallGasLimit > 9000n ? calculatedCallGasLimit : 9000n) + 21000n + 50000n | ||
|
||
return { verificationGasLimit, callGasLimit } | ||
} | ||
|
||
export const estimateUserOperationGas = async ( | ||
provider: HardhatEthersProvider, | ||
entryPointSimulations: EntryPointSimulations, | ||
safeOp: SafeUserOperation, | ||
entryPointAddress: string, | ||
// ): Promise<EstimateUserOpGasResult> => { | ||
) => { | ||
const packedUserOp = buildPackedUserOperationFromSafeUserOperation({ safeOp, signature: PLACEHOLDER_SIGNATURE }) | ||
const encodedSimulateHandleOp = entryPointSimulations.interface.encodeFunctionData('simulateHandleOp', [ | ||
packedUserOp, | ||
ethers.ZeroAddress, | ||
'0x', | ||
]) | ||
|
||
const simulationData = await provider.send('eth_call', [ | ||
{ | ||
to: entryPointAddress, | ||
data: encodedSimulateHandleOp, | ||
}, | ||
'latest', | ||
{ | ||
[entryPointAddress]: { | ||
code: await entryPointSimulations.getDeployedCode(), | ||
}, | ||
}, | ||
]) | ||
const executionResultStruct = entryPointSimulations.interface.decodeFunctionResult( | ||
'simulateHandleOp', | ||
simulationData, | ||
)[0] as unknown as ExecutionResultStructOutput | ||
|
||
console.log({ executionResultStruct }) | ||
const { verificationGasLimit, callGasLimit } = calcVerificationGasAndCallGasLimit(safeOp, executionResultStruct) | ||
|
||
return { | ||
callGasLimit, | ||
verificationGasLimit, | ||
} | ||
} |
Oops, something went wrong.