import {z} from 'zod' export type GetApiV10DlnOrderIdArgs = {id: string}; export type GetApiV10DlnOrderIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const offer = z.object({chainId: z.enum(["1", "10", "56", "137", "8453", "42161", "43114", "59144", "7565164", "100000001", "100000002", "100000008", "100000009", "100000013", "100000014", "100000017", "100000019", "100000020", "100000021", "100000022", "100000023", "100000025", "100000026", "100000027", "100000028", "100000029", "100000030", "100000031"]), tokenAddress: z.string(), amount: z.number().int()}); export const orderStruct = z.object({makerOrderNonce: z.number().int(), makerSrc: z.string(), giveOffer: offer, receiverDst: z.string(), takeOffer: offer, givePatchAuthoritySrc: z.string(), orderAuthorityAddressDst: z.string(), allowedTakerDst: z.string(), allowedCancelBeneficiarySrc: z.string(), externalCall: z.string()}); export const dlnOrderResponse = z.object({orderId: z.string(), status: z.enum(["None", "Created", "Fulfilled", "SentUnlock", "OrderCancelled", "SentOrderCancel", "ClaimedUnlock", "ClaimedOrderCancel"]), externalCallState: z.enum(["NoExtCall", "AwaitingOrderFulfillment", "AwaitingExecution", "Executing", "Completed", "Failed", "Cancelled"]).optional(), orderStruct: orderStruct}); 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 }; /** * This endpoint returns the data of order. * * GET /v1.0/dln/order/{id} */ export const getApiV10DlnOrderId = async (args: GetApiV10DlnOrderIdArgs, options?: GetApiV10DlnOrderIdOptions) => { const {baseUrl = "https://dln.debridge.finance", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1.0/dln/order/${encodeURIComponent(String(args.id))}${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(dlnOrderResponse, await res.json()) };