import {z} from 'zod' export type BigDecimalFromString = string; export type BooleanFromString = 'true' | 'false'; export type GetApiQuotePublicArgs = {token: string, chainIn: string, chainOut: string, amount: BigDecimalFromString, amountNative?: BigDecimalFromString | undefined, mode: 'pay' | 'receive', isSda?: BooleanFromString | undefined}; export type GetApiQuotePublicOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bigDecimalFromString = z.string(); export const bigDecimalFromNumber = z.number(); export const singleBridgePublicQuoteResponseSchema = z.object({chainIn: z.string(), chainOut: z.string(), payAmount: bigDecimalFromString, payAmountUsd: bigDecimalFromNumber, receiveAmount: bigDecimalFromString, receiveAmountUsd: bigDecimalFromNumber, fees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number(), percentageFeeAmount: bigDecimalFromNumber.nullable(), percentageFeeThreshold: bigDecimalFromNumber.nullable(), sponsoredFees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number()}).optional()}), promotion: z.object({name: z.string(), maxLimitUsd: bigDecimalFromNumber.nullable()}).optional(), gasBoost: z.object({amountNative: bigDecimalFromString, amountNativeUsd: bigDecimalFromNumber, amountNativeTokenCost: bigDecimalFromString}).optional(), speed: z.enum(["fast", "standard", "slow"]).optional(), estimatedDuration: z.number().gte(0).optional(), token: z.string(), _tag: z.literal("bridge")}); export const bridgeSwapPublicQuoteResponseSchema = z.object({chainIn: z.string(), chainOut: z.string(), payAmount: bigDecimalFromString, payAmountUsd: bigDecimalFromNumber, receiveAmount: bigDecimalFromString, receiveAmountUsd: bigDecimalFromNumber, fees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number(), percentageFeeAmount: bigDecimalFromNumber.nullable(), percentageFeeThreshold: bigDecimalFromNumber.nullable(), sponsoredFees: z.object({fee: z.string(), feeUsd: z.number(), gasFee: bigDecimalFromString, gasFeeUsd: bigDecimalFromNumber, sourceGasFee: bigDecimalFromString, sourceGasFeeUsd: bigDecimalFromNumber, platformFee: z.string(), platformFeeUsd: z.number(), percentageFee: z.string(), percentageFeeUsd: z.number()}).optional()}), promotion: z.object({name: z.string(), maxLimitUsd: bigDecimalFromNumber.nullable()}).optional(), gasBoost: z.object({amountNative: bigDecimalFromString, amountNativeUsd: bigDecimalFromNumber, amountNativeTokenCost: bigDecimalFromString}).optional(), speed: z.enum(["fast", "standard", "slow"]).optional(), estimatedDuration: z.number().gte(0).optional(), tokenIn: z.string(), tokenOut: z.string(), bridgeToken: z.string(), bridgePayAmount: bigDecimalFromString, bridgePayAmountUsd: bigDecimalFromNumber, bridgeReceiveAmount: bigDecimalFromString, minReceiveAmount: bigDecimalFromString, minReceiveAmountUsd: bigDecimalFromNumber, usdPriceTokenIn: bigDecimalFromNumber, usdPriceTokenOut: bigDecimalFromNumber, isAtomicSwap: z.boolean().optional(), _tag: z.literal("bridgeSwap")}); export const getApiQuotePublicResponse = z.union([singleBridgePublicQuoteResponseSchema, bridgeSwapPublicQuoteResponseSchema]); 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 }; /** * Public Quote * * GET /quote/public */ export const getApiQuotePublic = async (args: GetApiQuotePublicArgs, options?: GetApiQuotePublicOptions) => { const {baseUrl = "https://api.rhino.fi/bridge", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) if (args.chainIn != null) query.append("chainIn", String(args.chainIn)) if (args.chainOut != null) query.append("chainOut", String(args.chainOut)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.amountNative != null) query.append("amountNative", String(args.amountNative)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.isSda != null) query.append("isSda", String(args.isSda)) const url = `${baseUrl.replace(/\/+$/, '')}/quote/public${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(getApiQuotePublicResponse, await res.json()) };