Ejemplo n.º 1
0
 public function getFeeTrade($market_id)
 {
     $fee_trade = FeeTrade::where('market_id', '=', $market_id)->first();
     if (isset($fee_trade->fee_buy)) {
         return array('fee_buy' => $fee_trade->fee_buy, "fee_sell" => $fee_trade->fee_sell);
     } else {
         return array('fee_buy' => 0, "fee_sell" => 0);
     }
 }
Ejemplo n.º 2
0
 public function routePage($page = '')
 {
     //echo "<pre>user: "******"</pre>";
     switch ($page) {
         case "fees":
             $market = new Market();
             $wallet = new Wallet();
             $fees_trade = FeeTrade::get()->toArray();
             //echo "<pre>list_buy_orders: "; print_r($list_buy_orders); echo "</pre>";
             $fees_withdraw = FeeWithdraw::leftJoin('wallets', 'fee_withdraw.wallet_id', '=', 'wallets.id')->select('fee_withdraw.*', 'wallets.type', 'wallets.name')->get();
             foreach ($fees_trade as $key => $value) {
                 $wallet_type = $market->getWalletType($value['market_id']);
                 if (!empty($wallet_type)) {
                     $fees_trade[$key]['wallet_from'] = $wallet_type['wallet_from'];
                     $fees_trade[$key]['wallet_to'] = $wallet_type['wallet_to'];
                 }
             }
             $fee['fees_trade'] = $fees_trade;
             $fee['fees_withdraw'] = $fees_withdraw;
             return View::make('fees', $fee);
             break;
         case "voting":
             $setting = new Setting();
             $coinvotes = DB::table('coin_votes')->get();
             try {
                 $wallet = Wallet::where('type', 'BTC')->first();
                 $wallet->connectJsonRPCclient($wallet->wallet_username, $wallet->wallet_password, $wallet->wallet_ip, $wallet->port);
                 foreach ($coinvotes as $key => $value) {
                     $num_vote = Vote::where('coinvote_id', '=', $value->id)->count();
                     //echo "<pre>getreceivedbyaccount"; print_r($wallet->getReceivedByAddress($value->btc_address)); echo "</pre>";//$value->label_address
                     $btc_payment = $wallet->getReceivedByAddress($value->btc_address);
                     //'12X9jVe4S8pAqJ7EoKN7B4YwMQpzfgArtX'
                     $amount_btc_per_vote = $setting->getSetting('amount_btc_per_vote', 0.0001);
                     $num_payment = floor($btc_payment / $amount_btc_per_vote);
                     //echo "btc_payment: ".$btc_payment;
                     //echo "<br>num_payment: ".$num_payment;
                     $coinvotes[$key]->num_vote = $num_vote + $num_payment;
                 }
             } catch (Exception $e) {
                 $data['error_message'] = Lang::get('texts.not_connect_wallet');
                 //'Caught exception: '.$e->getMessage()."\n";  //"Not connect to this
             }
             //echo "<pre>coinvotes"; print_r($coinvotes); echo "</pre>";
             $data['coinvotes'] = $coinvotes;
             return View::make('voting', $data);
             break;
         case "security":
             return View::make('security');
             break;
         case "api":
             if (isset($_REQUEST['method'])) {
                 $method = $_REQUEST['method'];
                 $value = $this->api($method);
             } else {
                 $setting = new Setting();
                 $data['pusher_app_key'] = $setting->getSetting('pusher_app_key', '');
                 return View::make('api', $data);
             }
             print_r($value);
             break;
         case "apiprivate":
             $value = $this->apiprivate();
             break;
         case "all-trades":
             if (Auth::guest()) {
                 return Redirect::to('/login');
             }
             $record_per_page = 15;
             if (empty($_GET['pager_page'])) {
                 $pager_page = 1;
             } else {
                 $pager_page = $_GET['pager_page'];
             }
             $data['cur_page'] = $pager_page;
             $offset_start = ($pager_page - 1) * $record_per_page;
             $trade_history = Trade::leftjoin('market', 'market.id', '=', 'trade_history.market_id')->select('trade_history.*', 'market.wallet_from as from', 'market.wallet_to as to');
             $where = '';
             if (Input::has('market')) {
                 $fil_market = Input::get('market');
                 $trade_history->where("trade_history.market_id", "=", $fil_market);
             }
             if (Input::has('type')) {
                 $fil_type = Input::get('type');
                 $trade_history->where("trade_history.type", "=", $fil_type);
             }
             $total_records = $trade_history->get();
             $data['total_pages'] = ceil(count($total_records) / $record_per_page);
             $trades = $trade_history->skip($offset_start)->take($record_per_page)->orderby("created_at", "desc")->get();
             #echo "<pre>trade_history"; print_r($trades->toArray()); echo "</pre>";
             //echo "<pre>getQueryLog: ".dd(DB::getQueryLog())."</pre>";
             $data['tradehistories'] = $trades;
             $market = new Market();
             $markets = Market::get();
             $market_wallet = array();
             foreach ($markets as $value) {
                 $market_wallet[$value->id] = $market->getWalletType($value->id);
             }
             $data['markets'] = $market_wallet;
             return View::make('alltrades', $data);
             break;
         case "all-news":
             $news = Post::where('type', 'news')->orderby('created_at', 'desc')->get();
             $data['news'] = $news;
             return View::make('allnews', $data);
             break;
         default:
             return View::make('index');
             break;
     }
 }
Ejemplo n.º 3
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 deleteMarket()
 {
     $market_id = Input::get('market_id');
     $market = Market::find($market_id);
     if (isset($market->id)) {
         FeeTrade::where('market_id', $market_id)->delete();
         Order::where('market_id', $market_id)->delete();
         Trade::where('market_id', $market_id)->delete();
         Market::where('id', $market_id)->delete();
         $message = Lang::get('admin_messages.delete_success');
         if (Input::get('isAjax')) {
             echo json_encode(array('status' => 'success', 'message' => $message));
             exit;
         } else {
             return Redirect::to('admin/manage/markets')->with('success', $message);
         }
     } else {
         $message = Lang::get('admin_messages.market_not_exist');
         if (Input::get('isAjax')) {
             echo json_encode(array('status' => 'error', 'message' => $message));
             exit;
         } else {
             return Redirect::to('admin/manage/markets')->with('error', $message);
         }
     }
 }