tetthys/crypto-price

Ultra-light crypto price service (USD spot + converters) for PHP 8.3+.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/tetthys/crypto-price

0.0.1 2025-10-19 13:32 UTC

This package is auto-updated.

Last update: 2025-10-19 13:34:02 UTC


README

Lightweight, framework-agnostic crypto price utility for PHP 8.3+.
Provides simple USD spot price access, crypto-to-USD conversion, and scale-aware math using BCMath.

📦 Installation

composer require tetthys/crypto-price

Requires PHP 8.3+ and the bcmath extension.

🚀 Quick Example

use Tetthys\CryptoPrice\Providers\CallableCryptoPriceProvider;
use Tetthys\CryptoPrice\Services\CryptoPriceService;

$provider = new CallableCryptoPriceProvider(
    fn(string $symbol) => match (strtoupper($symbol)) {
        'BTC' => '98765.4321',
        'ETH' => '3456.78',
        'XMR' => '150.50',
        default => throw new RuntimeException("Unknown symbol: {$symbol}")
    }
);

$service = new CryptoPriceService($provider);

echo $service->usd('BTC'); // "98765.4321"
echo $service->cryptoToUsd('0.015', 'BTC'); // "1481.48"
echo $service->usdToCrypto('1000', 'BTC');  // "0.01012345"

🧩 Core Concepts

CryptoPriceProviderInterface

Defines a source that returns USD spot price per crypto symbol.

public function getUsdPrice(string $symbol): string;

Example:

  • getUsdPrice('BTC')"98765.4321"
  • getUsdPrice('ETH')"3456.78"

CryptoPriceService

A high-level service that performs conversions and handles decimal precision per asset.

$service = new CryptoPriceService($provider);

$usd  = $service->cryptoToUsd('0.005', 'BTC'); // "493.82"
$btc  = $service->usdToCrypto('100', 'BTC');   // "0.0010132"
$spot = $service->usd('BTC');                  // "98765.4321"

All math uses BCMath to ensure fixed-scale precision.

CryptoScale

A static mapping of default decimal scales per symbol:

Symbol Default scale
BTC 8
LTC 8
XMR 12
ETH 8
USDT 6

Use CryptoScale::get('BTC') to retrieve the default scale.

CallableCryptoPriceProvider

A minimal provider that wraps a closure. Perfect for tests or static pricing environments.

$provider = new CallableCryptoPriceProvider(
    fn(string $symbol) => ['BTC' => '98765.43', 'ETH' => '3456.78'][$symbol] ?? '0'
);

⚙️ Integrating with Laravel

You can easily wire this package into Laravel using a small provider:

use App\Support\CryptoPrice\CoinMarketCapProvider;
use Tetthys\CryptoPrice\Services\CryptoPriceService;

$provider = new CoinMarketCapProvider(apiKey: env('COINMARKETCAP_API_KEY'));
$service  = new CryptoPriceService($provider);

echo $service->cryptoToUsd('0.5', 'ETH');

or register both in a custom CryptoPriceServiceProvider:

$this->app->singleton(CryptoPriceProviderInterface::class, fn () =>
    new CoinMarketCapProvider(env('COINMARKETCAP_API_KEY'))
);

$this->app->bind(CryptoPriceService::class, fn ($app) =>
    new CryptoPriceService($app->make(CryptoPriceProviderInterface::class))
);

🧪 Testing with a Mock Provider

use Tetthys\CryptoPrice\Providers\CallableCryptoPriceProvider;
use Tetthys\CryptoPrice\Services\CryptoPriceService;

$mock = new CallableCryptoPriceProvider(fn($s) => match ($s) {
    'BTC' => '10000', 'ETH' => '1000', default => '1'
});

$svc = new CryptoPriceService($mock);
expect($svc->cryptoToUsd('0.5', 'BTC'))->toBe('5000.00');

🪪 License

MIT © Tetthys Labs