import {z} from 'zod' export type GetApiV1ChainNameAddressWalletAddressTransfersV2Args = {chainName: string, walletAddress: string, 'quote-currency'?: string | undefined, 'contract-address': string, 'starting-block'?: number | undefined, 'ending-block'?: number | undefined, 'page-size'?: number | undefined, 'page-number'?: number | undefined}; export type GetApiV1ChainNameAddressWalletAddressTransfersV2Options = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ChainNameAddressWalletAddressTransfersV2Response = z.object({address: z.string().optional(), updated_at: z.string().optional(), quote_currency: z.string().optional(), chain_id: z.number().int().optional(), chain_name: z.string().optional(), chain_tip_height: z.number().int().optional(), chain_tip_signed_at: z.string().optional(), items: z.array(z.object({block_signed_at: z.string().optional(), block_height: z.number().int().optional(), block_hash: z.string().optional(), tx_hash: z.string().optional(), tx_offset: z.number().int().optional(), successful: z.boolean().optional(), miner_address: z.string().optional(), from_address: z.string().optional(), to_address: z.string().optional(), value: z.string().optional(), value_quote: z.number().optional(), pretty_value_quote: z.string().optional(), gas_metadata: z.object({contract_decimals: z.number().int().optional(), contract_name: z.string().optional(), contract_ticker_symbol: z.string().optional(), contract_address: z.string().optional(), supports_erc: z.array(z.string()).optional(), logo_url: z.string().optional()}).optional(), gas_offered: z.number().int().optional(), gas_spent: z.number().int().optional(), gas_price: z.number().int().optional(), fees_paid: z.string().optional(), gas_quote: z.number().optional(), pretty_gas_quote: z.string().optional(), gas_quote_rate: z.number().optional(), transfers: z.array(z.object({block_signed_at: z.string().optional(), tx_hash: z.string().optional(), from_address: z.string().optional(), to_address: z.string().optional(), contract_decimals: z.number().int().optional(), contract_name: z.string().optional(), contract_ticker_symbol: z.string().optional(), contract_address: z.string().optional(), logo_url: z.string().optional(), transfer_type: z.string().optional(), delta: z.string().optional(), balance: z.string().optional(), quote_rate: z.number().optional(), delta_quote: z.number().optional(), pretty_delta_quote: z.string().optional(), balance_quote: z.number().optional(), method_calls: z.array(z.object({sender_address: z.string().optional(), method: z.string().optional()})).optional(), explorers: z.array(z.object({label: z.string().optional(), url: z.string().optional()})).optional()})).optional()})).optional(), pagination: z.object({has_more: z.boolean().optional(), page_number: z.number().int().optional(), page_size: z.number().int().optional(), total_count: z.number().int().optional()}).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 }; /** * Returns the transfer-in and transfer-out of a token for an address, along with historical prices. * * GET /v1/{chainName}/address/{walletAddress}/transfers_v2/ */ export const getApiV1ChainNameAddressWalletAddressTransfersV2 = async (args: GetApiV1ChainNameAddressWalletAddressTransfersV2Args, options?: GetApiV1ChainNameAddressWalletAddressTransfersV2Options) => { const {baseUrl = "https://api.covalenthq.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['quote-currency'] != null) query.append("quote-currency", String(args['quote-currency'])) if (args['contract-address'] != null) query.append("contract-address", String(args['contract-address'])) if (args['starting-block'] != null) query.append("starting-block", String(args['starting-block'])) if (args['ending-block'] != null) query.append("ending-block", String(args['ending-block'])) if (args['page-size'] != null) query.append("page-size", String(args['page-size'])) if (args['page-number'] != null) query.append("page-number", String(args['page-number'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/${encodeURIComponent(String(args.chainName))}/address/${encodeURIComponent(String(args.walletAddress))}/transfers_v2/${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(getApiV1ChainNameAddressWalletAddressTransfersV2Response, await res.json()) };