forked from yunus-0x/meridian
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconfig.js
More file actions
433 lines (395 loc) · 19.9 KB
/
Copy pathconfig.js
File metadata and controls
433 lines (395 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { getEffectiveMinSolToOpen } from "./runtime-helpers.js";
import { getDefaultModelForProvider, getLlmProvider } from "./llm-provider.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const USER_CONFIG_PATH = path.join(__dirname, "user-config.json");
const GMGN_CONFIG_PATH = path.join(__dirname, "gmgn-config.json");
function readJsonIfExists(filePath) {
return fs.existsSync(filePath)
? JSON.parse(fs.readFileSync(filePath, "utf8"))
: {};
}
const u = readJsonIfExists(USER_CONFIG_PATH);
const gmgnUserConfig = readJsonIfExists(GMGN_CONFIG_PATH);
// Apply wallet/RPC from user-config if not already in env
if (u.rpcUrl) process.env.RPC_URL ||= u.rpcUrl;
if (u.walletKey) process.env.WALLET_PRIVATE_KEY ||= u.walletKey;
if (u.llmProvider) process.env.LLM_PROVIDER ||= u.llmProvider;
if (u.llmModel) process.env.LLM_MODEL ||= u.llmModel;
if (u.dryRun !== undefined) process.env.DRY_RUN ||= String(u.dryRun);
if (u.usdcMode !== undefined) process.env.USDC_MODE ||= String(u.usdcMode);
if (gmgnUserConfig.apiKey || u.gmgnApiKey) {
process.env.GMGN_API_KEY ||= gmgnUserConfig.apiKey || u.gmgnApiKey;
}
function nonEmptyString(...values) {
for (const value of values) {
if (typeof value !== "string") continue;
const trimmed = value.trim();
if (trimmed) return trimmed;
}
return null;
}
function gmgnValue(key, legacyKey, fallback) {
return gmgnUserConfig[key] ?? u[legacyKey] ?? fallback;
}
function gmgnArray(key, legacyKey, fallback) {
if (Array.isArray(gmgnUserConfig[key])) return gmgnUserConfig[key];
if (Array.isArray(u[legacyKey])) return u[legacyKey];
return fallback;
}
const DEFAULT_MODEL = getDefaultModelForProvider(getLlmProvider());
export const config = {
// ─── Risk Limits ─────────────────────────
risk: {
maxPositions: u.maxPositions ?? 3,
maxDeployAmount: u.maxDeployAmount ?? 50,
},
// ─── Pool Screening Thresholds ───────────
screening: {
source: u.screeningSource ?? "meteora", // "meteora" | "gmgn"
minFeeActiveTvlRatio: u.minFeeActiveTvlRatio ?? 0.05,
minTvl: u.minTvl ?? 10_000,
maxTvl: u.maxTvl ?? 150_000,
minVolume: u.minVolume ?? 500,
minOrganic: u.minOrganic ?? 60,
minHolders: u.minHolders ?? 500,
minMcap: u.minMcap ?? 150_000,
maxMcap: u.maxMcap ?? 10_000_000,
minBinStep: u.minBinStep ?? 80,
maxBinStep: u.maxBinStep ?? 125,
maxVolatility: u.maxVolatility ?? 8,
maxPriceChangePct: u.maxPriceChangePct ?? 300,
timeframe: u.timeframe ?? "5m",
category: u.category ?? "trending",
minTokenFeesSol: u.minTokenFeesSol ?? 30, // global fees paid (priority+jito tips). below = bundled/scam
athTopThresholdPct: u.athTopThresholdPct ?? 90,
minTokenAgeHours: u.minTokenAgeHours ?? null, // null = no minimum (token age from GMGN creation_timestamp)
maxTokenAgeHours: u.maxTokenAgeHours ?? null, // null = no maximum
},
// ─── GMGN Screening (opt-in via screening.source = "gmgn") ───────────
// Sourced from gmgn-config.json (preferred), then legacy user-config.json
// keys (gmgn*), then defaults. See gmgn-config.example.json for the schema.
gmgn: {
apiKey: nonEmptyString(gmgnUserConfig.apiKey, u.gmgnApiKey, process.env.GMGN_API_KEY),
// Stage-1 (trending rank) controls
interval: gmgnValue("interval", "gmgnInterval", "5m"),
orderBy: gmgnValue("orderBy", "gmgnOrderBy", "volume"),
direction: gmgnValue("direction", "gmgnDirection", "desc"),
limit: gmgnValue("limit", "gmgnLimit", 100),
enrichLimit: gmgnValue("enrichLimit", "gmgnEnrichLimit", 20),
filters: gmgnArray("filters", "gmgnFilters", ["renounced", "frozen", "not_wash_trading"]),
platforms: gmgnArray("platforms", "gmgnPlatforms", ["Pump.fun", "meteora_virtual_curve", "pool_meteora"]),
// Stage-1 basic filters (trending row)
minMcap: gmgnValue("minMcap", "gmgnMinMcap", 150_000),
maxMcap: gmgnValue("maxMcap", "gmgnMaxMcap", 10_000_000),
maxBundlerRate: gmgnValue("maxBundlerRate", "gmgnMaxBundlerRate", 0.5),
minTokenAgeHours: gmgnValue("minTokenAgeHours", "gmgnMinTokenAgeHours", 2),
maxTokenAgeHours: gmgnValue("maxTokenAgeHours", "gmgnMaxTokenAgeHours", 24 * 7),
minVolume: gmgnValue("minVolume", "gmgnMinVolume", 1000),
minTvl: gmgnValue("minTvl", "gmgnMinTvl", u.minTvl ?? 10_000),
// Stage-2 token-info filters
minHolders: gmgnValue("minHolders", "gmgnMinHolders", 1000),
minTotalFeeSol: gmgnValue("minTotalFeeSol", "gmgnMinTotalFeeSol", 30),
maxTop10HolderRate: gmgnValue("maxTop10HolderRate", "gmgnMaxTop10HolderRate", 0.5),
maxDevTeamHoldRate: gmgnValue("maxDevTeamHoldRate", "gmgnMaxDevTeamHoldRate", 0.02),
maxBotDegenRate: gmgnValue("maxBotDegenRate", "gmgnMaxBotDegenRate", 0.4),
maxFreshWalletRate: gmgnValue("maxFreshWalletRate", "gmgnMaxFreshWalletRate", 0.2),
maxRatTraderRate: gmgnValue("maxRatTraderRate", "gmgnMaxRatTraderRate", 0.2),
athFilterPct: gmgnValue("athFilterPct", "gmgnAthFilterPct", null),
// Optional Stage-1/2 gates (read from trending row where available)
minSmartDegenCount: gmgnValue("minSmartDegenCount", "gmgnMinSmartDegenCount", 1),
requireKol: gmgnValue("requireKol", "gmgnRequireKol", true),
minKolCount: gmgnValue("minKolCount", "gmgnMinKolCount", 1),
maxRugRatio: gmgnValue("maxRugRatio", "gmgnMaxRugRatio", 0.3),
maxSniperCount: gmgnValue("maxSniperCount", "gmgnMaxSniperCount", 20),
// Stage-3 holders/traders enrichment + KOL config
holdersLimit: gmgnValue("holdersLimit", "gmgnHoldersLimit", 100),
maxSniperHoldRate: gmgnValue("maxSniperHoldRate", "gmgnMaxSniperHoldRate", 0.3),
preferredKolNames: gmgnArray("preferredKolNames", "gmgnPreferredKolNames", []),
preferredKolMinHoldPct: gmgnValue("preferredKolMinHoldPct", "gmgnPreferredKolMinHoldPct", 1),
dumpKolNames: gmgnArray("dumpKolNames", "gmgnDumpKolNames", []),
dumpKolMinHoldPct: gmgnValue("dumpKolMinHoldPct", "gmgnDumpKolMinHoldPct", 0.5),
// Stage-4 indicator filter (reuses local fetchGmgnPriceInfo().candles)
indicatorFilter: gmgnValue("indicatorFilter", "gmgnIndicatorFilter", true),
indicatorRules: (() => {
const r = gmgnUserConfig.indicatorRules || {};
return {
requireBullishSupertrend: r.requireBullishSupertrend ?? true,
rejectAlreadyAtBottom: r.rejectAlreadyAtBottom ?? true,
requireAboveSupertrend: r.requireAboveSupertrend ?? false,
minRsi: r.minRsi ?? null,
maxRsi: r.maxRsi ?? null,
requireBbPosition: r.requireBbPosition ?? null,
};
})(),
},
// ─── Position Management ────────────────
management: {
minClaimAmount: u.minClaimAmount ?? 5,
outOfRangeBinsToClose: u.outOfRangeBinsToClose ?? 5,
outOfRangeWaitMinutes: u.outOfRangeWaitMinutes ?? 30,
minVolumeToRebalance: u.minVolumeToRebalance ?? 1000,
emergencyPriceDropPct: u.emergencyPriceDropPct ?? -50,
stopLossPct: u.stopLossPct ?? -20,
takeProfitFeePct: u.takeProfitFeePct ?? 5,
trailingTakeProfit: u.trailingTakeProfit ?? true,
trailingTriggerPct: u.trailingTriggerPct ?? 3,
trailingDropPct: u.trailingDropPct ?? 1.5,
minSolToOpen: getEffectiveMinSolToOpen({
minSolToOpen: u.minSolToOpen ?? 0.55,
deployAmountSol: u.deployAmountSol ?? 0.5,
gasReserve: u.gasReserve ?? 0.2,
}),
deployAmountSol: u.deployAmountSol ?? 0.5,
gasReserve: u.gasReserve ?? 0.2, // always keep this much SOL for gas
positionSizePct: u.positionSizePct ?? 0.35, // % of deployable capital per position
pnlUnit: u.pnlUnit ?? "sol", // "sol" or "usd" — how PnL is displayed
priorityFeeLevel: u.priorityFeeLevel ?? "Medium",
},
// ─── Strategy Mapping ───────────────────
strategy: {
activeStrategy: u.activeStrategy ?? "evil_panda",
strategy: u.strategy ?? "spot",
binsBelow: u.binsBelow ?? 69, // activeBin - 69 to activeBin = 70 bins total (program max)
evilPanda: {
minTokenVolume24h: u.evilPandaMinTokenVolume24h ?? 750_000,
minMcap: u.evilPandaMinMcap ?? 200_000,
priceRangePct: u.evilPandaPriceRangePct ?? 80,
},
},
// ─── USDC Mode ──────────────────────────
// When enabled, the agent holds capital in USDC. On entry it swaps the
// configured USD amount into SOL and LPs single-sided (bid_ask); on exit
// it settles all recovered tokens + surplus SOL back to USDC, keeping only
// a small native-SOL buffer for gas. Pools are still SOL-quoted; USDC is
// just the home/accounting currency.
usdc: {
enabled: u.usdcMode ?? (process.env.USDC_MODE === "true"),
deployAmountUsd: u.deployAmountUsd ?? 50, // USD deployed per position
maxDeployUsd: u.maxDeployUsd ?? 500, // hard per-position USD cap
minUsdcToOpen: u.minUsdcToOpen ?? (u.deployAmountUsd ?? 50), // min USDC to start screening
gasReserveSol: u.gasReserveSol ?? 0.2, // native SOL kept for gas (hard floor — enforced in swapToken + settle; matches upstream)
},
// ─── Scheduling ─────────────────────────
schedule: {
managementIntervalMin: u.managementIntervalMin ?? 10,
screeningIntervalMin: u.screeningIntervalMin ?? 30,
healthCheckIntervalMin: u.healthCheckIntervalMin ?? 60,
pnlWatcherIntervalSec: u.pnlWatcherIntervalSec ?? 30,
},
// ─── LLM Settings ──────────────────────
llm: {
temperature: u.temperature ?? 0.373,
maxTokens: u.maxTokens ?? 4096,
maxSteps: u.maxSteps ?? 20,
managementModel: u.managementModel ?? process.env.LLM_MODEL ?? DEFAULT_MODEL,
screeningModel: u.screeningModel ?? process.env.LLM_MODEL ?? DEFAULT_MODEL,
generalModel: u.generalModel ?? process.env.LLM_MODEL ?? DEFAULT_MODEL,
managementFallbackModel: u.managementFallbackModel ?? null,
screeningFallbackModel: u.screeningFallbackModel ?? null,
generalFallbackModel: u.generalFallbackModel ?? null,
},
memory: {
nuggetsFirst: u.memoryNuggetsFirst ?? true,
},
// ─── Web UI ───────────────────────────
web: {
port: parseInt(u.webPort || process.env.WEB_PORT || "3737", 10),
},
// ─── Darwinian Signal Weighting ─────────
darwin: {
enabled: u.darwinianWeights ?? false,
windowDays: u.darwinianWindowDays ?? 60,
boostFactor: u.darwinianBoostFactor ?? 1.05,
decayFactor: u.darwinianDecayFactor ?? 0.95,
weightFloor: u.darwinianWeightFloor ?? 0.3,
weightCeiling: u.darwinianWeightCeiling ?? 2.5,
minSamples: u.darwinianMinSamples ?? 10,
perSignalMinSamples: u.darwinianPerSignalMinSamples ?? 12,
minAbsLiftToAdjust: u.darwinianMinAbsLiftToAdjust ?? 0.05,
strongLiftThreshold: u.darwinianStrongLiftThreshold ?? 0.2,
calibrationMinSamples: u.darwinianCalibrationMinSamples ?? 20,
meanReversionRate: u.darwinianMeanReversionRate ?? 0.02,
},
// ─── Autoresearch (ATLAS-inspired prompt optimization) ─────
autoresearch: {
enabled: u.autoresearch ?? false,
minClosesPerTrial: u.autoresearchMinCloses ?? 7,
minEvidenceCloses: u.autoresearchMinEvidenceCloses ?? Math.max(10, (u.autoresearchMinCloses ?? 7) + 2),
minAttributedLosses: u.autoresearchMinAttributedLosses ?? 3,
minAbsoluteWinRateDeltaPct: u.autoresearchMinAbsoluteWinRateDeltaPct ?? 10,
minAbsolutePnlDeltaPct: u.autoresearchMinAbsolutePnlDeltaPct ?? 0.5,
improvementPct: u.autoresearchImprovementPct ?? 15,
declinePct: u.autoresearchDeclinePct ?? 15,
cooldownCloses: u.autoresearchCooldownCloses ?? 5,
llmModel: u.autoresearchModel ?? DEFAULT_MODEL,
reasoningEffort: u.autoresearchReasoningEffort ?? "medium",
},
// ─── Knowledge Base ─────────────��──────
knowledgeBase: {
enabled: u.knowledgeBase?.enabled ?? true,
dir: u.knowledgeBase?.dir ?? "./knowledge",
autoFile: u.knowledgeBase?.autoFile ?? true,
healthCheckIntervalHours: u.knowledgeBase?.healthCheckIntervalHours ?? 12,
maxArticles: u.knowledgeBase?.maxArticles ?? 500,
},
// ─── Common Token Mints ────────────────
tokens: {
SOL: "So11111111111111111111111111111111111111112",
USDC: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
USDT: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
},
};
/**
* Compute the optimal deploy amount for a given wallet balance.
* Scales position size with wallet growth (compounding).
*/
export function computeDeployAmount(walletSol) {
const reserve = config.management.gasReserve ?? 0.2;
const pct = config.management.positionSizePct ?? 0.35;
const floor = config.management.deployAmountSol;
const ceil = config.risk.maxDeployAmount;
const deployable = Math.max(0, walletSol - reserve);
const dynamic = deployable * pct;
const result = Math.min(ceil, Math.max(floor, dynamic));
return parseFloat(result.toFixed(2));
}
// Keys that map into each config section
const SECTION_MAP = {
screening: new Set(Object.keys(config.screening)),
gmgn: new Set(Object.keys(config.gmgn)),
management: new Set(Object.keys(config.management)),
risk: new Set(Object.keys(config.risk)),
schedule: new Set(Object.keys(config.schedule)),
strategy: new Set(Object.keys(config.strategy)),
llm: new Set(Object.keys(config.llm)),
memory: new Set(Object.keys(config.memory)),
knowledgeBase: new Set(Object.keys(config.knowledgeBase)),
research: config.research ? new Set(Object.keys(config.research)) : new Set(),
};
// Keys that no caller may change
const LOCKED_KEYS = new Set(["walletKey", "rpcUrl", "llmModel"]);
// Keys that atlas_autotune may NOT change (cadence / owner-level)
const ATLAS_DISALLOWED = new Set([
"managementIntervalMin",
"healthCheckIntervalMin",
"pnlWatcherIntervalSec",
]);
// Keys whose values should be rounded to the nearest integer
const INTEGER_KEYS = new Set([
"minTvl", "maxTvl", "minVolume", "minOrganic", "minHolders",
"minMcap", "maxMcap", "minBinStep", "maxBinStep", "maxVolatility",
"maxPriceChangePct", "minTokenFeesSol", "athTopThresholdPct",
"maxTop10Pct", "maxBundlersPct",
"outOfRangeBinsToClose", "outOfRangeWaitMinutes",
"emergencyPriceDropPct", "stopLossPct", "takeProfitFeePct",
"maxPositions", "maxDeployAmount",
"managementIntervalMin", "screeningIntervalMin",
"healthCheckIntervalMin", "pnlWatcherIntervalSec",
"maxTokens", "maxSteps",
]);
function findSection(key) {
for (const [name, keys] of Object.entries(SECTION_MAP)) {
if (keys.has(key)) return name;
}
return null;
}
/**
* Apply a set of config changes to the in-memory config and persist them
* to user-config.json. Returns { success, applied, normalized, rejected }.
*/
export function applyConfigChanges({ changes = {}, source = "manual", reason = "" } = {}) {
const applied = {};
const normalized = {};
const rejected = { locked: {}, atlas_disallowed: {}, unknown: {} };
for (const [key, value] of Object.entries(changes)) {
// Block locked keys
if (LOCKED_KEYS.has(key)) {
rejected.locked[key] = value;
continue;
}
// Block atlas-disallowed keys when source is atlas_autotune
if (source === "atlas_autotune" && ATLAS_DISALLOWED.has(key)) {
rejected.atlas_disallowed[key] = value;
continue;
}
const section = findSection(key);
if (!section) {
rejected.unknown[key] = value;
continue;
}
// Normalize
let final = value;
if (INTEGER_KEYS.has(key) && typeof value === "number") {
final = Math.round(value);
}
config[section][key] = final;
applied[key] = final;
if (final !== value) normalized[key] = final;
}
// Persist applied changes to user-config.json
if (Object.keys(applied).length > 0) {
try {
const existing = fs.existsSync(USER_CONFIG_PATH)
? JSON.parse(fs.readFileSync(USER_CONFIG_PATH, "utf8"))
: {};
Object.assign(existing, applied);
fs.writeFileSync(USER_CONFIG_PATH, JSON.stringify(existing, null, 2));
} catch { /* best effort */ }
}
// Clean up empty rejection buckets
for (const bucket of Object.keys(rejected)) {
if (Object.keys(rejected[bucket]).length === 0) delete rejected[bucket];
}
return {
success: Object.keys(applied).length > 0,
applied,
normalized,
rejected,
};
}
/**
* Reload user-config.json and apply updated screening thresholds to the
* in-memory config object. Called after threshold evolution so the next
* agent cycle uses the evolved values without a restart.
*/
export function reloadScreeningThresholds() {
try {
const fresh = readJsonIfExists(USER_CONFIG_PATH);
const s = config.screening;
if (fresh.screeningSource != null) s.source = fresh.screeningSource;
if (fresh.minFeeActiveTvlRatio != null) s.minFeeActiveTvlRatio = fresh.minFeeActiveTvlRatio;
if (fresh.minOrganic != null) s.minOrganic = fresh.minOrganic;
if (fresh.minHolders != null) s.minHolders = fresh.minHolders;
if (fresh.minMcap != null) s.minMcap = fresh.minMcap;
if (fresh.maxMcap != null) s.maxMcap = fresh.maxMcap;
if (fresh.minTvl != null) s.minTvl = fresh.minTvl;
if (fresh.maxTvl != null) s.maxTvl = fresh.maxTvl;
if (fresh.minVolume != null) s.minVolume = fresh.minVolume;
if (fresh.minBinStep != null) s.minBinStep = fresh.minBinStep;
if (fresh.maxBinStep != null) s.maxBinStep = fresh.maxBinStep;
if (fresh.maxVolatility != null) s.maxVolatility = fresh.maxVolatility;
if (fresh.maxPriceChangePct != null) s.maxPriceChangePct = fresh.maxPriceChangePct;
if (fresh.timeframe != null) s.timeframe = fresh.timeframe;
if (fresh.category != null) s.category = fresh.category;
if (fresh.athTopThresholdPct != null) s.athTopThresholdPct = fresh.athTopThresholdPct;
if (fresh.minTokenAgeHours !== undefined) s.minTokenAgeHours = fresh.minTokenAgeHours;
if (fresh.maxTokenAgeHours !== undefined) s.maxTokenAgeHours = fresh.maxTokenAgeHours;
// Also reload management thresholds that evolution may have changed
const m = config.management;
if (fresh.stopLossPct != null) m.stopLossPct = fresh.stopLossPct;
if (fresh.takeProfitFeePct != null) m.takeProfitFeePct = fresh.takeProfitFeePct;
if (fresh.outOfRangeWaitMinutes != null) m.outOfRangeWaitMinutes = fresh.outOfRangeWaitMinutes;
} catch { /* ignore */ }
// Refresh GMGN screening keys from gmgn-config.json (mirrors the gmgn block above)
try {
const freshGmgn = readJsonIfExists(GMGN_CONFIG_PATH);
const g = config.gmgn;
for (const [key, value] of Object.entries(freshGmgn)) {
if (key in g && key !== "apiKey") g[key] = value;
}
if (freshGmgn.apiKey) g.apiKey = freshGmgn.apiKey;
} catch { /* ignore */ }
}