import {z} from 'zod' export type TokenAmount = {tokenAmount: number, tokenSymbol: string}; export type GetPayoutTokenFeeInput = {amount: TokenAmount, fiatAndRailCode: 'usd' | 'cop' | 'cop-bre-b' | 'cop-cobre-balance' | 'ars' | 'eur' | 'mxn' | 'mxn-debit-card' | 'brl' | 'clp' | 'pen' | 'bob' | 'crc' | 'zar' | 'usd-peru' | 'usd-china' | 'usd-panama' | 'usd-hong-kong' | 'usd-el-salvador'}; export type GetPayoutTokenFeesInput = {tokenFeeRequests: Array}; export type CreateApiApiPayoutsFeesTokenToFiatArgs = {'on-behalf-of'?: string | undefined, body: GetPayoutTokenFeesInput}; export type CreateApiApiPayoutsFeesTokenToFiatOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const simpleFiatAmount = z.object({fiatAmount: z.number(), fiatCurrencyCode: z.enum(["USD", "COP", "ARS", "EUR", "MXN", "BRL", "CLP", "PEN", "BOB", "CRC", "ZAR"])}); export const tokenAmount = z.object({tokenAmount: z.number(), tokenSymbol: z.string()}); export const tokenPayoutFeeSuccess = z.object({estimatedFiatAmount: simpleFiatAmount, exchangeFeePercentage: z.number(), exchangeRate: z.number(), feeTotal: tokenAmount, fiatAndRailCode: z.enum(["usd", "cop", "cop-bre-b", "cop-cobre-balance", "ars", "eur", "mxn", "mxn-debit-card", "brl", "clp", "pen", "bob", "crc", "zar", "usd-peru", "usd-china", "usd-panama", "usd-hong-kong", "usd-el-salvador"]), minTransactionValue: tokenAmount.optional(), tokenAmount: tokenAmount, transactionFee: tokenAmount, type: z.literal("success")}); export const payoutFeeErrorReason = z.enum(["UNSUPPORTED_PAYOUT_RAIL", "TEMPORARILY_UNAVAILABLE"]); export const tokenPayoutFeeError = z.object({fiatAndRailCode: z.enum(["usd", "cop", "cop-bre-b", "cop-cobre-balance", "ars", "eur", "mxn", "mxn-debit-card", "brl", "clp", "pen", "bob", "crc", "zar", "usd-peru", "usd-china", "usd-panama", "usd-hong-kong", "usd-el-salvador"]), message: z.string(), reason: payoutFeeErrorReason, tokenAmount: tokenAmount, type: z.literal("error")}); export const createApiApiPayoutsFeesTokenToFiatResponse = z.array(z.union([tokenPayoutFeeSuccess, tokenPayoutFeeError])); 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 fees for token amount * * POST /api/payouts/fees/token-to-fiat */ export const createApiApiPayoutsFeesTokenToFiat = async (args: CreateApiApiPayoutsFeesTokenToFiatArgs, options?: CreateApiApiPayoutsFeesTokenToFiatOptions) => { const {baseUrl = "https://api.muralpay.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['on-behalf-of'] != null) headers.set("on-behalf-of", String(args['on-behalf-of'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/payouts/fees/token-to-fiat${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(createApiApiPayoutsFeesTokenToFiatResponse, await res.json()) };