-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdexbot_profiles.ts
More file actions
1296 lines (1136 loc) · 43.6 KB
/
Copy pathdexbot_profiles.ts
File metadata and controls
1296 lines (1136 loc) · 43.6 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { path } from '../../modules/path_api.js';
import { getStorage } from '../../modules/storage/index.js';
import { DEFAULT_CONFIG, GRID_LIMITS, INCREMENT_BOUNDS } from '../../modules/constants.js';
import { resolveRelativePrice } from '../../modules/order/utils/math.js';
import { Config } from '../../modules/config.js';
import { PATHS } from '../../modules/paths.js';
import { writeJsonFileAtomic as baseWriteJsonFileAtomic } from '../../modules/bots_file_lock.js';
import { acquireFileLock } from '../../market_adapter/utils/file_lock.js';
import { assertNoDuplicateBotKeys } from '../../modules/bot_settings.js';
import { BOT_LIVE_CONFIG_KEYS } from '../../modules/runtime_settings.js';
import { clone } from './utils.js';
import { createBotKey, sanitizeKey } from '../../modules/account_orders.js';
import { isSameBotName } from '../../modules/utils/sanitize_key.js';
const storage = getStorage();
import type { BotSettings, ProfileOptions, ClawProfileBundle } from './types.js';
import { getErrorMessage } from '../../modules/utils/errors.js';
const DEFAULT_MANIFEST_FILE = 'config.json';
const DEFAULT_BOTS_FILE = 'bots.json';
const DEFAULT_GENERAL_SETTINGS_FILE = 'general.settings.json';
const DEFAULT_MARKET_PROFILES_FILE = 'market_profiles.json';
const DEFAULT_ORDERS_DIR = 'orders';
const KNOWN_BOT_KEYS = new Set([
'active', 'activeOrders', 'assetA', 'assetAId', 'assetB', 'assetBId',
'botFunds', 'debtPolicy', 'dryRun', 'gridPrice',
'incrementPercent', 'maxPrice',
'minPrice', 'name', 'preferredAccount', 'startPrice', 'strategy',
'targetSpreadPercent', 'weightDistribution',
// Added by normalization
'botIndex', 'botKey'
]);
const BOT_SETTINGS_READ_ONLY_KEYS = new Set(['botIndex', 'botKey']);
const BOT_SETTINGS_TRIGGER_KEYS = new Set([
'active',
'activeOrders',
'assetA',
'assetAId',
'assetB',
'assetBId',
'botFunds',
'debtPolicy',
'dryRun',
'gridPrice',
'incrementPercent',
'maxPrice',
'minPrice',
'preferredAccount',
'startPrice',
'strategy',
'targetSpreadPercent',
'weightDistribution'
]);
const REQUIRED_BOT_KEY_ALIASES = {
assetA: ['assetA', 'assetAId'],
assetB: ['assetB', 'assetBId']
};
const BOT_SETTINGS_NESTED_KEYS = Object.freeze({
activeOrders: new Set(['buy', 'sell']),
botFunds: new Set(['buy', 'sell']),
weightDistribution: new Set(['buy', 'sell'])
});
const BOT_SETTINGS_STATE_FIELDS = Object.freeze({
numeric: [
'incrementPercent',
'targetSpreadPercent'
],
boolean: [
'active',
'dryRun'
],
priceLike: [
'startPrice',
'gridPrice',
'minPrice',
'maxPrice'
],
string: [
'name',
'preferredAccount',
'assetA',
'assetB',
'assetAId',
'assetBId',
'strategy'
]
});
function isPlainObject(value: any) {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
function isFiniteNumber(value: any) {
return typeof value === 'number' && Number.isFinite(value);
}
function isNumericString(value: any) {
if (typeof value !== 'string') {
return false;
}
const trimmed = value.trim();
return trimmed !== '' && Number.isFinite(Number(trimmed));
}
function isPositiveNumericString(value: any) {
if (!isNumericString(value)) {
return false;
}
return Number(value.trim()) > 0;
}
function isPositiveMultiplierString(value: any) {
if (typeof value !== 'string') {
return false;
}
const trimmed = value.trim();
if (!/^[0-9]+(?:\.[0-9]+)?x$/i.test(trimmed)) {
return false;
}
return parseFloat(trimmed) > 0;
}
function isPositivePriceLike(value: any) {
return (
(typeof value === 'number' && Number.isFinite(value) && value > 0) ||
isPositiveNumericString(value) ||
isPositiveMultiplierString(value)
);
}
function resolveComparablePriceValue(value: any, startPrice: any = null, mode: string = 'min') {
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
if (isNumericString(value)) {
return Number(value.trim());
}
if (typeof value === 'string' && Number.isFinite(startPrice)) {
const resolved = resolveRelativePrice(value, startPrice, mode);
return Number.isFinite(resolved) ? resolved : null;
}
return null;
}
function validateNestedBotSettingKeys(field: any, value: any, errors: any[]) {
const allowedKeys = (BOT_SETTINGS_NESTED_KEYS as any)[field];
if (!allowedKeys || !isPlainObject(value)) {
return;
}
const unknownKeys = Object.keys(value).filter((key) => !allowedKeys.has(key));
if (unknownKeys.length > 0) {
errors.push(`${field} contains unrecognized keys: ${unknownKeys.join(', ')}`);
}
}
function normalizeBooleanField(value: any, fallback: any) {
if (value === undefined || value === null) {
return fallback;
}
if (typeof value === 'boolean') {
return value;
}
if (typeof value === 'string') {
const lowered = value.trim().toLowerCase();
if (['1', 'true', 'yes', 'on'].includes(lowered)) return true;
if (['0', 'false', 'no', 'off'].includes(lowered)) return false;
}
return fallback;
}
function normalizeNumberField(value: any, fallback: any) {
const numeric = Number(value);
return Number.isFinite(numeric) ? numeric : fallback;
}
function isPercentageString(value: any) {
if (typeof value !== 'string') {
return false;
}
const trimmed = value.trim();
if (!trimmed.endsWith('%')) {
return false;
}
const numeric = Number(trimmed.slice(0, -1).trim());
return Number.isFinite(numeric);
}
function cloneBotSettings(value: any) {
if (value === undefined) {
return undefined;
}
return JSON.parse(JSON.stringify(value));
}
/**
* Normalize a partial bot settings object with defaults.
* Fills missing fields from DEFAULT_CONFIG and clamps values within bounds.
*/
function normalizeBotSettings(bot: Partial<BotSettings> = {}) {
const normalized = cloneBotSettings(bot) || {};
normalized.active = normalizeBooleanField(normalized.active, DEFAULT_CONFIG.active);
normalized.dryRun = normalizeBooleanField(normalized.dryRun, DEFAULT_CONFIG.dryRun);
normalized.startPrice = normalized.startPrice === undefined ? DEFAULT_CONFIG.startPrice : normalized.startPrice;
normalized.minPrice = normalized.minPrice === undefined ? DEFAULT_CONFIG.minPrice : normalized.minPrice;
normalized.maxPrice = normalized.maxPrice === undefined ? DEFAULT_CONFIG.maxPrice : normalized.maxPrice;
normalized.gridPrice = normalized.gridPrice === undefined ? DEFAULT_CONFIG.gridPrice : normalized.gridPrice;
normalized.incrementPercent = normalized.incrementPercent === undefined
? DEFAULT_CONFIG.incrementPercent
: normalizeNumberField(normalized.incrementPercent, DEFAULT_CONFIG.incrementPercent);
normalized.targetSpreadPercent = normalized.targetSpreadPercent === undefined
? DEFAULT_CONFIG.targetSpreadPercent
: normalizeNumberField(normalized.targetSpreadPercent, DEFAULT_CONFIG.targetSpreadPercent);
normalized.weightDistribution = {
...cloneBotSettings(DEFAULT_CONFIG.weightDistribution),
...(isPlainObject(normalized.weightDistribution) ? normalized.weightDistribution : {})
};
normalized.botFunds = {
...cloneBotSettings(DEFAULT_CONFIG.botFunds),
...(isPlainObject(normalized.botFunds) ? normalized.botFunds : {})
};
normalized.activeOrders = {
...cloneBotSettings(DEFAULT_CONFIG.activeOrders),
...(isPlainObject(normalized.activeOrders) ? normalized.activeOrders : {})
};
return normalized;
}
function mergeBotSettingsPatch(currentBot: Record<string, any> = {}, patch: Record<string, any> = {}) {
const next = cloneBotSettings(currentBot) || {};
const patchKeys = Object.keys(patch || {});
for (const key of patchKeys) {
const value = patch[key];
if (value === undefined) {
continue;
}
if (['weightDistribution', 'botFunds', 'activeOrders'].includes(key) && isPlainObject(value)) {
next[key] = {
...(isPlainObject(next[key]) ? next[key] : {}),
...value
};
continue;
}
next[key] = value;
}
return next;
}
/**
* Describe which bot settings are read-only vs writable, and which trigger a recalc.
* Useful for tool-catalog generation and consumer-side validation.
*/
function describeBotSettingMutability() {
const readOnly = [...BOT_SETTINGS_READ_ONLY_KEYS].sort();
const writable = [...KNOWN_BOT_KEYS].filter((key) => !BOT_SETTINGS_READ_ONLY_KEYS.has(key)).sort();
// triggerOnChange is the full trigger-eligible set; the default trigger
// fires only for triggerOnChange minus livePickupOnChange (those are
// applied by the running bot within ~1min, no resync needed).
return {
livePickupOnChange: [...(BOT_LIVE_CONFIG_KEYS as readonly string[])].sort(),
readOnly,
triggerOnChange: [...BOT_SETTINGS_TRIGGER_KEYS].sort(),
writable
};
}
/**
* Default resync-trigger predicate: a trigger-eligible key that is NOT
* applied live by the running bot. Single predicate for the validator
* (triggerRequired) and the apply path (shouldWriteTrigger).
*/
function isResyncTriggerKey(key: string): boolean {
return BOT_SETTINGS_TRIGGER_KEYS.has(key)
&& !(BOT_LIVE_CONFIG_KEYS as readonly string[]).includes(key);
}
function validateBotSettingsValue(field: any, value: any, errors: any[]) {
const push = (message: any) => errors.push(message);
switch (field) {
case 'active':
case 'dryRun':
if (typeof value !== 'boolean') {
push(`${field} must be a boolean`);
}
break;
case 'name':
case 'preferredAccount':
case 'assetA':
case 'assetB':
case 'strategy':
if (typeof value !== 'string' || value.trim() === '') {
push(`${field} must be a non-empty string`);
}
break;
case 'assetAId':
case 'assetBId':
if (typeof value !== 'string' || value.trim() === '') {
push(`${field} must be a non-empty string`);
}
break;
case 'incrementPercent': {
const increment = Number(value);
if (!Number.isFinite(increment) || increment <= 0) {
push('incrementPercent must be a positive number');
break;
}
if (Number.isFinite(INCREMENT_BOUNDS.MIN_PERCENT) && increment < INCREMENT_BOUNDS.MIN_PERCENT) {
push(`incrementPercent must be >= ${INCREMENT_BOUNDS.MIN_PERCENT}`);
}
if (Number.isFinite(INCREMENT_BOUNDS.MAX_PERCENT) && increment > INCREMENT_BOUNDS.MAX_PERCENT) {
push(`incrementPercent must be <= ${INCREMENT_BOUNDS.MAX_PERCENT}`);
}
break;
}
case 'targetSpreadPercent': {
const spread = Number(value);
if (!Number.isFinite(spread) || spread <= 0) {
push('targetSpreadPercent must be a positive number');
}
break;
}
case 'startPrice':
if (!isPositivePriceLike(value)
&& !(typeof value === 'string' && ['pool', 'book'].includes(value.toLowerCase()))) {
push('startPrice must be a positive number, "pool", or "book"');
}
break;
case 'gridPrice':
if (value !== null
&& !isPositivePriceLike(value)
&& !(typeof value === 'string' && /^(pool|book|ama(?:[1-4])?)$/i.test(value))) {
push('gridPrice must be null, a positive number, or one of pool/book/ama/ama1..ama4');
}
break;
case 'minPrice':
case 'maxPrice':
if (!isPositivePriceLike(value)) {
push(`${field} must be a positive number or a multiplier string like 3x`);
}
break;
case 'weightDistribution':
if (!isPlainObject(value)) {
push('weightDistribution must be an object with sell and buy');
break;
}
validateNestedBotSettingKeys('weightDistribution', value, errors);
if (!isFiniteNumber(value.sell)) {
push('weightDistribution.sell must be a finite number');
}
if (!isFiniteNumber(value.buy)) {
push('weightDistribution.buy must be a finite number');
}
break;
case 'botFunds':
if (!isPlainObject(value)) {
push('botFunds must be an object with sell and buy');
break;
}
validateNestedBotSettingKeys('botFunds', value, errors);
for (const side of ['sell', 'buy']) {
const sideValue = value[side];
if (sideValue === undefined) {
continue;
}
if (typeof sideValue === 'number') {
if (!Number.isFinite(sideValue) || sideValue < 0) {
push(`botFunds.${side} must be a finite number greater than or equal to 0`);
}
continue;
}
if (typeof sideValue === 'string' && isPercentageString(sideValue)) {
const numeric = Number(sideValue.trim().slice(0, -1).trim());
if (numeric < 0) {
push(`botFunds.${side} percentage must be greater than or equal to 0`);
}
continue;
}
push(`botFunds.${side} must be a finite number or a percentage string`);
}
break;
case 'activeOrders':
if (!isPlainObject(value)) {
push('activeOrders must be an object with sell and buy');
break;
}
validateNestedBotSettingKeys('activeOrders', value, errors);
for (const side of ['sell', 'buy']) {
const sideValue = value[side];
if (sideValue === undefined) {
continue;
}
if (!Number.isInteger(Number(sideValue)) || Number(sideValue) < 0) {
push(`activeOrders.${side} must be an integer greater than or equal to 0`);
}
}
break;
case 'debtPolicy':
if (value !== undefined && value !== null && typeof value !== 'object') {
push('debtPolicy must be an object');
break;
}
if (value && typeof value === 'object') {
const lending = Array.isArray(value.lending) ? value.lending : [];
for (let i = 0; i < lending.length; i++) {
const item = lending[i];
if (!item || typeof item !== 'object') {
push(`debtPolicy.lending[${i}] must be an object`);
continue;
}
if (!item.type || !['mpa', 'creditOffer'].includes(item.type)) {
push(`debtPolicy.lending[${i}].type must be "mpa" or "creditOffer"`);
}
if (!item.collateralAsset || typeof item.collateralAsset !== 'string') {
push(`debtPolicy.lending[${i}].collateralAsset must be a non-empty string`);
}
if (!item.asset || typeof item.asset !== 'string') {
push(`debtPolicy.lending[${i}].asset must be a non-empty string`);
}
}
}
break;
case 'botIndex':
case 'botKey':
push(`${field} is read-only`);
break;
default:
break;
}
}
/**
* Validate a bot settings object against known keys, nested-key schemas, and bounds.
* Returns collected errors and warnings without mutating the input.
*/
function validateBotSettingsState(bot: Record<string, any> = {}) {
const errors: string[] = [];
const warnings = [];
for (const field of Object.keys(BOT_SETTINGS_NESTED_KEYS)) {
if (bot[field] !== undefined) {
validateBotSettingsValue(field, bot[field], errors);
}
}
for (const field of [
...BOT_SETTINGS_STATE_FIELDS.boolean,
...BOT_SETTINGS_STATE_FIELDS.numeric,
...BOT_SETTINGS_STATE_FIELDS.priceLike
].filter((field) => !(BOT_SETTINGS_NESTED_KEYS as any)[field])) {
if (bot[field] !== undefined) {
validateBotSettingsValue(field, bot[field], errors);
}
}
for (const field of BOT_SETTINGS_STATE_FIELDS.string.filter((field) => !(BOT_SETTINGS_NESTED_KEYS as any)[field])) {
if (bot[field] !== undefined) {
validateBotSettingsValue(field, bot[field], errors);
}
}
const increment = Number(bot.incrementPercent);
const spread = Number(bot.targetSpreadPercent);
if (Number.isFinite(increment) && Number.isFinite(spread)) {
const minSpread = increment * GRID_LIMITS.MIN_SPREAD_FACTOR;
if (spread + Number.EPSILON < minSpread) {
errors.push(`targetSpreadPercent must be >= ${GRID_LIMITS.MIN_SPREAD_FACTOR}x incrementPercent (${minSpread.toFixed(6)})`);
}
}
const resolvedStartPrice = resolveComparablePriceValue(bot.startPrice);
const resolvedMinPrice = resolveComparablePriceValue(bot.minPrice, resolvedStartPrice, 'min');
const resolvedMaxPrice = resolveComparablePriceValue(bot.maxPrice, resolvedStartPrice, 'max');
if (resolvedMinPrice !== null && resolvedMaxPrice !== null) {
if (resolvedMinPrice >= resolvedMaxPrice) {
errors.push('minPrice must be less than maxPrice');
}
}
if (resolvedStartPrice !== null) {
if (resolvedMinPrice !== null && resolvedStartPrice < resolvedMinPrice) {
errors.push('startPrice must be greater than or equal to minPrice');
}
if (resolvedMaxPrice !== null && resolvedStartPrice > resolvedMaxPrice) {
errors.push('startPrice must be less than or equal to maxPrice');
}
}
const unknownKeys = Object.keys(bot).filter((key) => !KNOWN_BOT_KEYS.has(key));
if (unknownKeys.length > 0) {
warnings.push(`unrecognized keys: ${unknownKeys.join(', ')}`);
}
return {
errors,
warnings,
valid: errors.length === 0
};
}
/**
* Validate a partial patch against a current bot's settings.
* Returns validation result with merged settings, errors, warnings, and trigger-required flag.
*/
function validateBotSettingsPatch(patch: Record<string, any> = {}, currentBot: Record<string, any> = {}, options: Partial<ProfileOptions> = {}) {
const errors: string[] = [];
const warnings = [];
const patchKeys = Object.keys(patch || {});
const allowUnknownKeys = Boolean(options.allowUnknownKeys);
if (!isPlainObject(patch)) {
return {
errors: ['patch must be a non-null object'],
merged: cloneBotSettings(currentBot) || {},
patchKeys: [],
triggerRequired: false,
valid: false,
warnings: []
};
}
for (const key of patchKeys) {
if (!KNOWN_BOT_KEYS.has(key)) {
if (allowUnknownKeys) {
warnings.push(`unrecognized patch key: ${key}`);
} else {
errors.push(`unrecognized patch key: ${key}`);
}
continue;
}
if (['weightDistribution', 'botFunds', 'activeOrders'].includes(key) && isPlainObject(patch[key])) {
validateNestedBotSettingKeys(key, patch[key], errors);
const mergedField = {
...(isPlainObject(currentBot[key]) ? currentBot[key] : {}),
...patch[key]
};
validateBotSettingsValue(key, mergedField, errors);
continue;
}
validateBotSettingsValue(key, patch[key], errors);
}
const merged = mergeBotSettingsPatch(currentBot, patch);
const mergedValidation = validateBotSettingsState(normalizeBotSettings(merged));
errors.push(...mergedValidation.errors.filter((entry) => !errors.includes(entry)));
warnings.push(...mergedValidation.warnings);
// Live-pickup keys (BOT_LIVE_CONFIG_KEYS, shared with the bot runtime) are
// applied by the running bot within ~1min without restart or resync, so
// they must not force a full grid-resync trigger on their own. A patch
// touching only live keys writes no trigger; any non-live trigger key
// keeps the previous behavior. Explicit options.trigger still overrides.
const triggerRequired = patchKeys.some((key) => isResyncTriggerKey(key));
return {
errors,
merged,
patchKeys,
triggerRequired,
valid: errors.length === 0,
warnings
};
}
function buildBotSettingsView(bot: Record<string, any> | null, bundle: ClawProfileBundle | null, options: Record<string, any> = {}) {
const current = cloneBotSettings(bot) || null;
const effective = current ? normalizeBotSettings(current) : null;
const currentValidation = current ? validateBotSettingsState(current) : { errors: [], warnings: [], valid: true };
const effectiveValidation = effective ? validateBotSettingsState(effective) : { errors: [], warnings: [], valid: true };
const mutability = describeBotSettingMutability();
const b = bundle as any;
const selectedBotFiles = current && bundle ? {
gridPriceSnapshot: path.join(b.ordersDir || path.join(b.profilesDir, DEFAULT_ORDERS_DIR), `${current.botKey}.dynamicgrid.json`),
orderSnapshot: path.join(b.ordersDir || path.join(b.profilesDir, DEFAULT_ORDERS_DIR), `${current.botKey}.json`),
trigger: path.join(b.profilesDir, `recalculate.${current.botKey}.trigger`)
} : null;
return {
current,
defaults: cloneBotSettings(DEFAULT_CONFIG),
effective,
files: selectedBotFiles,
identifier: options.identifier || null,
mutability,
rawValidation: currentValidation,
validation: effectiveValidation
};
}
/**
* Extract raw bot entries from a bots.json settings object.
* Handles both array format ({ bots: [...] }) and single-bot object format.
*/
function resolveRawBotEntries(settings: any) {
if (!settings || typeof settings !== 'object') return [];
if (Array.isArray(settings.bots)) return settings.bots;
if (Object.keys(settings).length > 0) return [settings];
return [];
}
/**
* Validate a single bot entry for required keys and aliases.
* Emits warnings via logger for missing required fields.
*/
function validateBotEntry(entry: any, index: any, logger: any) {
const warnings = [];
for (const [label, aliases] of Object.entries(REQUIRED_BOT_KEY_ALIASES)) {
const hasAnyAlias = aliases.some((key) => {
const value = entry[key];
return value !== undefined && value !== null && value !== '';
});
if (!hasAnyAlias) {
warnings.push(`bot[${index}]: missing required key '${label}'`);
}
}
const unrecognizedKeys = [];
for (const key of Object.keys(entry)) {
if (!KNOWN_BOT_KEYS.has(key)) {
unrecognizedKeys.push(key);
}
}
if (logger && warnings.length > 0) {
const warnFn = (typeof logger?.warn === 'function' ? logger.warn : typeof logger?.log === 'function' ? logger.log : console.warn);
for (const warning of warnings) {
warnFn.call(logger, `[dexbot-profiles] ${warning}`);
}
}
if (logger && unrecognizedKeys.length > 0) {
const debugFn = (typeof logger?.debug === 'function' ? logger.debug : typeof logger?.log === 'function' ? logger.log : console.debug);
debugFn.call(logger, `[dexbot-profiles] bot[${index}]: unrecognized keys: ${unrecognizedKeys.join(', ')}`);
}
return warnings;
}
/**
* Normalize an array of raw bot entries — clones, assigns keys and indices, applies defaults.
*/
async function normalizeBotEntries(rawEntries: Record<string, any>[], options: Partial<ProfileOptions> = {}) {
const logger = options.logger || null;
const results: any[] = [];
for (const [index, entry] of rawEntries.entries()) {
if (logger) {
validateBotEntry(entry, index, logger);
}
const normalized = { ...entry, active: entry.active === undefined ? true : !!entry.active };
results.push({ ...normalized, botIndex: index, botKey: createBotKey(normalized, index) });
}
return results;
}
function isFileLike(targetPath: any) {
try {
return storage.exists(targetPath) && storage.stat(targetPath).isFile();
} catch {
return false;
}
}
function isDirectoryLike(targetPath: any) {
try {
return storage.exists(targetPath) && storage.stat(targetPath).isDirectory();
} catch {
return false;
}
}
/**
* Resolve the profiles directory from a profile root path.
* Checks root itself, root/profiles, and returns the first match.
*/
function resolveProfilesDir(profileRoot: any) {
const candidates = [];
const root = profileRoot ? path.resolve(profileRoot) : null;
if (root) {
candidates.push(root);
candidates.push(path.join(root, 'profiles'));
if (path.basename(root) === 'profiles') {
candidates.push(path.dirname(root));
}
}
if (Config.DEXBOT_PROFILE_ROOT) {
const envRoot = path.resolve(Config.DEXBOT_PROFILE_ROOT);
candidates.push(envRoot);
candidates.push(path.join(envRoot, 'profiles'));
}
// No bare CWD/profiles candidate here: an existing home config is
// authoritative (matching modules/paths.ts), and legacy cwd profiles are
// handled by the central resolver's migration logic below.
for (const candidate of candidates) {
if (isFileLike(candidate)) {
return path.dirname(candidate);
}
if (!isDirectoryLike(candidate)) {
continue;
}
const manifestFile = path.join(candidate, DEFAULT_MANIFEST_FILE);
const botsFile = path.join(candidate, DEFAULT_BOTS_FILE);
const generalSettingsFile = path.join(candidate, DEFAULT_GENERAL_SETTINGS_FILE);
const marketProfilesFile = path.join(candidate, DEFAULT_MARKET_PROFILES_FILE);
if (
storage.exists(manifestFile) ||
storage.exists(botsFile) ||
storage.exists(generalSettingsFile) ||
storage.exists(marketProfilesFile)
) {
return candidate;
}
}
if (root && isDirectoryLike(root)) {
return root;
}
// Fresh default: the central resolver (home for fresh/npm installs, with
// legacy repo/cwd migration and DEXBOT_PROFILE_ROOT override). Do not fall
// back to a bare CWD/profiles — that ignored a migrated home config.
return PATHS.PROFILES_DIR;
}
function readJsonFile(filePath: any) {
try {
return storage.readJSON(filePath);
} catch (error: any) {
if (error && error.code === 'ENOENT') {
return null;
}
throw new Error(`Failed to read ${filePath}: ${getErrorMessage(error)}`);
}
}
function writeTextPayload(filePath: any, content: any) {
storage.writeFile(filePath, `${content}\n`, 'utf8');
}
/**
* Write JSON data atomically via file lock.
* Acquires a lock on the target path, delegates to the shared atomic-write helper, then releases.
*/
async function writeJsonFileAtomic(filePath: any, data: any) {
const release = await acquireFileLock(filePath);
try {
baseWriteJsonFileAtomic(filePath, data);
} finally {
await release();
}
}
/**
* Read a recalculate-trigger file and return its parsed payload.
* Returns { exists, payload } — payload is null if empty or unparseable.
*/
function readTriggerFile(triggerPath: any) {
try {
const raw = storage.readFile(triggerPath, 'utf8');
const trimmed = raw.trim();
if (!trimmed) return { exists: true, payload: null };
try {
return { exists: true, payload: JSON.parse(trimmed) };
} catch {
return { exists: true, payload: trimmed };
}
} catch (err: any) {
if (err.code === 'ENOENT') return { exists: false, payload: null };
throw err;
}
}
function listFiles(dirPath: any) {
try {
return storage.readdir(dirPath).filter((name: string) => {
const stat = storage.stat(path.join(dirPath, name));
return stat.isFile();
}).sort();
} catch (error: any) {
if (error && error.code === 'ENOENT') {
return [];
}
throw new Error(`Failed to list ${dirPath}: ${getErrorMessage(error)}`);
}
}
/**
* Match a bot entry against an identifier (string key, numeric index, or object ref).
*/
function matchBotIdentifier(bot: any, identifier: any) {
if (!bot || identifier === null || identifier === undefined) {
return false;
}
if (typeof identifier === 'object') {
if (identifier.botKey && (bot.botKey === identifier.botKey || isSameBotName(bot.botKey, identifier.botKey))) {
return true;
}
if (identifier.name && isSameBotName(bot.name, identifier.name)) {
return true;
}
if (identifier.assetA && identifier.assetB && bot.assetA === identifier.assetA && bot.assetB === identifier.assetB) {
return true;
}
if (identifier.assetAId && identifier.assetBId) {
const botAId = bot.assetAId || null;
const botBId = bot.assetBId || null;
if (botAId === identifier.assetAId && botBId === identifier.assetBId) {
return true;
}
}
return false;
}
const value = String(identifier).trim();
if (!value) {
return false;
}
if (bot.botKey === value || isSameBotName(bot.botKey, value) || isSameBotName(bot.name, value)) {
return true;
}
if (bot.assetA && bot.assetB && `${bot.assetA}/${bot.assetB}` === value) {
return true;
}
if (bot.assetAId && bot.assetBId && `${bot.assetAId}/${bot.assetBId}` === value) {
return true;
}
// Cross-match: identifier may use IDs while bot has symbols, or vice versa
const [pairA, pairB] = value.includes('/') ? value.split('/', 2) : [null, null];
if (pairA && pairB) {
const botA = bot.assetA || bot.assetAId || null;
const botB = bot.assetB || bot.assetBId || null;
if (botA && botB && pairA === botA && pairB === botB) {
return true;
}
}
return sanitizeKey(bot.name) === sanitizeKey(value);
}
function findAmaProfile(bundle: any, bot: any) {
const profiles = Array.isArray(bundle?.amaProfiles?.profiles) ? bundle.amaProfiles.profiles : [];
if (profiles.length === 0 || !bot) {
return null;
}
const botAssetA = bot.assetAId || bot.assetA || null;
const botAssetB = bot.assetBId || bot.assetB || null;
const match = profiles.find((profile: any) => {
if (!profile || typeof profile !== 'object') {
return false;
}
const profileAssetA = profile.assetAId || profile.assetA || null;
const profileAssetB = profile.assetBId || profile.assetB || null;
return profileAssetA === botAssetA && profileAssetB === botAssetB;
});
return match ? clone(match) : null;
}
/**
* Build a profile context object for a specific bot from a loaded bundle.
* Returns the current settings view, active state, and market context.
*/
function buildClawProfileContext(bundle: Record<string, any>, options: Partial<ProfileOptions> = {}) {
if (!bundle || typeof bundle !== 'object') {
return null;
}
const botIdentifier = options.botIdentifier || options.botRef || options.botKey || options.name || null;
const selectedBot =
options.selectedBot ||
(botIdentifier ? bundle.bots.find((bot: any) => matchBotIdentifier(bot, botIdentifier)) : null) ||
bundle.activeBots[0] ||
bundle.bots[0] ||
null;
const selectedAmaProfile = findAmaProfile(bundle, selectedBot);
const selectedOrderSnapshotPath = selectedBot ? path.join(bundle.ordersDir, `${selectedBot.botKey}.json`) : null;
const selectedGridPriceSnapshotPath = selectedBot ? path.join(bundle.ordersDir, `${selectedBot.botKey}.dynamicgrid.json`) : null;
const selectedTriggerPath = selectedBot ? path.join(bundle.profilesDir, `recalculate.${selectedBot.botKey}.trigger`) : null;
const selectedOrderSnapshot = selectedBot
? clone(options.orderSnapshot !== undefined ? options.orderSnapshot : null)
: null;
const selectedGridPriceSnapshot = selectedBot
? clone(options.gridPriceSnapshot !== undefined ? options.gridPriceSnapshot : null)
: null;
return {
profileRoot: bundle.profilesDir,
runtime: {
loadedAt: new Date().toISOString(),
selectedBotRef: botIdentifier,
sourceFiles: clone(bundle.files)
},
settings: {
amaProfiles: clone(bundle.amaProfiles),
bots: clone(bundle.bots),
general: clone(bundle.generalSettings),
manifest: clone(bundle.manifest)
},
selectedBot: selectedBot ? clone(selectedBot) : null,
selectedBotFiles: selectedBot ? {
gridPriceSnapshot: selectedGridPriceSnapshotPath,
orderSnapshot: selectedOrderSnapshotPath,
trigger: selectedTriggerPath
} : null,
selectedBotState: selectedBot ? {
gridPriceSnapshot: selectedGridPriceSnapshot,
orderSnapshot: selectedOrderSnapshot,
selectedAmaProfile,
triggerExists: selectedTriggerPath ? storage.exists(selectedTriggerPath) : false,
triggerPayload: options.triggerPayload !== undefined ? options.triggerPayload : null
} : null,
summary: {
activeBotCount: bundle.activeBots.length,
amaProfileCount: Array.isArray(bundle.amaProfiles?.profiles) ? bundle.amaProfiles.profiles.length : 0,
botCount: bundle.bots.length,
hasAmaProfiles: Boolean(bundle.amaProfiles),
hasGeneralSettings: Boolean(bundle.generalSettings),
hasManifest: Boolean(bundle.manifest),
orderFileCount: bundle.orderFiles.length
}
};
}
/**
* Load the full DEXBot2 profile bundle from disk: bots.json, general.settings.json,
* market_profiles.json, and order snapshots. Returns normalized parsings of each.
*/
async function loadDexbotProfileBundle(profileRoot: string | null, options: Partial<ProfileOptions> = {}) {
const profilesDir = resolveProfilesDir(profileRoot || options.profileRoot);
const ordersDir = path.join(profilesDir, DEFAULT_ORDERS_DIR);
const manifestFile = options.manifestFile || path.join(profilesDir, DEFAULT_MANIFEST_FILE);
const botsFile = options.botsFile || path.join(profilesDir, DEFAULT_BOTS_FILE);
const generalSettingsFile = options.generalSettingsFile || path.join(profilesDir, DEFAULT_GENERAL_SETTINGS_FILE);
const marketProfilesFile = options.marketProfilesFile || path.join(profilesDir, DEFAULT_MARKET_PROFILES_FILE);
const [manifest, botsConfig, generalSettings, marketProfiles, orderFiles] = await Promise.all([
readJsonFile(manifestFile),
readJsonFile(botsFile),
readJsonFile(generalSettingsFile),
readJsonFile(marketProfilesFile),
listFiles(ordersDir)
]);
const bots = await normalizeBotEntries(resolveRawBotEntries(botsConfig), { logger: options.logger });
const activeBots = bots.filter((bot) => bot.active !== false);
const botsByKey = Object.fromEntries(bots.map((bot) => [bot.botKey, bot]));
const botsByName = Object.fromEntries(bots.filter((bot) => bot.name).map((bot) => [bot.name, bot]));