Skip to content

feat(entrykit): add wiresaw transport #3703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-kangaroos-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/entrykit": patch
---

Added experimental support for fast user operations on wiresaw-enabled chains.
1 change: 1 addition & 0 deletions packages/common/src/exports/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "../deploy/ensureContractsDeployed";
export * from "../deploy/ensureDeployer";
export * from "../deploy/getContractAddress";
export * from "../deploy/getDeployer";
export * from "../transports/wiresaw";
14 changes: 14 additions & 0 deletions packages/common/src/transports/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RpcSchema, UnionToTuple } from "viem";

export type getRpcMethod<rpcSchema extends RpcSchema, method extends rpcSchema[number]["Method"]> = Extract<
rpcSchema[number],
{ Method: method }
>;

export type getRpcSchema<rpcSchema extends RpcSchema, method extends rpcSchema[number]["Method"]> = UnionToTuple<
getRpcMethod<rpcSchema, method>
>;

export type getRpcReturnType<rpcSchema extends RpcSchema, method extends rpcSchema[number]["Method"]> = {
[k in keyof rpcSchema & number as rpcSchema[k]["Method"]]: rpcSchema[k]["ReturnType"];
}[method];
110 changes: 110 additions & 0 deletions packages/common/src/transports/entryPointSimulationsAbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
export const entryPointSimulationsAbi = [
{
inputs: [
{
components: [
{
internalType: "address",
name: "sender",
type: "address",
},
{
internalType: "uint256",
name: "nonce",
type: "uint256",
},
{
internalType: "bytes",
name: "initCode",
type: "bytes",
},
{
internalType: "bytes",
name: "callData",
type: "bytes",
},
{
internalType: "bytes32",
name: "accountGasLimits",
type: "bytes32",
},
{
internalType: "uint256",
name: "preVerificationGas",
type: "uint256",
},
{
internalType: "bytes32",
name: "gasFees",
type: "bytes32",
},
{
internalType: "bytes",
name: "paymasterAndData",
type: "bytes",
},
{
internalType: "bytes",
name: "signature",
type: "bytes",
},
],
internalType: "struct PackedUserOperation",
name: "op",
type: "tuple",
},
{
internalType: "address",
name: "target",
type: "address",
},
{
internalType: "bytes",
name: "targetCallData",
type: "bytes",
},
],
name: "simulateHandleOp",
outputs: [
{
components: [
{
internalType: "uint256",
name: "preOpGas",
type: "uint256",
},
{
internalType: "uint256",
name: "paid",
type: "uint256",
},
{
internalType: "uint256",
name: "accountValidationData",
type: "uint256",
},
{
internalType: "uint256",
name: "paymasterValidationData",
type: "uint256",
},
{
internalType: "bool",
name: "targetSuccess",
type: "bool",
},
{
internalType: "bytes",
name: "targetResult",
type: "bytes",
},
],
internalType: "struct IEntryPointSimulations.ExecutionResult",
name: "",
type: "tuple",
},
],
stateMutability: "nonpayable",
type: "function",
},
] as const;
171 changes: 171 additions & 0 deletions packages/common/src/transports/methods/estimateUserOperationGas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import {
BundlerRpcSchema,
decodeFunctionResult,
DecodeFunctionResultReturnType,
EIP1193RequestFn,
encodeFunctionData,
Hex,
zeroAddress,
} from "viem";
import { getRpcMethod } from "../common";
import {
entryPoint07Address,
formatUserOperation,
formatUserOperationRequest,
toPackedUserOperation,
UserOperation,
} from "viem/account-abstraction";
import { bigIntMax } from "../../utils";
import { entryPointSimulationsAbi } from "../entryPointSimulationsAbi";

type rpcMethod = getRpcMethod<BundlerRpcSchema, "eth_estimateUserOperationGas">;

type EstimateUserOperationGasOptions = {
request: EIP1193RequestFn;
params: rpcMethod["Parameters"];
};

export async function estimateUserOperationGas({
request,
params,
}: EstimateUserOperationGasOptions): Promise<rpcMethod["ReturnType"]> {
const userOp = formatUserOperation(params[0]);
const hasPaymaster = userOp.paymaster != null && userOp.paymaster !== zeroAddress;

const [simulationResult, simulationResultWithPaymaster] = await Promise.all([
simulateHandleOp({ userOp, removePaymaster: hasPaymaster, request }),
hasPaymaster ? simulateHandleOp({ userOp, request }) : undefined,
]);

const gasEstimates = getGasEstimates({ userOp, simulationResult, simulationResultWithPaymaster });

return formatUserOperationRequest({
...gasEstimates,
});
}

