import {z} from 'zod' export type GetApiChainIdApiV2MainPageTransactionsArgs = {chain_id: string}; export type GetApiChainIdApiV2MainPageTransactionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fullHashNullable = z.string().nullable(); export const timestampNullable = z.string().nullable(); export const integerString = z.string(); export const addressHash = z.string(); export const signedAuthorization = z.object({address_hash: addressHash, authority: addressHash, chain_id: z.number().int(), nonce: integerString, r: integerString, s: integerString, status: z.enum(["ok", "invalid_chain_id", "invalid_signature", "invalid_nonce"]).nullable(), v: z.number().int()}); export const integerStringNullable = z.string().nullable(); export const implementation = z.object({address_hash: addressHash, name: z.string().nullable()}); export const metadataTag = z.object({meta: z.record(z.string(), z.unknown()).nullable(), name: z.string(), ordinal: z.number().int(), slug: z.string(), tagType: z.enum(["name", "generic", "classifier", "information", "note", "protocol"])}); export const metadata = z.object({tags: z.array(metadataTag)}); export const tag = z.object({address_hash: addressHash, display_name: z.string(), label: z.string()}); export const proxyType = z.enum(["eip1167", "eip1967", "eip1822", "eip1967_oz", "eip1967_beacon", "master_copy", "basic_implementation", "basic_get_implementation", "comptroller", "eip2535", "clone_with_immutable_arguments", "eip7702", "resolved_delegate_proxy", "erc7760", "minimal_proxy"]).nullable(); export const watchlistName = z.object({display_name: z.string(), label: z.string()}); export const address = z.object({ens_domain_name: z.string().nullable(), hash: addressHash, implementations: z.array(implementation), is_contract: z.boolean().nullable(), is_scam: z.boolean(), is_verified: z.boolean().nullable(), metadata: metadata.nullable(), name: z.string().nullable(), private_tags: z.array(tag).optional(), proxy_type: proxyType, public_tags: z.array(tag).optional(), reputation: z.enum(["ok", "scam"]), watchlist_names: z.array(watchlistName).optional()}); export const decodedInput = z.object({method_call: z.string().nullable(), method_id: z.string().nullable(), parameters: z.array(z.object({name: z.string().optional(), type: z.string().optional(), value: z.union([z.object({}), z.array(z.union([z.object({}), z.array(z.string()), z.string()])), z.string()]).optional()}))}); export const floatStringNullable = z.string().nullable(); export const fee = z.object({type: z.enum(["maximum", "actual"]), value: integerStringNullable}); export const fullHash = z.string(); export const methodNameNullable = z.string().nullable(); export const hexData = z.string(); export const uRLNullable = z.string().nullable(); export const tokenType = z.enum(["ERC-20", "ERC-721", "ERC-1155", "ERC-404", "ERC-7984"]); export const token = z.object({address_hash: addressHash, bridge_type: z.enum(["omni", "amb"]).nullable().optional(), circulating_market_cap: floatStringNullable, circulating_supply: floatStringNullable, decimals: integerStringNullable, exchange_rate: floatStringNullable, foreign_address: z.string().nullable().optional(), holders_count: integerStringNullable, icon_url: uRLNullable, name: z.string().nullable(), origin_chain_id: integerStringNullable.optional(), reputation: z.enum(["ok", "scam"]).nullable(), symbol: z.string().nullable(), total_supply: integerStringNullable, type: tokenType.nullable(), volume_24h: floatStringNullable}); export const tokenInstance = z.object({animation_media_type: z.enum(["image", "video", "html"]).nullable().optional(), animation_url: uRLNullable, external_app_url: uRLNullable, id: integerString, image_media_type: z.enum(["image", "video", "html"]).nullable().optional(), image_url: uRLNullable, is_unique: z.boolean().nullable(), media_type: z.string().nullable(), media_url: uRLNullable, metadata: z.record(z.string(), z.unknown()).nullable(), owner: address.nullable(), thumbnails: z.object({"250x250": z.string().optional(), "500x500": z.string().optional(), "60x60": z.string().optional(), original: z.string()}).nullable(), token: token.nullable(), token_type: tokenType.nullable().optional(), value: integerStringNullable.optional()}); export const totalERC721 = z.object({token_id: integerStringNullable, token_instance: tokenInstance.nullable()}); export const totalERC1155 = z.object({decimals: integerStringNullable, token_id: integerStringNullable, token_instance: tokenInstance.nullable(), value: integerStringNullable}); export const totalERC7984 = z.object({decimals: integerStringNullable, value: integerStringNullable}); export const total = z.object({decimals: integerStringNullable, value: integerStringNullable}); export const tokenTransfer = z.object({block_hash: fullHash, block_number: z.number().int(), from: address, log_index: z.number().int(), method: methodNameNullable, timestamp: timestampNullable, to: address, token: token, token_type: tokenType, total: z.union([totalERC721, totalERC1155, totalERC7984, total]).nullable(), transaction_hash: fullHash, type: z.enum(["token_burning", "token_minting", "token_spawning", "token_transfer"])}); export const transactionResponse = z.object({arbitrum: z.object({batch_data_container: z.string().nullable().optional(), batch_number: z.number().int().nullable().optional(), commitment_transaction: z.object({hash: fullHashNullable, status: z.enum(["unfinalized", "finalized"]).nullable(), timestamp: timestampNullable}).optional(), confirmation_transaction: z.object({hash: fullHashNullable, status: z.enum(["unfinalized", "finalized"]).nullable(), timestamp: timestampNullable}).optional(), contains_message: z.enum(["incoming", "outcoming"]).nullable().optional(), gas_used_for_l1: integerString, gas_used_for_l2: integerString, message_related_info: z.object({associated_l1_transaction_hash: fullHashNullable.optional(), message_id: z.number().int().optional(), message_status: z.enum(["Syncing with base layer", "Relayed", "Settlement pending", "Waiting for confirmation", "Ready for relay"]).optional()}).optional(), network_fee: integerString, poster_fee: integerString, status: z.enum(["Confirmed on base", "Sent to base", "Sealed on rollup", "Processed on rollup"]).optional()}).optional(), authorization_list: z.array(signedAuthorization).nullable(), base_fee_per_gas: integerStringNullable, block_number: z.number().int().nullable(), confirmation_duration: z.array(z.number().int().gte(0)), confirmations: z.number().int().gte(0), created_contract: address.nullable(), decoded_input: decodedInput.nullable(), exchange_rate: floatStringNullable, fee: fee, fhe_operations_count: z.number().int(), from: address, gas_limit: integerString, gas_price: integerStringNullable, gas_used: integerStringNullable, has_error_in_internal_transactions: z.boolean().nullable(), hash: fullHash, historic_exchange_rate: floatStringNullable, is_pending_update: z.boolean().nullable(), max_fee_per_gas: integerStringNullable, max_priority_fee_per_gas: integerStringNullable, method: methodNameNullable, nonce: z.number().int().gte(0), position: z.number().int().gte(0).nullable(), priority_fee: integerStringNullable, raw_input: hexData, result: z.union([z.enum(["pending", "awaiting_internal_transactions", "success", "dropped/replaced"]), z.string()]), revert_reason: z.union([decodedInput, z.object({raw: z.union([hexData, z.string()]).nullable()})]).nullable(), status: z.enum(["ok", "error"]).nullable(), timestamp: timestampNullable, to: address, token_transfers: z.array(tokenTransfer).nullable(), token_transfers_overflow: z.boolean().nullable(), transaction_burnt_fee: integerStringNullable, transaction_tag: z.string().nullable(), transaction_types: z.array(z.enum(["coin_transfer", "contract_call", "contract_creation", "rootstock_bridge", "rootstock_remasc", "token_creation", "token_transfer", "blob_transaction", "set_code_transaction"])), type: z.number().int().nullable(), value: integerString}); export const getApiChainIdApiV2MainPageTransactionsResponse = z.array(transactionResponse); 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 }; /** * Retrieve recent transactions as displayed on Blockscout homepage * * GET /{chain_id}/api/v2/main-page/transactions * * Also accepts auth: apiKeyAuth */ export const getApiChainIdApiV2MainPageTransactions = async (args: GetApiChainIdApiV2MainPageTransactionsArgs, options?: GetApiChainIdApiV2MainPageTransactionsOptions) => { const {baseUrl = "https://api.blockscout.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.chain_id))}/api/v2/main-page/transactions${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(getApiChainIdApiV2MainPageTransactionsResponse, await res.json()) };