Compare commits

..

2 Commits

Author SHA1 Message Date
hackerESQ 23ed2e7155 wip 2026-03-24 21:04:40 -05:00
hackerESQ e8997ecc3e Create BinanceMarketData.php 2026-03-24 19:01:09 -05:00
10 changed files with 1227 additions and 866 deletions
+1 -2
View File
@@ -8,8 +8,7 @@ on:
jobs:
build:
# runs-on: self-hosted
runs-on: ubuntu-latest
runs-on: self-hosted
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
+1 -1
View File
@@ -26,7 +26,7 @@ class EnsureDailyChangeIsSynced
) {
defer(fn () => $model->portfolio->syncDailyChanges());
Cache::put($cacheKey, true, now()->addMinutes(5));
Cache::put($cacheKey, now(), now()->addMinutes(5));
}
}
@@ -0,0 +1,141 @@
<?php
declare(strict_types=1);
namespace App\Interfaces\MarketData;
use App\Interfaces\MarketData\Types\Ohlc;
use App\Interfaces\MarketData\Types\Quote;
use Carbon\CarbonInterval;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BinanceMarketData implements MarketDataInterface
{
public PendingRequest $client;
public string $apiBaseUrl = 'https://data-api.binance.vision/api/v3/';
public function __construct()
{
$this->createNewClient();
}
private function createNewClient()
{
$this->client = Http::withOptions([
'headers' => [
'content-type' => 'application/json',
'accept' => 'application/json',
],
]);
}
public function exists(string $symbol): bool
{
return (bool) $this->quote($symbol);
}
public function quote(string $symbol): Quote
{
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters([
'symbol' => $symbol,
])->get('ticker');
$quote = $response->json();
throw_if(empty(Arr::get($quote, 'weightedAvgPrice')), NotFoundHttpException::class, "Symbol `{$symbol}` was not found");
$fundamental = cache()->remember(
'binance-fdmtl-'.$symbol,
1440,
function () use ($symbol) {
$this->createNewClient();
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters(['symbol' => $symbol])
->get('exchangeInfo');
return $response->json();
}
);
return new Quote([
'name' => $symbol,
'symbol' => $symbol,
'currency' => Arr::get($fundamental, 'symbols.0.quoteAsset'),
'market_value' => (float) Arr::get($quote, 'weightedAvgPrice'),
]);
}
public function dividends(string $symbol, $startDate, $endDate): Collection
{
// noop
return collect();
}
public function splits(string $symbol, $startDate, $endDate): Collection
{
// noop
return collect();
}
public function history(string $symbol, $startDate, $endDate): Collection
{
$startDate = Carbon::parse($startDate);
$endDate = Carbon::parse($endDate);
$allHistory = collect();
$chunks = 500;
$period = CarbonInterval::days($chunks)->toPeriod($startDate, $endDate);
foreach ($period as $startDate) {
$chunkEnd = $startDate->copy()->addDays($chunks - 1);
if ($chunkEnd->gt($endDate)) {
$chunkEnd = $endDate;
}
$this->createNewClient();
$response = $this->client
->baseUrl($this->apiBaseUrl)
->withQueryParameters([
'symbol' => $symbol,
'interval' => '1d',
'startTime' => $startDate->timestamp * 1000,
'endTime' => $chunkEnd->timestamp * 1000,
])->get('klines');
$history = $response->json();
throw_if(empty($history), NotFoundHttpException::class, "Symbol `{$symbol}` was not found");
$chunkedHistory = collect($history)
->mapWithKeys(function ($history_item) use ($symbol) {
$date = Carbon::parse($history_item[0])->format('Y-m-d');
return [$date => new Ohlc([
'symbol' => $symbol,
'date' => $date,
'close' => $history_item[4],
])];
});
$allHistory = $allHistory->merge($chunkedHistory);
}
return $allHistory;
}
}
+4 -4
View File
@@ -22,10 +22,10 @@
"investbrainapp/frankfurter-client": "dev-main",
"laravel/ai": "^0.2.5",
"laravel/fortify": "^1.30.0",
"laravel/framework": "^13.0",
"laravel/framework": "^12.0",
"laravel/sanctum": "^4.0",
"laravel/socialite": "^5.16",
"laravel/tinker": "^3.0",
"laravel/tinker": "^2.9",
"league/flysystem-aws-s3-v3": "^3.0",
"livewire/livewire": "^4.0",
"livewire/volt": "^1.6",
@@ -39,11 +39,11 @@
},
"require-dev": {
"fakerphp/faker": "^1.23",
"laravel/boost": "^2.0",
"laravel/boost": "^1.8",
"laravel/pint": "^1.25",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^12.0"
"phpunit/phpunit": "^11.0"
},
"repositories": [
{
Generated
+1075 -689
View File
File diff suppressed because it is too large Load Diff
-13
View File
@@ -107,17 +107,4 @@ return [
'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'),
/*
|--------------------------------------------------------------------------
| Cache Serializable Classes
|--------------------------------------------------------------------------
|
| For security, unserialization of cached PHP objects is restricted. Set
| this to false to disallow all object unserialization, or list the
| specific classes your application intentionally caches as objects.
|
*/
'serializable_classes' => false,
];
+3 -6
View File
@@ -2,9 +2,6 @@
declare(strict_types=1);
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;
use Laravel\Sanctum\Http\Middleware\AuthenticateSession;
use Laravel\Sanctum\Sanctum;
return [
@@ -80,9 +77,9 @@ return [
*/
'middleware' => [
'authenticate_session' => AuthenticateSession::class,
'encrypt_cookies' => EncryptCookies::class,
'validate_csrf_token' => PreventRequestForgery::class,
'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class,
'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class,
'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
],
];
File diff suppressed because one or more lines are too long
-75
View File
@@ -1,75 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Api;
use App\Models\MarketData;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MarketDataTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
public function test_can_get_market_data_for_symbol(): void
{
MarketData::getMarketData('AAPL');
$this->actingAs($this->user)
->getJson(route('api.market-data.show', ['symbol' => 'AAPL']))
->assertOk()
->assertJsonStructure([
'symbol',
'name',
'market_value',
'fifty_two_week_low',
'fifty_two_week_high',
'last_dividend_date',
'last_dividend_amount',
'dividend_yield',
'market_cap',
'trailing_pe',
'forward_pe',
'book_value',
'created_at',
'updated_at',
]);
}
public function test_market_data_returns_correct_symbol(): void
{
$this->actingAs($this->user)
->getJson(route('api.market-data.show', ['symbol' => 'ACME']))
->assertSuccessful()
->assertJsonFragment([
'symbol' => 'ACME',
]);
}
public function test_market_data_response_has_expected_fields(): void
{
MarketData::getMarketData('MSFT');
$this->actingAs($this->user)
->getJson(route('api.market-data.show', ['symbol' => 'MSFT']))
->assertOk()
->assertJsonPath('symbol', 'MSFT')
->assertJsonPath('market_value', 230.19);
}
public function test_cannot_access_market_data_when_unauthenticated(): void
{
$this->getJson(route('api.market-data.show', ['symbol' => 'AAPL']))->assertUnauthorized();
}
}
-74
View File
@@ -1,74 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Api;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
public function test_can_get_authenticated_user_profile(): void
{
$this->actingAs($this->user)
->getJson(route('api.me'))
->assertOk()
->assertJsonStructure([
'id',
'name',
'email',
'profile_photo_url',
'options' => ['display_currency', 'locale'],
'created_at',
'updated_at',
]);
}
public function test_profile_returns_correct_user_data(): void
{
$this->actingAs($this->user)
->getJson(route('api.me'))
->assertOk()
->assertJsonFragment([
'id' => $this->user->id,
'name' => $this->user->name,
'email' => $this->user->email,
]);
}
public function test_profile_returns_correct_options(): void
{
$this->actingAs($this->user)
->getJson(route('api.me'))
->assertOk()
->assertJsonPath('options.display_currency', $this->user->getCurrency())
->assertJsonPath('options.locale', $this->user->getLocale());
}
public function test_cannot_access_profile_when_unauthenticated(): void
{
$this->getJson(route('api.me'))->assertUnauthorized();
}
public function test_profile_does_not_expose_password(): void
{
$response = $this->actingAs($this->user)
->getJson(route('api.me'))
->assertOk();
$this->assertArrayNotHasKey('password', $response->json());
}
}