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 RareSatsListingSchema = {sellerReceiveAddress: string, price: number, utxoId: string, satRangePrices: Array, signedListingPSBTBase64: string, makerFee: number, publicKey?: string | undefined}; export type RareSatsListingRequestSchema = {listings: Array, signedCombinedPSBTBase64: string}; export type CreateApiV2OrdBtcRaresatsListingArgs = {body: RareSatsListingRequestSchema}; export type CreateApiV2OrdBtcRaresatsListingOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rareSatsListingResultSchema = z.object({ok: z.boolean(), listed: z.number(), failed: z.array(z.string())}); 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 }; /** * Submit Batch Listing * * POST /v2/ord/btc/raresats/listing */ export const createApiV2OrdBtcRaresatsListing = async (args: CreateApiV2OrdBtcRaresatsListingArgs, options?: CreateApiV2OrdBtcRaresatsListingOptions) => { 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${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(rareSatsListingResultSchema, await res.json()) };