import {z} from 'zod' export type DateFilterDto = {isnull?: boolean | undefined, notnull?: boolean | undefined, gt?: string | undefined, gte?: string | undefined, lt?: string | undefined, lte?: string | undefined}; export type GetApiApiV1TransactionsArgs = {page?: number | undefined, limit?: number | undefined, createdAt?: DateFilterDto | undefined, ids?: string | undefined, subscriptionId?: string | undefined, paymentIntentId?: string | undefined, type?: string | undefined}; export type GetApiApiV1TransactionsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dateFilterDto = z.object({isnull: z.boolean().optional(), notnull: z.boolean().optional(), gt: z.string().optional(), gte: z.string().optional(), lt: z.string().optional(), lte: z.string().optional()}); export const paginationFilterDto = z.object({page: z.number().gte(1).optional(), limit: z.number().gte(1).lte(200).optional(), createdAt: dateFilterDto.optional()}); export const transactionType = z.enum(["incoming", "outgoing", "event", "transfer"]); export const currency = z.enum(["usdc", "usdt", "pyusd", "eurc", "brett", "dai", "btc", "eth", "matic", "bnb", "sol", "busd", "trx", "food", "bonk", "wif", "goat", "spx", "truf", "pengu", "usd", "inr"]); export const superTokenDto = z.object({address: z.string(), decimals: z.number(), symbol: z.string()}); export const chainType = z.enum(["evm", "solana", "tron", "stripe", "binance_pay"]); export const chainSlimDto = z.object({id: z.number(), name: z.string(), icon: z.string(), currency: currency, blockExplorerTransactionUrl: z.string(), blockExplorerName: z.string(), type: chainType, isBeta: z.boolean()}); export const checkoutSessionAssetDto = z.object({id: z.string(), name: z.string(), address: z.string(), chainId: z.number(), currency: currency, symbol: z.string(), decimals: z.number(), coingeckoId: z.string().optional(), isPublic: z.boolean(), superToken: superTokenDto.optional(), chain: chainSlimDto}); export const getApiApiV1TransactionsResponse = z.object({page: z.number(), limit: z.number(), count: z.number(), hasMore: z.boolean(), query: paginationFilterDto.optional(), data: z.array(z.object({type: transactionType, transactionRelayId: z.string().optional(), transactionHash: z.string().optional(), nonce: z.number().optional(), gasLimit: z.number().optional(), validUntil: z.string().optional(), fromAddress: z.string(), toAddress: z.string(), currency: currency, createdAt: z.string().optional(), amount: z.number().int().optional(), amountRaw: z.string(), assetId: z.string(), asset: checkoutSessionAssetDto, transactionUrl: 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 }; /** * List of transactions * * GET /api/v1/transactions */ export const getApiApiV1Transactions = async (args: GetApiApiV1TransactionsArgs, options?: GetApiApiV1TransactionsOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = 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.limit != null) query.append("limit", String(args.limit)) if (args.createdAt != null) query.append("createdAt", JSON.stringify(args.createdAt)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.subscriptionId != null) query.append("subscriptionId", String(args.subscriptionId)) if (args.paymentIntentId != null) query.append("paymentIntentId", String(args.paymentIntentId)) if (args.type != null) query.append("type", String(args.type)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transactions${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(getApiApiV1TransactionsResponse, await res.json()) };