Skip to main content

mateusjatenee/crypto-tracker

48 stars
2 code files
View mateusjatenee/crypto-tracker on GitHub

app/Jobs/UpdateCryptoAssetPrice.php

Open in GitHub
use App\Models\Asset;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\MarketInformationProviders\Coinbase;
use Illuminate\Contracts\Queue\ShouldBeUnique;
 
class UpdateCryptoAssetPrice implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
 
public function __construct(public Asset $asset)
{
//
}
 
public function handle()
{
$freshAsset = resolve(Coinbase::class)->fetchAsset($this->asset);
 
if ($freshAsset->price == 0) {
return;
}
 
$this->asset->updateFromFreshAsset($freshAsset);
}
}

app/Console/Commands/FetchCoinsPrices.php

Open in GitHub
use App\Jobs\UpdateCryptoAssetPrice;
use App\Models\Asset;
use Illuminate\Console\Command;
 
class FetchCoinsPrices extends Command
{
protected $signature = 'coins:fetch-price';
 
protected $description = 'Command description';
 
protected $shouldQuit = false;
 
public function __construct()
{
parent::__construct();
}
 
public function handle()
{
$this->info('Fetching coin prices.');
 
$this->listenForSignals();
 
while (true) {
if ($this->shouldQuit) {
return false;
}
 
$this->fetchPrices();
sleep(20);
}
}
 
public function listenForSignals()
{
pcntl_signal(SIGTERM, function () {
$this->shouldQuit = true;
});
}
 
protected function fetchPrices()
{
$this->info('Fetching...');
Asset::crypto()->get()->each(function (Asset $asset) {
UpdateCryptoAssetPrice::dispatch($asset);
});
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.