Current path: home/webdevt/cryptoimpot.fr/tools/
?? Go up: /home/webdevt/cryptoimpot.fr
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of ServiceLedgio
*
* @author jimmy
*/
class Aspirateur {
protected $plusValue = 0;
protected $mapperFileKraken = array(
'time' => 'time',
'type' => 'type',
'pair' => 'pair',
'cost' => 'cost',
'vol' => 'vol',
'tradeId' => 'txid'
);
protected $mapperFileHitBTC = array(
'time' => 'Date (UTC)',
'type' => 'Side',
'pair' => 'Instrument',
'quantity' => 'Quantity',
'total' => 'Total',
'tradeId' => 'Trade ID'
);
protected $mapperFileBittrex = array(
'time' => 'TimeStamp',
'type' => 'OrderType',
'pair' => 'Exchange',
'quantity' => 'Quantity',
'total' => 'Price',
'tradeId' => 'Uuid'
);
protected $colInfosComplementaires = array(
"Date",
"Symbole acquisition",
"Quantite acquisition",
"Symbole cession",
"Quantite cession",
"Nom echange",
"ID transaction"
);
protected $mapperAsset = array(
'XDAO' => 'DAO',
'XETC' => 'ETC',
'XETH' => 'ETH',
'XLTC' => 'LTC',
'XMLN' => 'MLN',
'XNMC' => 'NMC',
'XREP' => 'REP',
'XXVN' => 'XVN',
'XICN' => 'ICN',
'XXBT' => 'BTC',
'XXDG' => 'XDG',
'XXLM' => 'XLM',
'XXMR' => 'XMR',
'XXTZ' => 'XTZ',
'XXRP' => 'XRP',
'XZEC' => 'ZEC',
'ZCAD' => 'CAD',
'ZEUR' => 'EUR',
'ZGBP' => 'GBP',
'ZJPY' => 'JPY',
'ZKRW' => 'KRW',
'ZUSD' => 'USD',
'KFEE' => 'FEE');
/**
*
* @var type
* array of asset with quantity
*/
protected $instantWallet = array();
/**
*
* @var type
* array containing fiat quantity invest
*/
protected $investFiat = ['EUR' => 0];
protected $ledgio = [];
protected $market_dir_base;
protected $fileLog;
public $arrayIndexDevise;
public function __construct($dateFrom, $dateTo, $asset) {
set_time_limit(0);
$this->arrayIndexDevise = array(
'ETH' => 1,
'BTC' => 2
);
$dsn = "mysql:host=localhost;dbname=ledgio";
$user = "ledgio";
$passwd = "jUH5DhzJrTvBxy88";
$this->pdo = new PDO($dsn, $user, $passwd);
$this->market_dir_base = './crypto-cours';
if (!is_dir($this->market_dir_base))
mkdir($this->market_dir_base, 0777, true);
$this->fileLog = $this->market_dir_base . '/' . $dateFrom . '-' . $dateTo . '-' . $asset;
$this->logMarket($dateFrom, $dateTo, $asset);
}
protected function logMarket($dateFrom, $dateTo, $asset) {
if (is_file( $this->fileLog)) {
unlink( $this->fileLog);
}
//$this->logLine('"id", "devise", "cours_euro", "date_euro"', $this->fileLog);
$begin = new DateTime($dateFrom);
$end = new DateTime($dateTo);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$i = 1;
foreach ($this->pdo->query('SELECT id FROM devise WHERE code = "'.$asset.'"', PDO::FETCH_ASSOC) as $row) {
$idAsset = $row['id'];
}
foreach ($period as $dt) {
$price = $this->getPrice($asset, $dt);
// echo $price; exit;
//var_dump($this->arrayIndexDevise);
//echo $this->arrayIndexDevise['ETH']; echo $asset; exit;
$str = $i++ .',"' . $idAsset .'","'.$price.'","'.$dt->format('Y-m-d H:i:s').'"';
//echo $str;
$this->logLine($str, $this->fileLog);
// $sql = "INSERT INTO users (name, surname, sex) VALUES (?,?,?)";
// $pdo->prepare($sql)->execute($data);
//
// verifier si enregistrement devise_id et date existe deja
$result = $this->pdo->query('SELECT id FROM `cours_devise` WHERE `devise_id` = "'.$idAsset.'" AND `date_cours` = "'. $dt->format('Y-m-d H:i:s').'"', PDO::FETCH_ASSOC);
if (empty($result->fetchColumn())) {
$this->pdo->query('INSERT INTO `cours_devise` (`id`, `devise_id`, `cours_euro`, `date_cours`) VALUES (NULL, ' .$idAsset . ', ' . $price . ', "' . $dt->format('Y-m-d H:i:s') .'");');
echo('INSERT INTO `cours_devise` (`id`, `devise_id`, `cours_euro`, `date_cours`) VALUES (NULL, ' .$idAsset . ', ' . $price . ', "' . $dt->format('Y-m-d H:i:s') .'");');
} else {
// var_dump($result->fetchColumn()); exit;
echo 'deja present';
}
}
}
protected function logLine($str, $file) {
// var_dump($file); exit;
file_put_contents($file, $str . PHP_EOL, FILE_APPEND | LOCK_EX);
}
public function getPrice($asset, \DateTime $dateTime) {
//@todo call data market
//$value = 0;
/*
tryConversion If set to false, it will try to get only direct trading values [ Default - true]
fsym REQUIRED The cryptocurrency symbol of interest [ Min length - 1] [ Max length - 10]
tsyms REQUIRED Comma separated cryptocurrency symbols list to convert into [ Min length - 1] [ Max length - 30]
ts The unix timestamp of interest
e The exchange to obtain data from (our aggregated average - CCCAGG - by default) [ Min length - 2] [ Max length - 30] [ Default - CCCAGG]
extraParams The name of your application (we recommend you send it) [ Min length - 1] [ Max length - 2000] [ Default - NotAvailable]
calculationType Type of average to calculate (Close - a Close of the day close price, MidHighLow - the average between the 24 H high and low, VolFVolT - the total volume to / the total volume from) [ Min length - 2] [ Max length - 30] [ Default - Close]
sign */
//$date = new DateTime($currentTime);
//$datestr = $date->format('Y-m-d');
//var_dump($currentTime);
$urlApi = 'https://min-api.cryptocompare.com/data/pricehistorical?';
// asset fsym
$urlApi .= 'fsym=' . $asset;
$urlApi .= '&tsyms=EUR';
$urlApi .= '&ts=' . $dateTime->format('U');
$urlApi .= '&api_key=fd3aea02118639009c4a4de12b5f8a0f4619b08a814e04e9fa0aafc678127ea2';
// fetch data
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $urlApi);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
curl_close($curl);
// decode to array
$data = json_decode($rawData, true);
if (!isset($data[$asset])) {
//throw new Exception('Market value not found');
// var_dump($asset);
// exit;
$url = 'https://pro-api.coinmarketcap.com/v1/tools/price-conversion';
$parameters = [
'id' => '1',
'amount' => '1',
'convert' => 'EUR'
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: 5922b05c-4c84-47c6-a462-f303e6c209be'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response)); // print json decoded response
curl_close($curl); // Close request
exit;
}
$price = $data[$asset]['EUR'];
/*
$arrayMarket = array(
'2019-04-09' => array('XXBT' => 3300, 'XETH' => 160, 'XLTC' => 290),
'2019-04-10' => array('XXBT' => 4000, 'XETH' => 190, 'XLTC' => 295),
'2019-04-11' => array('XXBT' => 4100, 'XETH' => 410, 'XLTC' => 300),
'2019-04-12' => array('XXBT' => 4050, 'XETH' => 480, 'XLTC' => 300),
'2019-04-13' => array('XXBT' => 4200, 'XETH' => 600, 'XLTC' => 320),
'2019-04-14' => array('XXBT' => 4230, 'XETH' => 250, 'XLTC' => 400),
'2019-04-15' => array('XXBT' => 3900, 'XETH' => 200, 'XLTC' => 500),
'2019-04-16' => array('XXBT' => 3000, 'XETH' => 450, 'XLTC' => 510),
'2019-04-17' => array('XXBT' => 2500, 'XETH' => 400, 'XLTC' => 520),
);
if (empty($arrayMarket[$datestr]) || empty($arrayMarket[$datestr][$asset])) {
throw new Exception('Market value not found');
}
* */
//$currentTime
/*
BTC ETH LTC
3300 160 290
4000 190 295
4100 410 300
4050 480 300
4200 600 320
4230 250 400
3900 200 500
3000 450 510
2500 400 520
* */
// if ($asset === 'XETH'){
// $value = 160;
// }
return $price;
}
}