import {z} from 'zod' export type SatributesSchema = 'Common' | 'Uncommon' | 'Rare' | 'Epic' | 'Legendary' | 'Black Uncommon' | 'Black Rare' | 'Black Epic' | 'Black Legendary' | 'Mythic' | 'Palindrome' | 'Uniform Palinception' | 'Perfect Palinception' | 'Paliblock Palindrome' | 'Vintage' | 'Nakamoto' | 'Block 9' | 'Block 9 450x' | 'Block 286' | 'Block 78' | 'Block 666' | 'First Transaction' | 'Pizza' | 'Taproot' | 'Alpha' | 'Omega' | 'JPEG' | 'Legacy' | 'Hitman'; export type SatributesArraySchema = Array; export type RareSatsSatRangePriceSchema = {id: string, from: number, to: number, parentFrom: number, parentTo: number, unitPrice?: number | undefined, satributes: SatributesArraySchema}; export type RareSatsListingsPSBTRequestSchemaSingleItem = {sellerReceiveAddress?: string | undefined, publicKey?: string | undefined, utxoId: string, satRangePrices: Array}; export type RareSatsListingsPSBTRequestSchema = {listings: Array}; export type CreateApiV2OrdBtcRaresatsListingPsbtArgs = {body: RareSatsListingsPSBTRequestSchema}; export type CreateApiV2OrdBtcRaresatsListingPsbtOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rareSatsListingPSBTResultSchema = z.object({sellerReceiveAddress: z.string(), price: z.number(), utxoId: z.string(), unsignedListingPSBTBase64: z.string(), makerFee: z.number(), publicKey: z.string().optional()}); export const rareSatsListingsPSBTResultSchema = z.object({results: z.array(rareSatsListingPSBTResultSchema), failed: z.array(z.string()), unsignedCombinedPSBTBase64: z.string(), toSignInputs: z.array(z.number()), toSignSigHash: z.number()}); 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 Batch Listing PSBT * * POST /v2/ord/btc/raresats/listing-psbt */ export const createApiV2OrdBtcRaresatsListingPsbt = async (args: CreateApiV2OrdBtcRaresatsListingPsbtArgs, options?: CreateApiV2OrdBtcRaresatsListingPsbtOptions) => { 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/raresats/listing-psbt${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(rareSatsListingsPSBTResultSchema, await res.json()) };