import {z} from 'zod' export type MeteoraConfigType = 'fa29606e-5e48-4c37-827f-4b03d58ee23d' | 'd16d3585-6488-4a6c-9a6f-e6c39ca0fda3' | 'a7c8e1f2-3d4b-5a6c-9e0f-1b2c3d4e5f6a' | '48e26d2f-0a9d-4625-a3cc-c3987d874b9e' | '810faadb-030b-47de-a68b-7211c1cbbee3' | 'e2963a6c-441a-4862-9d80-b94e3a481cd7' | 'ba28db46-ea6f-4452-8218-5587f6aca0a1'; export type CreateApiFeeShareConfigArgs = {body: {payer: string, baseMint: string, claimersArray: Array, basisPointsArray: Array, partner?: (string | null) | undefined, partnerConfig?: (string | null) | undefined, additionalLookupTables?: (Array | null) | undefined, bagsConfigType?: MeteoraConfigType | undefined, enableFirstSwapWithMinFee?: (boolean | null) | undefined, tipWallet?: (string | null) | undefined, tipLamports?: (number | null) | undefined}}; export type CreateApiFeeShareConfigOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const blockhashWithExpiryBlockHeight = z.object({blockhash: z.string(), lastValidBlockHeight: z.number()}); export const transactionWithBlockhash = z.object({blockhash: blockhashWithExpiryBlockHeight, transaction: z.string()}); export const createApiFeeShareConfigResponse = z.object({success: z.boolean(), response: z.object({needsCreation: z.boolean(), feeShareAuthority: z.string(), meteoraConfigKey: z.string(), transactions: z.array(transactionWithBlockhash).nullable().optional(), bundles: z.array(z.array(transactionWithBlockhash)).nullable().optional()}).optional()}); 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 }; /** * Create Fee Share Config creation transaction (v2) * * POST /fee-share/config */ export const createApiFeeShareConfig = async (args: CreateApiFeeShareConfigArgs, options?: CreateApiFeeShareConfigOptions) => { const {baseUrl = "https://public-api-v2.bags.fm/api/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/fee-share/config${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(createApiFeeShareConfigResponse, await res.json()) };