定制 tetthys/crypto-price 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tetthys/crypto-price

最新稳定版本:0.0.1

Composer 安装命令:

composer require tetthys/crypto-price

包简介

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

README 文档

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

统计信息

  • 总下载量: 28
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-19