Add min tip amount, min withdrawal amount and withdrawal fee.
This commit is contained in:
@@ -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
|
||||
];
|
||||
|
||||
@@ -33,6 +33,6 @@ $context = [
|
||||
'raw' => $message['text'] ?? '',
|
||||
];
|
||||
|
||||
$handler = new SalviumTipBotCommands($db, $wallet);
|
||||
$handler = new SalviumTipBotCommands($db, $wallet, $config);
|
||||
$handler->handle($command, $args, $context);
|
||||
?>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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, '@'));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user