304 lines
12 KiB
JavaScript
304 lines
12 KiB
JavaScript
"use strict";
|
|
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 });
|
|
exports.getOverview = getOverview;
|
|
exports.listColdWallets = listColdWallets;
|
|
exports.setColdWallet = setColdWallet;
|
|
exports.listSweeps = listSweeps;
|
|
exports.runSweeps = runSweeps;
|
|
exports.requestFunding = requestFunding;
|
|
exports.confirmFunding = confirmFunding;
|
|
exports.listFunding = listFunding;
|
|
exports.cancelFunding = cancelFunding;
|
|
exports.previewHotToColdUsdt = previewHotToColdUsdt;
|
|
exports.requestHotToColdUsdt = requestHotToColdUsdt;
|
|
exports.listStuckDeposits = listStuckDeposits;
|
|
exports.scanStuckDeposits = scanStuckDeposits;
|
|
exports.previewGasTransfer = previewGasTransfer;
|
|
exports.requestGasTransfer = requestGasTransfer;
|
|
const treasuryService = __importStar(require("../services/treasuryService"));
|
|
const fundApprovalService = __importStar(require("../services/fundApprovalService"));
|
|
const sweepBatchService_1 = require("../services/sweepBatchService");
|
|
async function getOverview(_req, res) {
|
|
try {
|
|
const overview = await treasuryService.getTreasuryOverview();
|
|
res.json(overview);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to load treasury' });
|
|
}
|
|
}
|
|
async function listColdWallets(_req, res) {
|
|
try {
|
|
res.json({ items: await treasuryService.listColdWallets() });
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list cold wallets' });
|
|
}
|
|
}
|
|
async function setColdWallet(req, res) {
|
|
try {
|
|
const { walletTypeId, address, label } = req.body;
|
|
if (typeof walletTypeId !== 'number' || !address) {
|
|
res.status(400).json({ error: 'walletTypeId and address are required' });
|
|
return;
|
|
}
|
|
const row = await treasuryService.setColdWallet({ walletTypeId, address, label });
|
|
res.json(row);
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Failed to set cold wallet' });
|
|
}
|
|
}
|
|
async function listSweeps(req, res) {
|
|
try {
|
|
const status = req.query.status || undefined;
|
|
const limit = parseInt(req.query.limit || '50', 10);
|
|
const offset = parseInt(req.query.offset || '0', 10);
|
|
const result = await treasuryService.listSweeps({ status, limit, offset });
|
|
res.json(result);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list sweeps' });
|
|
}
|
|
}
|
|
async function runSweeps(_req, res) {
|
|
try {
|
|
// Close open batches → Approvals (sweep_batch) → on-chain execute after super-admin approve.
|
|
// (Legacy sweep_manual + unbatchedOnly never saw deposits, which are always batched.)
|
|
const batchesSubmitted = await (0, sweepBatchService_1.createSweepBatchApprovalRequests)();
|
|
res.status(202).json({
|
|
batchesSubmitted,
|
|
message: batchesSubmitted > 0
|
|
? `${batchesSubmitted} sweep batch(es) submitted — approve under Approvals to broadcast to cold`
|
|
: 'No open sweep batches ready. Need SWEEP_ENABLED=true, an active cold wallet, and a credited deposit that enqueued a sweep.',
|
|
});
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Sweep request failed' });
|
|
}
|
|
}
|
|
async function requestFunding(req, res) {
|
|
try {
|
|
const { walletTypeId, amount, notes } = req.body;
|
|
if (typeof walletTypeId !== 'number' || amount == null) {
|
|
res.status(400).json({ error: 'walletTypeId and amount are required' });
|
|
return;
|
|
}
|
|
const result = await fundApprovalService.createFundRequest({
|
|
operationType: 'treasury_cold_to_hot',
|
|
payload: {
|
|
walletTypeId,
|
|
amountRaw: BigInt(amount).toString(),
|
|
notes,
|
|
},
|
|
requestedBy: req.user.userId,
|
|
});
|
|
res.status(202).json({
|
|
requestId: result.id,
|
|
status: result.status,
|
|
message: 'Funding request submitted — pending super-admin approval',
|
|
});
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Funding request failed' });
|
|
}
|
|
}
|
|
async function confirmFunding(req, res) {
|
|
try {
|
|
const fundingId = req.params.fundingId;
|
|
const { blockchainTxId } = req.body;
|
|
if (!blockchainTxId) {
|
|
res.status(400).json({ error: 'blockchainTxId is required' });
|
|
return;
|
|
}
|
|
const result = await treasuryService.confirmColdToHotFunding(req.user.userId, fundingId, blockchainTxId);
|
|
res.json(result);
|
|
}
|
|
catch (e) {
|
|
const msg = e instanceof Error ? e.message : 'Confirm failed';
|
|
res.status(msg.includes('not found') ? 404 : 400).json({ error: msg });
|
|
}
|
|
}
|
|
async function listFunding(req, res) {
|
|
try {
|
|
const status = req.query.status || undefined;
|
|
const limit = parseInt(req.query.limit || '50', 10);
|
|
const offset = parseInt(req.query.offset || '0', 10);
|
|
const result = await treasuryService.listFunding({ status, limit, offset });
|
|
res.json(result);
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list funding' });
|
|
}
|
|
}
|
|
async function cancelFunding(req, res) {
|
|
try {
|
|
await treasuryService.cancelFunding(req.params.fundingId);
|
|
res.json({ message: 'Cancelled' });
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Cancel failed' });
|
|
}
|
|
}
|
|
async function previewHotToColdUsdt(req, res) {
|
|
try {
|
|
const walletTypeId = parseInt(String(req.query.walletTypeId ?? ''), 10);
|
|
if (!Number.isFinite(walletTypeId) || walletTypeId <= 0) {
|
|
res.status(400).json({ error: 'walletTypeId is required' });
|
|
return;
|
|
}
|
|
res.json(await treasuryService.previewHotToColdUsdt(walletTypeId));
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Hot→Cold preview failed' });
|
|
}
|
|
}
|
|
async function requestHotToColdUsdt(req, res) {
|
|
try {
|
|
const { walletTypeId, amount, notes } = req.body;
|
|
if (typeof walletTypeId !== 'number' || amount == null) {
|
|
res.status(400).json({ error: 'walletTypeId and amount are required' });
|
|
return;
|
|
}
|
|
const preview = await treasuryService.previewHotToColdUsdt(walletTypeId);
|
|
const amountRaw = BigInt(amount);
|
|
if (amountRaw <= 0n) {
|
|
res.status(400).json({ error: 'amount must be greater than zero' });
|
|
return;
|
|
}
|
|
if (!preview.gasOk) {
|
|
res.status(400).json({ error: preview.gasHint || 'Hot wallet has insufficient native gas' });
|
|
return;
|
|
}
|
|
if (amountRaw > BigInt(preview.maxSendable)) {
|
|
res.status(400).json({
|
|
error: `Amount exceeds max sendable after USDT reserve. Max: ${preview.maxSendable}`,
|
|
});
|
|
return;
|
|
}
|
|
const result = await fundApprovalService.createFundRequest({
|
|
operationType: 'treasury_hot_to_cold',
|
|
payload: {
|
|
walletTypeId,
|
|
amountRaw: amountRaw.toString(),
|
|
notes,
|
|
},
|
|
requestedBy: req.user.userId,
|
|
});
|
|
res.status(202).json({
|
|
requestId: result.id,
|
|
status: result.status,
|
|
message: 'Hot→Cold USDT submitted — approve under Approvals; the platform will send from hot to cold',
|
|
});
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Hot→Cold request failed' });
|
|
}
|
|
}
|
|
async function listStuckDeposits(_req, res) {
|
|
try {
|
|
res.json(await treasuryService.listStuckDeposits());
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Failed to list stuck deposits' });
|
|
}
|
|
}
|
|
async function scanStuckDeposits(_req, res) {
|
|
try {
|
|
res.json(await treasuryService.scanStuckDeposits());
|
|
}
|
|
catch (e) {
|
|
res.status(500).json({ error: e instanceof Error ? e.message : 'Stuck-deposit scan failed' });
|
|
}
|
|
}
|
|
async function previewGasTransfer(req, res) {
|
|
try {
|
|
const walletTypeId = parseInt(String(req.query.walletTypeId ?? ''), 10);
|
|
if (!Number.isFinite(walletTypeId) || walletTypeId <= 0) {
|
|
res.status(400).json({ error: 'walletTypeId is required' });
|
|
return;
|
|
}
|
|
res.json(await treasuryService.previewHotToColdGas(walletTypeId));
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Gas preview failed' });
|
|
}
|
|
}
|
|
async function requestGasTransfer(req, res) {
|
|
try {
|
|
const { walletTypeId, direction, amount, notes } = req.body;
|
|
if (typeof walletTypeId !== 'number' || !amount || !direction) {
|
|
res.status(400).json({ error: 'walletTypeId, direction and amount are required' });
|
|
return;
|
|
}
|
|
if (direction !== 'hot_to_cold' && direction !== 'cold_to_hot') {
|
|
res.status(400).json({ error: 'direction must be hot_to_cold or cold_to_hot' });
|
|
return;
|
|
}
|
|
const preview = await treasuryService.previewHotToColdGas(walletTypeId);
|
|
const amountRaw = BigInt(amount);
|
|
if (amountRaw <= 0n) {
|
|
res.status(400).json({ error: 'amount must be greater than zero' });
|
|
return;
|
|
}
|
|
if (direction === 'hot_to_cold' && amountRaw > BigInt(preview.maxSendable)) {
|
|
res.status(400).json({
|
|
error: `Amount would leave the hot wallet below the gas reserve. Max: ${preview.maxSendable}`,
|
|
});
|
|
return;
|
|
}
|
|
const result = await fundApprovalService.createFundRequest({
|
|
operationType: direction === 'hot_to_cold' ? 'treasury_hot_to_cold_gas' : 'treasury_cold_to_hot_gas',
|
|
payload: {
|
|
walletTypeId,
|
|
amountRaw: amountRaw.toString(),
|
|
notes,
|
|
},
|
|
requestedBy: req.user.userId,
|
|
});
|
|
res.status(202).json({
|
|
requestId: result.id,
|
|
status: result.status,
|
|
message: direction === 'hot_to_cold'
|
|
? 'Gas transfer submitted — approve under Approvals to send native gas from hot to cold'
|
|
: 'Gas transfer submitted — after approval, send from MetaMask (cold) to hot, then Confirm tx',
|
|
});
|
|
}
|
|
catch (e) {
|
|
res.status(400).json({ error: e instanceof Error ? e.message : 'Gas transfer request failed' });
|
|
}
|
|
}
|
|
//# sourceMappingURL=treasuryController.js.map
|