Skip to content

Amounts

The Transaction API counts in satoshis (100,000,000 to one BSV), and uses BigInt for every amount. This way arithmetic never loses precision, including on the web, where int is a JavaScript number.

// Every amount in the Transaction API is a BigInt number of satoshis.
final amount = BigInt.from(150000); // 0.0015 BSV
final output = TransactionOutput(amount, SVScript());
output.satoshis; // BigInt 150000

Coin wraps a satoshi amount, along with conversions and arithmetic. The Interpreter takes the spent amount as a Coin (Coin.ofSat(satoshis)). To read a BSV amount that a user typed, we use Coin.ofBtc with a Decimal from package:decimal:

// Coin wraps a satoshi amount. Decimal comes from package:decimal.
final price = Coin.ofBtc(Decimal.parse('0.0015')); // from a user-entered BSV amount
price.getValue(); // BigInt 150000 satoshis
final balance = Coin.ofSat(BigInt.from(12345678));
balance.toBtc().toString(); // 0.12345678
final remaining = balance.subtract(price); // 12195678 satoshis
remaining.isGreaterThan(Coin.CENT); // true: Coin.CENT is 1000000 satoshis

The constants are Coin.ZERO, Coin.SATOSHI, Coin.MICROCOIN, Coin.MILLICOIN, Coin.CENT and Coin.COIN.