import {z} from 'zod' export type CreateApiStellarGetAccountBalanceArgs = {body: {id: number, jsonrpc: '2.0', method: 'stellar_getAccountBalance', params: {blockchain: 'stellar_soroban', walletAddress: string, onlyWhitelisted?: boolean | undefined, nativeFirst?: boolean | undefined, pageSize?: number | undefined, pageToken?: string | undefined}}}; export type CreateApiStellarGetAccountBalanceOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const syncStatus = z.object({timestamp: z.number().int().optional(), blockNumber: z.number().int().optional(), lag: z.string().optional(), status: z.string().optional()}); export const createApiStellarGetAccountBalanceResponse = z.object({id: z.number().int(), jsonrpc: z.literal("2.0"), result: z.object({totalBalanceUsd: z.string(), totalCount: z.number().int().optional(), nextPageToken: z.string().optional(), assets: z.array(z.object({blockchain: z.literal("stellar_soroban").optional(), tokenName: z.string().optional(), tokenSymbol: z.string().optional(), tokenDecimals: z.number().int().optional(), tokenType: z.string().optional(), contractAddress: z.string().optional(), holderAddress: z.string().optional(), balance: z.string().optional(), balanceRawInteger: z.string().optional(), balanceUsd: z.string().optional(), tokenPrice: z.string().optional(), thumbnail: z.string().optional()})), syncStatus: syncStatus.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 }; /** * Read a Stellar account's XLM and Stellar Asset Contract balances * * POST /?stellar_getAccountBalance */ export const createApiStellarGetAccountBalance = async (args: CreateApiStellarGetAccountBalanceArgs, options?: CreateApiStellarGetAccountBalanceOptions) => { const {baseUrl = "https://rpc.ankr.com/multichain/569ca1f392f7d3d0bb1073a1173ecb649af1d9249231fe59fa95db19d5ae41fe", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/?stellar_getAccountBalance${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(createApiStellarGetAccountBalanceResponse, await res.json()) };