import {z} from 'zod' export type GetApiV1GasRefetchArgs = {txHash: string, chainId: string, 'x-lifi-api-key'?: string | undefined}; export type GetApiV1GasRefetchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const lIFuelStatusStateEnum = z.enum(["NOT_FOUND", "PENDING", "DONE"]); export const token = z.object({address: z.string(), decimals: z.number(), symbol: z.string(), chainId: z.number(), coinKey: z.string().optional(), name: z.string(), logoURI: z.string().optional(), priceUSD: z.string().optional()}); export const txInfo = z.object({txHash: z.string().optional(), txLink: z.string().optional(), amount: z.string().optional(), token: token.optional(), chainId: z.number().optional(), block: z.number().optional()}); export const lIFuelStatus = z.object({status: lIFuelStatusStateEnum.optional(), sending: txInfo.optional(), receiving: txInfo.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 }; /** * In case a transaction was missed by a relayer, this endpoint can be used to force a tx to be re-fetched. * * GET /v1/gas/refetch */ export const getApiV1GasRefetch = async (args: GetApiV1GasRefetchArgs, options?: GetApiV1GasRefetchOptions) => { const {baseUrl = "https://li.quest", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.txHash != null) query.append("txHash", String(args.txHash)) if (args.chainId != null) query.append("chainId", String(args.chainId)) if (args['x-lifi-api-key'] != null) headers.set("x-lifi-api-key", String(args['x-lifi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gas/refetch${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(lIFuelStatus, await res.json()) };