import {z} from 'zod' export type CreateApiV2MigrationWalletsArgs = {currencies?: Array | undefined, body: {account_ids: Array}}; export type CreateApiV2MigrationWalletsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const defiProvider = z.object({card: z.string(), description: z.string(), full: z.string(), icon: z.string(), link: z.string(), miniapp_link: z.string().optional(), name: z.string(), tag: z.string()}); export const scaledUI = z.object({denominator: z.string(), numerator: z.string()}); export const jettonVerificationType = z.enum(["whitelist", "graylist", "blacklist", "none"]); export const jettonPreview = z.object({address: z.string(), custom_payload_api_uri: z.string().optional(), decimals: z.number().int(), description: z.string().optional(), image: z.string(), name: z.string(), scaled_ui: scaledUI.optional(), score: z.number().int(), symbol: z.string(), verification: jettonVerificationType}); export const defiLockedAsset = z.object({jetton: jettonPreview.optional(), type: z.enum(["native", "jetton"])}); export const defiLiquidPoolAssets = z.object({asset0: defiLockedAsset, asset1: defiLockedAsset}); export const defiAssetType = z.enum(["staking", "lending_supply", "lending_borrow", "liquid_staking", "liquid_pool", "yield_token"]); export const jettonAssetInfo = z.object({defi_provider: defiProvider, pool_assets: defiLiquidPoolAssets.optional(), token_type: defiAssetType, type: defiAssetType.optional()}); export const tokenRates = z.object({diff_24h: z.record(z.string(), z.string()).optional(), diff_30d: z.record(z.string(), z.string()).optional(), diff_7d: z.record(z.string(), z.string()).optional(), prices: z.record(z.string(), z.number()).optional()}); export const accountAddress = z.object({address: z.string(), icon: z.string().optional(), is_scam: z.boolean(), is_wallet: z.boolean(), name: z.string().optional()}); export const jettonBalance = z.object({balance: z.string(), defi_asset: jettonAssetInfo.optional(), extensions: z.array(z.string()).optional(), jetton: jettonPreview, lock: z.object({amount: z.string(), till: z.number().int()}).optional(), price: tokenRates.optional(), wallet_address: accountAddress}); export const accountStatus = z.enum(["nonexist", "uninit", "active", "frozen"]); export const migrationWalletValue = z.object({account: z.string(), balance: z.number().int(), jettons: z.array(jettonBalance), nft_count: z.number().int(), status: accountStatus}); export const migrationWallets = z.object({wallets: z.array(migrationWalletValue)}); 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 migratable assets value (TON balance, jettons with prices, NFT count) for several wallets at once. * * POST /v2/migration/wallets */ export const createApiV2MigrationWallets = async (args: CreateApiV2MigrationWalletsArgs, options?: CreateApiV2MigrationWalletsOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.currencies != null) query.append("currencies", args.currencies.join(',')) const url = `${baseUrl.replace(/\/+$/, '')}/v2/migration/wallets${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(migrationWallets, await res.json()) };