Ejemplo n.º 1
0
 public function userbalancesubmitAction()
 {
     if (!$this->isValidUserBalance()) {
         $this->_forward('userbalance', null, null, array('validate' => $this->emptyField));
         return;
     }
     try {
         $array_of_params = $this->_request->getParams();
         $balanceServer = new Balance();
         $response = $balanceServer->getBalance($this->getUserFromParam($array_of_params));
         $this->_forward('userbalance', null, null, array('balance' => $response));
     } catch (Exception $e) {
         var_dump($e);
         die;
     }
 }
Ejemplo n.º 2
0
 public function init()
 {
     try {
         $config = Zend_Registry::get('appconf');
         $storageAdapter = new Zing_Auth_Storage_Session(array('host' => $config->authorization->storage->host, 'port' => $config->authorization->storage->port));
         //$auth=new Zing_App_Auth();
         // $auth->setStorage($storageAdapter);
         $auth = Zing_Authv2::getInstance();
         if ($auth->isLogged()) {
             $identity = $auth->getIdentity();
             $this->userID = $this->uName2uID($identity['username']);
             $this->view->userID = $this->userID;
             if (!$this->checkUser($identity['username'])) {
                 echo "<p style='text-align:center'>Bạn không được phép truy cập hệ thống Ví Zing Me</p>";
                 exit;
             }
         }
         $this->loadConfig(ROOT_PATH . '/config/config.ini');
         require_once ROOT_PATH . '/util/Logger.php';
         try {
             $balance = new Balance();
             $res = $balance->getBalance($this->userID);
             if ($res->code == 0) {
                 $this->view->balance = $res->currentBalance;
             } else {
                 if ($res->code == -3) {
                     $this->view->balance = 0;
                 } else {
                     $this->view->errMsg = "fail";
                 }
             }
         } catch (Exception $e) {
         }
     } catch (Exception $e) {
         echo 'Hệ Thống Ví Zing Me đang được bảo trì. Mời bạn quay lại sau.';
         die;
     }
 }
 /**
  * @param $confirms
  * @param $transaction_model
  * @param $common_data
  * @param $btc_amount
  * @param $to_address
  * @param $satoshi_amount
  *
  * @return mixed
  */
 private function processUnknownAddress($confirms, $transaction_model = null, $common_data, $btc_amount, $to_address, $satoshi_amount)
 {
     if ($confirms > 0) {
         /* bitcoind sent 2nd callback for the transaction which is 1st confirmation
          * no need to shoot to the application, since application is updating first confirmation anyway on block-notify */
         Transaction::updateTxConfirmation($transaction_model, $common_data);
     } else {
         /* either its change address or somebody sent to some address that is not registered in db!
          * say some shit that address is unknown, and mail too! */
         MailHelper::sendEmailPlain(['email' => Config::get('mail.admin_email'), 'subject' => 'RECEIVED BITCOINS TO UNKNOWN ADDRESS', 'text' => 'RECEIVED ' . $btc_amount . ' BITCOINS TO UNKNOWN ADDRESS. Address that received it: ' . $to_address]);
         $initialUserBalance = Balance::getBalance($this->user->id, $this->crypto_type_id);
         $common_data['transaction_type'] = TX_RECEIVE;
         $common_data['note'] = TX_UNREGISTERED_ADDRESS;
         $common_data['user_balance'] = bcadd($initialUserBalance, $satoshi_amount);
         // new API user balance
         $common_data['previous_balance'] = $initialUserBalance->balance;
         // API user balance before that transaction, because user balance has not been updated yet
         $common_data['bitcoind_balance'] = bcmul($this->bitcoin_core->getbalance(), SATOSHIS_FRACTION);
         // bitcoind balance on received! that means this transaction is not included, because it has 0 conf
         // insert new transaction anyway
         Transaction::insertNewTransaction($common_data);
         Log::warning('Received payment to unregistered address');
     }
 }
Ejemplo n.º 4
0
 public function balanceAction()
 {
     $this->_helper->viewRenderer->setNoRender(true);
     $data = $this->_getParam("data");
     $agentID = $this->_getParam("appID");
     $result = 0;
     try {
         $balance = new Balance();
         $parser = $balance->zingUnSignature($agentID, $data, 1);
         /*if($parser[0]!=$agentID){
           echo '0';
           return;
           }
           */
         $res = $balance->getBalance($parser[0]);
         if ($res->code == 0) {
             $result = $res->currentBalance;
         } else {
             if ($res->code == -3) {
                 $result = 0;
             } else {
                 $result = 0;
             }
         }
     } catch (Exception $e) {
     }
     echo $result;
 }
