import type { Address, Hex } from "viem" import type { PartialBy } from "viem/types/utils" import { type StackupPaymasterClient } from "../../clients/stackup" import type { ENTRYPOINT_ADDRESS_V06_TYPE, EntryPoint } from "../../types/entrypoint" import type { StackupPaymasterContext } from "../../types/stackup" import type { UserOperation } from "../../types/userOperation" import { deepHexlify } from "../../utils/deepHexlify" import { ENTRYPOINT_ADDRESS_V06 } from "../../utils/getEntryPointVersion" export type SponsorUserOperationParameters = { userOperation: entryPoint extends ENTRYPOINT_ADDRESS_V06_TYPE ? PartialBy< UserOperation<"v0.6">, "callGasLimit" | "preVerificationGas" | "verificationGasLimit" > : PartialBy< UserOperation<"v0.7">, | "callGasLimit" | "preVerificationGas" | "verificationGasLimit" | "paymasterVerificationGasLimit" | "paymasterPostOpGasLimit" > entryPoint: entryPoint context: StackupPaymasterContext } export type SponsorUserOperationReturnType = entryPoint extends ENTRYPOINT_ADDRESS_V06_TYPE ? Pick< UserOperation<"v0.6">, | "callGasLimit" | "verificationGasLimit" | "preVerificationGas" | "paymasterAndData" > : Pick< UserOperation<"v0.7">, | "callGasLimit" | "verificationGasLimit" | "preVerificationGas" | "paymaster" | "paymasterVerificationGasLimit" | "paymasterPostOpGasLimit" | "paymasterData" > /** * Returns paymasterAndData & updated gas parameters required to sponsor a userOperation. * * - Docs: https://docs.pimlico.io/permissionless/reference/stackup-paymaster-actions/sponsorUserOperation * * @param client {@link PimlicoBundlerClient} that you created using viem's createClient whose transport url is pointing to the Pimlico's bundler. * @param args {@link sponsorUserOperationParameters} UserOperation you want to sponsor & entryPoint. * @returns paymasterAndData & updated gas parameters, see {@link SponsorUserOperationReturnType} * * * @example * import { createClient } from "viem" * import { sponsorUserOperation } from "permissionless/actions/stackup" * * const bundlerClient = createClient({ * chain: goerli, * transport: http("https://api.stackup.sh/v2/paymaster/YOUR_API_KEY_HERE") * }) * * await sponsorUserOperation(bundlerClient, { * userOperation: userOperationWithDummySignature, * entryPoint: entryPoint * }}) * */ export const sponsorUserOperation = async ( client: StackupPaymasterClient, args: SponsorUserOperationParameters ): Promise> => { const response = await client.request({ method: "pm_sponsorUserOperation", params: [deepHexlify(args.userOperation), args.entryPoint, args.context] }) if (args.entryPoint === ENTRYPOINT_ADDRESS_V06) { const responseV06 = response as { paymasterAndData: Hex preVerificationGas: Hex verificationGasLimit: Hex callGasLimit: Hex paymaster?: never paymasterVerificationGasLimit?: never paymasterPostOpGasLimit?: never paymasterData?: never } return { paymasterAndData: responseV06.paymasterAndData, preVerificationGas: BigInt(responseV06.preVerificationGas), verificationGasLimit: BigInt(responseV06.verificationGasLimit), callGasLimit: BigInt(responseV06.callGasLimit) } as SponsorUserOperationReturnType } const responseV07 = response as { preVerificationGas: Hex verificationGasLimit: Hex callGasLimit: Hex paymaster: Address paymasterVerificationGasLimit: Hex paymasterPostOpGasLimit: Hex paymasterData: Hex paymasterAndData?: never } return { callGasLimit: BigInt(responseV07.callGasLimit), verificationGasLimit: BigInt(responseV07.verificationGasLimit), preVerificationGas: BigInt(responseV07.preVerificationGas), paymaster: responseV07.paymaster, paymasterVerificationGasLimit: BigInt( responseV07.paymasterVerificationGasLimit ), paymasterPostOpGasLimit: BigInt(responseV07.paymasterPostOpGasLimit), paymasterData: responseV07.paymasterData } as SponsorUserOperationReturnType }