64 lines
2.7 KiB
JavaScript
64 lines
2.7 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Per-network USDT deposit inventory (ops telemetry; not a withdrawal cap).
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getAmount = getAmount;
|
|
exports.getAllForUser = getAllForUser;
|
|
exports.creditUsingClient = creditUsingClient;
|
|
exports.debitUsingClient = debitUsingClient;
|
|
exports.debit = debit;
|
|
const connection_1 = require("../db/connection");
|
|
async function getAmount(userId, network) {
|
|
const rows = await (0, connection_1.query)(`SELECT amount::text AS amount FROM user_network_inventory WHERE user_id = $1 AND network = $2`, [userId, network]);
|
|
return BigInt(rows[0]?.amount ?? '0');
|
|
}
|
|
async function getAllForUser(userId) {
|
|
const rows = await (0, connection_1.query)(`SELECT network, amount::text AS amount FROM user_network_inventory WHERE user_id = $1`, [userId]);
|
|
const out = { TRON: 0n, BSC: 0n };
|
|
for (const row of rows) {
|
|
if (row.network === 'TRON' || row.network === 'BSC') {
|
|
out[row.network] = BigInt(row.amount);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
/** Credit inventory inside an open DB transaction. */
|
|
async function creditUsingClient(client, userId, network, amount) {
|
|
if (amount <= 0n)
|
|
return;
|
|
await client.query(`INSERT INTO user_network_inventory (user_id, network, amount, updated_at)
|
|
VALUES ($1, $2, $3, NOW())
|
|
ON CONFLICT (user_id, network) DO UPDATE
|
|
SET amount = user_network_inventory.amount + EXCLUDED.amount,
|
|
updated_at = NOW()`, [userId, network, amount.toString()]);
|
|
}
|
|
/** Debit inventory inside an open DB transaction; throws if insufficient. */
|
|
async function debitUsingClient(client, userId, network, amount) {
|
|
if (amount <= 0n)
|
|
return;
|
|
const res = await client.query(`UPDATE user_network_inventory
|
|
SET amount = amount - $3, updated_at = NOW()
|
|
WHERE user_id = $1 AND network = $2 AND amount >= $3
|
|
RETURNING amount`, [userId, network, amount.toString()]);
|
|
if (res.rowCount === 0) {
|
|
const err = new Error(`Insufficient ${network} network inventory for withdrawal`);
|
|
err.code = 'INSUFFICIENT_NETWORK_INVENTORY';
|
|
throw err;
|
|
}
|
|
}
|
|
/** Debit inventory (own connection). */
|
|
async function debit(userId, network, amount) {
|
|
if (amount <= 0n)
|
|
return;
|
|
const rows = await (0, connection_1.query)(`UPDATE user_network_inventory
|
|
SET amount = amount - $3, updated_at = NOW()
|
|
WHERE user_id = $1 AND network = $2 AND amount >= $3
|
|
RETURNING amount::text AS amount`, [userId, network, amount.toString()]);
|
|
if (rows.length === 0) {
|
|
const err = new Error(`Insufficient ${network} network inventory for withdrawal`);
|
|
err.code = 'INSUFFICIENT_NETWORK_INVENTORY';
|
|
throw err;
|
|
}
|
|
}
|
|
//# sourceMappingURL=networkInventoryRepository.js.map
|