← back to Ken

kalshi-dash/arm-live-run.mjs

54 lines

// Arm a BOUNDED real-money run on Ken: $10/day for 5 calendar days, auto-off after.
// Writes: ken_config.live_run + ken_config.trade_config(enabled=true) [ken DB],
//         risk_state.config.safe_mode=false [bertha_betting].
// After running this, restart pm2 ken so loadTradeConfig() picks up enabled=true.
// Idempotent-ish: re-running resets the run window to start today.
import pg from 'pg';

const DAILY_CENTS = 1000;   // $10/day new-spend cap
const TOTAL_CENTS = 5000;   // $50 total concurrent-exposure ceiling (= account balance)
const DAYS = 5;

const kenUrl = process.env.KEN_DATABASE_URL;
const bbUrl  = process.env.DATABASE_URL;
if (!kenUrl || !bbUrl) { console.error('KEN_DATABASE_URL / DATABASE_URL not set'); process.exit(1); }

const ken = new pg.Pool({ connectionString: kenUrl });
const bb  = new pg.Pool({ connectionString: bbUrl });

const d = new Date();
const start = d.toISOString().split('T')[0];
const endD = new Date(d.getTime() + (DAYS - 1) * 86400000);
const end = endD.toISOString().split('T')[0];

// 1) live_run window
const liveRun = { active: true, daily_budget_cents: DAILY_CENTS, start_date: start, end_date: end, days: DAYS, armed_at: d.toISOString() };
await ken.query(
  `INSERT INTO ken_config (key, value, updated_at) VALUES ('live_run', $1, NOW())
   ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW()`,
  [JSON.stringify(liveRun)]
);

// 2) trade_config: enable + set budgets (merge onto whatever is saved)
const tcRow = await ken.query("SELECT value FROM ken_config WHERE key = 'trade_config'");
const tc = tcRow.rows[0]?.value || {};
const newTc = { ...tc, enabled: true, budget_cents: TOTAL_CENTS };
await ken.query(
  `INSERT INTO ken_config (key, value, updated_at) VALUES ('trade_config', $1, NOW())
   ON CONFLICT (key) DO UPDATE SET value = $1, updated_at = NOW()`,
  [JSON.stringify(newTc)]
);

// 3) flip safe_mode OFF in risk_state (bertha_betting)
const rr = await bb.query('SELECT id, config FROM risk_state ORDER BY updated_at DESC LIMIT 1');
const rid = rr.rows[0].id;
const rcfg = { ...(rr.rows[0].config || {}), safe_mode: false };
await bb.query('UPDATE risk_state SET config=$1, updated_at=NOW() WHERE id=$2', [JSON.stringify(rcfg), rid]);

console.log('LIVE RUN ARMED');
console.log('  window     :', start, '→', end, `(${DAYS} days, auto-off after ${end})`);
console.log('  daily cap  : $' + (DAILY_CENTS/100).toFixed(2) + '/day new spend');
console.log('  total cap  : $' + (TOTAL_CENTS/100).toFixed(2) + ' concurrent exposure');
console.log('  gates      : enabled=true, safe_mode=false  (RESTART ken to load enabled)');
await ken.end(); await bb.end();