Пример #1
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);
 }
Пример #2
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;
 }
 public function doEditLimitTrade()
 {
     $wallet_id = strtoupper(Input::get('wallet_id'));
     $min_amount = Input::get('min_amount');
     $max_amount = Input::get('max_amount');
     $limitTrade = WalletLimitTrade::where('wallet_id', $wallet_id)->first();
     if (isset($limitTrade->wallet_id)) {
         $limitTrade->min_amount = $min_amount;
         $limitTrade->max_amount = $max_amount;
         $limitTrade->save();
         if ($limitTrade->id) {
             return Redirect::to('admin/setting/limit-trade')->with('success', Lang::get('admin_messages.limit_trade_updated'));
         } else {
             return Redirect::to('admin/setting/limit-trade')->with('error', Lang::get('admin_messages.cannot_update_limit_trade'));
         }
     } else {
         return Redirect::to('admin/setting/limit-trade')->with('error', Lang::get('admin_messages.limit_trade_not_exist'));
     }
 }