Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function importDepositsB($userid)
- {
- // This get's the API key combination
- if(Key::where([['userid', '=', $userid], ['exchange', '=', 'Bittrex']])->exists())
- {
- $key = Key::where([['userid', '=', $userid], ['exchange', '=', 'Bittrex']])->first();
- } else
- {
- event(new PushEvent('No API keys found, please add your Bittrex API keys.', 'error', $userid));
- // This is a cache key which makes sure that not more than 1 import is running at a time
- Cache::forget('Import-Bittrex2'.$userid);
- die("No API keys found, please add your Bittrex API keys.");
- }
- // Decrypt the API keys
- $apikey = decrypt($key->public);
- $apisecret = decrypt($key->private);
- // This gets the data from the API.
- $client = new \GuzzleHttp\Client();
- try{
- $nonce=time();
- $uri='https://bittrex.com/api/v1.1/account/getdeposithistory?apikey='.$apikey.'&nonce='.$nonce;
- $sign=hash_hmac('sha512',$uri,$apisecret);
- $res = $client->request('GET', $uri, [
- 'headers' => [
- 'apisign' => $sign,
- ]]);
- $response = $res->getBody();
- $deposits = json_decode($response, true);
- if($deposits['success'] == "true")
- {
- // Send a push notification to the user that the import has started
- event(new PushEvent('Your import has started!', 'success', $userid));
- // Loop through the data we received from the API.
- foreach($deposits['result'] as $d)
- {
- $ddate = strtotime($d['LastUpdated']);
- $newformat = date('Y-m-d H:i:s', $ddate);
- $newformat2 = date('Y-m-d', $ddate);
- // This renames the Currency to work properly with updates names and such.
- if($d['Currency'] == "ANS")
- {
- $d['Currency'] = "NEO";
- } elseif($d['Currency'] == "BCC") {
- $d['Currency'] = "BCH";
- } elseif($d['Currency'] == "SEC") {
- $d['Currency'] = "SAFEX";
- }
- // This makes sure that the imported data has not been processed in a previous import.
- if(!Deposit::where('txid', $userid . $d['TxId'])->exists())
- {
- // Get bitcoin price of day:
- $historical = History::getHistorical($newformat);
- $btc_usd = $historical->USD;
- $btc_eur = $historical->USD * Multiplier::where('currency', 'EUR')->select('price')->first()->price;
- $btc_gbp = $historical->USD * Multiplier::where('currency', 'GBP')->select('price')->first()->price;
- $btc_usdt = $historical->USD;
- $btc_eth = $historical->ETH;
- $value = 0;
- // This gets historical prices
- $client = new \GuzzleHttp\Client();
- $res = $client->request('GET', 'https://poloniex.com/public?command=returnChartData¤cyPair=BTC_'.$d['Currency'].'&start='.strtotime($newformat2).'&end='.strtotime($newformat2).'&period=14400');
- $response = $res->getBody();
- $prices = json_decode($response, true);
- if($d['Currency'] != "BTC")
- {
- foreach($prices as $price)
- {
- if(is_array($prices) && isset($price['open']))
- {
- $value = $price['open'];
- } else {
- $value = 0;
- }
- }
- } else {
- $value = 1;
- }
- // Insert the deposit to the database
- $deposit = new Deposit;
- $deposit->userid = $userid;
- $deposit->exchange = "Bittrex";
- $deposit->txid = $userid . $d['TxId'];
- $deposit->date = $newformat;
- $deposit->currency = $d['Currency'];
- $deposit->amount = $d['Amount'];
- $deposit->btc_price_deposit_usd = $btc_usd;
- $deposit->btc_price_deposit_eur = $btc_eur;
- $deposit->btc_price_deposit_gbp = $btc_gbp;
- $deposit->btc_price_deposit_eth = $btc_eth;
- $deposit->btc_price_deposit_usdt = $btc_usdt;
- $deposit->price = $value;
- $deposit->save();
- // This checks if a balance exists, if it doesn't we create a new balance
- if(!Balance::where([['userid', '=', $userid], ['currency', '=', $d['Currency']], ['exchange', '=', 'Bittrex']])->exists())
- {
- $balance = new Balance;
- $balance->exchange = "Bittrex";
- $balance->userid = $userid;
- $balance->currency = $d['Currency'];
- $balance->amount = $d['Amount'];
- $balance->save();
- } else
- {
- $balance = Balance::where([['userid', '=', $userid], ['currency', '=', $d['Currency']], ['exchange', '=', 'Bittrex']])->first();
- $balance->amount += $d['Amount'];
- $balance->save();
- }
- }
- }
- } else {
- // Here is some fallback stuff
- if($deposits['message'] == "APIKEY_INVALID")
- {
- Cache::forget('Import-Bittrex2'.$userid);
- event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
- abort(403);
- } else {
- Cache::forget('Import-Bittrex2'.$userid);
- event(new PushEvent($deposits['message'], 'error', $userid));
- abort(403);
- }
- }
- } catch(\GuzzleHttp\Exception\RequestException $e){
- event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
- Cache::forget('Import-Bittrex2'.$userid);
- } catch(\GuzzleHttp\Exception\ClientException $e) {
- event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
- Cache::forget('Import-Bittrex2'.$userid);
- } catch(\GuzzleHttp\Exception\BadResponseException $e) {
- event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
- Cache::forget('Import-Bittrex2'.$userid);
- } catch(\GuzzleHttp\Exception\ServerException $e) {
- event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
- Cache::forget('Import-Bittrex2'.$userid);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement