Files
salvium-telegram-tipbot/salvium_tipbot_monitor.php
T
2026-05-30 16:10:24 +00:00

97 lines
3.7 KiB
PHP
Executable File

<?php
// salvium_tipbot_monitor.php
use Salvium\SalviumTipBotDB;
use Salvium\SalviumWallet;
$config = require __DIR__ . '/config.php';
require_once __DIR__ . '/src/salvium_tipbot_db.php';
require_once __DIR__ . '/src/salvium_tipbot_wallet.php';
require_once __DIR__ . '/src/salvium_tipbot_common.php';
$db = new SalviumTipBotDB($config);
$wallet = new SalviumWallet(
$config['SALVIUM_RPC_HOST'],
$config['SALVIUM_RPC_PORT'],
$config['SALVIUM_RPC_USERNAME'],
$config['SALVIUM_RPC_PASSWORD']
);
// Handle incoming transfers
$incoming = $wallet->getTransfers('in');
if ($incoming) {
foreach ($incoming as $tx) {
$subaddress = $tx['address'];
$user = $db->getUserBySubaddress($subaddress);
if (!$user) continue;
try {
$assetType = $db->normalizeAssetType($tx['asset_type'] ?? 'SAL1');
} catch (Throwable $e) {
error_log("Unsupported asset type in {$tx['txid']}: " . ($tx['asset_type'] ?? 'missing'));
continue;
}
if ($db->isDepositLogged($tx['txid'], $user['id'], $assetType)) continue;
$amount = $tx['amount'] / 1e8; // Convert atomic to major units (1 SAL = 1e8 atomic)
$db->logDeposit($user['id'], $tx['txid'], $amount, $tx['height'], $assetType);
$db->updateUserBalance($user['id'], $amount, 'add', $assetType);
sendMessage($user['telegram_user_id'], "Deposit received: {$amount} {$assetType} added to your balance.");
}
}
// Handle pending withdrawals
if (!filter_var($config['WITHDRAWALS_ENABLED'] ?? false, FILTER_VALIDATE_BOOL)) {
error_log('Withdrawals disabled; skipping pending withdrawal processing.');
} else {
$withdrawals = $db->getPendingWithdrawals();
foreach ($withdrawals as $withdrawal) {
// Skip if amount too low
if ($withdrawal['amount'] < $config['MIN_WITHDRAWAL_AMOUNT']) {
$db->updateWithdrawalStatus($withdrawal['id'], 'failed');
sendMessage($withdrawal['user_id'], "Withdrawal amount too low. Minimum is {$config['MIN_WITHDRAWAL_AMOUNT']} SAL.");
continue;
}
$amountToSend = $withdrawal['amount'] - $config['WITHDRAWAL_FEE'];
$result = $wallet->transfer([
[
'address' => $withdrawal['address'],
'amount' => (int)($amountToSend * 1e8)
]
]);
if ($result && isset($result['tx_hash'])) {
$db->updateWithdrawalTxid($withdrawal['id'], $result['tx_hash']);
$db->updateWithdrawalStatus($withdrawal['id'], 'sent');
sendMessage($withdrawal['user_id'], "Withdrawal of {$amountToSend} SAL sent. Fee: {$config['WITHDRAWAL_FEE']} SAL. TxID: {$result['tx_hash']}");
} else {
// Refund full amount including fee
$db->updateWithdrawalStatus($withdrawal['id'], 'failed');
$db->updateUserBalance($withdrawal['user_id'], $withdrawal['amount'], 'add', $withdrawal['asset_type'] ?? 'SAL1');
sendMessage($withdrawal['user_id'], "Withdrawal failed. {$withdrawal['amount']} SAL returned to your balance. Please try again later.");
}
}
}
// Handle pending tips
$users = [];
$tips = $db->getAllPendingTips();
foreach ($tips as $tip) {
if ($tip['amount'] < $config['MIN_TIP_AMOUNT']) continue; // skip small tips
$recipientId = $tip['recipient_user_id'];
$assetType = $tip['asset_type'] ?? 'SAL1';
if (!isset($users[$recipientId])) {
$users[$recipientId] = $db->getUserByTelegramId($recipientId);
}
$db->updateUserBalance($recipientId, $tip['amount'], 'add', $assetType);
$db->markTipsAsCredited([$tip['id']]);
sendMessage($recipientId, "You received a credited tip of {$tip['amount']} {$assetType}. Use /balance to check.");
}
?>