games_app/backend/dist-test-faq/scripts/enqueueSweepForDepositTx.js

129 lines
5.4 KiB
JavaScript

"use strict";
/**
* Enqueue a cold-wallet sweep for an already-credited deposit (e.g. credited
* before SWEEP_ENABLED / cold wallet were configured).
*
* VPS:
* sudo docker compose run --rm --no-deps --build backend \
* node dist/scripts/enqueueSweepForDepositTx.js \
* --tx=0x...
*
* Requires SWEEP_ENABLED=true and an active cold wallet for the deposit's custody rail.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
require("./loadEnv");
const transactionRepository = __importStar(require("../repositories/transactionRepository"));
const walletRepository = __importStar(require("../repositories/walletRepository"));
const coldWalletRepository = __importStar(require("../repositories/coldWalletRepository"));
const sweepService_1 = require("../services/sweepService");
const config_1 = require("../config");
const depositSweepRepository = __importStar(require("../repositories/depositSweepRepository"));
async function main() {
const txArg = process.argv.find((a) => a.startsWith('--tx='))?.slice('--tx='.length).trim();
if (!txArg) {
console.error('Usage: node dist/scripts/enqueueSweepForDepositTx.js --tx=<blockchain_tx_hash>');
process.exit(1);
}
if (!config_1.config.treasury.sweepEnabled) {
console.error('SWEEP_ENABLED is not true in this environment — set it and recreate the backend container.');
process.exit(2);
}
const deposit = await transactionRepository.findByBlockchainTxId(txArg);
if (!deposit) {
console.error(`No transaction found for blockchain_tx_id=${txArg}`);
process.exit(3);
}
if (deposit.type !== 'deposit') {
console.error(`Transaction ${deposit.id} is type=${deposit.type}, expected deposit`);
process.exit(3);
}
const meta = (deposit.metadata ?? {});
const sourceCode = typeof meta.source_wallet_type === 'string' ? meta.source_wallet_type : null;
const chainAmountRaw = typeof meta.chain_amount === 'string'
? meta.chain_amount
: typeof meta.chainAmount === 'string'
? meta.chainAmount
: null;
if (!sourceCode || !chainAmountRaw) {
console.error('Deposit metadata missing source_wallet_type / chain_amount — cannot determine custody rail amount');
process.exit(4);
}
const custodyType = await walletRepository.getWalletTypeByCode(sourceCode);
if (!custodyType) {
console.error(`Unknown custody wallet type code: ${sourceCode}`);
process.exit(4);
}
const cold = await coldWalletRepository.getActiveColdWallet(custodyType.id);
if (!cold) {
console.error(`No active cold wallet for ${sourceCode} (wallet_type_id=${custodyType.id}). Set it in Admin → Treasury first.`);
process.exit(5);
}
const existing = await depositSweepRepository.findByDepositTransactionId(deposit.id);
if (existing) {
console.log(JSON.stringify({
enqueued: false,
alreadyExists: true,
sweepId: existing.id,
status: existing.status,
batchId: existing.batch_id,
note: 'Use Treasury → Submit sweeps for approval (if still pending), then Approvals.',
}, null, 2));
return;
}
const chainAmount = BigInt(chainAmountRaw);
await (0, sweepService_1.enqueueSweepAfterDeposit)(deposit.user_id, custodyType.id, chainAmount, deposit.id);
const created = await depositSweepRepository.findByDepositTransactionId(deposit.id);
console.log(JSON.stringify({
enqueued: !!created,
sweepId: created?.id ?? null,
status: created?.status ?? null,
batchId: created?.batch_id ?? null,
depositId: deposit.id,
userId: deposit.user_id,
custodyWalletType: sourceCode,
toCold: cold.address,
chainAmount: chainAmount.toString(),
blockchainTxId: txArg,
note: 'Next: Treasury → Submit sweeps for approval → Approvals → approve sweep_batch.',
}, null, 2));
if (!created)
process.exit(6);
}
main().catch((e) => {
console.error(e);
process.exit(1);
});
//# sourceMappingURL=enqueueSweepForDepositTx.js.map