import {z} from 'zod' export type BalanceRequestChainEntry = {address?: string | undefined, denoms?: Array | undefined}; export type BalancesRequest = {chains?: {[key: string]: BalanceRequestChainEntry} | undefined}; export type CreateApiV2InfoBalancesArgs = {body: BalancesRequest}; export type CreateApiV2InfoBalancesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiError = z.object({message: z.string().optional()}); export const balanceResponseDenomEntry = z.object({amount: z.string(), decimals: z.number().int().nullable().optional(), formatted_amount: z.string(), price: z.string().nullable().optional(), value_usd: z.string().nullable().optional(), error: apiError.optional()}); export const balanceResponseChainEntry = z.object({denoms: z.record(z.string(), balanceResponseDenomEntry).optional()}); export const createApiV2InfoBalancesResponse = z.object({chains: z.record(z.string(), balanceResponseChainEntry).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 the balances of a given set of assets on a given chain and wallet address. Compatible with all Skip Go-supported assets, excluding CW20 assets, across SVM, EVM, and Cosmos chains. * * POST /v2/info/balances */ export const createApiV2InfoBalances = async (args: CreateApiV2InfoBalancesArgs, options?: CreateApiV2InfoBalancesOptions) => { const {baseUrl = "https://api.skip.build", 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(/\/+$/, '')}/v2/info/balances${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(createApiV2InfoBalancesResponse, await res.json()) };