Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.08 KB | None | 0 0
  1. public function importDepositsB($userid)
  2.     {
  3.       // This get's the API key combination
  4.       if(Key::where([['userid', '=', $userid], ['exchange', '=', 'Bittrex']])->exists())
  5.       {
  6.         $key = Key::where([['userid', '=', $userid], ['exchange', '=', 'Bittrex']])->first();
  7.       } else
  8.       {
  9.         event(new PushEvent('No API keys found, please add your Bittrex API keys.', 'error', $userid));
  10.  
  11.         // This is a cache key which makes sure that not more than 1 import is running at a time
  12.         Cache::forget('Import-Bittrex2'.$userid);
  13.         die("No API keys found, please add your Bittrex API keys.");
  14.       }
  15.  
  16.       // Decrypt the API keys
  17.       $apikey = decrypt($key->public);
  18.       $apisecret = decrypt($key->private);
  19.  
  20.       // This gets the data from the API.
  21.       $client = new \GuzzleHttp\Client();
  22.       try{
  23.       $nonce=time();
  24.       $uri='https://bittrex.com/api/v1.1/account/getdeposithistory?apikey='.$apikey.'&nonce='.$nonce;
  25.       $sign=hash_hmac('sha512',$uri,$apisecret);
  26.       $res = $client->request('GET', $uri, [
  27.       'headers' => [
  28.       'apisign' => $sign,
  29.       ]]);
  30.       $response = $res->getBody();
  31.       $deposits = json_decode($response, true);
  32.       if($deposits['success'] == "true")
  33.       {
  34.         // Send a push notification to the user that the import has started
  35.         event(new PushEvent('Your import has started!', 'success', $userid));
  36.  
  37.         // Loop through the data we received from the API.
  38.       foreach($deposits['result'] as $d)
  39.       {
  40.         $ddate = strtotime($d['LastUpdated']);
  41.         $newformat = date('Y-m-d H:i:s', $ddate);
  42.         $newformat2 = date('Y-m-d', $ddate);
  43.  
  44.         // This renames the Currency to work properly with updates names and such.
  45.         if($d['Currency'] == "ANS")
  46.         {
  47.           $d['Currency'] = "NEO";
  48.         } elseif($d['Currency'] == "BCC") {
  49.           $d['Currency'] = "BCH";
  50.         } elseif($d['Currency'] == "SEC") {
  51.           $d['Currency'] = "SAFEX";
  52.         }
  53.  
  54.         // This makes sure that the imported data has not been processed in a previous import.
  55.         if(!Deposit::where('txid', $userid . $d['TxId'])->exists())
  56.         {
  57.           // Get bitcoin price of day:
  58.             $historical = History::getHistorical($newformat);
  59.             $btc_usd = $historical->USD;
  60.             $btc_eur = $historical->USD * Multiplier::where('currency', 'EUR')->select('price')->first()->price;
  61.             $btc_gbp = $historical->USD * Multiplier::where('currency', 'GBP')->select('price')->first()->price;
  62.             $btc_usdt = $historical->USD;
  63.             $btc_eth = $historical->ETH;
  64.             $value = 0;
  65.  
  66.             // This gets historical prices
  67.             $client = new \GuzzleHttp\Client();
  68.             $res = $client->request('GET', 'https://poloniex.com/public?command=returnChartData&currencyPair=BTC_'.$d['Currency'].'&start='.strtotime($newformat2).'&end='.strtotime($newformat2).'&period=14400');
  69.             $response = $res->getBody();
  70.             $prices = json_decode($response, true);
  71.  
  72.             if($d['Currency'] != "BTC")
  73.             {
  74.               foreach($prices as $price)
  75.               {
  76.                 if(is_array($prices) && isset($price['open']))
  77.                 {
  78.                   $value = $price['open'];
  79.                 } else {
  80.                   $value = 0;
  81.                 }
  82.               }
  83.             } else {
  84.               $value = 1;
  85.             }
  86.  
  87.  
  88.  
  89.         // Insert the deposit to the database
  90.         $deposit = new Deposit;
  91.         $deposit->userid = $userid;
  92.         $deposit->exchange = "Bittrex";
  93.         $deposit->txid = $userid . $d['TxId'];
  94.         $deposit->date = $newformat;
  95.         $deposit->currency = $d['Currency'];
  96.         $deposit->amount = $d['Amount'];
  97.         $deposit->btc_price_deposit_usd = $btc_usd;
  98.         $deposit->btc_price_deposit_eur = $btc_eur;
  99.         $deposit->btc_price_deposit_gbp = $btc_gbp;
  100.         $deposit->btc_price_deposit_eth = $btc_eth;
  101.         $deposit->btc_price_deposit_usdt = $btc_usdt;
  102.         $deposit->price = $value;
  103.         $deposit->save();
  104.  
  105.         // This checks if a balance exists, if it doesn't we create a new balance
  106.         if(!Balance::where([['userid', '=', $userid], ['currency', '=', $d['Currency']], ['exchange', '=', 'Bittrex']])->exists())
  107.         {
  108.           $balance = new Balance;
  109.           $balance->exchange = "Bittrex";
  110.           $balance->userid = $userid;
  111.           $balance->currency = $d['Currency'];
  112.           $balance->amount = $d['Amount'];
  113.           $balance->save();
  114.         } else
  115.         {
  116.           $balance = Balance::where([['userid', '=', $userid], ['currency', '=', $d['Currency']], ['exchange', '=', 'Bittrex']])->first();
  117.           $balance->amount += $d['Amount'];
  118.           $balance->save();
  119.         }
  120.  
  121.         }
  122.  
  123.       }
  124.       } else {
  125.  
  126.         // Here is some fallback stuff
  127.         if($deposits['message'] == "APIKEY_INVALID")
  128.         {
  129.           Cache::forget('Import-Bittrex2'.$userid);
  130.           event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
  131.           abort(403);
  132.         } else {
  133.           Cache::forget('Import-Bittrex2'.$userid);
  134.           event(new PushEvent($deposits['message'], 'error', $userid));
  135.           abort(403);
  136.         }
  137.         }
  138.       } catch(\GuzzleHttp\Exception\RequestException $e){
  139.         event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
  140.         Cache::forget('Import-Bittrex2'.$userid);
  141.       } catch(\GuzzleHttp\Exception\ClientException $e) {
  142.         event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
  143.         Cache::forget('Import-Bittrex2'.$userid);
  144.       } catch(\GuzzleHttp\Exception\BadResponseException $e) {
  145.         event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
  146.         Cache::forget('Import-Bittrex2'.$userid);
  147.       } catch(\GuzzleHttp\Exception\ServerException $e) {
  148.         event(new PushEvent('No API keys found or invalid combination.', 'error', $userid));
  149.         Cache::forget('Import-Bittrex2'.$userid);
  150.       }
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement