import type { KernelSmartAccount } from "@zerodev/sdk" import type { EntryPoint, Prettify } from "permissionless/types" import type { Address, Chain, Client, Transport } from "viem" import type { CabPaymasterRpcScheme } from "../../types" export type GetEnabledChainsParameters = { invoiceManagerAddress?: Address } export type EnabledTokenInfo = { chainId: number tokens: string[] } export type GetEnabledChainsResult = { enabledChains: EnabledTokenInfo[] isEnabledOnCurrentChain: boolean } export async function getEnabledChains< TEntryPoint extends EntryPoint, TChain extends Chain | undefined = Chain | undefined, TTransport extends Transport = Transport, TSmartAccount extends | KernelSmartAccount | undefined = | KernelSmartAccount | undefined >( client: Client< TTransport, TChain, TSmartAccount, CabPaymasterRpcScheme >, args: Prettify, transport: Transport ): Promise { const account = client.account const chain = client.chain if (!account) { throw new Error("Invalid account") } const { value } = transport({}) if (!value?.url) { throw new Error("Invalid transport") } const paymasterUrl = value.url.replace(/\/$/, "") const response = await fetch( `${paymasterUrl}/cab/${account.address}/${args.invoiceManagerAddress}` ) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`) } const data = await response.json() const enabledChains = data.enabledTokens as EnabledTokenInfo[] const isEnabledOnCurrentChain = chain ? enabledChains.some((info) => info.chainId === chain.id) : false return { enabledChains, isEnabledOnCurrentChain } }