orderBy('datetime', 'desc')->first()->name;
$real_path = base_path() . "\json\cmc${last_json_path_row}";
$cmc_list = json_decode(file_get_contents($real_path), true);
// Open json file from Ccxt api result
$last_json_path_row = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first();
$last_json_path_name = $last_json_path_row->name;
$last_json_path_datetime = $last_json_path_row->datetime;
$real_path = base_path() . "\json\ccxt\price${last_json_path_name}";
$ccxt_list = json_decode(file_get_contents($real_path), true);
// Open last one day before price json file for comparing
$lastDatetime = DateTime::createFromFormat('Y-m-d H:i:s', $last_json_path_datetime);
$past_datetime = $lastDatetime->add(new DateInterval('PT1M'));
$compare_datetime = $past_datetime->sub(new DateInterval('PT12H'));//sub(new DateInterval('P1D'));
$compare_datetime_str = $compare_datetime->format('Y-m-d H:i:s');
$compare_row = DB::table('ccxt_price_list')->where('datetime', '>=', $compare_datetime_str)->orderBy('datetime', 'asc')->first();
$compare_json_path_name = $compare_row->name;
$real_path = base_path() . "\json\ccxt\price${compare_json_path_name}";
$ccxt_compare_list = json_decode(file_get_contents($real_path), true);
// Get currencies from model
$currencies = Currency::where('active', '=', 1)->orderBy('cmc_id', 'asc')->get();
// Filter and Update information for each coin
$updated_currencies = $currencies->map(function($value, $key) use($cmc_list, $ccxt_list, $ccxt_compare_list) {
$symbol = $value['symbol'];
// Get values from CMC json input
$cmc_result = isset($cmc_list[$symbol]) ? $cmc_list[$symbol] : null;
$value['max_supply'] = is_null($cmc_result) ? null : $cmc_result['max_supply'];
$value['total_supply'] = is_null($cmc_result) ? null : $cmc_result['total_supply'];
$value['circulating_supply'] = is_null($cmc_result) ? null : $cmc_result['circulating_supply'];
// Get values from CCXT json input
$ccxt_result = isset($ccxt_list['currency'][$symbol]) ? $ccxt_list['currency'][$symbol] : null;
$value['price'] = is_null($ccxt_result) ? null : $ccxt_result['price'];
$value['volume'] = is_null($ccxt_result) ? null : $ccxt_result['volume'];
$value['market_cap'] = is_null($cmc_result) || is_null($ccxt_result) ? null : $value['circulating_supply'] * $value['price'];
if(is_null($value['max_supply'])) {
$value['circulating_percent'] = 0.0;
} else {
$value['circulating_percent'] = 100.0 * $value['circulating_supply'] / $value['max_supply'];
}
$ccxt_compare_result = isset($ccxt_compare_list['currency'][$symbol]) ? $ccxt_compare_list['currency'][$symbol] : null;
$value['change'] = null; //$compare_json_path_name; //$ccxt_compare_result['price'];
if(isset($ccxt_compare_result['price']) && $ccxt_compare_result['price'] > 0)
$value['change'] = $ccxt_compare_result['price']; //fmod(floatval($ccxt_result['price']), floatval($ccxt_compare_result['price']));
return $value;
});
// Sort the collection by Key value
$sorted_collet = collect($updated_currencies)->sortByDesc('market_cap')->values();
return response()->json($sorted_collet->all());
}
public function getwatchlist(Request $request)
{
$watch_list_array = explode('%', $request->data);
if(count($watch_list_array) == 0) {
return null;
}
$collect_watch_list = collect($watch_list_array);
// Open json file from Coinmarketcap api result
$last_json_path_row = DB::table('cmc_coin_list')->orderBy('datetime', 'desc')->first()->name;
$real_path = base_path() . "\json\cmc${last_json_path_row}";
$cmc_list = json_decode(file_get_contents($real_path), true);
// Open json file from Ccxt api result
$last_json_path_row = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first();
$last_json_path_name = $last_json_path_row->name;
$last_json_path_datetime = $last_json_path_row->datetime;
$real_path = base_path() . "\json\ccxt\price${last_json_path_name}";
$ccxt_list = json_decode(file_get_contents($real_path), true);
// Open last one day before price json file for comparing
$lastDatetime = DateTime::createFromFormat('Y-m-d H:i:s', $last_json_path_datetime);
$past_datetime = $lastDatetime->add(new DateInterval('PT1M'));
$compare_datetime = $past_datetime->sub(new DateInterval('PT12H'));//sub(new DateInterval('P1D'));
$compare_datetime_str = $compare_datetime->format('Y-m-d H:i:s');
$compare_row = DB::table('ccxt_price_list')->where('datetime', '>=', $compare_datetime_str)->orderBy('datetime', 'asc')->first();
$compare_json_path_name = $compare_row->name;
$real_path = base_path() . "\json\ccxt\price${compare_json_path_name}";
$ccxt_compare_list = json_decode(file_get_contents($real_path), true);
// Get currencies from model
// $currencies = Currency::orderBy('cmc_id', 'asc')->get();
// Filter and Update information for each coin
$updated_currencies = []; //$collect_watch_list->map(function($value, $key) use($cmc_list, $ccxt_list, $ccxt_compare_list) {
for($i=0; $iwhere('active', '=', 1)->first();
// Get values from CMC json input
$cmc_result = isset($cmc_list[$symbol]) ? $cmc_list[$symbol] : null;
$newItem['max_supply'] = is_null($cmc_result) ? null : $cmc_result['max_supply'];
$newItem['total_supply'] = is_null($cmc_result) ? null : $cmc_result['total_supply'];
$newItem['circulating_supply'] = is_null($cmc_result) ? null : $cmc_result['circulating_supply'];
// Get values from CCXT json input
$ccxt_result = isset($ccxt_list['currency'][$symbol]) ? $ccxt_list['currency'][$symbol] : null;
$newItem['price'] = is_null($ccxt_result) ? null : $ccxt_result['price'];
$newItem['volume'] = is_null($ccxt_result) ? null : $ccxt_result['volume'];
$newItem['market_cap'] = is_null($cmc_result) || is_null($ccxt_result) ? null : $newItem['circulating_supply'] * $newItem['price'];
if(is_null($newItem['max_supply'])) {
$newItem['circulating_percent'] = 0.0;
} else {
$newItem['circulating_percent'] = 100.0 * $newItem['circulating_supply'] / $newItem['max_supply'];
}
$ccxt_compare_result = isset($ccxt_compare_list['currency'][$symbol]) ? $ccxt_compare_list['currency'][$symbol] : null;
$newItem['change'] = null; //$compare_json_path_name; //$ccxt_compare_result['price'];
if(isset($ccxt_compare_result['price']) && $ccxt_compare_result['price'] > 0)
$newItem['change'] = $ccxt_compare_result['price']; //fmod(floatval($ccxt_result['price']), floatval($ccxt_compare_result['price']));
array_push($updated_currencies, $newItem);
}
// Sort the collection by Key value
$sorted_collet = collect($updated_currencies);//->all(); //->sortByDesc('market_cap')->values();
return response()->json($sorted_collet->all());
}
public function getbtcprice()
{
// Open json file from Ccxt api result
$last_json_path_row = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first()->name;
$real_path = str_replace('\\', '/', base_path() . "\json\ccxt\price${last_json_path_row}");
//$real_path = "/var/www/vhosts/datumcrypto.com/httpdocs/json/ccxt/price/2020/2/27/d39a8b86-5b50-4333-8e01-65de20dbf768.json";
$ccxt_list = json_decode(file_get_contents($real_path), true);
// Get BTC/USD value
$btc_price = $ccxt_list['currency']['BTC']['price'];
return response()->json($btc_price);
}
public function getcurrency(Request $request, $currency)
{
$currency_item = Currency::where('symbol', $currency)->where('active', '=', 1)->first();
if(is_null($currency_item))
return response()->json(array('invalid' => true));
return response()->json($currency_item);
}
public function getstatus(Request $request, $currency)
{
$symbol = strtoupper($currency);
// Open json file from Coinmarketcap api result
$last_json_path_row = DB::table('cmc_coin_list')->orderBy('datetime', 'desc')->first()->name;
$real_path = base_path() . "\json\cmc${last_json_path_row}";
$cmc_list = json_decode(file_get_contents($real_path), true);
// Open json file from Ccxt api result
$last_json_path_row = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first()->name;
$last_update_datetime = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first()->datetime;
$real_path = base_path() . "\json\ccxt\price${last_json_path_row}";
$ccxt_list = json_decode(file_get_contents($real_path), true);
$lastDatetime = DateTime::createFromFormat('Y-m-d H:i:s', $last_update_datetime);
$past_datetime = $lastDatetime->sub(new DateInterval('PT12H'));
$past_datetime_str = $past_datetime->format('Y-m-d H:i:s');
$past_datetime_row = DB::table('ccxt_price_list')->where('datetime', '>=', $past_datetime_str)->orderBy('datetime', 'asc')->first()->name;
$real_path = base_path() . "\json\ccxt\price${past_datetime_row}";
$ccxt_past_list = json_decode(file_get_contents($real_path), true);
// Get values from CMC json input
$cmc_result = isset($cmc_list[$symbol]) ? $cmc_list[$symbol] : null;
$value = collect();
$value['max_supply'] = is_null($cmc_result) ? null : $cmc_result['max_supply'];
$value['total_supply'] = is_null($cmc_result) ? null : $cmc_result['total_supply'];
$value['circulating_supply'] = is_null($cmc_result) ? null : $cmc_result['circulating_supply'];
// Get values from CCXT json input
$ccxt_result = isset($ccxt_list['currency'][$symbol]) ? $ccxt_list['currency'][$symbol] : null;
$value['price'] = is_null($ccxt_result) ? null : $ccxt_result['price'];
$value['volume'] = is_null($ccxt_result) ? null : $ccxt_result['volume'];
$value['market_cap'] = is_null($cmc_result) || is_null($ccxt_result) ? null : $value['circulating_supply'] * $value['price'];
$value['lastUpdate'] = $last_update_datetime;
$value['past'] = isset($ccxt_past_list['currency'][$symbol]) ? $ccxt_past_list['currency'][$symbol]['price'] : null;
if(is_null($value['max_supply'])) {
$value['circulating_percent'] = 0.0;
} else {
$value['circulating_percent'] = 100.0 * $value['circulating_supply'] / $value['max_supply'];
}
return response()->json($value);
}
public function getmarkets(Request $request, $currency)
{
$symbol = strtoupper($currency);
// Open json file from Ccxt api result
$last_json_path_row = DB::table('ccxt_ticker_list')->orderBy('datetime', 'desc')->first();
$last_json_path_row_name = $last_json_path_row->name;
$last_json_path_row_datetime = $last_json_path_row->datetime;
$real_path = base_path() . "\\json\\ccxt\\ticker${last_json_path_row_name}";
$ccxt_list = json_decode(file_get_contents($real_path), true);
// Open json file from Ccxt api result
$last_json_path_row = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first()->name;
$real_path = base_path() . "\json\ccxt\price${last_json_path_row}";
$ccxt_price_list = json_decode(file_get_contents($real_path), true);
// Open last one day before price json file for comparing
$lastDatetime = DateTime::createFromFormat('Y-m-d H:i:s', $last_json_path_row_datetime);
$past_datetime = $lastDatetime->add(new DateInterval('PT1M'));
$compare_datetime = $past_datetime->sub(new DateInterval('PT12H'));//sub(new DateInterval('P1D'));
$compare_datetime_str = $compare_datetime->format('Y-m-d H:i:s');
$compare_row = DB::table('ccxt_ticker_list')->where('datetime', '>=', $compare_datetime_str)->orderBy('datetime', 'asc')->first();
$compare_json_path_name = $compare_row->name;
$real_path = base_path() . "\\json\\ccxt\\ticker${compare_json_path_name}";
$ccxt_past_ticker_list = json_decode(file_get_contents($real_path), true);
$compare_row = DB::table('ccxt_price_list')->where('datetime', '>=', $compare_datetime_str)->orderBy('datetime', 'asc')->first();
$compare_json_path_name = $compare_row->name;
$real_path = base_path() . "\json\ccxt\price${compare_json_path_name}";
$ccxt_past_price_list = json_decode(file_get_contents($real_path), true);
// Get values from CCXT price json input
$ccxt_price = isset($ccxt_price_list['currency']) ? $ccxt_price_list['currency'] : null;
// filter by currency symbol
$markets = [];
foreach($ccxt_list as $ex_symbol => $ex_markets) {
$exchange_id = $ex_symbol;
foreach($ex_markets as $market_symbol => $market_info) {
if(isset($market_info['base']) && isset($market_info['baseVolume'])) {
if($symbol == $market_info['base']) {
if(isset($ccxt_price[$symbol]) && isset($ccxt_price[$market_info['quote']]) && $market_info['baseVolume']) {
$new_item = $market_info;
$new_item['exchange'] = $exchange_id;
$new_item['market'] = $market_symbol;
$new_item['volume'] = $market_info['baseVolume'] * $ccxt_price[$symbol]['price'];
$new_item['price'] = $market_info['price'] * $ccxt_price[$market_info['quote']]['price'];
if(isset($ccxt_past_ticker_list[$exchange_id][$market_symbol]) && isset($ccxt_past_price_list['currency'][$symbol])) {
$new_item['past'] = $ccxt_past_ticker_list[$exchange_id][$market_symbol]['baseVolume'] * $ccxt_past_price_list['currency'][$symbol]['price'];
}
array_push($markets, $new_item);
}
}
} else if(isset($market_info['quote']) && isset($market_info['quoteVolume'])) {
if($symbol == $market_info['quote']) {
if(isset($ccxt_price[$symbol]) && isset($ccxt_price[$market_info['base']]) && $market_info['quoteVolume']) {
$new_item = $market_info;
$new_item['exchange'] = $exchange_id;
$new_item['market'] = $market_symbol;
$new_item['volume'] = $market_info['quoteVolume'] * $ccxt_price[$symbol]['price'];
$new_item['price'] = (1.0 / $market_info['price']) * $ccxt_price[$market_info['base']]['price'];
if(isset($ccxt_past_ticker_list[$exchange_id][$market_symbol]) && isset($ccxt_past_price_list['currency'][$symbol])) {
$new_item['past'] = $ccxt_past_ticker_list[$exchange_id][$market_symbol]['quoteVolume'] * $ccxt_past_price_list['currency'][$symbol]['price'];
}
array_push($markets, $new_item);
}
}
}
}
}
$return_collate = collect($markets);
$final_coolate = $return_collate->map(function($value, $key) {
return $value;
});
$sorted_collet = $final_coolate->sortByDesc('volume')->values();
return response()->json($sorted_collet->all());
}
public function gethistory(Request $request, $currency)
{
$symbol = strtoupper($currency);
// Open json file from Ccxt api result
$last_row = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first();
$json_path = $last_row->name;
$json_datetime = $last_row->datetime;
$lastDatetime = DateTime::createFromFormat('Y-m-d H:i:s', $json_datetime);
$past_datetime = $lastDatetime->sub(new DateInterval('PT1M'));
$past_datetime_str = $past_datetime->format('Y-m-d H:i:s');
$history_limit = 20;
$history = [];
for($index = 0; $index < $history_limit; $index++ ) {
$history_row = DB::table('ccxt_price_list')->where('datetime', '>=', $past_datetime_str)->orderBy('datetime', 'asc')->first();
if(!is_null($history_row)) {
$history_json_path = $history_row->name;
$history_json_datetime = $history_row->datetime;
$real_path = base_path() . "\\json\\ccxt\\price${history_json_path}";
$ccxt_price_list = json_decode(file_get_contents($real_path), true);
$new_item = [];
$new_item['price'] = $ccxt_price_list['currency'][$symbol]['price'];
$new_item['volume'] = $ccxt_price_list['currency'][$symbol]['volume'];
$new_item['btc'] = $ccxt_price_list['currency']['BTC']['price'];
$new_item['datetime'] = $history_json_datetime;
array_push($history, $new_item);
// $past_datetime = $past_datetime->sub(new DateInterval('P1D'));
$past_datetime = $past_datetime->sub(new DateInterval('PT1H'));
$past_datetime_str = $past_datetime->format('Y-m-d H:i:s');
} else {
break;
}
}
$sorted_collet = collect($history);
return response()->json($sorted_collet->all());
}
public function getchartdata(Request $request, $currency)
{
$symbol = strtoupper($currency);
$export_data = [];
$price_chart = [];
$volume_chart = [];
$price_chart_data = [];
$volume_chart_data = [];
$datetime_chart_data = [];
$last_datetime = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->first()->datetime;
$last_datetime_value = DateTime::createFromFormat('Y-m-d H:i:s', $last_datetime);
$past_datetime = $last_datetime_value->sub(new DateInterval('PT12H'));
$past_datetime_str = $past_datetime->format('Y-m-d H:i:s');
$fetch_rows = DB::table('ccxt_price_list')->where('datetime', '>=', $past_datetime_str)->orderBy('datetime', 'asc')->get();
// $fetch_rows = DB::table('ccxt_price_list')->orderBy('datetime', 'desc')->take(10)->get();
foreach($fetch_rows as $row) {
$json_path = $row->name;
$datetime = $row->datetime;
// open json file
$real_path = base_path() . "\\json\\ccxt\\price${json_path}";
$ccxt_list = json_decode(file_get_contents($real_path), true);
//$datetime_item = strtotime($datetime);
// $datetime_value = date_create($datetime);
$datetime_value = DateTime::createFromFormat('Y-m-d H:i:s', $datetime);
$datetime_item = $datetime_value->format('Y-m-d\TH:i:s');
// get currency price information
// $btc_price = $ccxt_list['BTC']['price'];
$ccxt_result = isset($ccxt_list['currency'][$symbol]) ? $ccxt_list['currency'][$symbol] : null;
// if($symbol != 'BTC') {
// $price_item = is_null($ccxt_result) ? null : $ccxt_result['price'] * $btc_price;
// $volume_item = is_null($ccxt_result) ? null : $ccxt_result['volume'] * $btc_price;
// }
// else {
// $price_item = is_null($ccxt_result) ? null : $ccxt_result['price'];
// $volume_item = is_null($ccxt_result) ? null : $ccxt_result['volume'];
// }
$price_item = is_null($ccxt_result) ? null : $ccxt_result['price'];
$volume_item = is_null($ccxt_result) ? null : $ccxt_result['volume'];
array_push($price_chart_data, $price_item);
array_push($volume_chart_data, $volume_item);
array_push($datetime_chart_data, $datetime_item);
}
$price_chart['name'] = 'price ($)';
$price_chart['data'] = $price_chart_data;
$volume_chart['name'] = 'volume';
$volume_chart['data'] = $volume_chart_data;
$datetime_chart['type'] = 'datetime';
$datetime_chart['categories'] = $datetime_chart_data;
$series = [];
array_push($series, $price_chart);
//array_push($series, $volume_chart);
$export_data = [];
array_push($export_data, $series);
array_push($export_data, $datetime_chart);
return response()->json($export_data);
}
public function getitemchartdata(Request $request, $currency)
{
$symbol = strtoupper($currency);
$price_chart_data = [];
$fetch_rows = DB::table('ccxt_price_list')->orderBy('datetime', 'asc')->take(50)->get();
foreach($fetch_rows as $row) {
$json_path = $row->name;
$datetime = $row->datetime;
// open json file
$real_path = base_path() . "\\json\\ccxt\\price${json_path}";
$ccxt_list = json_decode(file_get_contents($real_path), true);
//$datetime_item = strtotime($datetime);
$datetime_value = date_create($datetime);
$datetime_item = $datetime_value->format('Y-m-d\TH:i:s');
// get currency price information
$btc_price = $ccxt_list['BTC']['price'];
$ccxt_result = isset($ccxt_list[$symbol]) ? $ccxt_list[$symbol] : null;
if($symbol != 'BTC') {
$price_item = is_null($ccxt_result) ? null : $ccxt_result['price'] * $btc_price;
$volume_item = is_null($ccxt_result) ? null : $ccxt_result['volume'] * $btc_price;
}
else {
$price_item = is_null($ccxt_result) ? null : $ccxt_result['price'];
$volume_item = is_null($ccxt_result) ? null : $ccxt_result['volume'];
}
array_push($price_chart_data, $price_item);
}
return response()->json(collect($price_chart_data)->all());
}
private function get_exchange_list($symbol)
{
return null; // CurrencyTicker::where('symbol', $symbol)->get();
}
}