import {z} from 'zod' export type MichelineFormat = 'Json' | 'JsonString' | 'Raw' | 'RawString'; export type Symbols = 'None' | 'Btc' | 'Eur' | 'Usd' | 'Cny' | 'Jpy' | 'Krw' | 'Eth' | 'Gbp'; export type GetApiV1OperationsTransactionsHashCounterArgs = {hash: string, counter: number, micheline?: MichelineFormat | undefined, quote?: Symbols | undefined}; export type GetApiV1OperationsTransactionsHashCounterOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const alias = z.object({alias: z.string().nullable().optional(), address: z.string()}); export const txParameter = z.object({entrypoint: z.string(), value: z.unknown()}); export const bigMapKeyShort = z.object({hash: z.string(), key: z.unknown(), value: z.unknown()}); export const bigMapDiff = z.object({bigmap: z.number().int().optional(), path: z.string(), action: z.string(), content: bigMapKeyShort.nullable().optional()}); export const operationError = z.object({type: z.string()}); export const quoteShort = z.object({btc: z.number().nullable().optional(), eur: z.number().nullable().optional(), usd: z.number().nullable().optional(), cny: z.number().nullable().optional(), jpy: z.number().nullable().optional(), krw: z.number().nullable().optional(), eth: z.number().nullable().optional(), gbp: z.number().nullable().optional()}); export const transactionOperation = z.object({type: z.string().optional(), id: z.number().int().optional(), level: z.number().int().optional(), timestamp: z.string().optional(), block: z.string(), hash: z.string(), counter: z.number().int().optional(), initiator: alias.nullable().optional(), sender: alias, senderCodeHash: z.number().int().nullable().optional(), nonce: z.number().int().nullable().optional(), gasLimit: z.number().int().optional(), gasUsed: z.number().int().optional(), storageLimit: z.number().int().optional(), storageUsed: z.number().int().optional(), bakerFee: z.number().int().optional(), storageFee: z.number().int().optional(), allocationFee: z.number().int().optional(), target: alias.nullable().optional(), targetCodeHash: z.number().int().nullable().optional(), amount: z.number().int().optional(), parameter: txParameter.nullable().optional(), storage: z.unknown(), diffs: z.array(bigMapDiff).nullable().optional(), status: z.string(), errors: z.array(operationError).nullable().optional(), hasInternals: z.boolean().optional(), tokenTransfersCount: z.number().int().nullable().optional(), ticketTransfersCount: z.number().int().nullable().optional(), eventsCount: z.number().int().nullable().optional(), quote: quoteShort.nullable().optional()}); export const getApiV1OperationsTransactionsHashCounterResponse = z.array(transactionOperation); 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 transaction by hash and counter * * GET /v1/operations/transactions/{hash}/{counter} */ export const getApiV1OperationsTransactionsHashCounter = async (args: GetApiV1OperationsTransactionsHashCounterArgs, options?: GetApiV1OperationsTransactionsHashCounterOptions) => { const {baseUrl = "https://api.tzkt.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.micheline != null) query.append("micheline", String(args.micheline)) if (args.quote != null) query.append("quote", String(args.quote)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/operations/transactions/${encodeURIComponent(String(args.hash))}/${encodeURIComponent(String(args.counter))}${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(getApiV1OperationsTransactionsHashCounterResponse, await res.json()) };