|
| 1 | +/** |
| 2 | + * @file schemas.ts |
| 3 | + * Zod schemas for every request payload / query-parameter bag that the |
| 4 | + * YieldVault API client dispatches. |
| 5 | + * |
| 6 | + * Import the schema you need and pass it to `validate()` from ./validation |
| 7 | + * before calling any API function. |
| 8 | + * |
| 9 | + * Naming convention: <Entity><Action>Schema (e.g. DepositRequestSchema) |
| 10 | + */ |
| 11 | + |
| 12 | +import { z } from "zod"; |
| 13 | + |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | +// Shared primitives |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +/** |
| 19 | + * Stellar / Soroban public key: G... base-32 address, 56 characters. |
| 20 | + * Validates format only — not an on-chain account existence check. |
| 21 | + */ |
| 22 | +export const StellarAddressSchema = z |
| 23 | + .string({ required_error: "Wallet address is required" }) |
| 24 | + .trim() |
| 25 | + .regex(/^G[A-Z2-7]{55}$/, { |
| 26 | + message: "Must be a valid Stellar public key (starts with G, 56 chars)", |
| 27 | + }); |
| 28 | + |
| 29 | +/** |
| 30 | + * Positive decimal amount represented as a string (preserves precision). |
| 31 | + * Allows up to 7 decimal places to match Stellar's stroop precision. |
| 32 | + */ |
| 33 | +export const AmountSchema = z |
| 34 | + .string({ required_error: "Amount is required" }) |
| 35 | + .trim() |
| 36 | + .regex(/^\d+(\.\d{1,7})?$/, { |
| 37 | + message: "Amount must be a positive number with up to 7 decimal places", |
| 38 | + }) |
| 39 | + .refine((v) => parseFloat(v) > 0, { |
| 40 | + message: "Amount must be greater than zero", |
| 41 | + }); |
| 42 | + |
| 43 | +/** Positive integer share count. */ |
| 44 | +export const ShareCountSchema = z |
| 45 | + .number({ required_error: "Share count is required", invalid_type_error: "Share count must be a number" }) |
| 46 | + .int("Share count must be a whole number") |
| 47 | + .positive("Share count must be greater than zero") |
| 48 | + .max(1_000_000_000, "Share count exceeds maximum allowed value"); |
| 49 | + |
| 50 | +/** Supported asset codes. Extend as new assets are on-boarded. */ |
| 51 | +export const AssetCodeSchema = z.enum(["XLM", "USDC", "yUSDC", "RWA"], { |
| 52 | + errorMap: () => ({ message: "Asset must be one of: XLM, USDC, yUSDC, RWA" }), |
| 53 | +}); |
| 54 | + |
| 55 | +/** ISO 8601 date string (YYYY-MM-DD). */ |
| 56 | +export const IsoDatestamp = z |
| 57 | + .string() |
| 58 | + .regex(/^\d{4}-\d{2}-\d{2}$/, { |
| 59 | + message: "Date must be in YYYY-MM-DD format", |
| 60 | + }); |
| 61 | + |
| 62 | +// --------------------------------------------------------------------------- |
| 63 | +// Deposit request |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | + |
| 66 | +/** |
| 67 | + * Payload sent when a user deposits assets into a vault. |
| 68 | + */ |
| 69 | +export const DepositRequestSchema = z.object({ |
| 70 | + walletAddress: StellarAddressSchema, |
| 71 | + amount: AmountSchema, |
| 72 | + asset: AssetCodeSchema, |
| 73 | + /** Optional slippage tolerance in basis points (0–500). */ |
| 74 | + slippageBps: z |
| 75 | + .number() |
| 76 | + .int("Slippage must be a whole number of basis points") |
| 77 | + .min(0, "Slippage cannot be negative") |
| 78 | + .max(500, "Slippage tolerance may not exceed 500 bps (5%)") |
| 79 | + .optional(), |
| 80 | +}); |
| 81 | + |
| 82 | +export type DepositRequest = z.infer<typeof DepositRequestSchema>; |
| 83 | + |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +// Withdrawal request |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | + |
| 88 | +/** |
| 89 | + * Payload sent when a user redeems vault shares for underlying assets. |
| 90 | + */ |
| 91 | +export const WithdrawalRequestSchema = z.object({ |
| 92 | + walletAddress: StellarAddressSchema, |
| 93 | + shares: ShareCountSchema, |
| 94 | + asset: AssetCodeSchema, |
| 95 | + /** Optional destination override; defaults to walletAddress. */ |
| 96 | + destinationAddress: StellarAddressSchema.optional(), |
| 97 | + slippageBps: z |
| 98 | + .number() |
| 99 | + .int("Slippage must be a whole number of basis points") |
| 100 | + .min(0, "Slippage cannot be negative") |
| 101 | + .max(500, "Slippage tolerance may not exceed 500 bps (5%)") |
| 102 | + .optional(), |
| 103 | +}); |
| 104 | + |
| 105 | +export type WithdrawalRequest = z.infer<typeof WithdrawalRequestSchema>; |
| 106 | + |
| 107 | +// --------------------------------------------------------------------------- |
| 108 | +// Vault history query parameters |
| 109 | +// --------------------------------------------------------------------------- |
| 110 | + |
| 111 | +/** |
| 112 | + * Query-string parameters for the vault performance history endpoint. |
| 113 | + */ |
| 114 | +export const VaultHistoryQuerySchema = z.object({ |
| 115 | + from: IsoDatestamp.optional(), |
| 116 | + to: IsoDatestamp.optional(), |
| 117 | + /** Maximum number of data points to return (1–365). */ |
| 118 | + limit: z |
| 119 | + .number() |
| 120 | + .int("Limit must be a whole number") |
| 121 | + .min(1, "Limit must be at least 1") |
| 122 | + .max(365, "Limit may not exceed 365 data points") |
| 123 | + .optional(), |
| 124 | +}).refine( |
| 125 | + (q) => { |
| 126 | + if (q.from && q.to) { |
| 127 | + return q.from <= q.to; |
| 128 | + } |
| 129 | + return true; |
| 130 | + }, |
| 131 | + { message: "\"from\" date must not be later than \"to\" date", path: ["from"] }, |
| 132 | +); |
| 133 | + |
| 134 | +export type VaultHistoryQuery = z.infer<typeof VaultHistoryQuerySchema>; |
| 135 | + |
| 136 | +// --------------------------------------------------------------------------- |
| 137 | +// Portfolio holdings query parameters |
| 138 | +// --------------------------------------------------------------------------- |
| 139 | + |
| 140 | +/** |
| 141 | + * Query-string parameters for the portfolio holdings endpoint. |
| 142 | + */ |
| 143 | +export const PortfolioQuerySchema = z.object({ |
| 144 | + walletAddress: StellarAddressSchema, |
| 145 | + status: z.enum(["active", "pending", "all"]).optional().default("all"), |
| 146 | +}); |
| 147 | + |
| 148 | +export type PortfolioQuery = z.infer<typeof PortfolioQuerySchema>; |
| 149 | + |
| 150 | +// --------------------------------------------------------------------------- |
| 151 | +// Wallet address lookup |
| 152 | +// --------------------------------------------------------------------------- |
| 153 | + |
| 154 | +/** |
| 155 | + * Single-param schema used when an endpoint only needs the caller's address. |
| 156 | + */ |
| 157 | +export const WalletAddressSchema = z.object({ |
| 158 | + walletAddress: StellarAddressSchema, |
| 159 | +}); |
| 160 | + |
| 161 | +export type WalletAddressParam = z.infer<typeof WalletAddressSchema>; |
| 162 | + |
| 163 | +// --------------------------------------------------------------------------- |
| 164 | +// Transaction list query parameters |
| 165 | +// --------------------------------------------------------------------------- |
| 166 | + |
| 167 | +/** |
| 168 | + * Query-string parameters for the transaction history endpoint. |
| 169 | + */ |
| 170 | +export const TransactionQuerySchema = z.object({ |
| 171 | + walletAddress: StellarAddressSchema, |
| 172 | + /** Maximum number of records to return (1–200). */ |
| 173 | + limit: z |
| 174 | + .number() |
| 175 | + .int("Limit must be a whole number") |
| 176 | + .min(1, "Limit must be at least 1") |
| 177 | + .max(200, "Limit may not exceed 200 records") |
| 178 | + .optional() |
| 179 | + .default(50), |
| 180 | + order: z.enum(["asc", "desc"]).optional().default("desc"), |
| 181 | + type: z.enum(["deposit", "withdrawal", "all"]).optional().default("all"), |
| 182 | +}); |
| 183 | + |
| 184 | +export type TransactionQuery = z.infer<typeof TransactionQuerySchema>; |
0 commit comments