import {z} from 'zod' export type GetApiApiV1TradingInfoDemoAggregatePortfolioArgs = {'x-request-id': string, 'x-api-key': string, 'x-user-key': string, conversionMode?: ('eToroApp' | 'Realtime') | undefined, instrumentIds?: Array | undefined, mirrorIds?: Array | undefined}; export type GetApiApiV1TradingInfoDemoAggregatePortfolioOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const aggregatedPortfolioAccountTotals = z.object({accountAvailableCash: z.number().optional(), accountFrozenCash: z.number().optional(), accountCurrentPnl: z.number().optional(), accountTotalValue: z.number().optional(), accountTotalUsedMargin: z.number().optional(), accountBalance: z.number().optional()}); export const aggregatedPortfolioInstrumentAggregate = z.object({instrumentId: z.number().int().optional(), assetCurrency: z.string().optional(), totalMarginAccountCurrency: z.number().optional(), totalFees: z.number().optional(), totalFeesAcctCcy: z.number().optional(), totalTaxes: z.number().optional(), totalTaxesAcctCcy: z.number().optional(), totalMarginAssetCurrency: z.number().optional(), pnlAssetCurrency: z.number().nullable().optional(), accountCurrencyRoePercent: z.number().optional(), netContracts: z.number().optional(), netUnits: z.number().optional(), netCurrentExposureAssetCurrency: z.number().optional(), netCurrentExposureAccountCurrency: z.number().optional(), netInitialExposureAccountCurrency: z.number().optional(), accountCurrencyReturn: z.number().optional(), liquidationValueAccountCurrency: z.number().optional(), liquidationValueAssetCurrency: z.number().optional(), avgLeverage: z.number().optional(), avgOpenRate: z.number().optional(), netAvgOpenRate: z.number().optional(), avgConversionRate: z.number().optional()}); export const aggregatedPortfolioMirrorTotals = z.object({mirrorNetFunding: z.number().optional(), mirrorPositionsPnl: z.number().optional(), mirrorLiquidationValue: z.number().optional(), mirrorPositionsPnlPercent: z.number().optional(), mirrorMarginPercent: z.number().optional(), mirrorValuePercent: z.number().optional(), mirrorActiveMargin: z.number().optional()}); export const aggregatedPortfolioMirrorAggregate = z.object({mirrorId: z.number().int().optional(), mirrorAvailableCash: z.number().optional(), mirrorDepositTotal: z.number().optional(), mirrorWithdrawalTotal: z.number().optional(), mirrorStopLossPercentage: z.number().optional(), mirrorStopLoss: z.number().optional(), mirrorClosedPositionsPnl: z.number().optional(), mirrorTotals: aggregatedPortfolioMirrorTotals.optional(), instrumentAggregates: z.array(aggregatedPortfolioInstrumentAggregate).optional()}); export const aggregatedPortfolioResponse = z.object({cid: z.number().int().optional(), timestamp: z.string().optional(), accountCurrency: z.string().optional(), accountTotals: aggregatedPortfolioAccountTotals.optional(), instrumentAggregates: z.array(aggregatedPortfolioInstrumentAggregate).optional(), mirrors: z.array(aggregatedPortfolioMirrorAggregate).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 aggregated portfolio snapshot * * GET /api/v1/trading/info/demo/aggregate-portfolio * * Also accepts auth: oauth2, oauth2, oauth2 */ export const getApiApiV1TradingInfoDemoAggregatePortfolio = async (args: GetApiApiV1TradingInfoDemoAggregatePortfolioArgs, options?: GetApiApiV1TradingInfoDemoAggregatePortfolioOptions) => { const {baseUrl = "https://public-api.etoro.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-request-id'] != null) headers.set("x-request-id", String(args['x-request-id'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['x-user-key'] != null) headers.set("x-user-key", String(args['x-user-key'])) if (args.conversionMode != null) query.append("conversionMode", String(args.conversionMode)) if (args.instrumentIds != null) query.append("instrumentIds", args.instrumentIds.join(',')) if (args.mirrorIds != null) query.append("mirrorIds", args.mirrorIds.join(',')) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/trading/info/demo/aggregate-portfolio${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(aggregatedPortfolioResponse, await res.json()) };