import {z} from 'zod' export type GetApiSuperuserSellTokenToPixHistoryArgs = {page?: number | undefined, pageSize?: number | undefined, id?: string | undefined, taxId?: string | undefined, txId?: string | undefined}; export type GetApiSuperuserSellTokenToPixHistoryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coin = z.enum(["BRLA", "USDC", "USDT", "ETH", "GLMR", "MATIC", "CHZ"]); export const chain = z.enum(["Polygon", "Ethereum", "Moonbeam", "Tron", "XRP", "Chiliz"]); export const markup = z.string(); export const smartcontractOperationFeedbackLog = z.object({id: z.string().optional(), success: z.boolean().optional(), errorMsg: z.string().optional(), createdAt: z.string().optional()}); export const smartcontractOperationLog = z.object({id: z.string().optional(), operationName: z.string().optional(), posted: z.boolean().optional(), tx: z.string().optional(), notPostedReason: z.string().optional(), createdAt: z.string().optional(), isRetry: z.boolean().optional(), feedback: smartcontractOperationFeedbackLog.optional()}); export const transferFeedbackLog = z.object({id: z.string().optional(), transferStatus: z.string().optional(), updatedAt: z.string().optional(), logType: z.string().optional(), error: z.string().optional()}); export const transferLog = z.object({amount: z.number().int().optional(), name: z.string().optional(), taxId: z.string().optional(), bankCode: z.string().optional(), branchCode: z.string().optional(), accountNumber: z.string().optional(), id: z.string().optional(), createdAt: z.string().optional(), feedbacks: z.array(transferFeedbackLog).optional()}); export const tokenToPixTx = z.object({id: z.object({}).optional(), fromWallet: z.string().optional(), txHash: z.string().optional(), rawAmount: z.string().optional(), amountBrl: z.number().int().optional(), markupBrla: z.number().int().optional(), accepted: z.boolean().optional(), smartContractOps: z.array(smartcontractOperationLog).optional(), transfers: z.array(transferLog).optional(), createdAt: z.string().optional()}); export const tokenToPixOrderLog = z.object({id: z.string().optional(), pixKey: z.string().optional(), userName: z.string().optional(), userTaxId: z.string().optional(), token: coin.optional(), chain: chain.optional(), paymentWalletAddress: z.string().optional(), markup: markup.optional(), markupAddress: z.string().optional(), createdAt: z.string().optional(), due: z.string().optional(), tokenToPixTxs: z.array(tokenToPixTx).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 }; /** * Retrieves history of token to pix operations * * GET /superuser/sell/token-to-pix/history * * Also accepts auth: apiKeyAuth + apiSignatureAuth + apiTimestampAuth */ export const getApiSuperuserSellTokenToPixHistory = async (args: GetApiSuperuserSellTokenToPixHistoryArgs, options?: GetApiSuperuserSellTokenToPixHistoryOptions) => { const {baseUrl = "https://api.brla.digital:5567/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.id != null) query.append("id", String(args.id)) if (args.taxId != null) query.append("taxId", String(args.taxId)) if (args.txId != null) query.append("txId", String(args.txId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/superuser/sell/token-to-pix/history${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(tokenToPixOrderLog, await res.json()) };