UNPKG

@owlprotocol/contracts-account-abstraction

Version:

ERC4337 Account Abstraction support for deploying relevant smart contracts and interacting with UserOps.

92 lines (91 loc) 3.54 kB
import { hexToNumber, numberToHex } from "viem"; import { getAction } from "viem/utils"; import { getPaymasterData, getPaymasterStubData, getUserOperationGasPrice } from "../actions/index.js"; import { decodeUserOp } from "../models/UserOperation.js"; const paymasterRpcMethods = [ "pimlico_getUserOperationGasPrice", "pm_getPaymasterStubData", "pm_getPaymasterData" ]; function isPaymasterRpcMethod(method) { return paymasterRpcMethods.includes(method); } function createBackendPaymasterEIP1193(client) { return async function(args) { try { if (args.method === "pimlico_getUserOperationGasPrice") { const gasPrice = await getAction(client, getUserOperationGasPrice, "getUserOperationGasPrice")({}); return { slow: { maxFeePerGas: numberToHex(gasPrice.slow.maxFeePerGas), maxPriorityFeePerGas: numberToHex(gasPrice.slow.maxPriorityFeePerGas) }, standard: { maxFeePerGas: numberToHex(gasPrice.slow.maxFeePerGas), maxPriorityFeePerGas: numberToHex(gasPrice.slow.maxPriorityFeePerGas) }, fast: { maxFeePerGas: numberToHex(gasPrice.slow.maxFeePerGas), maxPriorityFeePerGas: numberToHex(gasPrice.slow.maxPriorityFeePerGas) } }; } else if (args.method === "pm_getPaymasterStubData") { const [userOperationHex, entryPointAddress, chainIdHex] = args.params; const userOperation = decodeUserOp(userOperationHex); const { paymaster, paymasterData, paymasterVerificationGasLimit, paymasterPostOpGasLimit, sponsor, isFinal } = await getAction( client, getPaymasterStubData, "getPaymasterStubData" )({ ...userOperation, entryPointAddress, chainId: hexToNumber(chainIdHex) }); const result = { paymaster, paymasterData, //Interfaces don't match between official viem action & RPC. Default to 0 paymasterVerificationGasLimit: paymasterVerificationGasLimit ? numberToHex(paymasterVerificationGasLimit) : "0x", paymasterPostOpGasLimit: numberToHex(paymasterPostOpGasLimit) }; if (sponsor) { result.sponsor = sponsor; } if (isFinal != void 0) { result.isFinal = isFinal; } return result; } else if (args.method === "pm_getPaymasterData") { const [userOperationHex, entryPointAddress, chainIdHex] = args.params; const userOperation = decodeUserOp(userOperationHex); const { paymaster, paymasterData, paymasterVerificationGasLimit, paymasterPostOpGasLimit } = await getAction( client, getPaymasterData, "getPaymasterData" )({ ...userOperation, entryPointAddress, chainId: hexToNumber(chainIdHex) }); const result = { paymaster, paymasterData, //Interfaces don't match between official viem action & RPC. Default to 0 paymasterVerificationGasLimit: paymasterVerificationGasLimit ? numberToHex(paymasterVerificationGasLimit) : "0x", //Interfaces don't match between official viem action & RPC. Default to 0 paymasterPostOpGasLimit: paymasterPostOpGasLimit ? numberToHex(paymasterPostOpGasLimit) : "0x" }; return result; } } catch (error) { return null; } }; } export { createBackendPaymasterEIP1193, isPaymasterRpcMethod, paymasterRpcMethods };