import type { Chain, Client, Hash, Transport } from "viem" import { getAction } from "viem/utils" import type { SmartAccount } from "../../accounts/types" import type { GetAccountParameter, PartialBy, Prettify, UserOperation } from "../../types/" import type { ENTRYPOINT_ADDRESS_V06_TYPE, EntryPoint, GetEntryPointVersion } from "../../types/entrypoint" import { AccountOrClientNotFoundError, parseAccount } from "../../utils/" import { sendUserOperation as sendUserOperationBundler } from "../bundler/sendUserOperation" import { type Middleware, prepareUserOperationRequest } from "./prepareUserOperationRequest" export type SendUserOperationParameters< entryPoint extends EntryPoint, TAccount extends SmartAccount | undefined = | SmartAccount | undefined > = { userOperation: entryPoint extends ENTRYPOINT_ADDRESS_V06_TYPE ? PartialBy< UserOperation<"v0.6">, | "sender" | "nonce" | "initCode" | "callGasLimit" | "verificationGasLimit" | "preVerificationGas" | "maxFeePerGas" | "maxPriorityFeePerGas" | "paymasterAndData" | "signature" > : PartialBy< UserOperation<"v0.7">, | "sender" | "nonce" | "factory" | "factoryData" | "callGasLimit" | "verificationGasLimit" | "preVerificationGas" | "maxFeePerGas" | "maxPriorityFeePerGas" | "paymaster" | "paymasterVerificationGasLimit" | "paymasterPostOpGasLimit" | "paymasterData" | "signature" > } & GetAccountParameter & Middleware export async function sendUserOperation< entryPoint extends EntryPoint, TTransport extends Transport = Transport, TChain extends Chain | undefined = Chain | undefined, TAccount extends SmartAccount | undefined = | SmartAccount | undefined >( client: Client, args: Prettify> ): Promise { const { account: account_ = client.account } = args if (!account_) throw new AccountOrClientNotFoundError() const account = parseAccount(account_) as SmartAccount const userOperation = await getAction( client, prepareUserOperationRequest, "prepareUserOperationRequest" )(args) userOperation.signature = await account.signUserOperation( userOperation as UserOperation> ) return sendUserOperationBundler(client, { userOperation: userOperation as UserOperation< GetEntryPointVersion >, entryPoint: account.entryPoint }) }