cake_wallet/lib/bitcoin/bitcoin_balance.dart

59 lines
1.7 KiB
Dart
Raw Normal View History

2020-06-20 07:10:00 +00:00
import 'dart:convert';
import 'package:cake_wallet/entities/balance_display_mode.dart';
2020-05-12 12:04:54 +00:00
import 'package:flutter/foundation.dart';
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/balance.dart';
2020-05-12 12:04:54 +00:00
class BitcoinBalance extends Balance {
const BitcoinBalance({@required this.confirmed, @required this.unconfirmed})
2020-12-17 13:39:21 +00:00
: super(const [
BalanceDisplayMode.availableBalance,
BalanceDisplayMode.fullBalance
]);
2020-06-20 07:10:00 +00:00
factory BitcoinBalance.fromJSON(String jsonSource) {
if (jsonSource == null) {
return null;
}
final decoded = json.decode(jsonSource) as Map;
return BitcoinBalance(
confirmed: decoded['confirmed'] as int ?? 0,
unconfirmed: decoded['unconfirmed'] as int ?? 0);
}
2020-05-12 12:04:54 +00:00
final int confirmed;
final int unconfirmed;
2020-06-20 07:10:00 +00:00
int get total => confirmed + unconfirmed;
2020-12-18 15:49:10 +00:00
int get availableBalance =>
(confirmed ?? 0) + (unconfirmed < 0 ? unconfirmed : 0);
2020-06-20 07:10:00 +00:00
2020-05-12 12:04:54 +00:00
String get confirmedFormatted => bitcoinAmountToString(amount: confirmed);
2020-06-20 07:10:00 +00:00
2020-05-12 12:04:54 +00:00
String get unconfirmedFormatted => bitcoinAmountToString(amount: unconfirmed);
2020-06-20 07:10:00 +00:00
2020-05-12 12:04:54 +00:00
String get totalFormatted => bitcoinAmountToString(amount: total);
2020-06-20 07:10:00 +00:00
2020-12-18 15:49:10 +00:00
String get availableBalanceFormatted =>
bitcoinAmountToString(amount: availableBalance);
@override
String formattedBalance(BalanceDisplayMode mode) {
switch (mode) {
case BalanceDisplayMode.fullBalance:
return totalFormatted;
case BalanceDisplayMode.availableBalance:
2020-12-18 15:49:10 +00:00
return availableBalanceFormatted;
default:
return null;
}
}
2020-06-20 07:10:00 +00:00
String toJSON() =>
json.encode({'confirmed': confirmed, 'unconfirmed': unconfirmed});
2020-05-12 12:04:54 +00:00
}