Ejemplo n.º 5
0
 public function doSell()
 {
     if (Auth::guest()) {
         echo json_encode(array('status' => 'error', 'message' => Lang::get('messages.login_to_buy')));
         exit;
     }
     $logFile = 'trades.log';
     Log::useDailyFiles(storage_path() . '/logs/trades/' . $logFile);
     Log::info('------------------------- Do Sell -----------------------------');
     $user = Confide::user();
     $price_sell = sprintf('%.8f', $_POST['price']);
     $amount_sell = sprintf('%.8f', $_POST['amount']);
     $total_sell = $price_sell * $amount_sell;
     //sprintf('%.8f',$price_sell*$amount_sell);
     $market_id = $_POST['market_id'];
     //Session::get('market_id');
     //get info market
     $market_default = Market::find($market_id);
     if (!isset($market_default->active) || $market_default->active == 0) {
         echo json_encode(array('status' => 'error', 'message' => Lang::get('messages.market_not_active')));
         exit;
     }
     $wallet_from = $market_default->wallet_from;
     $wallet_to = $market_default->wallet_to;
     $wallet = new Wallet();
     $from = $wallet->getType($wallet_from);
     $to = $wallet->getType($wallet_to);
     //cleck limit trade
     $limit_trade = WalletLimitTrade::where('wallet_id', $wallet_from)->first();
     if ($limit_trade) {
         $limit_trade = $limit_trade->toArray();
     } else {
         $limit_trade = array('min_amount' => 0.0001, 'max_amount' => 1000);
     }
     if (sprintf('%.8f', $_POST['amount']) < $limit_trade['min_amount'] || sprintf('%.8f', $_POST['amount']) > $limit_trade['max_amount']) {
         echo json_encode(array('status' => 'error', 'message' => Lang::get('messages.message_limit_trade')));
         exit;
     }
     //set default return value
     $status = 'error';
     $message = Lang::get('messages.error_contact_admin');
     $balance = new Balance();
     $message_socket = array();
     $message_socket['market_id'] = $market_id;
     //sub $total_buy money
     if ($balance->takeMoney($amount_sell, $wallet_from, $user->id)) {
         $orders_sell = new Order();
         $orders_sell->price = $price_sell;
         $orders_sell->status = 'active';
         $orders_sell->from_value = $amount_sell;
         $orders_sell->to_value = $total_sell;
         $orders_sell->market_id = $market_id;
         $orders_sell->user_id = $user->id;
         $orders_sell->type = 'sell';
         $amount_real = 0;
         $amount_rest = 0;
         $total_rest = 0;
         //get list order sell matching
         $orders = new Order();
         $buy_orders_matching = $orders->getBuyOrdersMatching($market_id, $price_sell);
         $buy_orders_matching = $buy_orders_matching->toArray();
         Log::info('buy_orders_matching: market_id: ' . $market_id . ' -- Price: ' . $price_sell, $buy_orders_matching);
         if (count($buy_orders_matching) > 0) {
             //fee_buy, fee_sell
             $fee_trade = new FeeTrade();
             $fee = $fee_trade->getFeeTrade($market_id);
             $per_fee_buy = $fee['fee_buy'];
             $per_fee_sell = $fee['fee_sell'];
             $message = '';
             foreach ($buy_orders_matching as $buy_order) {
                 $amount_rest = 0;
                 $total_rest = 0;
                 $amount_sold = 0;
                 if ($amount_sell > 0) {
                     $amount_real = $amount_sell;
                     $trade_history = new Trade();
                     Log::info("\n" . '---*****-----SELL : amount_sell: ' . $amount_sell . ' -- Price: ' . $price_sell);
                     $buy_matching = $buy_order;
                     //$buy_orders_matching[0];
                     $price_buy = $buy_matching['price'];
                     $amount_buy = $buy_matching['from_value'];
                     $total_buy = $buy_matching['to_value'];
                     $user_buy = $buy_matching['user_id'];
                     $class_price = str_replace(".", "-", $price_buy);
                     $class_price = str_replace(",", "-", $price_buy);
                     if ($amount_buy == $amount_sell) {
                         $total_sell = $amount_sell * $price_buy;
                         //sprintf('%.8f',$amount_sell*$price_buy);//really total sell;
                         $fee_buy = $total_buy * $per_fee_buy / 100;
                         $fee_sell = $total_sell * $per_fee_sell / 100;
                         if ($balance->takeMoney($fee_buy, $wallet_to, $user->id)) {
                             //tru phi nguoi mua
                             //add coin for seller/buyer
                             $balance->addMoney($amount_buy, $wallet_from, $user_buy);
                             $balance->addMoney($total_sell - $fee_sell, $wallet_to, $user->id);
                             Order::where('id', $buy_matching['id'])->update(array('status' => 'filled'));
                             $status = 'success';
                             //$message .= "\n".Lang::get('messages.filled').' : Fee buy: '.sprintf('%.8f',$fee_buy).' - Fee Sell: '.sprintf('%.8f',$fee_sell).' - Total buy: '.(sprintf('%.8f',$amount_buy)) . ' - Total sell: '.sprintf('%.8f',($total_sell-$fee_sell));
                             $message .= '<p>' . Lang::get('messages.filled') . ' Sold ' . $amount_buy . ' ' . $from . ' at price ' . $price_buy . ' Fee sell: ' . $fee_sell . ' ' . $to . ' - Received: ' . ($total_sell - $fee_sell) . ' ' . $to . '</p>';
                             Log::info('amount_buy = amount_sell Update sell orders: ', array('status' => 'filled'));
                             $orders_sell->status = 'filled';
                             //add history
                             $trade_id = $trade_history->addTradeHistory(array('seller_id' => $user->id, 'buyer_id' => $user_buy, 'amount' => $amount_buy, 'price' => $price_buy, 'market_id' => $market_id, 'type' => 'sell', 'fee_buy' => $fee_buy, 'fee_sell' => $fee_sell));
                             $message_socket['message_socket'][$class_price]['order_b'] = array('action' => "delete", 'id' => $buy_matching['id'], 'price' => $price_buy);
                             $message_socket['message_socket'][$buy_matching['id']]['history_trade'] = array('id' => $trade_id, 'seller_id' => $user->id, 'buyer_id' => $user_buy, 'amount' => $amount_buy, 'price' => $price_buy, 'market_id' => $market_id, 'type' => 'sell', "created_at" => date("Y-m-d H:i:s"), "order_id" => $buy_matching['id']);
                         } else {
                             $message .= Lang::get('messages.balance_not_enought_for_fee');
                         }
                     } elseif ($amount_buy > $amount_sell) {
                         $total_sell = $amount_sell * $price_buy;
                         //really total sell;
                         $fee_buy = $total_sell * $per_fee_buy / 100;
                         $fee_sell = $total_sell * $per_fee_sell / 100;
                         if ($balance->takeMoney($fee_buy, $wallet_to, $user->id)) {
                             //tru phi nguoi mua
                             if (isset($message_socket['message_socket'][$class_price]['order_s']['amount'])) {
                                 $amount_sold = $message_socket['message_socket'][$class_price]['order_b']['amount'] + $amount_sell;
                             } else {
                                 $amount_sold = $amount_sell;
                             }
                             //add coin for seller/buyer
                             $balance->addMoney($amount_sell, $wallet_from, $user_buy);
                             $balance->addMoney($total_sell - $fee_sell, $wallet_to, $user->id);
                             $amount_rest = sprintf('%.8f', $amount_buy - $amount_sell);
                             $total_rest = $amount_rest * $price_buy;
                             //sprintf('%.8f',$amount_rest * $price_buy);
                             Order::where('id', $buy_matching['id'])->update(array('status' => 'partly filled', 'from_value' => $amount_rest, 'to_value' => $total_rest));
                             Log::info('amount_buy > amount_sell Update sell orders: ', array('status' => 'partly filled', 'from_value' => $amount_rest, 'to_value' => $total_rest));
                             $status = 'success';
                             //$message .= "\n".Lang::get('messages.partly_filled').' : Fee buy: '.sprintf('%.8f',$fee_buy).' - Fee Sell: '.sprintf('%.8f',$fee_sell).' - Total buy: '.(sprintf('%.8f',$amount_sell)) . ' - Total sell: '.sprintf('%.8f',($total_sell-$fee_sell));
                             $message .= '<p>' . Lang::get('messages.filled') . ' Sold ' . $amount_sell . ' ' . $from . ' at price ' . $price_buy . ' Fee sell: ' . $fee_sell . ' ' . $to . ' - Received: ' . ($total_sell - $fee_sell) . ' ' . $to . '</p>';
                             $orders_sell->status = 'filled';
                             $trade_id = $trade_history->addTradeHistory(array('seller_id' => $user->id, 'buyer_id' => $user_buy, 'amount' => $amount_sell, 'price' => $price_buy, 'market_id' => $market_id, 'type' => 'sell', 'fee_buy' => $fee_buy, 'fee_sell' => $fee_sell));
                             //call socket
                             $message_socket['message_socket'][$class_price]['order_b'] = array("action" => "update", "id" => $buy_matching['id'], "amount" => $amount_sold, "price" => $price_buy, "total" => $amount_sold * $price_buy);
                             $message_socket['message_socket'][$buy_matching['id']]['history_trade'] = array('id' => $trade_id, 'seller_id' => $user->id, 'buyer_id' => $user_buy, 'amount' => $amount_sell, 'price' => $price_buy, 'market_id' => $market_id, 'type' => 'sell', "created_at" => date("Y-m-d H:i:s"), "order_id" => $buy_matching['id']);
                         } else {
                             $message .= Lang::get('messages.balance_not_enought_for_fee');
                         }
                     } else {
                         $total_sell = $amount_buy * $price_buy;
                         //sprintf('%.8f',$amount_buy*$price_buy);//really total sell;
                         $fee_buy = $total_sell * $per_fee_buy / 100;
                         $fee_sell = $total_sell * $per_fee_sell / 100;
                         if ($balance->takeMoney($fee_buy, $wallet_to, $user->id)) {
                             //tru phi nguoi mua
                             //add coin for seller/buyer
                             $balance->addMoney($amount_buy, $wallet_from, $user_buy);
                             $balance->addMoney($total_sell - $fee_sell, $wallet_to, $user->id);
                             $amount_rest = sprintf('%.8f', $amount_sell - $amount_buy);
                             $total_rest = $amount_rest * $price_sell;
                             //sprintf('%.8f',$amount_rest * $price_sell);
                             Order::where('id', $buy_matching['id'])->update(array('status' => 'filled'));
                             $status = 'success';
                             //$message .= "\n".Lang::get('messages.partly_filled').' : Fee buy: '.sprintf('%.8f',$fee_buy).' - Fee Sell: '.sprintf('%.8f',$fee_sell).' - Total buy: '.(sprintf('%.8f',$amount_buy)) . ' - Total sell: '.sprintf('%.8f',($total_sell-$fee_sell));
                             $message .= '<p>' . Lang::get('messages.partly_filled') . ' Sold ' . $amount_buy . ' ' . $from . ' at price ' . $price_buy . ' Fee sell: ' . $fee_sell . ' ' . $to . ' - Received: ' . ($total_sell - $fee_sell) . ' ' . $to . '</p>';
                             Log::info('amount_buy < amount_sell Update sell orders: ', array('status' => 'filled'));
                             $orders_sell->status = 'partly filled';
                             $orders_sell->from_value = $amount_rest;
                             $orders_sell->to_value = $total_rest;
                             $trade_id = $trade_history->addTradeHistory(array('seller_id' => $user->id, 'buyer_id' => $user_buy, 'amount' => $amount_buy, 'price' => $price_buy, 'market_id' => $market_id, 'type' => 'sell', 'fee_buy' => $fee_buy, 'fee_sell' => $fee_sell));
                             $amount_real = $amount_buy;
                             //call socket
                             $message_socket['message_socket'][$class_price]['order_b'] = array("action" => "delete", "id" => $buy_matching['id'], 'price' => $price_buy);
                             $message_socket['message_socket'][$buy_matching['id']]['history_trade'] = array('id' => $trade_id, 'seller_id' => $user->id, 'buyer_id' => $user_buy, 'amount' => $amount_buy, 'price' => $price_buy, 'market_id' => $market_id, 'type' => 'sell', "created_at" => date("Y-m-d H:i:s"), "order_id" => $buy_matching['id']);
                         } else {
                             $message .= Lang::get('messages.balance_not_enought_for_fee');
                         }
                     }
                     //refund money
                     /*if($price_buy > $price_sell){
                     			Log::info('-------Refund: ');
                     			$total_refunds = ($amount_real*$price_buy) - ($amount_real*$price_sell);
                     			$balance->addMoney($total_refunds,$from,$user->id);
                     		}*/
                     //update amount
                     $amount_sell = sprintf('%.8f', $amount_sell - $amount_real);
                 }
             }
             Log::info('-------amount_sell final: ' . $amount_sell);
             if ($amount_sell > 0) {
                 $orders_sell->save();
                 $message_socket['message_socket'][$orders_sell['id']]['order_s'] = array('action' => "insert", "amount" => $amount_sell, "price" => $price_sell, "total" => $total_rest, "type" => "sell");
                 $message_socket['message_socket'][$orders_sell['id']]['order_s']['id'] = $orders_sell->id;
                 $message_socket['message_socket'][$orders_sell['id']]['order_s']['created_at'] = $orders_sell->created_at;
             }
             $trade = new Trade();
             $message_socket['data_price'] = $trade->getBlockPrice($market_id);
             $balance = new Balance();
             $message_socket['data_price']['balance_coinmain'] = sprintf('%.8f', $balance->getBalance($wallet_from, 0));
             $message_socket['data_price']['balance_coinsecond'] = sprintf('%.8f', $balance->getBalance($wallet_to, 0));
             $message_socket['change_price']['total_volume'] = $trade->getVolume($market_id);
             $message_socket['change_price']['cur_price'] = $price_buy;
         } else {
             $orders_sell->save();
             $status = 'success';
             $message = Lang::get('messages.order_created');
             $message_socket['message_socket'][$orders_sell['id']]['order_s'] = array('action' => "insert", "id" => $orders_sell->id, "amount" => $orders_sell->from_value, "price" => $orders_sell->price, "total" => $orders_sell->to_value, 'type' => 'sell', 'created_at' => $orders_sell->created_at);
         }
         $this->triggerPusherTicket($market_id);
     } else {
         $message = Lang::get("messages.not_enough_purchase", array('type' => strtoupper($from)));
     }
     echo json_encode(array('status' => $status, 'message' => $message, "message_socket" => $message_socket));
     exit;
 }
Ejemplo n.º 6
0
 public function index($market_id = '')
 {
     $setting = new Setting();
     $wallet = new Wallet();
     $m = Market::where('active', 1)->orderBy('id')->first();
     if ($market_id == '') {
         $market_id = $setting->getSetting('default_market', $m->id);
     }
     Session::put('market_id', $market_id);
     $market_default = Market::find($market_id);
     if (!isset($market_default->active) || $market_default->active == 0) {
         //$setting->setSetting('default_market',$m->id);
         return Redirect::to('market/' . $m->id);
     }
     $wallet_from = $market_default->wallet_from;
     $wallet_to = $market_default->wallet_to;
     $from = strtoupper($wallet->getType($wallet_from));
     $to = strtoupper($wallet->getType($wallet_to));
     //get limit amount
     $limit_day = WalletTimeLimitTrade::select('limit_amount')->where('wallet_id', $wallet_to)->where('time_limit', 'per day')->first();
     if (isset($limit_day)) {
         $data['limit_day'] = $limit_day->limit_amount;
     } else {
         $data['limit_day'] = 1000;
     }
     $limit_week = WalletTimeLimitTrade::select('limit_amount')->where('wallet_id', $wallet_to)->where('time_limit', 'per week')->first();
     if (isset($limit_week)) {
         $data['limit_week'] = $limit_week->limit_amount;
     } else {
         $data['limit_week'] = 1000;
     }
     //get amount : day
     $user = Confide::user();
     if (isset($user)) {
         $uid = $user->id;
         $select = "SELECT sum(t.to_value) as sumamount from orders t, market m where m.id=t.market_id and m.wallet_to=" . $wallet_to . " and t.user_id=" . $uid . " and t.created_at='" . date("Y-m-d") . "'";
         $selectsum = DB::select($select);
         $sumamount = $selectsum[0]->sumamount;
         $data['amount_sum_day'] = $sumamount;
     }
     if (!isset($data['amount_sum_day'])) {
         $data['amount_sum_day'] = 0;
     }
     //echo 'aaaaaaaaaaa:'; print_r($data['amount_sum_day']);
     //get name of wallet
     $wallet1 = Wallet::where('id', $wallet_from)->first();
     $wallet2 = Wallet::where('id', $wallet_to)->first();
     $data['market_from'] = $wallet1->name;
     $data['market_to'] = $wallet2->name;
     $data['coinmain'] = $from;
     $data['coinsecond'] = $to;
     //get balance
     $balance = new Balance();
     $data['balance_coinmain'] = sprintf('%.8f', $balance->getBalance($wallet_from, 0));
     $data['balance_coinsecond'] = sprintf('%.8f', $balance->getBalance($wallet_to, 0));
     //get Sell Lowest
     $data['sell_lowest'] = sprintf('%.8f', 0);
     $data['buy_highest'] = sprintf('%.8f', 0);
     $order = new Order();
     $sell_lowest = $order->getSellLowest($market_id);
     $buy_highest = $order->getBuyHighest($market_id);
     if (isset($sell_lowest->price)) {
         $data['sell_lowest'] = sprintf('%.8f', $sell_lowest->price);
     }
     if (isset($buy_highest->price)) {
         $data['buy_highest'] = sprintf('%.8f', $buy_highest->price);
     }
     //fee_buy, fee_sell
     $fee_trade = new FeeTrade();
     $fee = $fee_trade->getFeeTrade($market_id);
     $data['fee_buy'] = $fee['fee_buy'];
     $data['fee_sell'] = $fee['fee_sell'];
     //get list orders
     $num_transaction_display = $setting->getSetting('num_transaction_display', 0);
     $list_sell_orders = $order->getOrders($market_id, 'sell', $num_transaction_display);
     $list_buy_orders = $order->getOrders($market_id, 'buy', $num_transaction_display);
     $data['sell_orders'] = $list_sell_orders;
     $data['buy_orders'] = $list_buy_orders;
     //get all history
     $trade_history = Trade::where('market_id', '=', $market_id)->orderBy('created_at', 'desc')->take($num_transaction_display)->get();
     $data['trade_history'] = $trade_history;
     $data['market_id'] = $market_id;
     $current_orders_user = $order->getCurrentOrdersUser($market_id);
     if ($current_orders_user) {
         $data['current_orders_user'] = $current_orders_user;
     }
     $trade = new Trade();
     $datachart = $trade->getDatasChart($market_id, '6 hour');
     $news = Post::where('type', 'news')->take(5)->orderby('created_at', 'desc')->get();
     $data['news'] = $news;
     //price
     $data_price = $trade->getBlockPrice($market_id);
     $data["get_prices"] = $data_price['get_prices'];
     $data['lastest_price'] = $data_price['lastest_price'];
     //limit trade amount
     $limit_trade = WalletLimitTrade::where('wallet_id', $wallet_from)->first();
     if ($limit_trade) {
         $data['limit_trade'] = $limit_trade->toArray();
     } else {
         $data['limit_trade'] = array('min_amount' => 0.0001, 'max_amount' => 1000);
     }
     //get data for block statistic
     $btc_wallet = Wallet::where('type', 'BTC')->first();
     $ltc_wallet = Wallet::where('type', 'LTC')->first();
     $btc_markets = array();
     $ltc_markets = array();
     //btc market on sidebar left
     $all_market_btc = array();
     if (isset($btc_wallet->id)) {
         $btc_markets = Market::leftJoin('wallets', 'market.wallet_from', '=', 'wallets.id')->select('market.*', 'wallets.name', 'wallets.type')->where('wallet_to', $btc_wallet->id)->orderby('wallets.type')->get();
         $btc_datainfo = array();
         foreach ($btc_markets as $value) {
             $all_market_btc[] = $value->id;
             $btc_datainfo[$value->id] = Trade::where('market_id', $value->id)->orderby('created_at', 'desc')->take(2)->get()->toArray();
             //      	$total_btc = DB::table('trade_history')->select(DB::raw('SUM( amount * price ) AS total'))
             //                  ->where('market_id', '=', $value->id)->first();
             //              //echo "<pre>total_btc: "; print_r($total_btc); echo "</pre>";
             //              //echo "<pre>getQueryLog: ".dd(DB::getQueryLog())."</pre>";
             // if(isset($total_btc->total))
             // 	$ltc_datainfo[$value->id]['total'] = $total_btc->total;
             $select = "SELECT SUM( amount * price ) AS total FROM trade_history Where `market_id`='" . $value->id . "' GROUP BY market_id";
             $total_btc = DB::select($select);
             if (isset($total_btc[0])) {
                 $btc_datainfo[$value->id]['total'] = $total_btc[0]->total;
             } else {
                 $btc_datainfo[$value->id]['total'] = 0;
             }
         }
         $data['btc_datainfo'] = $btc_datainfo;
     }
     //ltc market on sidebar left
     $all_market_ltc = array();
     if (isset($ltc_wallet->id)) {
         $ltc_markets = Market::leftJoin('wallets', 'market.wallet_from', '=', 'wallets.id')->select('market.*', 'wallets.name', 'wallets.type')->where('wallet_to', $ltc_wallet->id)->orderby('wallets.type')->get();
         $ltc_datainfo = array();
         foreach ($ltc_markets as $value) {
             $all_market_ltc[] = $value->id;
             $ltc_datainfo[$value->id] = Trade::where('market_id', $value->id)->orderby('created_at', 'desc')->take(2)->get()->toArray();
             // $total_ltc = DB::table('trade_history')->select(DB::raw('SUM( amount * price ) AS total'))
             //                  ->where('market_id', '=', $value->id)->first();
             // if(isset($total_ltc->total))
             // 	$ltc_datainfo[$value->id]['total'] = $total_ltc->total;
             $select = "SELECT SUM( amount * price ) AS total FROM trade_history Where `market_id`='" . $value->id . "' GROUP BY market_id";
             $total_ltc = DB::select($select);
             if (isset($total_ltc[0])) {
                 $ltc_datainfo[$value->id]['total'] = $total_ltc[0]->total;
             } else {
                 $ltc_datainfo[$value->id]['total'] = 0;
             }
         }
         $data['ltc_datainfo'] = $ltc_datainfo;
     }
     $data['btc_markets'] = $btc_markets;
     $data['ltc_markets'] = $ltc_markets;
     $date = date("Y-m-d H:i:s", strtotime(" -24 hour"));
     //echo "+24 hours: ".$date;
     if (!empty($all_market_btc)) {
         $data['statistic_btc'] = DB::table('trade_history')->select(DB::raw('COUNT(*) as number_trade,SUM( amount * price ) AS total'))->where('created_at', '>=', $date)->whereIn('market_id', $all_market_btc)->first();
     }
     if (!empty($all_market_ltc)) {
         $data['statistic_ltc'] = DB::table('trade_history')->select(DB::raw('COUNT(*) as number_trade,SUM( amount * price ) AS total'))->where('created_at', '>=', $date)->whereIn('market_id', $all_market_ltc)->first();
     }
     $data['wallets'] = Wallet::orderby('type')->get();
     return View::make('home', $data);
 }
Ejemplo n.º 7
0
 public function doTransfer()
 {
     $trade_key = Input::get('trade_key');
     $amount = Input::get('amount');
     $wallet_id = Input::get('wallet_id');
     $password = Input::get('password');
     $wallet = Wallet::find($wallet_id);
     $balance = new Balance();
     $user = Confide::user();
     if (Hash::check($password, $user->password)) {
         $user_receive = User::where('trade_key', $trade_key)->first();
         $amount_balance = $balance->getBalance($wallet->id);
         if (!isset($user_receive->username)) {
             return Redirect::to('user/transfer-coin/' . $wallet->id)->with('error', 'Sorry. The trade key not exist!');
         } elseif ($user_receive->id == $user->id) {
             return Redirect::to('user/transfer-coin/' . $wallet->id)->with('error', 'Sorry. You can not referrer to yourself!');
         } elseif ($amount_balance < $amount) {
             return Redirect::to('user/transfer-coin/' . $wallet->id)->with('error', 'Amount should be less than or equal to your balance.');
         } else {
             if ($balance->takeMoney($amount, $wallet->id, $user->id)) {
                 $balance->addMoney($amount, $wallet->id, $user_receive->id);
                 $transfer_his = new Transfer();
                 $transfer_his->sender = $user->id;
                 $transfer_his->receiver = $user_receive->id;
                 $transfer_his->wallet_id = $wallet->id;
                 $transfer_his->amount = $amount;
                 $transfer_his->save();
                 return Redirect::to('user/transfer-coin/' . $wallet->id)->with('success', 'You sent to user "' . $user_receive->username . '" ' . $amount . ' ' . $wallet->getType($wallet->id) . '.');
             }
         }
     } else {
         return Redirect::to('user/transfer-coin/' . $wallet->id)->with('error', "Password invalid.");
     }
 }