import {z} from 'zod' export type GetApiV2InfoChainsArgs = {chain_ids?: Array | undefined, include_evm?: boolean | undefined, include_svm?: boolean | undefined, only_testnets?: boolean | undefined}; export type GetApiV2InfoChainsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const cosmosModuleSupport = z.object({authz: z.boolean().optional(), feegrant: z.boolean().optional()}); export const feeAsset = z.object({denom: z.string(), gas_price: z.object({average: z.string().optional(), high: z.string().optional(), low: z.string().optional()}).nullable().optional()}); export const chainType = z.enum(["cosmos", "evm", "svm"]); export const ibcCapabilities = z.object({cosmos_pfm: z.boolean().optional(), cosmos_ibc_hooks: z.boolean().optional(), cosmos_memo: z.boolean().optional(), cosmos_autopilot: z.boolean().optional()}); export const chain = z.object({chain_name: z.string(), chain_id: z.string(), pfm_enabled: z.boolean(), cosmos_module_support: cosmosModuleSupport, supports_memo: z.boolean(), logo_uri: z.string().nullable().optional(), bech32_prefix: z.string(), fee_assets: z.array(feeAsset), chain_type: chainType, ibc_capabilities: ibcCapabilities, is_testnet: z.boolean(), pretty_name: z.string()}); export const getApiV2InfoChainsResponse = z.object({chains: z.array(chain).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 }; /** * Get all supported chains along with additional data useful for building applications + frontends that interface with them (e.g. logo URI, IBC capabilities, fee assets, bech32 prefix, etc...) * * GET /v2/info/chains */ export const getApiV2InfoChains = async (args: GetApiV2InfoChainsArgs, options?: GetApiV2InfoChainsOptions) => { const {baseUrl = "https://api.skip.build", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.chain_ids ?? []) query.append("chain_ids", String(item)) if (args.include_evm != null) query.append("include_evm", String(args.include_evm)) if (args.include_svm != null) query.append("include_svm", String(args.include_svm)) if (args.only_testnets != null) query.append("only_testnets", String(args.only_testnets)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/info/chains${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(getApiV2InfoChainsResponse, await res.json()) };