diff --git a/config.sample.php b/config.sample.php index 2257685..32b45c0 100755 --- a/config.sample.php +++ b/config.sample.php @@ -12,5 +12,9 @@ return [ 'DB_CHARSET' => 'utf8mb4', 'TELEGRAM_BOT_TOKEN' => 'YOUR_TELEGRAM_BOT_TOKEN_HERE', - 'IPV4_ONLY' => 1 + 'IPV4_ONLY' => 1, + + 'MIN_TIP_AMOUNT' => 0.2, + 'MIN_WITHDRAWAL_AMOUNT' => 0.4, + 'WITHDRAWAL_FEE' => 0.1 ]; diff --git a/salvium_tipbot.php b/salvium_tipbot.php index a34e15e..17b4724 100755 --- a/salvium_tipbot.php +++ b/salvium_tipbot.php @@ -33,6 +33,6 @@ $context = [ 'raw' => $message['text'] ?? '', ]; -$handler = new SalviumTipBotCommands($db, $wallet); +$handler = new SalviumTipBotCommands($db, $wallet, $config); $handler->handle($command, $args, $context); ?> diff --git a/salvium_tipbot_monitor.php b/salvium_tipbot_monitor.php index a5864b7..c9126ce 100755 --- a/salvium_tipbot_monitor.php +++ b/salvium_tipbot_monitor.php @@ -37,28 +37,41 @@ if ($incoming) { // Handle pending withdrawals $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)($withdrawal['amount'] * 1e8) // major to atomic + '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 {$withdrawal['amount']} SAL sent. TxID: {$result['tx_hash']}"); + 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->updateUserTipBalance($withdrawal['user_id'], $withdrawal['amount'], 'add'); // <== Refund - sendMessage($withdrawal['user_id'], "Withdrawal failed. The amount has been returned to your balance. Please try again later or contact support."); + $db->updateUserTipBalance($withdrawal['user_id'], $withdrawal['amount'], 'add'); + 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']; if (!isset($users[$recipientId])) { $users[$recipientId] = $db->getUserByTelegramId($recipientId); diff --git a/src/salvium_tipbot_commands.php b/src/salvium_tipbot_commands.php index e359696..a48e944 100644 --- a/src/salvium_tipbot_commands.php +++ b/src/salvium_tipbot_commands.php @@ -5,10 +5,12 @@ use Salvium\SalviumWallet; class SalviumTipBotCommands { private SalviumTipBotDB $db; private SalviumWallet $wallet; + private array $config; - public function __construct(SalviumTipBotDB $db, SalviumWallet $wallet) { + public function __construct(SalviumTipBotDB $db, SalviumWallet $wallet, array $config) { $this->db = $db; $this->wallet = $wallet; + $this->config = $config; } public function handle(string $command, array $args, array $context): void { @@ -55,6 +57,11 @@ class SalviumTipBotCommands { list(, $address, $amount) = $args; $amount = (float) $amount; + + if ($amount < $this->config['MIN_WITHDRAWAL_AMOUNT']) { + return "Withdrawal amount too low. Minimum is {$this->config['MIN_WITHDRAWAL_AMOUNT']} SAL."; + } + $user = $this->db->getUserByTelegramId($ctx['user_id']); if (!$user || $user['tip_balance'] < $amount) return "Insufficient balance or invalid account."; @@ -71,6 +78,11 @@ class SalviumTipBotCommands { list(, $targetUsername, $amount) = $args; $amount = (float)$amount; + + if ($amount < $this->config['MIN_TIP_AMOUNT']) { + return "Tip too small. Minimum tip is {$this->config['MIN_TIP_AMOUNT']} SAL."; + } + $sender = $this->db->getUserByTelegramId($ctx['user_id']); $recipient = $this->db->getUserByUsername(ltrim($targetUsername, '@'));