/** * Seed N disposable loadtest users + fund main ledger + mint JWTs. * * Run INSIDE crypto_wallet_api (has DB + JWT_SECRET): * node /tmp/loadtest-seed-users.js [outPath] * * Writes JSON: [{ i, userId, token }, ...] * email_hash prefix: loadtest_ (padded to 64 hex-like chars) */ const crypto = require('crypto'); const fs = require('fs'); const jwt = require('/app/node_modules/jsonwebtoken'); const db = require('/app/dist/db/connection'); const count = Math.max(1, Number(process.argv[2] || 100)); const outPath = process.argv[3] || '/tmp/loadtest-tokens.json'; const creditRaw = BigInt(process.env.LOADTEST_CREDIT_RAW || '100000000'); // 100 USDT @ 6dp const HASH_PREFIX = 'loadtest_'; function emailHashFor(i) { // 64-char unique hash-looking string const dig = crypto.createHash('sha256').update(`${HASH_PREFIX}${i}`).digest('hex'); return dig; } (async () => { const wtRows = await db.query( `SELECT id, decimals FROM wallet_types WHERE code = 'USDT_TRC20' LIMIT 1` ); if (!wtRows[0]) throw new Error('USDT_TRC20 wallet type missing'); const walletTypeId = wtRows[0].id; const existing = await db.query( `SELECT id, email_hash, COALESCE(token_version, 1) AS token_version FROM users WHERE email_hash LIKE $1 ORDER BY created_at ASC`, // sha256 hex never starts with loadtest_ — we store marker in first_name instead ['__never__'] ); void existing; // Prefer lookup by first_name marker for re-runs const prior = await db.query( `SELECT id, email_hash, COALESCE(token_version, 1) AS token_version, first_name FROM users WHERE first_name LIKE 'loadtest_%' ORDER BY first_name ASC` ); const byIndex = new Map(); for (const u of prior) { const m = String(u.first_name || '').match(/^loadtest_(\d+)$/); if (m) byIndex.set(Number(m[1]), u); } const tokens = []; const secret = process.env.JWT_SECRET; if (!secret) throw new Error('JWT_SECRET missing'); const batchSize = 100; for (let start = 0; start < count; start += batchSize) { const end = Math.min(count, start + batchSize); for (let i = start; i < end; i++) { let user = byIndex.get(i); if (!user) { const eh = emailHashFor(i); const rows = await db.query( `INSERT INTO users (email_hash, password_hash, role_id, first_name, email_verified, is_enabled) VALUES ($1, 'loadtest-hash', 1, $2, TRUE, TRUE) ON CONFLICT (email_hash) DO UPDATE SET first_name = EXCLUDED.first_name RETURNING id, email_hash, COALESCE(token_version, 1) AS token_version, first_name`, [eh, `loadtest_${i}`] ); user = rows[0]; // ensure user_roles await db.query( `INSERT INTO user_roles (user_id, role_id) VALUES ($1, 1) ON CONFLICT DO NOTHING`, [user.id] ); } // Ensure ledger account + top-up available balance to at least creditRaw const accRows = await db.query( `INSERT INTO ledger_accounts (user_id, wallet_type_id, account_kind, available_balance, locked_balance) VALUES ($1, $2, 'main', $3, 0) ON CONFLICT (user_id, wallet_type_id, account_kind) DO UPDATE SET available_balance = GREATEST(ledger_accounts.available_balance, EXCLUDED.available_balance), updated_at = NOW() RETURNING id, available_balance::text`, [user.id, walletTypeId, creditRaw.toString()] ); // If ON CONFLICT didn't raise balance (older row higher already), ensure floor via UPDATE if (accRows[0] && BigInt(accRows[0].available_balance) < creditRaw) { await db.query( `UPDATE ledger_accounts SET available_balance = $1, updated_at = NOW() WHERE id = $2`, [creditRaw.toString(), accRows[0].id] ); } const token = jwt.sign( { userId: user.id, emailHash: user.email_hash, role: 'user', roles: ['user'], permissions: [], primaryRole: 'user', tv: Number(user.token_version) || 1, }, secret, { expiresIn: '6h' } ); tokens.push({ i, userId: user.id, token }); } process.stdout.write(`seeded ${end}/${count}\n`); } fs.writeFileSync(outPath, JSON.stringify({ count: tokens.length, walletTypeId, creditRaw: creditRaw.toString(), tokens })); console.log(JSON.stringify({ ok: true, count: tokens.length, outPath })); process.exit(0); })().catch((e) => { console.error(e); process.exit(1); });