public function doTask($console = false) { if ($console) { $this->info('Processing withdrawals'); } $txn_log = new Logger('TXN'); $txn_log->pushHandler(new StreamHandler(storage_path() . '/logs/withdrawals_cron.log', Logger::INFO)); $bar = null; if ($console) { $total_transactions = WalletsWithdrawal::whereNull('wallets_transaction_id')->where(function (Builder $query) { $query->where('need_approve', 0)->orWhere('approved', 1); })->count(); $bar = $this->output->createProgressBar($total_transactions); } WalletsWithdrawal::whereNull('wallets_transaction_id')->where(function (Builder $query) { $query->where('need_approve', 0)->orWhere('approved', 1); })->chunk(1000, function ($withdrawals) use($bar, $console, $txn_log) { foreach ($withdrawals as $withdrawal) { $balance = $withdrawal->wallet->command('getbalance'); if (empty($balance->error)) { if ($balance * 0.2 >= $withdrawal->amount || $withdrawal->approved == 1) { $amount = $withdrawal->amount - $withdrawal->wallet->transaction_fee; $txn = $withdrawal->wallet->command('sendtoaddress', [$withdrawal->address, $amount]); if (empty($txn->error)) { $wallet_transaction = new WalletsTransaction(); $wallet_transaction->txid = $txn; $wallet_transaction->user_id = $withdrawal->user_id; $wallet_transaction->wallet_id = $withdrawal->wallet_id; $wallet_transaction->amount = -$withdrawal->amount; $wallet_transaction->confirmed = false; $wallet_transaction->save(); $withdrawal->wallets_transaction_id = $wallet_transaction->id; $withdrawal->save(); $txn_log->error('Txn success (wal#' . $withdrawal->wallet->short . ')', (array) $txn); } else { $txn_log->error('Txn error (wal#' . $withdrawal->wallet->short . ')', (array) $txn); } } else { $withdrawal->need_approve = true; $withdrawal->save(); $txn_log->error('Too big txn need approve (wal#' . $withdrawal->wallet->short . ') amount:' . $withdrawal->amount, (array) $balance); Cache::flush('need_approve_count'); } } else { $txn_log->error('Getting balance(wal#' . $withdrawal->wallet->short . ') error', (array) $balance); } if ($console) { $bar->advance(); } } }); if ($console) { $bar->finish(); echo PHP_EOL; $this->info('done'); } }
public function doTask() { $txn_log = new Logger('TXN'); $txn_log->pushHandler(new StreamHandler(storage_path() . '/logs/transactions_cron.log', Logger::INFO)); $wallets = WalletModel::all(); foreach ($wallets as $wallet) { $result = $wallet->command('listtransactions', ['*', 1000]); if (empty($result->error)) { foreach ($result as $transaction) { if ($transaction->category == 'receive') { if ($transaction->account > 0) { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' start', (array) $transaction); $wallet_transaction = WalletsTransaction::where('txid', $transaction->txid)->where('wallet_id', $wallet->id)->first(); if (!$wallet_transaction) { $wallet_transaction = new WalletsTransaction(); $wallet_transaction->user_id = $transaction->account; $wallet_transaction->txid = $transaction->txid; $wallet_transaction->wallet_id = $wallet->id; $wallet_transaction->amount = $transaction->amount; $wallet_transaction->confirmed = false; $wallet_transaction->save(); Cache::tags('user' . $wallet_transaction->user_id)->forget('wallets'); } if (!empty($transaction->blockhash)) { $wallet_transaction->blockhash = $transaction->blockhash; } if ($wallet_transaction->confirmed) { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' already confirmed'); continue; } if ($transaction->confirmations >= $wallet->confirmations) { if ($transaction->amount > 0) { $address = WalletsAddress::where('wallet_id', $wallet->id)->where(function (\Illuminate\Database\Eloquent\Builder $query) use($transaction) { $query->where('address', $transaction->address)->orWhere('user_id', $transaction->account); })->first(); if ($address) { if ($address->user_id == $transaction->account) { $wallet_transaction->confirmed = true; $wallet_transaction->save(); $address->deposit($transaction->amount); $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' good', (array) $address); Cache::tags('user' . $wallet_transaction->user_id)->forget('wallets'); } else { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' users doesnt match'); } } else { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' user not found'); } } else { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' wrong amount'); } } else { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' not enough confirmations(need ' . $wallet->confirmations . ')'); } } else { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' account not defined'); } } else { $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' other category ' . $transaction->category); $wallet_transaction = WalletsTransaction::where('txid', $transaction->txid)->where('wallet_id', $wallet->id)->first(); if ($wallet_transaction) { if ($transaction->confirmations >= $wallet->confirmations) { $wallet_transaction->confirmed = true; $wallet_transaction->blockhash = $transaction->blockhash; $wallet_transaction->save(); $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' confirmed'); } } } } } else { $txn_log->error('wal#' . $wallet->id . '(' . $wallet->short . ') error', (array) $result); } } }