Exemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $intervals = [300, 900, 1800, 7200, 14400, 86400];
     $base = Wallet::where('base', true)->get();
     $all = Wallet::all();
     foreach ($base as $base_wallet) {
         foreach ($all as $market_wallet) {
             if ($market_wallet->id == $base_wallet->id) {
                 continue;
             }
             foreach ($intervals as $interval) {
                 OrdersTransaction::selectRaw('
                         FLOOR(MIN(unix_timestamp(`created_at`))/' . $interval . ')*' . $interval . ' AS date,
                         SUM(amount) AS volume,
                         SUBSTRING_INDEX(MIN(CONCAT(unix_timestamp(`created_at`), \'_\', price)), \'_\', -1) AS `open`,
                         MAX(price) AS high,
                         MIN(price) AS low,
                         SUBSTRING_INDEX(MAX(CONCAT(unix_timestamp(`created_at`), \'_\', price)), \'_\', -1) AS `close`
                       ')->where(function (\Illuminate\Database\Eloquent\Builder $query) use($base_wallet, $market_wallet) {
                     $query->where('wallet_id', $base_wallet->id)->where('want_wallet_id', $market_wallet->id);
                 })->where('created_at', '>=', DB::raw('DATE_SUB(NOW(), INTERVAL 24 HOUR)'))->groupBy(DB::raw('FLOOR(unix_timestamp(`created_at`)/' . $interval . ')'))->orderBy('created_at', 'asc')->chunk(100000, function ($orders) use($base_wallet, $market_wallet, $interval) {
                     foreach ($orders as $key => $order) {
                         try {
                             $cache = OrdersChart::firstOrNew(['wallet_id' => $base_wallet->id, 'want_wallet_id' => $market_wallet->id, 'date' => $order->date, 'interval' => $interval]);
                             $cache->date = $order->date;
                             $cache->open = $order->open;
                             $cache->high = $order->high;
                             $cache->low = $order->low;
                             $cache->close = $order->close;
                             $cache->volume = $order->volume;
                             $cache->wallet_id = $base_wallet->id;
                             $cache->want_wallet_id = $market_wallet->id;
                             $cache->interval = $interval;
                             $cache->save();
                         } catch (\Exception $e) {
                         }
                     }
                 });
             }
         }
     }
 }
Exemplo n.º 2
0
 public function doTask()
 {
     $txn_log = new Logger('TXN');
     $txn_log->pushHandler(new StreamHandler(storage_path() . '/logs/transactions_cron.log', Logger::INFO));
     $wallets = WalletModel::all();
     foreach ($wallets as $wallet) {
         $result = $wallet->command('listtransactions', ['*', 1000]);
         if (empty($result->error)) {
             foreach ($result as $transaction) {
                 if ($transaction->category == 'receive') {
                     if ($transaction->account > 0) {
                         $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' start', (array) $transaction);
                         $wallet_transaction = WalletsTransaction::where('txid', $transaction->txid)->where('wallet_id', $wallet->id)->first();
                         if (!$wallet_transaction) {
                             $wallet_transaction = new WalletsTransaction();
                             $wallet_transaction->user_id = $transaction->account;
                             $wallet_transaction->txid = $transaction->txid;
                             $wallet_transaction->wallet_id = $wallet->id;
                             $wallet_transaction->amount = $transaction->amount;
                             $wallet_transaction->confirmed = false;
                             $wallet_transaction->save();
                             Cache::tags('user' . $wallet_transaction->user_id)->forget('wallets');
                         }
                         if (!empty($transaction->blockhash)) {
                             $wallet_transaction->blockhash = $transaction->blockhash;
                         }
                         if ($wallet_transaction->confirmed) {
                             $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' already confirmed');
                             continue;
                         }
                         if ($transaction->confirmations >= $wallet->confirmations) {
                             if ($transaction->amount > 0) {
                                 $address = WalletsAddress::where('wallet_id', $wallet->id)->where(function (\Illuminate\Database\Eloquent\Builder $query) use($transaction) {
                                     $query->where('address', $transaction->address)->orWhere('user_id', $transaction->account);
                                 })->first();
                                 if ($address) {
                                     if ($address->user_id == $transaction->account) {
                                         $wallet_transaction->confirmed = true;
                                         $wallet_transaction->save();
                                         $address->deposit($transaction->amount);
                                         $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' good', (array) $address);
                                         Cache::tags('user' . $wallet_transaction->user_id)->forget('wallets');
                                     } else {
                                         $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' users doesnt match');
                                     }
                                 } else {
                                     $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' user not found');
                                 }
                             } else {
                                 $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' wrong amount');
                             }
                         } else {
                             $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' not enough confirmations(need ' . $wallet->confirmations . ')');
                         }
                     } else {
                         $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' account not defined');
                     }
                 } else {
                     $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' other category ' . $transaction->category);
                     $wallet_transaction = WalletsTransaction::where('txid', $transaction->txid)->where('wallet_id', $wallet->id)->first();
                     if ($wallet_transaction) {
                         if ($transaction->confirmations >= $wallet->confirmations) {
                             $wallet_transaction->confirmed = true;
                             $wallet_transaction->blockhash = $transaction->blockhash;
                             $wallet_transaction->save();
                             $txn_log->info('wal#' . $wallet->id . '(' . $wallet->short . ') trans#' . $transaction->txid . ' confirmed');
                         }
                     }
                 }
             }
         } else {
             $txn_log->error('wal#' . $wallet->id . '(' . $wallet->short . ') error', (array) $result);
         }
     }
 }