import {z} from 'zod' export type PaymentAccountType = 'VAULT_ACCOUNT' | 'EXCHANGE_ACCOUNT' | 'FIAT_ACCOUNT'; export type PaymentAccount = {id: string, type: PaymentAccountType}; export type PayeeAccountType = 'VAULT_ACCOUNT' | 'EXCHANGE_ACCOUNT' | 'INTERNAL_WALLET' | 'EXTERNAL_WALLET' | 'NETWORK_CONNECTION' | 'FIAT_ACCOUNT'; export type PayeeAccount = {id: string, type: PayeeAccountType}; export type InstructionAmount = {amount: string, assetId: string}; export type PayoutInstruction = {id?: string | undefined, payeeAccount: PayeeAccount, amount: InstructionAmount}; export type CreatePayoutRequest = {paymentAccount: PaymentAccount, instructionSet: Array}; export type CreateApiPaymentsPayoutArgs = {'Idempotency-Key'?: string | undefined, body: CreatePayoutRequest}; export type CreateApiPaymentsPayoutOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentAccountType = z.enum(["VAULT_ACCOUNT", "EXCHANGE_ACCOUNT", "FIAT_ACCOUNT"]); export const paymentAccountResponse = z.object({id: z.string().optional(), type: paymentAccountType.optional()}); export const payoutState = z.enum(["CREATED", "FILE_FOUND", "REQUESTED", "TRANSLATED", "PROCESSING", "SUBMITTED", "FINALIZED", "INSUFFICIENT_BALANCE", "FAILED"]); export const payoutStatus = z.enum(["REGISTERED", "VERIFYING", "IN_PROGRESS", "DONE", "INSUFFICIENT_BALANCE", "FAILED"]); export const payoutInitMethod = z.enum(["FILE", "API"]); export const payeeAccountType = z.enum(["VAULT_ACCOUNT", "EXCHANGE_ACCOUNT", "INTERNAL_WALLET", "EXTERNAL_WALLET", "NETWORK_CONNECTION", "FIAT_ACCOUNT"]); export const payeeAccountResponse = z.object({id: z.string().optional(), type: payeeAccountType.optional()}); export const instructionAmount = z.object({amount: z.string(), assetId: z.string()}); export const payoutInstructionState = z.enum(["NOT_STARTED", "TRANSACTION_SENT", "COMPLETED", "FAILED", "TRANSLATION_ERROR", "SKIPPED"]); export const transaction = z.object({id: z.string(), state: z.enum(["SUBMITTED", "QUEUED", "PENDING_AUTHORIZATION", "PENDING_SIGNATURE", "BROADCASTING", "PENDING_3RD_PARTY_MANUAL_APPROVAL", "PENDING_3RD_PARTY", "PENDING", "CONFIRMING", "CONFIRMED", "COMPLETED", "PARTIALLY_COMPLETED", "PENDING_AML_SCREENING", "CANCELLING", "CANCELLED", "REJECTED", "BLOCKED", "FAILED", "TIMEOUT"]), timestamp: z.number().optional(), instructionId: z.string().optional()}); export const payoutInstructionResponse = z.object({id: z.string().optional(), payeeAccount: payeeAccountResponse, amount: instructionAmount, state: payoutInstructionState, transactions: z.array(transaction)}); export const payoutResponse = z.object({payoutId: z.string(), paymentAccount: paymentAccountResponse, createdAt: z.number(), state: payoutState, status: payoutStatus, reasonOfFailure: z.string().optional(), initMethod: payoutInitMethod.optional(), instructionSet: z.array(payoutInstructionResponse), reportUrl: 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 a payout instruction set * * POST /payments/payout */ export const createApiPaymentsPayout = async (args: CreateApiPaymentsPayoutArgs, options?: CreateApiPaymentsPayoutOptions) => { const {baseUrl = "https://api.fireblocks.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/payments/payout${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(payoutResponse, await res.json()) };