import {z} from 'zod' export type GetApiExchangeApiOrdersTradeOptionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionLimitResponse = z.object({maxSingleTransactionLimit: z.number().optional(), minSingleTransactionLimit: z.number().optional(), dailyTransactionLimit: z.number().optional(), maxLifetimeLimit: z.number().optional()}); export const defaultConfigurationResponse = z.object({kyc: transactionLimitResponse, noKyc: transactionLimitResponse}); export const fiatCurrency = z.object({name: z.string(), logo: z.string(), isoCode: z.string(), symbol: z.string().optional(), unicodeSymbol: z.string().optional(), transactionLimitsConfig: defaultConfigurationResponse.optional()}); export const tokens = z.object({symbol: z.string(), networkId: z.string(), decimals: z.number(), isNative: z.boolean(), address: z.string().optional()}); export const blockchainNetwork = z.object({networkId: z.string(), name: z.string(), logo: z.string(), explorer: z.string(), chainId: z.number(), nativeAsset: z.string(), availableTokens: z.array(tokens).optional()}); export const paymentChannelSupportedCurrency = z.object({country: z.string().optional(), currency: z.string().optional()}); export const paymentChannel = z.object({paymentChannelId: z.string().optional(), name: z.string(), supportedCurrencies: z.array(paymentChannelSupportedCurrency).optional()}); export const tradeOptions = z.object({fiats: z.array(fiatCurrency), networks: z.array(blockchainNetwork), paymentChannels: z.array(paymentChannel).optional(), transactionLimitsConfig: defaultConfigurationResponse.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 }; /** * Trade options / configurations for onboard * * GET /exchange/api/orders/trade-options */ export const getApiExchangeApiOrdersTradeOptions = async (options?: GetApiExchangeApiOrdersTradeOptionsOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/exchange/api/orders/trade-options${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(tradeOptions, await res.json()) };