2023-01-12 18:15:28 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-01-11 18:17:29 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/format.dart';
|
|
|
|
|
|
|
|
class Balance {
|
|
|
|
final Coin coin;
|
|
|
|
final int total;
|
|
|
|
final int spendable;
|
|
|
|
final int blockedTotal;
|
|
|
|
final int pendingSpendable;
|
|
|
|
|
|
|
|
Balance({
|
|
|
|
required this.coin,
|
|
|
|
required this.total,
|
|
|
|
required this.spendable,
|
|
|
|
required this.blockedTotal,
|
|
|
|
required this.pendingSpendable,
|
|
|
|
});
|
|
|
|
|
2023-03-08 18:48:43 +00:00
|
|
|
Decimal getTotal({bool includeBlocked = true}) => Format.satoshisToAmount(
|
2023-01-11 18:17:29 +00:00
|
|
|
includeBlocked ? total : total - blockedTotal,
|
|
|
|
coin: coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
Decimal getSpendable() => Format.satoshisToAmount(
|
|
|
|
spendable,
|
|
|
|
coin: coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
Decimal getPending() => Format.satoshisToAmount(
|
|
|
|
pendingSpendable,
|
|
|
|
coin: coin,
|
|
|
|
);
|
|
|
|
|
|
|
|
Decimal getBlocked() => Format.satoshisToAmount(
|
|
|
|
blockedTotal,
|
|
|
|
coin: coin,
|
|
|
|
);
|
2023-01-12 18:15:28 +00:00
|
|
|
|
2023-03-08 18:48:43 +00:00
|
|
|
String toJsonIgnoreCoin() => jsonEncode(toMap()..remove("coin"));
|
2023-01-12 18:15:28 +00:00
|
|
|
|
|
|
|
factory Balance.fromJson(String json, Coin coin) {
|
|
|
|
final decoded = jsonDecode(json);
|
|
|
|
return Balance(
|
|
|
|
coin: coin,
|
|
|
|
total: decoded["total"] as int,
|
|
|
|
spendable: decoded["spendable"] as int,
|
|
|
|
blockedTotal: decoded["blockedTotal"] as int,
|
|
|
|
pendingSpendable: decoded["pendingSpendable"] as int,
|
|
|
|
);
|
|
|
|
}
|
2023-03-08 18:48:43 +00:00
|
|
|
|
|
|
|
Map<String, dynamic> toMap() => {
|
|
|
|
"coin": coin,
|
|
|
|
"total": total,
|
|
|
|
"spendable": spendable,
|
|
|
|
"blockedTotal": blockedTotal,
|
|
|
|
"pendingSpendable": pendingSpendable,
|
|
|
|
};
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toMap().toString();
|
|
|
|
}
|
2023-01-11 18:17:29 +00:00
|
|
|
}
|