import {z} from 'zod' export type GetApiV1Brc20SwapPreDeployPoolArgs = {address: string, tick0: string, tick1: string, ts: number}; export type GetApiV1Brc20SwapPreDeployPoolOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1Brc20SwapPreDeployPoolResponse = 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 }; /** * /deploy_pool interface pre-load, get the signature content, gas and byte information. * * GET /v1/brc20-swap/pre_deploy_pool */ export const getApiV1Brc20SwapPreDeployPool = async (args: GetApiV1Brc20SwapPreDeployPoolArgs, options?: GetApiV1Brc20SwapPreDeployPoolOptions) => { 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.ts != null) query.append("ts", String(args.ts)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/brc20-swap/pre_deploy_pool${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(getApiV1Brc20SwapPreDeployPoolResponse, await res.json()) };