import {z} from 'zod' export type CreateApiStellarGetEventsArgs = {body: {id: number, jsonrpc: '2.0', method: 'stellar_getEvents', params: {blockchain: 'stellar_soroban', startLedger: number, endLedger?: number | undefined, limit?: number | undefined, cursor?: string | undefined, filters?: (Array<{type?: ('contract' | 'system' | 'diagnostic') | undefined, contractIds?: Array | undefined, topics?: Array> | undefined}>) | undefined}}}; export type CreateApiStellarGetEventsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiStellarGetEventsResponse = z.object({id: z.number().int(), jsonrpc: z.literal("2.0"), result: z.object({events: z.array(z.object({type: z.enum(["contract", "system", "diagnostic"]).optional(), ledger: z.number().int().optional(), ledgerClosedAt: z.string().optional(), contractId: z.string().optional(), id: z.string().optional(), pagingToken: z.string().optional(), topic: z.array(z.string()).optional(), value: z.string().optional(), inSuccessfulContractCall: z.boolean().optional(), txHash: z.string().optional()})), cursor: z.string().optional(), latestLedger: z.number().int().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 }; /** * Stream Soroban smart-contract events over a ledger range * * POST /?stellar_getEvents */ export const createApiStellarGetEvents = async (args: CreateApiStellarGetEventsArgs, options?: CreateApiStellarGetEventsOptions) => { 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_getEvents${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(createApiStellarGetEventsResponse, await res.json()) };