Skip to content

Commit

Permalink
Merge pull request #23 from ninja5pizza/block-height
Browse files Browse the repository at this point in the history
Cache the bitcoin block height
  • Loading branch information
mvdnbrk authored Dec 22, 2024
2 parents 3e60a37 + bcc4349 commit 7398336
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/Console/Commands/CacheBitcoinBlockHeightCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use App\Jobs\CacheBitcoinBlockHeight;
use Illuminate\Support\Facades\Cache;

class CacheBitcoinBlockHeightCommand extends Command
{
protected $signature = 'bitcoin:cache-height';

protected $description = 'Cache the Bitcoin block height';

public function handle()
{
CacheBitcoinBlockHeight::dispatchSync();

$height = Collection::make(
Cache::get('bitcoin_block_height', default: [])
)
->get('height', default: 'unknown');

$this->info('The current Bitcoin block height is '.$height);
}
}
33 changes: 33 additions & 0 deletions app/Jobs/CacheBitcoinBlockHeight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

class CacheBitcoinBlockHeight implements ShouldQueue
{
use Queueable;

protected string $apiUrl;

public function __construct()
{
$this->apiUrl = Str::of(config('services.blockcypher.base_url'))
->append('btc/main')
->toString();
}

public function handle(): void
{
$response = Http::acceptJson()
->get($this->apiUrl);

if ($response->successful()) {
Cache::put('bitcoin_block_height', $response->json());
}
}
}
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
'site_id' => env('FATHOM_SITE_ID'),
],

'blockcypher' => [
'base_url' => 'https://api.blockcypher.com/v1/',
],

'coinmarketcap' => [
'base_url' => 'https://pro-api.coinmarketcap.com/v1/',
'api_key' => env('COIN_MARKET_CAP_API_KEY'),
Expand Down
1 change: 1 addition & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Support\Facades\Schedule;

Schedule::command('bitcoin:cache-height')->everyMinute();
Schedule::command('bitcoin:cache-price')->everyFifteenMinutes();
Schedule::command('ninja:cache-stats')->everyFifteenMinutes();
Schedule::command('pets:cache-stats')->everyFifteenMinutes();
31 changes: 31 additions & 0 deletions tests/Unit/Jobs/CacheBitcoinBlockHeightTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Unit\Jobs;

use Tests\TestCase;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use App\Jobs\CacheBitcoinBlockHeight;
use Illuminate\Support\Facades\Cache;
use PHPUnit\Framework\Attributes\Test;

class CacheBitcoinBlockHeightTest extends TestCase
{
#[Test]
public function it_caches_the_bitcoin_block_height(): void
{
Http::fake([
'api.blockcypher.com/*' => Http::response([
'name' => 'BTC.main',
'height' => 123456,
], 200),
]);

CacheBitcoinBlockHeight::dispatch();

$this->assertTrue(Cache::has('bitcoin_block_height'));
$this->assertEquals(123456, Collection::make(
Cache::get('bitcoin_block_height'))->get('height')
);
}
}

0 comments on commit 7398336

Please sign in to comment.