Пример #1
0
 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');
     }
 }
Пример #2
0
 public function getHistory()
 {
     $coins_list = function () {
         $data = ['' => 'All'];
         foreach (Cache::remember('coin_list', Config::get('cache.ttl'), function () {
             return Wallet::select('short as coin')->get();
         }) as $wallet) {
             $data[$wallet->coin] = $wallet->coin;
         }
         return $data;
     };
     $provider = new EloquentDataProvider(WalletsTransaction::selectRaw('wallets_transactions.id as id, wallets_transactions.amount, wallets_transactions.created_at, wallets_transactions.updated_at, wallets_transactions.txid, wallets_transactions.confirmed, wallets.short as coin')->where('user_id', Auth::user()->id)->leftJoin('wallets', 'wallets.id', '=', 'wallets_transactions.wallet_id'));
     Services::resourceManager()->ignoreCss(['bootstrap']);
     Services::resourceManager()->ignoreJs(['bootstrap']);
     $grid = new Grid($provider, [(new Column('wallets_transactions.id', 'ID'))->setValueFormatter(function ($value, WalletsTransaction $row = null) {
         if (!empty($row->id)) {
             return $row->id;
         } else {
             return $value;
         }
     }), new Column('txid', 'TxID'), (new Column('amount'))->setValueFormatter(function ($value, WalletsTransaction $row = null) {
         if (!empty($row)) {
             if ($value > 0) {
                 return HTML::tag('span', '+' . $value, ['class' => 'label label-success']);
             } else {
                 return HTML::tag('span', $value, ['class' => 'label label-danger']);
             }
         } else {
             return $value;
         }
     }), new Column('coin', 'Coin'), (new Column('confirmed'))->setValueFormatter(function ($value, WalletsTransaction $row = null) {
         if (!empty($row)) {
             if ($value) {
                 return HTML::tag('span', 'Confirmed', ['class' => 'label label-success']);
             } else {
                 return HTML::tag('span', 'Pending', ['class' => 'label label-info']);
             }
         }
     }), (new Column('wallets_transactions.created_at', 'Created At'))->setValueFormatter(function ($value, WalletsTransaction $row = null) {
         if (!empty($row->created_at)) {
             return $row->created_at;
         } else {
             return $value;
         }
     }), new Column('updated_at'), new AjaxDetailsRow(function (WalletsTransaction $row) {
         return url('/user/wallets/details', ['id' => $row->id]);
     }), new FilterControl('txid', FilterOperation::OPERATOR_STR_CONTAINS, new InputOption('txid', ['txid' => Input::get('txid')])), (new FilterControl('wallets.short', FilterOperation::OPERATOR_EQ, new InputOption('coin', ['coin' => Input::get('coin')])))->setView(new TemplateView('select', ['options' => $coins_list(), 'label' => 'Coin', 'inputAttributes' => ['style' => 'width: 50px;']])), (new FilterControl('confirmed', FilterOperation::OPERATOR_EQ, new InputOption('confirmed', ['confirmed' => Input::get('confirmed')])))->setView(new TemplateView('select', ['options' => ['' => 'All', 0 => 'Pending', 1 => 'Confirmed']])), new PageSizeSelectControl(new InputOption('ps', ['ps' => Input::get('ps')], 10), [10, 50, 100, 500, 1000]), new CsvExport(new InputOption('csv', ['csv' => Input::get('csv')])), new ResetButton(), new PageTotalsRow(['amount' => PageTotalsRow::OPERATION_SUM]), new PaginationControl(new InputOption('page', ['page' => Input::get('page')], 1), 10, $provider), new ColumnSortingControl('wallets_transactions.id', new InputOption('sort', ['sort' => Input::get('sort')], 'wallets_transactions.id-dir-desc')), new ColumnSortingControl('amount', new InputOption('sort', ['sort' => Input::get('sort')])), new ColumnSortingControl('wallets_transactions.created_at', new InputOption('sort', ['sort' => Input::get('sort')]))]);
     $customization = new BootstrapStyling();
     $customization->apply($grid);
     $grid = $grid->render();
     return view('user.wallets.history', compact('grid'));
 }
Пример #3
0
 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);
         }
     }
 }