games_app/backend/dist/repositories/walletRepository.js
2026-06-29 15:01:23 +02:00

84 lines
3.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWalletTypes = getWalletTypes;
exports.getWalletTypeByCode = getWalletTypeByCode;
exports.getUserWallet = getUserWallet;
exports.createUserWallet = createUserWallet;
exports.getNextDerivationIndex = getNextDerivationIndex;
exports.getUserWalletsByUserId = getUserWalletsByUserId;
exports.findUserByDepositAddress = findUserByDepositAddress;
const connection_1 = require("../db/connection");
/**
* Repository for wallet types and per-user deposit addresses.
* Manages wallet type catalog, user wallet assignment, and address lookup.
*/
/**
* Returns all configured wallet types ordered by ID.
* @returns Array of wallet type records.
*/
async function getWalletTypes() {
return (0, connection_1.query)('SELECT id, code, name, network, decimals, contract_address FROM wallet_types ORDER BY id');
}
/**
* Finds a wallet type by its unique code.
* @param code - Wallet type code (e.g. USDT_TRC20).
* @returns The matching wallet type, or null if not found.
*/
async function getWalletTypeByCode(code) {
const rows = await (0, connection_1.query)('SELECT id, code, name, network, decimals, contract_address FROM wallet_types WHERE code = $1', [code]);
return rows[0] || null;
}
/**
* Retrieves a user's wallet for a specific wallet type.
* @param userId - ID of the user.
* @param walletTypeId - Wallet type to look up.
* @returns The user wallet record, or null if none exists.
*/
async function getUserWallet(userId, walletTypeId) {
const rows = await (0, connection_1.query)('SELECT id, user_id, wallet_type_id, address, derivation_index FROM user_wallets WHERE user_id = $1 AND wallet_type_id = $2', [userId, walletTypeId]);
return rows[0] || null;
}
/**
* Creates or updates a user wallet (upsert on user_id + wallet_type_id).
* @param data - User wallet fields to persist.
* @param data.userId - ID of the owning user.
* @param data.walletTypeId - Wallet type for this address.
* @param data.address - On-chain deposit address.
* @param data.derivationIndex - HD derivation index used to generate the address.
* @returns The created or updated user wallet record.
*/
async function createUserWallet(data) {
const rows = await (0, connection_1.query)(`INSERT INTO user_wallets (user_id, wallet_type_id, address, derivation_index)
VALUES ($1, $2, $3, $4)
ON CONFLICT (user_id, wallet_type_id) DO UPDATE SET address = $3, derivation_index = $4
RETURNING id, user_id, wallet_type_id, address, derivation_index`, [data.userId, data.walletTypeId, data.address, data.derivationIndex]);
return rows[0];
}
/**
* Returns the next available HD derivation index for a wallet type.
* @param walletTypeId - Wallet type to allocate an index for.
* @returns Next derivation index (max existing index + 1, or 0 if none exist).
*/
async function getNextDerivationIndex(walletTypeId) {
const rows = await (0, connection_1.query)('SELECT MAX(derivation_index) as max FROM user_wallets WHERE wallet_type_id = $1', [walletTypeId]);
const max = rows[0]?.max ?? -1;
return max + 1;
}
/**
* Lists all wallets belonging to a user.
* @param userId - ID of the user.
* @returns Array of user wallet records.
*/
async function getUserWalletsByUserId(userId) {
return (0, connection_1.query)('SELECT id, user_id, wallet_type_id, address, derivation_index FROM user_wallets WHERE user_id = $1', [userId]);
}
/**
* Finds the user and wallet type associated with a deposit address.
* @param address - On-chain address to look up (case-insensitive).
* @returns User ID and wallet type ID, or null if the address is unknown.
*/
async function findUserByDepositAddress(address) {
const rows = await (0, connection_1.query)('SELECT user_id, wallet_type_id FROM user_wallets WHERE LOWER(address) = LOWER($1)', [address]);
return rows[0] || null;
}
//# sourceMappingURL=walletRepository.js.map