import {z} from 'zod' export type OnRampNetwork = 'POLYGON' | 'ETHEREUM' | 'STELLAR' | 'AVALANCHE' | 'SOLANA' | 'CELO' | 'BASE' | 'TON' | 'TRON' | 'LISK' | 'ARBITRUM' | 'OPTIMISM' | 'BNB'; export type OnRampAsset = 'USDC' | 'USDT' | 'CUSD' | 'CKES' | 'CGHS' | 'USDE'; export type WidgetAmountCurrency = 'local' | 'crypto'; export type OnRampPaymentChannel = 'bank' | 'mobile_money' | 'airtime'; export type GetApiApiOnrampBestOfferArgs = {network: OnRampNetwork, asset: OnRampAsset, currency: WidgetAmountCurrency, amount: number, countryIsoCode: string, paymentChannel: OnRampPaymentChannel, carrierCode?: string | undefined}; export type GetApiApiOnrampBestOfferOptions = {baseUrl?: string | undefined, ClientIdHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const onRampPaymentChannel = z.enum(["bank", "mobile_money", "airtime"]); export const requiredFieldType = z.enum(["number", "string", "date", "boolean", "email", "enum"]); export const onRampBestOfferResponse = z.object({quoteId: z.string().optional(), offer: z.object({paymentChannel: onRampPaymentChannel.optional(), countryIsoCode: z.string().optional(), currencyIsoCode: z.string().optional(), exchangeRate: z.number().optional(), cryptoExchangeRate: z.number().optional(), requiredFields: z.record(z.string(), z.object({type: requiredFieldType.optional(), label: z.string().optional(), required: z.boolean().optional(), options: z.array(z.object({value: z.string().optional(), label: z.string().optional()})).optional()})).optional()}).optional(), cashout: z.object({localCurrencyAmount: z.number().optional(), totalAmountUsd: z.number().optional(), totalAmountCrypto: z.number().optional(), withdrawAmountUsd: z.number().optional(), withdrawAmountCrypto: z.number().optional(), feePercent: z.number().optional(), feeAmountUsd: z.number().optional(), feeAmountLocalCurrency: z.number().optional(), feeAmountCrypto: z.number().optional(), feePercentFonbnk: z.number().optional(), feeAmountUsdFonbnk: z.number().optional(), feeAmountLocalCurrencyFonbnk: z.number().optional(), feeAmountCryptoFonbnk: z.number().optional(), feePercentPartner: z.number().optional(), feeAmountUsdPartner: z.number().optional(), feeAmountLocalCurrencyPartner: z.number().optional(), feeAmountCryptoPartner: z.number().optional(), gasAmountUsd: z.number().optional(), gasAmountLocalCurrency: z.number().optional(), gasAmountCrypto: z.number().optional()}).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 }; /** * Get best offer * * GET /api/onramp/best-offer * * Also accepts auth: TimestampHeader, SignatureHeader */ export const getApiApiOnrampBestOffer = async (args: GetApiApiOnrampBestOfferArgs, options?: GetApiApiOnrampBestOfferOptions) => { const {baseUrl = "https://sandbox-api.fonbnk.com", fetch: fetchFn = fetch, ClientIdHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.network != null) query.append("network", String(args.network)) if (args.asset != null) query.append("asset", String(args.asset)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.countryIsoCode != null) query.append("countryIsoCode", String(args.countryIsoCode)) if (args.paymentChannel != null) query.append("paymentChannel", String(args.paymentChannel)) if (args.carrierCode != null) query.append("carrierCode", String(args.carrierCode)) if (ClientIdHeader !== undefined) headers.set("x-client-id", ClientIdHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/onramp/best-offer${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(onRampBestOfferResponse, await res.json()) };