@owlprotocol/contracts-account-abstraction
Version:
ERC4337 Account Abstraction support for deploying relevant smart contracts and interacting with UserOps.
104 lines (103 loc) • 3.92 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var calcOptimismPreVerificationGas_exports = {};
__export(calcOptimismPreVerificationGas_exports, {
calcOptimismPreVerificationGas: () => calcOptimismPreVerificationGas
});
module.exports = __toCommonJS(calcOptimismPreVerificationGas_exports);
var import_viem = require("viem");
var import_actions = require("viem/actions");
var import_utils = require("viem/utils");
var import_PackedUserOperation = require("../../models/PackedUserOperation.js");
var import_EntryPoint = require("../../artifacts/EntryPoint.js");
var import_PackedUserOperation2 = require("../../models/PackedUserOperation.js");
const getL1FeeAbi = [
{
inputs: [
{
internalType: "bytes",
name: "data",
type: "bytes"
}
],
name: "getL1Fee",
outputs: [
{
internalType: "uint256",
name: "fee",
type: "uint256"
}
],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [],
name: "l1BaseFee",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function"
}
];
async function calcOptimismPreVerificationGas(client, parameters) {
const { packedUserOperation, entryPoint, staticFee } = parameters;
const chainId = client.chain?.id ?? await (0, import_utils.getAction)(client, import_actions.getChainId, "getChainId")({});
const randomDataUserOp = (0, import_PackedUserOperation.packedUserOperationToRandomDataUserOp)(packedUserOperation);
const handleOpsAbi = (0, import_viem.getAbiItem)({
abi: import_EntryPoint.abi,
name: "handleOps"
});
const selector = (0, import_viem.toFunctionSelector)(handleOpsAbi);
const paramData = (0, import_viem.encodeAbiParameters)(handleOpsAbi.inputs, [[randomDataUserOp], entryPoint]);
const data = (0, import_viem.concat)([selector, paramData]);
const latestBlock = await (0, import_utils.getAction)(client, import_actions.getBlock, "getBlock")({});
if (latestBlock.baseFeePerGas === null) {
throw new Error("block does not have baseFeePerGas");
}
const precompileAddress = "0x420000000000000000000000000000000000000F";
const serializedTx = (0, import_viem.serializeTransaction)(
{
to: entryPoint,
chainId,
nonce: 999999,
gasLimit: import_viem.maxUint64,
gasPrice: import_viem.maxUint64,
data
},
{
r: "0x123451234512345123451234512345123451234512345123451234512345",
s: "0x123451234512345123451234512345123451234512345123451234512345",
v: 28n
}
);
const { result: l1Fee } = await (0, import_utils.getAction)(
client,
import_actions.simulateContract,
"simulateContract"
)({
abi: getL1FeeAbi,
address: precompileAddress,
functionName: "getL1Fee",
args: [serializedTx]
});
const { maxFeePerGas, maxPriorityFeePerGas } = (0, import_PackedUserOperation2.unpackGasLimits)(packedUserOperation.gasFees);
const l2MaxFee = maxFeePerGas;
const l2PriorityFee = latestBlock.baseFeePerGas + maxPriorityFeePerGas;
const l2price = l2MaxFee < l2PriorityFee ? l2MaxFee : l2PriorityFee;
return staticFee + l1Fee / l2price;
}