|
| 1 | +import { z } from 'zod' |
| 2 | +import { getEnvVar } from './env' |
| 3 | + |
| 4 | +const StrategyConfigSchema = z.object({ |
| 5 | + overview: z.string(), |
| 6 | + riskParams: z.object({ |
| 7 | + profitTarget: z.number(), |
| 8 | + stopLoss: z.number(), |
| 9 | + maxPositions: z.number(), |
| 10 | + positionSize: z.string(), |
| 11 | + }), |
| 12 | + step1Rules: z.string(), |
| 13 | + step2Rules: z.string(), |
| 14 | + step3Rules: z.string(), |
| 15 | +}) |
| 16 | + |
| 17 | +export const DEFAULT_STRATEGY: StrategyConfig = { |
| 18 | + overview: |
| 19 | + 'Wall Street 3-Step: Data-driven day trading with clear profit/loss targets and risk management', |
| 20 | + riskParams: { |
| 21 | + profitTarget: 2, |
| 22 | + stopLoss: -1.5, |
| 23 | + maxPositions: 4, |
| 24 | + positionSize: '5-15% of USDC', |
| 25 | + }, |
| 26 | + step1Rules: |
| 27 | + "Risk targets: SELL at +2% profit OR -1.5% loss. Close losing positions faster than winners (cut losses, let profits run). Don't close positions with raw balance below 1000.", |
| 28 | + step2Rules: |
| 29 | + 'Screen for high-probability setups: Price momentum >3% with volume confirmation, Fear/Greed extremes, Order book imbalances. Use 1 analysis tool only if market data insufficient. Only trade clear directional moves.', |
| 30 | + step3Rules: |
| 31 | + 'Dynamic sizing: 5-15% per trade (scales with account). Size calculation: Min($10, Max($5, USDC_balance * 0.10)). Account for slippage: Minimum $8 positions. Max 3-4 open positions at once.', |
| 32 | +} |
| 33 | + |
| 34 | +const ed25519Pattern = /^ed25519:[1-9A-HJ-NP-Za-km-z]{43,87}$/ |
| 35 | + |
| 36 | +export const ed25519String = z |
| 37 | + .string() |
| 38 | + .regex(ed25519Pattern, 'Invalid NEAR private key: must be ed25519:<base58>') |
| 39 | + .transform((val) => val as `ed25519:${string}`) |
| 40 | + |
| 41 | +export const ServerConfigSchema = z.object({ |
| 42 | + strategy: StrategyConfigSchema.optional().default(DEFAULT_STRATEGY), |
| 43 | + cronSecret: z.string(), |
| 44 | + bitteKey: z.string(), |
| 45 | + nearPk: ed25519String, |
| 46 | +}) |
| 47 | + |
| 48 | +export type StrategyConfig = z.infer<typeof StrategyConfigSchema> |
| 49 | +export type ServerConfig = z.infer<typeof ServerConfigSchema> |
| 50 | + |
| 51 | +export function parseConfig(configString: string): ServerConfig { |
| 52 | + return ServerConfigSchema.parse(JSON.parse(configString)) |
| 53 | +} |
| 54 | + |
| 55 | +export function loadConfig(): ServerConfig { |
| 56 | + try { |
| 57 | + return parseConfig(getEnvVar('DEPLOYMENT_CONFIG')) |
| 58 | + } catch (error: unknown) { |
| 59 | + console.warn('Failed to parse DEPLOYMENT_CONFIG, trying individual env vars', String(error)) |
| 60 | + return { |
| 61 | + strategy: JSON.parse(getEnvVar('STRATEGY', JSON.stringify(DEFAULT_STRATEGY))), |
| 62 | + cronSecret: getEnvVar('CRON_SECRET'), |
| 63 | + bitteKey: getEnvVar('BITTE_API_KEY'), |
| 64 | + nearPk: getEnvVar('NEAR_PK') as `ed25519:${string}`, |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments