import {z} from 'zod' export type AddressHash = string; export type EmptyString = string; export type IntegerStringNullable = string | null; export type TokenType = 'ERC-20' | 'ERC-721' | 'ERC-1155' | 'ERC-404' | 'ERC-7984'; export type GetApiChainIdApiV2AddressesAddressHashParamNftArgs = {address_hash_param: AddressHash, type?: (EmptyString | string) | undefined, items_count?: number | undefined, token_contract_address_hash?: AddressHash | undefined, token_id?: IntegerStringNullable | undefined, token_type?: TokenType | undefined, chain_id: string}; export type GetApiChainIdApiV2AddressesAddressHashParamNftOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uRLNullable = z.string().nullable(); export const integerString = z.string(); export const addressHash = z.string(); 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 floatStringNullable = z.string().nullable(); export const integerStringNullable = 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 tokenInstanceInList = 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, value: integerStringNullable}); export const getApiChainIdApiV2AddressesAddressHashParamNftResponse = z.object({items: z.array(tokenInstanceInList), next_page_params: z.record(z.string(), z.unknown()).nullable()}); 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 }; /** * List NFTs owned by a specific address with optional type filtering * * GET /{chain_id}/api/v2/addresses/{address_hash_param}/nft * * Also accepts auth: apiKeyAuth */ export const getApiChainIdApiV2AddressesAddressHashParamNft = async (args: GetApiChainIdApiV2AddressesAddressHashParamNftArgs, options?: GetApiChainIdApiV2AddressesAddressHashParamNftOptions) => { 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 (args.type != null) query.append("type", String(args.type)) if (args.items_count != null) query.append("items_count", String(args.items_count)) if (args.token_contract_address_hash != null) query.append("token_contract_address_hash", String(args.token_contract_address_hash)) if (args.token_id != null) query.append("token_id", String(args.token_id)) if (args.token_type != null) query.append("token_type", String(args.token_type)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.chain_id))}/api/v2/addresses/${encodeURIComponent(String(args.address_hash_param))}/nft${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(getApiChainIdApiV2AddressesAddressHashParamNftResponse, await res.json()) };