-
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
6 changed files
with
127 additions
and
4 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
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,91 @@ | ||
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, IEntryPointSimulations } from '../../typechain-types' | ||
|
||
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 const calcVerificationGasAndCallGasLimit = ( | ||
userOperation: SafeUserOperation, | ||
executionResult: { | ||
preOpGas: bigint | ||
paid: bigint | ||
}, | ||
) => { | ||
const verificationGasLimit = | ||
((executionResult.preOpGas - userOperation.preVerificationGas) * 3n) / | ||
2n | ||
|
||
let gasPrice: bigint | ||
if (userOperation.maxPriorityFeePerGas === userOperation.maxFeePerGas) { | ||
gasPrice = userOperation.maxFeePerGas | ||
} else { | ||
gasPrice = userOperation.maxFeePerGas | ||
} | ||
|
||
const calculatedCallGasLimit = | ||
executionResult.paid / gasPrice - 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 decodedSimulationData = entryPointSimulations.interface.decodeFunctionResult('simulateHandleOp', simulationData) as unknown as IEntryPointSimulations.ExecutionResultStructOutput | ||
|
||
console.log({ decodedSimulationData }) | ||
const callGasLimit = calcVerificationGasAndCallGasLimit(safeOp, decodedSimulationData, 1).callGasLimit | ||
|
||
return { | ||
callGasLimit, | ||
} | ||
} |