Ejemplo n.º 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');
     }
 }
Ejemplo n.º 2
0
 public function postWithdrawal($wallet_id, Request $request)
 {
     if (Totp::configured()) {
         if (Totp::verify($request, false)) {
             $wallet = WalletsAddress::where('wallet_id', $wallet_id)->where('user_id', Auth::user()->id)->leftJoin('wallets', 'wallets.id', '=', 'wallet_id')->first();
             if ($wallet) {
                 if ($wallet->balance >= Input::get('balance')) {
                     $validate_address = $wallet->wallet->command('validateaddress', [Input::get('address')]);
                     if (!empty($validate_address->isvalid) && $validate_address->isvalid === true) {
                         $withdrawal = new WalletsWithdrawal();
                         $withdrawal->address = Input::get('address');
                         $withdrawal->amount = Input::get('balance');
                         $withdrawal->wallet_id = $wallet->wallet_id;
                         $withdrawal->user_id = $wallet->user_id;
                         $withdrawal->save();
                         $wallet->withdrawal(Input::get('balance'));
                         Cache::tags('user' . $wallet->user_id)->forget('wallets');
                         return redirect('/user/wallets')->with('success', 'Withdrawal successfully queued!');
                     } else {
                         if (!empty($validate_address->error)) {
                             Log::error('Wallet error #' . $wallet->wallet_id . ' - ' . $wallet->short, [$validate_address->error]);
                             return back()->withErrors('Please try again later!');
                         } else {
                             return back()->withErrors(Input::get('address') . ' is not valid ' . $wallet->title . ' address.');
                         }
                     }
                 } else {
                     return back()->withErrors('Not enough balace to withdrawal ' . Input::get('balance') . ' ' . $wallet->short);
                 }
             } else {
                 return back()->withErrors('Wallet not found');
             }
         } else {
             return Totp::error($request);
         }
     } else {
         return redirect('/auth/totp')->withErrors('Before withdrawal you must <a href="/auth/totp">configure One-Time password</a>!');
     }
 }
Ejemplo n.º 3
0
 /**
  * Handle an incoming request.
  *
  * @param \Illuminate\Http\Request $request
  * @param \Closure                 $next
  * @param string|null              $guard
  *
  * @return mixed
  */
 public function handle(Request $request, Closure $next)
 {
     if (Auth::user()) {
         if (Auth::user()->hasRole('owner')) {
             $withdrawals_approve = Cache::remember('need_approve_count', Config::get('cache.ttl'), function () {
                 return WalletsWithdrawal::where('need_approve', true)->where('approved', false)->count();
             });
             $request->attributes->set('withdrawals_approve', $withdrawals_approve);
         }
         $user = Auth::user();
         $token = JWTAuth::fromUser($user);
         $request->attributes->set('jwt_token', $token);
     }
     return $next($request);
 }
Ejemplo n.º 4
0
 public function getAdmin()
 {
     $roles = Cache::remember('roles_count', Config::get('cache.ttl'), function () {
         return Role::count();
     });
     $wallets = Cache::remember('wallets_count', Config::get('cache.ttl'), function () {
         return Wallet::count();
     });
     $users = Cache::remember('users_count', Config::get('cache.ttl'), function () {
         return User::count();
     });
     $withdrawals = Cache::remember('withdrawals_count', Config::get('cache.ttl'), function () {
         return WalletsWithdrawal::count();
     });
     return view('admin.index', compact('roles', 'wallets', 'users', 'withdrawals'));
 }
Ejemplo n.º 5
0
 /**
  * Remove the specified resource from storage.
  *
  * @param int $id
  */
 public function destroy($id)
 {
     WalletsWithdrawal::destroy($id);
     Session::flash('flash_message', 'WalletsWithdrawal deleted!');
     return redirect('admin/wallets-withdrawal');
 }