import {z} from 'zod' export type CreateApiAnkrGetNFTsByOwnerArgs = {body: {id: number, jsonrpc: '2.0', method: 'ankr_getNFTsByOwner', params: {walletAddress: string, blockchain?: (Array<'arbitrum' | 'avalanche' | 'base' | 'bsc' | 'eth' | 'fantom' | 'flare' | 'gnosis' | 'linea' | 'optimism' | 'polygon' | 'scroll' | 'somnia_mainnet' | 'taiko' | 'telos' | 'xlayer' | 'avalanche_fuji' | 'base_sepolia' | 'eth_sepolia' | 'optimism_sepolia' | 'polygon_amoy'>) | undefined, filter?: Array<{[key: string]: Array}> | undefined, pageSize?: number | undefined, pageToken?: string | undefined}}}; export type CreateApiAnkrGetNFTsByOwnerOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAnkrGetNFTsByOwnerResponse = z.object({id: z.number().int(), jsonrpc: z.literal("2.0"), result: z.object({owner: z.string(), nextPageToken: z.string().optional(), assets: z.array(z.object({blockchain: z.enum(["arbitrum", "avalanche", "base", "bsc", "eth", "fantom", "flare", "gnosis", "linea", "optimism", "polygon", "scroll", "somnia_mainnet", "taiko", "telos", "xlayer", "avalanche_fuji", "base_sepolia", "eth_sepolia", "optimism_sepolia", "polygon_amoy"]), collectionName: z.string().optional(), contractAddress: z.string().optional(), contractType: z.string(), imageUrl: z.string().optional(), name: z.string().optional(), quantity: z.string().optional(), symbol: z.string().optional(), tokenId: z.string(), tokenUrl: z.string().optional(), traits: z.array(z.object({trait_type: z.string().optional(), value: z.string().optional()})).optional()})).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 (ERC-721/ERC-1155/ENS/POAP) owned by a wallet * * POST /?ankr_getNFTsByOwner */ export const createApiAnkrGetNFTsByOwner = async (args: CreateApiAnkrGetNFTsByOwnerArgs, options?: CreateApiAnkrGetNFTsByOwnerOptions) => { 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(/\/+$/, '')}/?ankr_getNFTsByOwner${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(createApiAnkrGetNFTsByOwnerResponse, await res.json()) };