type SimulateHandleOpOptions = {
request: EIP1193RequestFn;
userOp: UserOperation<"0.7">;
removePaymaster?: boolean;
};

type SimulationResult = DecodeFunctionResultReturnType<typeof entryPointSimulationsAbi>;

async function simulateHandleOp({
userOp,
removePaymaster,
request,
}: SimulateHandleOpOptions): Promise<SimulationResult> {
if (removePaymaster) {
const {
/* eslint-disable */
paymaster,
paymasterData,
paymasterPostOpGasLimit,
paymasterVerificationGasLimit,
/* eslint-enable */
...userOpWithoutPaymaster
} = userOp;
userOp = userOpWithoutPaymaster;
}

// Prepare user operation for simulation
const paymasterGasLimits =
userOp.paymaster && !removePaymaster
? {
paymasterPostOpGasLimit: 2_000_000n,
paymasterVerificationGasLimit: 5_000_000n,
}
: {};
const simulationUserOp = {
...userOp,
preVerificationGas: 0n,
callGasLimit: 10_000_000n,
verificationGasLimit: 10_000_000n,
// https://github.com/pimlicolabs/alto/blob/471998695e5ec75ef88dda3f8a534f47c24bcd1a/src/rpc/methods/eth_estimateUserOperationGas.ts#L117
maxPriorityFeePerGas: userOp.maxFeePerGas,
...paymasterGasLimits,
} satisfies UserOperation<"0.7">;

const packedUserOp = toPackedUserOperation(simulationUserOp);
const simulationData = encodeFunctionData({
abi: entryPointSimulationsAbi,
functionName: "simulateHandleOp",
args: [packedUserOp, zeroAddress, "0x"],
});

const senderBalanceOverride = removePaymaster ? { [userOp.sender]: { balance: "0xFFFFFFFFFFFFFFFFFFFF" } } : {};
const simulationParams = [
{
to: entryPoint07Address,
data: simulationData,
},
"pending",
{
...senderBalanceOverride,
},
];
const encodedSimulationResult: Hex = await request({
method: "wiresaw_callEntryPointSimulations",
params: simulationParams,
});

return decodeFunctionResult({
abi: entryPointSimulationsAbi,
functionName: "simulateHandleOp",
data: encodedSimulationResult,
});
}

type GetGasEstimatesOptions = {
userOp: UserOperation<"0.7">;
simulationResult: SimulationResult;
simulationResultWithPaymaster?: SimulationResult;
};

type GasEstimates = {
verificationGasLimit: bigint;
callGasLimit: bigint;
paymasterVerificationGasLimit: bigint;
paymasterPostOpGasLimit: bigint;
preVerificationGas: bigint;
};

function getGasEstimates({
userOp,
simulationResult,
simulationResultWithPaymaster,
}: GetGasEstimatesOptions): GasEstimates {
const hasPaymaster = simulationResultWithPaymaster != null;

// The verification gas is the total gas available during the validation phase, including the gas used by the paymaster
const verificationGas = hasPaymaster ? simulationResultWithPaymaster.preOpGas : simulationResult.preOpGas;

// The paymaster verification gas is the difference between verification gas with and without paymaster
const paymasterVerificationGas = hasPaymaster
? simulationResultWithPaymaster.preOpGas - simulationResult.preOpGas
: 0n;

// The call gas is only the gas used by the user operation, not the paymaster
const callGas = simulationResult.paid / userOp.maxFeePerGas - simulationResult.preOpGas;

// The paymaster post-op gas is the difference between non-verification gas with and without paymaster
const paymasterPostOpGas = hasPaymaster
? simulationResultWithPaymaster.paid / userOp.maxFeePerGas - simulationResultWithPaymaster.preOpGas - callGas
: 0n;

// Apply a 2x buffer to the calculated limits
const verificationGasLimit = verificationGas * 2n;
const callGasLimit = bigIntMax(callGas * 2n, 9000n);
const paymasterVerificationGasLimit = paymasterVerificationGas * 2n;
const paymasterPostOpGasLimit = paymasterPostOpGas * 2n;

return {
verificationGasLimit,
callGasLimit,
paymasterVerificationGasLimit,
paymasterPostOpGasLimit,
preVerificationGas: 20_000n,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
} from "viem";
import { entryPoint07Abi } from "viem/account-abstraction";

// TODO: move to common package?

const userOperationRevertReasonAbi = [
entryPoint07Abi.find(
(item): item is ExtractAbiItem<typeof entryPoint07Abi, "UserOperationRevertReason"> =>
Expand Down
Loading
Loading