Example #1
0
 /**
  * Get user balances.
  */
 public function getIndex()
 {
     $response = ['success' => false];
     $error = false;
     if (!Auth::check()) {
         $response['success'] = false;
         $response['error'] = 'auth error';
         $error = true;
     }
     if (!$error) {
         $wallets = Cache::tags('user' . Auth::user()->id)->remember('wallets', Config::get('cache.ttl'), function () {
             return WalletsAddress::selectRaw('wallets.short, ifnull(wallets_addresses.balance, 0) as balance, (select sum(amount) from orders where orders.want_wallet_id = wallets_addresses.wallet_id and status = "open" and type = "sell") as orders')->whereNull('user_id')->orWhere('user_id', Auth::user()->id)->rightJoin('wallets', 'wallets.id', '=', 'wallets_addresses.wallet_id')->get();
         });
         foreach ($wallets as $wallet) {
             $response['wallets'][$wallet->short] = ['balance' => (double) $wallet->balance, 'orders_balance' => (double) $wallet->orders];
         }
         $response['success'] = true;
     }
     return response()->json($response);
 }
Example #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>!');
     }
 }
Example #3
0
 /**
  * Cancel user order and refund coins
  *
  * @param Order         $order
  */
 public static function cancel(Order $order)
 {
     $response = [];
     if ($order->isCancelable() && $order->user_id == Auth::user()->id) {
         try {
             DB::beginTransaction();
             if ($order->type == 'sell') {
                 $user_wallet_id = $order->wantWallet()->first()->generateUserAddress(Auth::user());
             } else {
                 $user_wallet_id = $order->wallet()->first()->generateUserAddress(Auth::user());
             }
             $user_wallet = WalletsAddress::where('id', $user_wallet_id)->first();
             $total_price = round($order->amount * $order->price, 8);
             if ($order->type == 'sell') {
                 $need_amount = $order->amount;
             } else {
                 $need_amount = $total_price;
             }
             $user_wallet->deposit($need_amount);
             $order->status = "cancel";
             $order->save();
             $response['success'] = true;
             $response['message'] = 'Order canceled';
             Cache::tags('user' . Auth::user()->id)->forget('wallets');
             DB::commit();
         } catch (\Exception $e) {
             DB::rollback();
             $response['error'] = $e->getMessage();
         }
     } else {
         $response['success'] = false;
         $response['error'] = 'You cant cancel this order';
     }
     return $response;
 }
Example #4
0
 /**
  * Generate personal user address on wallet.
  *
  * @var User
  *
  * @return string|bool $address
  */
 public function generateUserAddress(User $user)
 {
     $address = WalletsAddress::where('user_id', $user->id)->where('wallet_id', $this->id)->first();
     if ($address) {
         return $address->id;
     } else {
         $address = new WalletsAddress();
         $address->user_id = $user->id;
         $address->wallet_id = $this->id;
         $address->address = 'none';
         $address->save();
         return $address->id;
     }
 }
Example #5
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);
         }
     }
 }