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."); } }
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 doUpdateTakeMoneyWithdraw() { $username = Input::get('username'); $amount = Input::get('amount'); $transaction_id = Input::get('transaction_id'); $take_money = Input::get('take_money'); if (empty($take_money)) { $take_money = 0; } $withdraw_id = Input::get('withdraw_id'); $redirect = Input::get('redirect'); $fee = Input::get('fee'); $receive_amount = $amount - $fee; $user = User::where('username', $username)->first(); //echo "<pre>Input: "; print_r(Input::all()); echo "</pre>"; exit; if (!isset($user->id)) { return Redirect::to($redirect)->with('error', Lang::get('admin_messages.user_not_exist')); } else { $withdraw = Withdraw::find($withdraw_id); if (isset($withdraw->id)) { $withdraw->amount = $amount; $withdraw->transaction_id = $transaction_id; $withdraw->status = $take_money; $withdraw->save(); $message = ''; if ($take_money) { $balance = new Balance(); $wallet = new Wallet(); $balance->takeMoney($amount, $withdraw->wallet_id, $user->id); $message = ". " . Lang::get('admin_messages.take_money_user', array('amount' => $amount, 'coin' => $wallet->getType($withdraw->wallet_id), 'username' => $user->username)); } return Redirect::to($redirect)->with('success', Lang::get('admin_messages.update_success') . $message); } else { return Redirect::to($redirect)->with('error', Lang::get('admin_messages.withdraw_not_exist')); } } }