import {z} from 'zod' export type GetApiV1AccountFundingPaymentsArgs = {account?: string | undefined, market_id?: unknown | undefined, start_time?: unknown | undefined, end_time?: unknown | undefined, page?: number | undefined, limit?: number | undefined, sorted_by?: string | undefined}; export type GetApiV1AccountFundingPaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getFundingPaymentHistoryResponseFundingPaymentRecord = z.object({market_id: z.string().optional(), funding_payment: z.string().optional(), position_size: z.string().optional(), block_number: z.string().optional(), block_time: z.string().optional(), tx_hash: z.string().optional(), side: z.string().optional(), funding_rate: z.string().optional(), is_settled: z.boolean().optional(), funding_rate_8h: z.string().optional(), trade_side: z.string().optional(), entry_type: z.string().optional()}); export const apiGetFundingPaymentHistoryResponse = z.object({account: z.string().optional(), records: z.array(getFundingPaymentHistoryResponseFundingPaymentRecord).optional(), page: z.number().int().optional(), has_next_page: z.boolean().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 }; /** * Get funding payment history * * GET /v1/account/funding-payments */ export const getApiV1AccountFundingPayments = async (args: GetApiV1AccountFundingPaymentsArgs, options?: GetApiV1AccountFundingPaymentsOptions) => { const {baseUrl = "https://api.testnet.rise.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.market_id != null) query.append("market_id", String(args.market_id)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sorted_by != null) query.append("sorted_by", String(args.sorted_by)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/funding-payments${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(apiGetFundingPaymentHistoryResponse, await res.json()) };