-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move v06 gasEstimation into own folder
- Loading branch information
1 parent
2418772
commit fde6a08
Showing
7 changed files
with
244 additions
and
452 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,150 @@ | ||
import { | ||
EntryPointV06Abi, | ||
RpcError, | ||
ValidationErrors, | ||
EntryPointV06SimulationsAbi, | ||
executionResultSchema, | ||
hexDataSchema | ||
} from "@alto/types" | ||
import type { StateOverrides, UserOperationV06 } from "@alto/types" | ||
import type { Hex, RpcRequestErrorType } from "viem" | ||
import { | ||
type Address, | ||
type PublicClient, | ||
decodeErrorResult, | ||
encodeFunctionData, | ||
toHex | ||
} from "viem" | ||
import { z } from "zod" | ||
import type { SimulateHandleOpResult } from "./gasEstimation" | ||
|
||
export async function simulateHandleOpV06( | ||
userOperation: UserOperationV06, | ||
entryPoint: Address, | ||
publicClient: PublicClient, | ||
targetAddress: Address, | ||
targetCallData: Hex, | ||
blockTagSupport: boolean, | ||
utilityWalletAddress: Address, | ||
finalParam: StateOverrides | undefined = undefined, | ||
fixedGasLimitForEstimation?: bigint | ||
): Promise<SimulateHandleOpResult> { | ||
try { | ||
await publicClient.request({ | ||
method: "eth_call", | ||
params: [ | ||
{ | ||
to: entryPoint, | ||
from: utilityWalletAddress, | ||
data: encodeFunctionData({ | ||
abi: EntryPointV06Abi, | ||
functionName: "simulateHandleOp", | ||
args: [userOperation, targetAddress, targetCallData] | ||
}), | ||
...(fixedGasLimitForEstimation !== undefined && { | ||
gas: `0x${fixedGasLimitForEstimation.toString(16)}` | ||
}) | ||
}, | ||
blockTagSupport | ||
? "latest" | ||
: toHex(await publicClient.getBlockNumber()), | ||
// @ts-ignore | ||
...(finalParam ? [finalParam] : []) | ||
] | ||
}) | ||
} catch (e) { | ||
const err = e as RpcRequestErrorType | ||
|
||
if ( | ||
/return data out of bounds.*|EVM error OutOfOffset.*/.test( | ||
err.details | ||
) | ||
) { | ||
// out of bound (low level evm error) occurs when paymaster reverts with less than 32bytes | ||
return { | ||
result: "failed", | ||
data: "AA50 postOp revert (paymaster revert data out of bounds)" | ||
} as const | ||
} | ||
|
||
const causeParseResult = z | ||
.union([ | ||
z.object({ | ||
code: z.literal(3), | ||
message: z.string(), | ||
data: hexDataSchema | ||
}), | ||
/* Fuse RPCs return in this format. */ | ||
z.object({ | ||
code: z.number(), | ||
message: z.string().regex(/VM execution error.*/), | ||
data: z | ||
.string() | ||
.transform((data) => data.replace("Reverted ", "")) | ||
.pipe(hexDataSchema) | ||
}), | ||
z.object({ | ||
code: z.number(), | ||
message: z | ||
.string() | ||
.regex(/VM Exception while processing transaction:.*/), | ||
data: hexDataSchema | ||
}) | ||
]) | ||
.safeParse(err.cause) | ||
|
||
if (!causeParseResult.success) { | ||
throw new Error(JSON.stringify(err.cause)) | ||
} | ||
|
||
const cause = causeParseResult.data | ||
|
||
if (cause.data === "0x") { | ||
throw new RpcError( | ||
"AA23 reverted: UserOperation called non-existant contract, or reverted with 0x", | ||
ValidationErrors.SimulateValidation | ||
) | ||
} | ||
|
||
const decodedError = decodeErrorResult({ | ||
abi: [...EntryPointV06Abi, ...EntryPointV06SimulationsAbi], | ||
data: cause.data | ||
}) | ||
|
||
if ( | ||
decodedError && | ||
decodedError.errorName === "FailedOp" && | ||
decodedError.args | ||
) { | ||
return { | ||
result: "failed", | ||
data: decodedError.args[1] as string | ||
} as const | ||
} | ||
|
||
if ( | ||
decodedError && | ||
decodedError.errorName === "Error" && | ||
decodedError.args | ||
) { | ||
return { | ||
result: "failed", | ||
data: decodedError.args[0] | ||
} as const | ||
} | ||
|
||
if (decodedError.errorName === "ExecutionResult") { | ||
const parsedExecutionResult = executionResultSchema.parse( | ||
decodedError.args | ||
) | ||
|
||
return { | ||
result: "execution", | ||
data: { | ||
executionResult: parsedExecutionResult | ||
} as const | ||
} | ||
} | ||
} | ||
throw new Error("Unexpected error") | ||
} |
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
Oops, something went wrong.