|
| 1 | +import type { Transport, Chain, Hex } from 'viem'; |
| 2 | +import { |
| 3 | + createBundlerClient, |
| 4 | + type BundlerClientConfig, |
| 5 | + type SmartAccount, |
| 6 | +} from 'viem/account-abstraction'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Gas price tiers returned by pimlico_getUserOperationGasPrice |
| 10 | + */ |
| 11 | +export type GasPriceTier = { |
| 12 | + /** Maximum fee per gas in hex format */ |
| 13 | + maxFeePerGas: Hex; |
| 14 | + /** Maximum priority fee per gas in hex format */ |
| 15 | + maxPriorityFeePerGas: Hex; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Response from pimlico_getUserOperationGasPrice RPC method |
| 20 | + */ |
| 21 | +export type UserOperationGasPriceResponse = { |
| 22 | + /** Slow gas price tier */ |
| 23 | + slow: GasPriceTier; |
| 24 | + /** Standard gas price tier */ |
| 25 | + standard: GasPriceTier; |
| 26 | + /** Fast gas price tier */ |
| 27 | + fast: GasPriceTier; |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Infura bundler actions for extending bundler clients. |
| 32 | + * |
| 33 | + * @returns A function that takes a bundler client and returns the client extension with Infura bundler actions. |
| 34 | + */ |
| 35 | +const infuraBundlerActions = () => (client: any) => ({ |
| 36 | + /** |
| 37 | + * Get user operation gas prices from Infura bundler. |
| 38 | + * Calls the pimlico_getUserOperationGasPrice RPC method. |
| 39 | + * |
| 40 | + * @returns Promise resolving to gas price tiers (slow, standard, fast). |
| 41 | + * @example |
| 42 | + * ```typescript |
| 43 | + * const gasPrices = await bundlerClient.getUserOperationGasPrice(); |
| 44 | + * console.log(gasPrices.standard.maxFeePerGas); |
| 45 | + * ``` |
| 46 | + */ |
| 47 | + async getUserOperationGasPrice(): Promise<UserOperationGasPriceResponse> { |
| 48 | + const response = await client.request({ |
| 49 | + method: 'pimlico_getUserOperationGasPrice' as any, |
| 50 | + params: [] as any, |
| 51 | + }); |
| 52 | + |
| 53 | + return response as unknown as UserOperationGasPriceResponse; |
| 54 | + }, |
| 55 | +}); |
| 56 | + |
| 57 | +/** |
| 58 | + * Type for bundler client extended with Infura bundler actions. |
| 59 | + */ |
| 60 | +export type InfuraBundlerClient< |
| 61 | + TTransport extends Transport = Transport, |
| 62 | + TChain extends Chain | undefined = Chain | undefined, |
| 63 | + TAccount extends SmartAccount | undefined = SmartAccount | undefined, |
| 64 | +> = ReturnType<typeof createBundlerClient<TTransport, TChain, TAccount>> & |
| 65 | + ReturnType<ReturnType<typeof infuraBundlerActions>>; |
| 66 | + |
| 67 | +/** |
| 68 | + * Creates an Infura bundler client extended with Infura bundler actions. |
| 69 | + * |
| 70 | + * This is a wrapper around viem's createBundlerClient that extends it with |
| 71 | + * the getUserOperationGasPrice method for retrieving gas prices from Pimlico's |
| 72 | + * bundler infrastructure via Infura's proxy. |
| 73 | + * |
| 74 | + * @param config - Configuration for the bundler client. |
| 75 | + * @returns Extended bundler client with Infura bundler actions. |
| 76 | + * @example |
| 77 | + * ```typescript |
| 78 | + * import { createPublicClient, http } from 'viem'; |
| 79 | + * import { sepolia } from 'viem/chains'; |
| 80 | + * import { createInfuraBundlerClient } from '@metamask/delegation-toolkit'; |
| 81 | + * |
| 82 | + * const publicClient = createPublicClient({ |
| 83 | + * chain: sepolia, |
| 84 | + * transport: http('https://sepolia.infura.io/v3/YOUR_API_KEY'), |
| 85 | + * }); |
| 86 | + * |
| 87 | + * const bundlerClient = createInfuraBundlerClient({ |
| 88 | + * client: publicClient, |
| 89 | + * transport: http('https://sepolia.infura.io/v3/YOUR_API_KEY'), |
| 90 | + * chain: sepolia, |
| 91 | + * }); |
| 92 | + * |
| 93 | + * // Use standard bundler methods |
| 94 | + * const userOpHash = await bundlerClient.sendUserOperation({...}); |
| 95 | + * |
| 96 | + * // Use Infura specific methods |
| 97 | + * const gasPrices = await bundlerClient.getUserOperationGasPrice(); |
| 98 | + * ``` |
| 99 | + */ |
| 100 | +export function createInfuraBundlerClient< |
| 101 | + TTransport extends Transport, |
| 102 | + TChain extends Chain | undefined = undefined, |
| 103 | + TAccount extends SmartAccount | undefined = undefined, |
| 104 | +>( |
| 105 | + config: BundlerClientConfig<TTransport, TChain, TAccount>, |
| 106 | +): InfuraBundlerClient<TTransport, TChain, TAccount> { |
| 107 | + // Create the base bundler client using viem's function |
| 108 | + const baseBundlerClient = createBundlerClient(config); |
| 109 | + |
| 110 | + // Extend the client with Infura bundler actions |
| 111 | + return baseBundlerClient.extend( |
| 112 | + infuraBundlerActions(), |
| 113 | + ) as unknown as InfuraBundlerClient<TTransport, TChain, TAccount>; |
| 114 | +} |
0 commit comments