Цю сторінку ще не перекладено українською, тому нижче англійська версія.

Crypto currencies API

The crypto plugin works with popular crypto currencies: BTC, ETH, USDT, USDC, BNB, SOL, POL and TRX on Bitcoin, Ethereum, Solana, Polygon, Base, BNB Smart Chain and TRON. This first part gives the asset registry (the decimals of every coin and token on every chain), exact amount math and market rates with conversion. Payment gateways, blockchain data and watch-only wallets follow in later releases. Nothing runs until your code calls it; no library is needed (requests go out over curl).

Rates

$btc = crypto()->rate('BTC', 'USD');                 // '97050.5' (a string) or null
if ($btc === null) {
    echo _prepare_html(crypto()->last_error());     // why: too few fresh sources, the sources disagree...
}
$rates = crypto()->rates(['BTC', 'ETH', 'USDT'], 'EUR');   // ['BTC' => '...', 'ETH' => '...', 'USDT' => null]
$usd_in_btc = crypto()->rate('USD', 'BTC');         // the inverse, 18 decimals
$q = crypto()->quote('BTC', 'USD');                 // the whole picture: rate, spread_pct, used, sources, error

crypto() is _class('crypto') (yf_crypto). A rate is the median of the fresh quotes of several public sources (by default the Kraken and Binance tickers, with CoinGecko when one of them has no fresh price). No rate is given (null) when fewer than two sources have a quote younger than 300 seconds, or when the quotes spread more than 2 % around the median: a wrong price is worse than none. Answers of the sources are cached for 60 seconds, so a page that asks for many rates sends one request per source. Quote currencies are USD and EUR by default; a USD price from Binance is its USDT price.

MethodReturns
rate($symbol, $quote = 'USD')The median rate as a decimal string, or null; last_error() says why.
rates($symbols = null, $quote = 'USD')Symbol => rate or null; no symbols: every symbol the site asks the sources for.
quote($symbol, $quote)ok, rate, spread_pct, used (source names), error, and per source rate, age (seconds), fresh, fallback, error.
convert($amount, $from, $to, $opts = [])The converted amount as a decimal string, or null.
asset($id), assets(), chains()The registry: decimals, chain, contract (see below).
to_raw($amount, $asset), from_raw($raw, $asset)An amount in the smallest unit of an asset and back.
math()The decimal math (yf_crypto_math): add, sub, mul, div($a, $b, $scale, $mode), round, cmp, median, to_raw, from_raw.

Conversion

crypto()->convert('25.00', 'USD', 'USDT@eth');              // '24.996251'  (6 decimals, rounded up)
crypto()->convert('25.00', 'USD', 'USDT@bsc');              // '24.996250562415637655' (18 decimals on BNB Smart Chain)
crypto()->convert('25.00', 'USD', 'USDT@eth', ['raw' => true]);   // '24996251' (smallest unit)
crypto()->convert('0.001', 'BTC', 'USD');                   // '97.05' (cents, rounded down)
crypto()->convert('100000', 'BTC', 'USD', ['from_raw' => true]);  // 100000 satoshi in USD
crypto()->convert('1', 'ETH', 'BTC');                       // through USD

Amounts are decimal strings; a float is refused (it has already lost digits). Into an asset the result has the asset's decimals and is rounded up from fiat, so an amount a customer owes is never short; into fiat it has 2 decimals and is rounded down; between assets it is rounded down. Option round (down, up, half_up) changes that. Fiat to fiat is not converted here: the payment plugin's currency rates do that.

Assets and decimals

An asset id is SYMBOL@chain: BTC@btc, ETH@eth, USDT@tron, USDC@base. A native coin may be named by its symbol alone (BTC, ETH is ETH@eth); a token always needs its chain, because its decimals differ per chain:

ChainIdNative coinUSDTUSDC
BitcoinbtcBTC, 8
EthereumethETH, 1866
SolanasolSOL, 966
Polygon PoSpolygonPOL, 1866
BasebaseETH, 186
BNB Smart ChainbscBNB, 181818
TRONtronTRX, 66

asset('USDT@bsc') returns id, symbol, chain, decimals, kind (native or token), contract, contract_checked, stable and rate_symbol (the symbol its rate is read for: ETH on Base takes the ETH rate). Token contracts are empty until the site sets them after checking them against the issuer: code that needs a contract checks it is not empty. BUSD is not supported.

Exact amounts

$m = crypto()->math();
$m->add('0.1', '0.2');                  // '0.3'
$m->div('1', '3', 6, 'up');             // '0.333334'
crypto()->to_raw('1.5', 'USDT@bsc');    // '1500000000000000000'
crypto()->from_raw('1500000', 'USDT@eth');  // '1.500000'

Store amounts as strings or DECIMAL columns, never FLOAT. The math runs on the gmp or bcmath extension when PHP has one, else on plain PHP (exact, slower).

The plugin provides tools only. Whether a site may accept, hold or convert crypto assets, and which licences, KYC / AML duties, tax rules and sanctions screening apply, is the site owner's responsibility in their jurisdiction.