import {z} from 'zod' export type CoinNonEmptyString = string; export type WalletIdNonEmptyString = string; export type CreateApiApiV2CoinWalletWalletIdGetAccountArgs = {coin: CoinNonEmptyString, walletId: WalletIdNonEmptyString, body: {addresses: Array, destinationAddress?: string | undefined}}; export type CreateApiApiV2CoinWalletWalletIdGetAccountOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const frozenV2Type = z.enum(["BANDWIDTH", "ENERGY", "TRON_POWER"]); export const tronResourceDeficit = z.object({bandwidthDeficit: z.number(), bandwidthSunRequired: z.string(), energyDeficit: z.number().optional(), energySunRequired: z.string().optional()}); export const maxResourcesDelegatable = z.object({bandwidthSun: z.string(), energySun: z.string()}); export const getAccountResponse = z.object({accounts: z.array(z.object({frozenV2: z.array(z.object({type: frozenV2Type, amount: z.string()})), delegatedFrozenV2BalanceForEnergy: z.string(), acquiredDelegatedFrozenV2BalanceForEnergy: z.string(), delegatedFrozenV2BalanceForBandwidth: z.string(), acquiredDelegatedFrozenV2BalanceForBandwidth: z.string(), energyRatio: z.number(), bandwidthRatio: z.number(), address: z.string(), freeBandwidthAvailable: z.number(), freeBandwidthUsed: z.number(), stakedBandwidthAvailable: z.number(), stakedBandwidthUsed: z.number(), energyAvailable: z.number(), energyUsed: z.number(), resourceDeficitForAssetTransfer: tronResourceDeficit.optional(), maxResourcesDelegatable: maxResourcesDelegatable.optional(), TotalNetLimit: z.number().optional(), TotalNetWeight: z.number().optional(), TotalEnergyLimit: z.number().optional(), TotalEnergyWeight: z.number().optional()})), failedAddresses: z.array(z.object({address: z.string(), error: z.string()}))}); 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 TRON account resources and resource delegations * * POST /api/v2/{coin}/wallet/{walletId}/getAccount */ export const createApiApiV2CoinWalletWalletIdGetAccount = async (args: CreateApiApiV2CoinWalletWalletIdGetAccountArgs, options: CreateApiApiV2CoinWalletWalletIdGetAccountOptions) => { const {baseUrl, 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(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.coin))}/wallet/${encodeURIComponent(String(args.walletId))}/getAccount${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(getAccountResponse, await res.json()) };