69 lines
2.8 KiB
TypeScript
69 lines
2.8 KiB
TypeScript
import { db } from './lib/db';
|
|
|
|
async function testSweepDB() {
|
|
const txId = "5fda5442-23ea-4130-8646-d0a882ed2517";
|
|
const selectedNetwork = "SOLANA";
|
|
const selectedToken = "SOL";
|
|
const expectedCryptoAmount = "0.164824";
|
|
const txHash = "mocked-txhash-123";
|
|
|
|
try {
|
|
const result = await db.query('SELECT * FROM transactions WHERE id = $1', [txId]);
|
|
const transaction = result.rows[0];
|
|
|
|
const settings = await (async () => {
|
|
const result = await db.query('SELECT key, value FROM system_settings WHERE key IN (\'sol_platform_address\', \'evm_platform_address\', \'tron_platform_address\', \'btc_platform_address\', \'default_fee_percent\')');
|
|
const map: Record<string, string> = {};
|
|
result.rows.forEach(r => map[r.key] = r.value);
|
|
return {
|
|
fee: parseFloat(map.default_fee_percent || '1.0')
|
|
};
|
|
})();
|
|
|
|
const merchantResult = await db.query('SELECT * FROM merchants WHERE id = $1', [transaction.merchant_id]);
|
|
const merchant = merchantResult.rows[0];
|
|
|
|
const feePercent = merchant?.fee_percent !== undefined && merchant?.fee_percent !== null
|
|
? parseFloat(merchant.fee_percent)
|
|
: settings.fee;
|
|
|
|
const grossAmount = parseFloat(transaction.amount);
|
|
const feeAmount = (grossAmount * feePercent) / 100;
|
|
const merchantNetCredit = grossAmount - feeAmount;
|
|
|
|
const cryptoAmount = parseFloat(expectedCryptoAmount);
|
|
const cryptoFee = (cryptoAmount * feePercent) / 100;
|
|
const cryptoNetCredit = cryptoAmount - cryptoFee;
|
|
|
|
// DB Updates
|
|
console.log("Updating merchant balances...");
|
|
await db.query(`UPDATE merchants SET available_balance = available_balance + $1 WHERE id = $2`,
|
|
[merchantNetCredit, transaction.merchant_id]);
|
|
|
|
await db.query(`
|
|
INSERT INTO merchant_balances (merchant_id, network, token, balance, total_gross)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT (merchant_id, network, token)
|
|
DO UPDATE SET
|
|
balance = merchant_balances.balance + $4,
|
|
total_gross = merchant_balances.total_gross + $5
|
|
`, [transaction.merchant_id, selectedNetwork, selectedToken, cryptoNetCredit, cryptoAmount]);
|
|
|
|
console.log("Updating transaction...");
|
|
await db.query(`
|
|
UPDATE transactions
|
|
SET status = 'succeeded',
|
|
paid_network = $2,
|
|
paid_token = $3,
|
|
paid_amount_crypto = $4
|
|
WHERE id = $1`,
|
|
[transaction.id, selectedNetwork, selectedToken, expectedCryptoAmount]
|
|
);
|
|
|
|
console.log("Completed without error!");
|
|
} catch (e: any) {
|
|
console.error("Caught error:", e.message);
|
|
}
|
|
}
|
|
testSweepDB().then(() => process.exit(0));
|