tetthys/crypto-price
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
tetthys/crypto-price 适用场景与选型建议
tetthys/crypto-price 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tetthys/crypto-price 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tetthys/crypto-price 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-19