import {z} from 'zod' export type GetApiV1Brc20SwapPreRemoveLiqArgs = {address: string, tick0: string, tick1: string, amount0: string, amount1: string, lp: string, slippage: string, ts: number}; export type GetApiV1Brc20SwapPreRemoveLiqOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1Brc20SwapPreRemoveLiqResponse = z.object({code: z.number(), msg: z.string(), data: z.object({signMsg: z.string().optional(), bytesL1: z.number().optional(), bytesL2: z.number().optional(), feeRate: z.string().optional(), gasPrice: z.string().optional(), serviceFeeL1: z.string().optional(), serviceFeeL2: z.string().optional(), unitUsdPriceL1: z.string().optional(), unitUsdPriceL2: z.string().optional(), serviceTickBalance: z.string().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 }; /** * /remove_liq interface pre-load, get the signature content, gas and byte information. * * GET /v1/brc20-swap/pre_remove_liq */ export const getApiV1Brc20SwapPreRemoveLiq = async (args: GetApiV1Brc20SwapPreRemoveLiqArgs, options?: GetApiV1Brc20SwapPreRemoveLiqOptions) => { const {baseUrl = "https://open-api.unisat.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (args.tick0 != null) query.append("tick0", String(args.tick0)) if (args.tick1 != null) query.append("tick1", String(args.tick1)) if (args.amount0 != null) query.append("amount0", String(args.amount0)) if (args.amount1 != null) query.append("amount1", String(args.amount1)) if (args.lp != null) query.append("lp", String(args.lp)) if (args.slippage != null) query.append("slippage", String(args.slippage)) if (args.ts != null) query.append("ts", String(args.ts)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/brc20-swap/pre_remove_liq${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1Brc20SwapPreRemoveLiqResponse, await res.json()) };