import {z} from 'zod' export type BlockchainNetwork = 'TRON' | 'ETHEREUM' | 'LITECOIN' | 'BITCOIN' | 'RIPPLE' | 'DOGECOIN' | 'SOLANA' | 'ALGORAND' | 'CARDANO' | 'BINANCE' | 'POLYGON' | 'BITCOIN_CASH' | 'ARC' | 'ARBITRUM' | 'BASE'; export type EntityTypeEnumDto = 'INDIVIDUAL' | 'COMPANY'; export type IndividualPartyDetailDto = {type: string, entityType: EntityTypeEnumDto, firstName: string, lastName: string, dateOfBirth: string, relationshipType: string, countryCode: string, regionCode?: unknown | undefined}; export type CompanyPartyDetailDto = {type: string, entityType: EntityTypeEnumDto, legalName: string, relationshipType: string, countryCode: string, regionCode?: unknown | undefined, registrationNumber: string}; export type PartyDetailDto = IndividualPartyDetailDto | CompanyPartyDetailDto; export type ComplianceDetailDto = {requesterIpAddress?: string | undefined, contactId?: string | undefined, partyDetails: Array}; export type CreateEstimateRequestDto = {walletId: string, merchantId?: string | undefined, walletCurrency: string, paidCurrency: string, reference: string, network: BlockchainNetwork, complianceDetails: ComplianceDetailDto, walletRequiredAmount: number} | {walletId: string, merchantId?: string | undefined, walletCurrency: string, paidCurrency: string, reference: string, network: BlockchainNetwork, complianceDetails: ComplianceDetailDto, paidRequiredAmount: number}; export type CreateApiApiV1PayEstimateArgs = {body: CreateEstimateRequestDto}; export type CreateApiApiV1PayEstimateOptions = {baseUrl?: string | undefined, Hawk?: string | undefined, fetch?: typeof fetch | undefined}; export const estimatePayoutResponseDto = z.object({externalId: z.string().optional(), accountId: z.string().optional(), walletId: z.string().max(255).optional(), customerReference: z.string().optional(), walletCurrency: z.string().optional(), walletRequiredAmount: z.number().optional(), paidCurrency: z.string().optional(), paidRequiredAmount: z.number().optional(), feeCurrency: z.string().optional(), feePredictedAmount: z.number().optional(), networkFeeCurrency: z.string().optional(), networkFeePredictedAmount: z.number().optional(), totalWalletAmount: z.number().optional(), exchangeRate: z.number().optional(), customerId: z.string().optional(), paymentExternalId: z.string().optional(), network: z.string().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 }; /** * Create an estimate payout * * POST /api/v1/pay/estimate */ export const createApiApiV1PayEstimate = async (args: CreateApiApiV1PayEstimateArgs, options?: CreateApiApiV1PayEstimateOptions) => { const {baseUrl = "https://api.sandbox.bvnk.com", fetch: fetchFn = fetch, Hawk} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Hawk !== undefined) headers.set("Authorization", Hawk) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/pay/estimate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(estimatePayoutResponseDto, await res.json()) };