import {z} from 'zod' export type RouteStep = {programId: string, dex: string, pool: string, inputMint: string, outputMint: string, amountIn: string, amountOut: string, feeAmount: string, priceImpact: number, percent?: number | undefined}; export type PriorityFeeLevels = {min?: number | undefined, low?: number | undefined, medium?: number | undefined, high?: number | undefined, veryHigh?: number | undefined, unsafeMax?: number | undefined}; export type PriorityFeeInfo = {recommended: number, level: string, levels?: PriorityFeeLevels | undefined}; export type PlatformFeeConfig = {feeBps: number, feeAccount: string}; export type QuoteResponse = {inputMint: string, outputMint: string, amountIn: string, amountOut: string, minAmountOut: string, feeAmount: string, priceImpact: number, slippageBps: number, routePlan: Array, swapUsdValue?: string | undefined, priorityFee?: PriorityFeeInfo | undefined, platformFee?: PlatformFeeConfig | undefined, contextSlot: number, timeTaken: number}; export type SwapRequest = {userPublicKey: string, wrapUnwrapSol?: boolean | undefined, txVersion?: ('legacy' | 'v0') | undefined, computeUnitPriceMicroLamports?: number | undefined, computeUnitLimit?: number | undefined, priorityFee?: string | undefined, maxPriorityFee?: number | undefined, tipAccount?: string | undefined, tipLamports?: number | undefined, feeAccount?: string | undefined, feeBps?: number | undefined, feeFromInput?: boolean | undefined, chargeBps?: number | undefined, quoteResponse: QuoteResponse}; export type CreateApiSwapArgs = {body: SwapRequest}; export type CreateApiSwapOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const swapResponse = z.object({swapTransaction: z.string(), lastValidBlockHeight: z.number().int(), contextSlot: z.number().int(), prioritizationFeeLamports: z.number().int()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Build swap transaction * * POST /swap */ export const createApiSwap = async (args: CreateApiSwapArgs, options?: CreateApiSwapOptions) => { const {baseUrl = "https://data.solanatracker.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/swap${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(swapResponse, await res.json()) };