import {z} from 'zod' export type StellarChain = 'pubnet' | 'futurenet' | 'testnet'; export type StellarOptions = 'validation' | 'simulation' | 'gas_estimation'; export type StellarWalletRequestMetadata = {type: 'wallet', url: string, non_dapp?: (boolean | null) | undefined}; export type StellarInAppRequestMetadata = {type?: 'in_app' | undefined, non_dapp?: (boolean | null) | undefined}; export type TransactionScanSupportedChains = 'arbitrum' | 'avalanche' | 'base' | 'base-sepolia' | 'lordchain' | 'lordchain-testnet' | 'metacade' | 'metacade-testnet' | 'bsc' | 'ethereum' | 'optimism' | 'polygon' | 'zksync' | 'zksync-sepolia' | 'zora' | 'linea' | 'blast' | 'scroll' | 'ethereum-sepolia' | 'degen' | 'avalanche-fuji' | 'gnosis' | 'soneium-minato' | 'ronin' | 'apechain' | 'berachain' | 'berachain-bartio' | 'ink' | 'ink-sepolia' | 'abstract' | 'abstract-testnet' | 'soneium' | 'unichain' | 'sei' | 'flow-evm' | 'hyperevm' | 'megaeth' | 'katana' | 'plume' | 'xlayer' | 'monad' | 'monad-testnet' | 'tempo' | 'tempo-testnet' | 'kite-ai' | 'kaia' | 'plasma' | 'mantle' | 'robinhood' | 'arc' | 'flare'; export type CrossChainBridgeNativeAsset = {type: 'NATIVE', raw_value?: string | undefined}; export type CrossChainBridgeFungibleAsset = {type: 'FUNGIBLE', address: string, raw_value?: string | undefined}; export type CrossChainBridgeNonFungibleAsset = {type: 'NON_FUNGIBLE', address: string, token_id: string}; export type CrossChainBridgeAsset = CrossChainBridgeNativeAsset | CrossChainBridgeFungibleAsset | CrossChainBridgeNonFungibleAsset; export type CrossChainBridgeHint = {type: 'cross_chain_bridge', destination_address?: string | undefined, destination_chain?: (TransactionScanSupportedChains | string) | undefined, destination_asset?: CrossChainBridgeAsset | undefined}; export type GenericTransactionHint = {type: string}; export type TransactionHint = CrossChainBridgeHint | GenericTransactionHint; export type TransactionHints = Array; export type StellarTransactionScanRequest = {chain: StellarChain, options?: Array | undefined, account_address: string, transaction: string, metadata: StellarWalletRequestMetadata | StellarInAppRequestMetadata, transaction_hints?: TransactionHints | undefined}; export type CreateApiV0StellarTransactionScanArgs = {body: StellarTransactionScanRequest}; export type CreateApiV0StellarTransactionScanOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const stellarLegacyAssetDetailsSchema = z.object({type: z.literal("ASSET").optional(), code: z.string(), issuer: z.string(), org_name: z.string(), org_url: z.string()}); export const stellarAssetTransferDetailsSchema = z.object({usd_price: z.number().nullable().optional(), summary: z.string().nullable().optional(), value: z.number(), raw_value: z.number().int()}); export const stellarLegacyAssetDiff = z.object({asset: stellarLegacyAssetDetailsSchema, in: stellarAssetTransferDetailsSchema.optional(), out: stellarAssetTransferDetailsSchema.optional(), asset_type: z.string()}); export const stellarNativeAssetDetailsSchema = z.object({type: z.literal("NATIVE").optional(), code: z.literal("XLM").optional()}); export const stellarNativeAssetDiff = z.object({asset: stellarNativeAssetDetailsSchema, in: stellarAssetTransferDetailsSchema.optional(), out: stellarAssetTransferDetailsSchema.optional(), asset_type: z.string()}); export const stellarAssetContractDetailsSchema = z.object({type: z.literal("CONTRACT").optional(), address: z.string(), name: z.string(), symbol: z.string()}); export const stellarContractAssetDiff = z.object({asset: stellarAssetContractDetailsSchema, in: stellarAssetTransferDetailsSchema.optional(), out: stellarAssetTransferDetailsSchema.optional(), asset_type: z.string()}); export const stellarERC20DiffSchema = z.object({usd_price: z.number().nullable().optional(), summary: z.string().nullable().optional(), value: z.string(), raw_value: z.number().int()}); export const stellarSingleAssetExposureSchema = z.object({exposure: z.array(stellarERC20DiffSchema), summary: z.string().nullable().optional(), approval: z.number(), expiration: z.string().nullable().optional()}); export const stellarLegacyAssetExposure = z.object({asset: stellarLegacyAssetDetailsSchema, spenders: z.record(z.string(), stellarSingleAssetExposureSchema).optional()}); export const stellarNativeAssetExposure = z.object({asset: stellarNativeAssetDetailsSchema, spenders: z.record(z.string(), stellarSingleAssetExposureSchema).optional()}); export const stellarAccountSetOptionsOwnershipDiffSchema = z.object({type: z.literal("SET_OPTIONS").optional(), pre_signers: z.array(z.string()), post_signers: z.array(z.string())}); export const stellarAddressDetailsBaseSchema = z.object({account_address: z.unknown(), description: z.string().nullable().optional()}); export const stellarTotalUsdDiffSchema = z.object({in: z.number(), out: z.number(), total: z.number().optional()}); export const stellarAccountSummary = z.object({account_assets_diffs: z.array(z.union([stellarLegacyAssetDiff, stellarNativeAssetDiff, stellarContractAssetDiff])).optional(), account_exposures: z.array(z.union([stellarLegacyAssetExposure, stellarNativeAssetExposure])), account_ownerships_diff: z.array(stellarAccountSetOptionsOwnershipDiffSchema), total_usd_diff: stellarTotalUsdDiffSchema, total_usd_exposure: z.record(z.string(), z.number()).optional()}); export const stellarSimulationResult = z.object({status: z.literal("Success"), assets_diffs: z.record(z.string(), z.array(z.union([stellarLegacyAssetDiff, stellarNativeAssetDiff, stellarContractAssetDiff]))).optional(), exposures: z.record(z.string(), z.array(z.union([stellarLegacyAssetExposure, stellarNativeAssetExposure]))).optional(), assets_ownership_diff: z.record(z.string(), z.array(stellarAccountSetOptionsOwnershipDiffSchema)), address_details: z.array(stellarAddressDetailsBaseSchema).optional(), account_summary: stellarAccountSummary}); export const stellarSimulationErrorSchema = z.object({status: z.literal("Error"), error: z.string()}); export const stellarVerdict = z.enum(["Benign", "Warning", "Malicious"]); export const stellarValidationFeatureType = z.enum(["Benign", "Warning", "Malicious", "Info"]); export const stellarValidationFeature = z.object({type: stellarValidationFeatureType, feature_id: z.string(), description: z.string(), address: z.string()}); export const stellarValidationResult = z.object({status: z.literal("Success"), result_type: stellarVerdict, description: z.string(), reason: z.string(), classification: z.string(), features: z.array(stellarValidationFeature)}); export const stellarValidationErrorSchema = z.object({status: z.literal("Error"), error: z.string()}); export const stellarValidationResponse = z.union([stellarValidationResult, stellarValidationErrorSchema]); export const stellarClassicGasEstimationSchema = z.object({status: z.literal("Success"), base_fee: z.string(), operation_count: z.string(), used: z.string()}); export const stellarSorobanResourcesSchema = z.object({cpu_instructions: z.string(), read_bytes: z.string(), write_bytes: z.string()}); export const stellarSorobanGasEstimationSchema = z.object({status: z.literal("Success"), inclusion_fee: z.string(), resource_fee: z.string(), resources: stellarSorobanResourcesSchema, used: z.string()}); export const stellarGasEstimationErrorSchema = z.object({status: z.literal("Error"), error: z.string()}); export const stellarTransactionScanResponse = z.object({simulation: z.union([stellarSimulationResult, stellarSimulationErrorSchema]).nullable().optional(), validation: stellarValidationResponse.optional(), gas_estimation: z.union([stellarClassicGasEstimationSchema, stellarSorobanGasEstimationSchema, stellarGasEstimationErrorSchema]).nullable().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 }; /** * Scan Transaction * * POST /v0/stellar/transaction/scan * * Also accepts auth: ClientID */ export const createApiV0StellarTransactionScan = async (args: CreateApiV0StellarTransactionScanArgs, options?: CreateApiV0StellarTransactionScanOptions) => { const {baseUrl = "https://api.blockaid.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/stellar/transaction/scan${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(stellarTransactionScanResponse, await res.json()) };