Skip to content

Commit 8144e31

Browse files
committed
fix: ensure bot metadata is initialized before grid operations
Move ensureBotEntries() call to execute right after AccountOrders creation, before any Grid initialization. This prevents new order files from being created with null values for name, assetA, assetB. Changes: - bot.js: Move ensureBotEntries() to start() method before Grid operations - dexbot.js: Add ensureBotEntries() call to start() method (was missing) - account_orders.js: Add debug logging to trace metadata initialization - constants.js: Set LOG_LEVEL to debug for troubleshooting The issue occurred because storeMasterGrid() would create a new bot entry with hardcoded null metadata if called before ensureBotEntries(). By calling ensureBotEntries() first, we ensure the metadata is properly initialized from the bot configuration in profiles/bots.json. Metadata is updated at startup and persists across restarts, which is sufficient since the bot's runtime config comes from this.config, not the order file metadata.
1 parent d954560 commit 8144e31

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ All notable changes to this project will be documented in this file.
4646
- Removed orphaned variable definitions
4747
- Cleaned up fund display logic in logFundsStatus()
4848

49+
- **Bot Metadata Initialization**: Fixed new order files being created with null metadata
50+
- Ensured `ensureBotEntries()` is called before any Grid initialization
51+
- Prevents order files from having null values for name, assetA, assetB
52+
- Metadata properly initialized from bot configuration in profiles/bots.json at startup
53+
- Applied fix to both bot.js and dexbot.js DEXBot classes
54+
4955
### Migration Guide
5056
1. **Backup** your `profiles/orders/` directory before updating
5157
2. **Run migration** (if you have existing bots with pendingProceeds):

bot.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,21 @@ class DEXBot {
607607
// Create AccountOrders with bot-specific file (one file per bot)
608608
this.accountOrders = new AccountOrders({ botKey: this.config.botKey });
609609

610+
// Ensure bot metadata is properly initialized in storage BEFORE any Grid operations
611+
// This prevents new order files from being created with null values for assetA, assetB, name
612+
const allBotsConfig = parseJsonWithComments(fs.readFileSync(PROFILES_BOTS_FILE, 'utf8')).bots || [];
613+
const allActiveBots = allBotsConfig
614+
.filter(b => b.active !== false)
615+
.map((b, idx) => normalizeBotEntry(b, idx));
616+
617+
// DEBUG: Log what we're passing to ensureBotEntries
618+
console.log(`[bot.js] DEBUG ensureBotEntries: passing ${allActiveBots.length} active bot(s):`);
619+
allActiveBots.forEach(bot => {
620+
console.log(` - name=${bot.name}, assetA=${bot.assetA}, assetB=${bot.assetB}, active=${bot.active}, index=${bot.botIndex}, botKey=${bot.botKey}`);
621+
});
622+
623+
this.accountOrders.ensureBotEntries(allActiveBots);
624+
610625
if (!this.manager) {
611626
this.manager = new OrderManager(this.config || {});
612627
this.manager.account = this.account;
@@ -776,14 +791,6 @@ class DEXBot {
776791
}
777792
});
778793

779-
// Ensure entries exist for ALL active bots (prevents pruning other bots)
780-
// Must be done BEFORE loading persisted grid to avoid overwriting saved grids
781-
const allBotsConfig = parseJsonWithComments(fs.readFileSync(PROFILES_BOTS_FILE, 'utf8')).bots || [];
782-
const allActiveBots = allBotsConfig
783-
.filter(b => b.active !== false)
784-
.map((b, idx) => normalizeBotEntry(b, idx));
785-
this.accountOrders.ensureBotEntries(allActiveBots);
786-
787794
const persistedGrid = this.accountOrders.loadBotGrid(this.config.botKey);
788795
const persistedCacheFunds = this.accountOrders.loadCacheFunds(this.config.botKey);
789796
const persistedBtsFeesOwed = this.accountOrders.loadBtsFeesOwed(this.config.botKey);

dexbot.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,24 @@ class DEXBot {
703703
*/
704704
async start(masterPassword = null) {
705705
await this.initialize(masterPassword);
706-
706+
707707
// Create AccountOrders with bot-specific file (one file per bot)
708708
this.accountOrders = new AccountOrders({ botKey: this.config.botKey });
709-
709+
710+
// Ensure bot metadata is properly initialized in storage BEFORE any Grid operations
711+
// This prevents new order files from being created with null values for assetA, assetB, name
712+
const allBotsConfig = parseJsonWithComments(fs.readFileSync(PROFILES_BOTS_FILE, 'utf8')).bots || [];
713+
// Normalize all bots first (to get correct indices), then filter to active ones
714+
const allActiveBots = normalizeBotEntries(allBotsConfig)
715+
.filter(b => b.active !== false);
716+
717+
console.log(`[dexbot.js] DEBUG ensureBotEntries: passing ${allActiveBots.length} active bot(s):`);
718+
allActiveBots.forEach(bot => {
719+
console.log(` - name=${bot.name}, assetA=${bot.assetA}, assetB=${bot.assetB}, active=${bot.active}, index=${bot.botIndex}, botKey=${bot.botKey}`);
720+
});
721+
722+
this.accountOrders.ensureBotEntries(allActiveBots);
723+
710724
if (!this.manager) {
711725
this.manager = new OrderManager(this.config || {});
712726
// Attach account identifiers so OrderManager can fetch on-chain totals when needed

modules/account_orders.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ class AccountOrders {
146146
const entriesToProcess = this.botKey
147147
? botEntries.filter(bot => {
148148
const key = bot.botKey || createBotKey(bot, botEntries.indexOf(bot));
149-
return key === this.botKey;
149+
const matches = key === this.botKey;
150+
console.log(`[AccountOrders] per-bot filter: checking bot name=${bot.name}, key=${key}, this.botKey=${this.botKey}, matches=${matches}`);
151+
return matches;
150152
})
151153
: botEntries;
152154

@@ -186,9 +188,16 @@ class AccountOrders {
186188

187189
entry.grid = entry.grid || [];
188190
if (this._metaChanged(entry.meta, meta)) {
191+
console.log(`[AccountOrders] Metadata changed for bot ${key}: updating from old metadata to new`);
192+
console.log(` OLD: name=${entry.meta?.name}, assetA=${entry.meta?.assetA}, assetB=${entry.meta?.assetB}, active=${entry.meta?.active}`);
193+
console.log(` NEW: name=${meta.name}, assetA=${meta.assetA}, assetB=${meta.assetB}, active=${meta.active}`);
189194
entry.meta = { ...entry.meta, ...meta, createdAt: entry.meta?.createdAt || meta.createdAt };
190195
entry.lastUpdated = nowIso();
191196
changed = true;
197+
} else {
198+
console.log(`[AccountOrders] No metadata change for bot ${key} - skipping update`);
199+
console.log(` CURRENT: name=${entry.meta?.name}, assetA=${entry.meta?.assetA}, assetB=${entry.meta?.assetB}, active=${entry.meta?.active}`);
200+
console.log(` PASSED: name=${meta.name}, assetA=${meta.assetA}, assetB=${meta.assetB}, active=${meta.active}`);
192201
}
193202
}
194203
bot.botKey = key;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dexbot2",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "A sophisticated market making bot for the BitShares Decentralized Exchange (DEX), implementing optimized staggered order strategies for automated trading.",
55
"main": "modules/chain_orders.js",
66
"bin": {

0 commit comments

Comments
 (0)