import {z} from 'zod' export type RuneSymbol = string; export type PostRunesPSBTSweepingRequestSchema = {orderIds: Array, runeSymbol: RuneSymbol, takerPaymentAddress: unknown, takerPublicKey: unknown, takerReceiveAddress: unknown, feeRateTier?: ('minimumFee' | 'halfHourFee' | 'hourFee' | 'fastestFee') | undefined, feeRateNum?: number | undefined, enableRBFProtection?: boolean | undefined}; export type CreateApiV2OrdBtcRunesPsbtGetSweepingArgs = {body: PostRunesPSBTSweepingRequestSchema}; export type CreateApiV2OrdBtcRunesPsbtGetSweepingOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const settlementSchema = z.object({amount: z.string(), price: z.number()}); export const fullRBFProtectedSweepingResultSchema = z.object({settlement: settlementSchema, validOrderIds: z.array(z.string()), unsignedFundsPreparationPsbtBase64: z.string().nullable().optional(), psbtIndexesToSign: z.array(z.number()), error: z.object({message: z.string().optional()}).nullable().optional()}); export const postRunesPSBTSweepingResultSchema = z.object({invalidOrders: z.array(z.string()), validOrders: z.array(z.string()).optional(), psbtBase64: z.string().optional(), paymentUtxoIndexes: z.number(), settlement: settlementSchema.optional(), fullRBFProtectedSweepingResult: fullRBFProtectedSweepingResultSchema.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 }; /** * Get an unsigned PSBT for buying a rune * * POST /v2/ord/btc/runes/psbt/get-sweeping */ export const createApiV2OrdBtcRunesPsbtGetSweeping = async (args: CreateApiV2OrdBtcRunesPsbtGetSweepingArgs, options?: CreateApiV2OrdBtcRunesPsbtGetSweepingOptions) => { const {baseUrl = "https://api-mainnet.magiceden.dev/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/ord/btc/runes/psbt/get-sweeping${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(postRunesPSBTSweepingResultSchema, await res.json()) };