moneyphp/crypto-currencies
PHP/Laravel-friendly crypto currency tools built on moneyphp: currency definitions, formatting and conversion helpers for common cryptocurrencies (e.g., BTC, ETH), plus utilities to integrate crypto amounts into Money objects and existing money workflows.
Start by installing the package via Composer: composer require moneyphp/crypto-currencies. Once installed, use it alongside moneyphp/money to instantiate crypto currencies consistently. The primary entry point is the CryptoCurrencies class, which provides static methods for common operations. For example:
use Money\Currency;
use Money\CryptoCurrencies;
// Get a predefined crypto currency (e.g., BTC)
$currency = CryptoCurrencies::BTC();
// Or fetch by code
$currency = CryptoCurrencies::get('ETH'); // Returns Currency object
All returned values are Money\Currency instances, meaning they integrate seamlessly with Money\Money for arithmetic and formatting. The first thing to do in your app is replace any hardcoded arrays like ['code' => 'BTC', 'subunits' => 8] with calls to CryptoCurrencies::.
Centralized domain model: Define services or factories that rely solely on CryptoCurrencies for currency validation and instantiation. For example, in an invoicing service:
public function createInvoice(string $currencyCode, string $amount): Invoice
{
$currency = CryptoCurrencies::get($currencyCode); // throws if invalid
return new Invoice($currency, Money::parse($amount, $currency));
}
Configuration-free discovery: Use CryptoCurrencies::all() to list all supported crypto currencies (e.g., for UI dropdowns) or bootstrapping admin panels.
Extensibility via context: For custom tokens (e.g., protocol-specific LP tokens), create a custom registry that wraps or extends CryptoCurrencies:
final class AppCurrencies
{
public static function myToken(): Currency
{
return new Currency('MYTKN', 18); // 18 decimals for EVM-compatible tokens
}
}
Validation guards: Pre-flight currency codes in API controllers or domain services using CryptoCurrencies::exists($code) before attempting processing.
Subunit precision matters: Crypto tokens often have non-standard precision (e.g., BTC=8, ETH=18). Confusing this with decimal places leads to off-by-100x errors. Always use CryptoCurrencies::get($code)->getSubUnits() — never assume.
Non-ISO codes: Unlike fiat, crypto codes aren’t ISO 4217. Accept that BTC, ETH, USDC, etc., are arbitrary but standardized de facto by this package. Don’t try to normalize them further.
Lookups fail silently unless guarded: CryptoCurrencies::get('xyz') throws InvalidArgumentException. Use exists() first or wrap in try/catch if code comes from user input.
Version alignment: Though the package is small, its definitions depend on community consensus. Pin to exact versions in production to avoid breaking changes from added/renamed tokens.
Extensibility via attributes: If extending with your own currencies, consider storing their metadata in config (config/currencies.php) and initializing Currency objects via a factory that merges with CryptoCurrencies — but avoid mixing static and dynamic definitions in core logic.
How can I help you explore Laravel packages today?