import {z} from 'zod' export type StringNoEdgeSpaces = string; export type CountryCodeForRequest = string; export type SubdivisionCodeForRequest = string; export type CreateQuoteOriginNode = {type: 'account', id: string} | {type: 'external-account', id: string, network?: string | undefined} | {type: 'apm', method: 'apple-pay' | 'google-pay' | 'paypal' | 'venmo'}; export type CreateQuoteDestinationNode = {type: 'account', id: string} | {type: 'external-account', id: string, network?: string | undefined} | {type: 'apm', method: 'apple-pay' | 'paypal' | 'venmo'} | {type: 'crypto-address', asset: string, network: string, address: string, reference?: string | undefined}; export type CreateApiCoreTransactionsQuoteArgs = {'X-On-Behalf-Of'?: string | undefined, 'X-Uphold-User-Ip'?: (string | string) | undefined, 'X-Uphold-User-Agent'?: StringNoEdgeSpaces | undefined, 'X-Uphold-User-Origin'?: StringNoEdgeSpaces | undefined, 'X-Uphold-User-Country'?: CountryCodeForRequest | undefined, 'X-Uphold-User-Subdivision'?: SubdivisionCodeForRequest | undefined, 'X-Uphold-User-City'?: StringNoEdgeSpaces | undefined, body: {origin: CreateQuoteOriginNode, destination: CreateQuoteDestinationNode, denomination: {asset: string, amount: string, target?: ('origin' | 'destination') | undefined}, ttl?: number | undefined}}; export type CreateApiCoreTransactionsQuoteOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionNodeExternalAccountBank = z.object({type: z.literal("external-account"), id: z.string(), ownerId: z.string(), via: z.literal("bank"), network: z.string()}); export const transactionNodeExternalAccountCardBase = z.object({type: z.literal("external-account"), id: z.string(), ownerId: z.string(), via: z.literal("card"), network: z.string()}); export const transactionNodeExternalAccountApmBase = z.object({type: z.literal("external-account"), id: z.string(), ownerId: z.string(), via: z.literal("apm")}); export const stringNoEdgeSpaces = z.string(); export const transactionNodeCryptoAddressExecution = z.discriminatedUnion("mode", [z.object({mode: z.literal("onchain"), transactionHash: stringNoEdgeSpaces.optional()}), z.object({mode: z.literal("offchain"), accountId: z.string().optional(), accountOwnerId: z.string().optional()}), z.object({mode: z.literal("simulated"), transactionHash: stringNoEdgeSpaces.optional()})]); export const transactionNodeExternalAccountCrypto = z.object({type: z.literal("external-account"), id: z.string(), ownerId: z.string(), via: z.literal("crypto"), network: z.string(), execution: transactionNodeCryptoAddressExecution.optional()}); export const quoteOriginNode = z.discriminatedUnion("type", [z.object({type: z.literal("account"), id: z.string(), ownerId: z.string()}), transactionNodeExternalAccountBank, transactionNodeExternalAccountCardBase, transactionNodeExternalAccountApmBase, transactionNodeExternalAccountCrypto, z.object({type: z.literal("apm"), method: z.enum(["apple-pay", "google-pay", "paypal", "venmo"])})]); export const quoteOriginEndpoint = z.object({asset: z.string(), amount: z.string(), rate: z.string(), node: quoteOriginNode}); export const quoteDestinationNode = z.discriminatedUnion("type", [z.object({type: z.literal("account"), id: z.string(), ownerId: z.string()}), transactionNodeExternalAccountBank, transactionNodeExternalAccountCardBase, transactionNodeExternalAccountApmBase, transactionNodeExternalAccountCrypto, z.object({type: z.literal("apm"), method: z.enum(["apple-pay", "google-pay", "paypal", "venmo"])}), z.object({type: z.literal("crypto-address"), network: stringNoEdgeSpaces, address: stringNoEdgeSpaces.optional(), reference: z.string().optional(), execution: transactionNodeCryptoAddressExecution.optional()})]); export const quoteDestinationEndpoint = z.object({asset: z.string(), amount: z.string(), rate: z.string(), node: quoteDestinationNode}); export const transactionDenomination = z.object({asset: z.string(), amount: z.string(), rate: z.string(), target: z.enum(["origin", "destination"])}); export const transactionFees = z.array(z.object({type: z.enum(["deposit", "exchange", "network", "withdrawal"]), code: z.string(), asset: z.string(), amount: z.string(), percentage: z.string().optional(), beneficiary: z.enum(["uphold", "partner"]).optional()})); export const quoteRequirements = z.array(z.enum(["travel-rule", "authorize:3ds", "authorize:apple-pay", "authorize:google-pay", "authorize:paypal", "authorize:venmo"])); export const quote = z.object({id: z.string(), origin: quoteOriginEndpoint, destination: quoteDestinationEndpoint, denomination: transactionDenomination, fees: transactionFees, requirements: quoteRequirements, refreshAt: z.string(), expiresAt: z.string()}); export const createApiCoreTransactionsQuoteResponse = z.object({quote: quote}); 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 quote * * POST /core/transactions/quote */ export const createApiCoreTransactionsQuote = async (args: CreateApiCoreTransactionsQuoteArgs, options?: CreateApiCoreTransactionsQuoteOptions) => { const {baseUrl = "https://api.enterprise.sandbox.uphold.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-On-Behalf-Of'] != null) headers.set("X-On-Behalf-Of", String(args['X-On-Behalf-Of'])) if (args['X-Uphold-User-Ip'] != null) headers.set("X-Uphold-User-Ip", String(args['X-Uphold-User-Ip'])) if (args['X-Uphold-User-Agent'] != null) headers.set("X-Uphold-User-Agent", String(args['X-Uphold-User-Agent'])) if (args['X-Uphold-User-Origin'] != null) headers.set("X-Uphold-User-Origin", String(args['X-Uphold-User-Origin'])) if (args['X-Uphold-User-Country'] != null) headers.set("X-Uphold-User-Country", String(args['X-Uphold-User-Country'])) if (args['X-Uphold-User-Subdivision'] != null) headers.set("X-Uphold-User-Subdivision", String(args['X-Uphold-User-Subdivision'])) if (args['X-Uphold-User-City'] != null) headers.set("X-Uphold-User-City", String(args['X-Uphold-User-City'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/core/transactions/quote${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(createApiCoreTransactionsQuoteResponse, await res.json()) };