2023-01-12 18:15:28 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-04-06 21:24:56 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-01-11 18:17:29 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-04-05 22:06:31 +00:00
|
|
|
|
|
|
|
enum Unit {
|
|
|
|
base,
|
|
|
|
u,
|
|
|
|
m,
|
|
|
|
normal;
|
|
|
|
}
|
2023-01-11 18:17:29 +00:00
|
|
|
|
|
|
|
class Balance {
|
|
|
|
final Coin coin;
|
2023-04-05 22:06:31 +00:00
|
|
|
final Amount total;
|
|
|
|
final Amount spendable;
|
|
|
|
final Amount blockedTotal;
|
|
|
|
final Amount pendingSpendable;
|
2023-01-11 18:17:29 +00:00
|
|
|
|
|
|
|
Balance({
|
|
|
|
required this.coin,
|
|
|
|
required this.total,
|
|
|
|
required this.spendable,
|
|
|
|
required this.blockedTotal,
|
|
|
|
required this.pendingSpendable,
|
|
|
|
});
|
|
|
|
|
2023-04-05 22:06:31 +00:00
|
|
|
// Decimal getTotal({bool includeBlocked = true}) => Format.satoshisToAmount(
|
|
|
|
// 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-04-05 22:06:31 +00:00
|
|
|
String toJsonIgnoreCoin() => jsonEncode({
|
|
|
|
"total": total.toJsonString(),
|
|
|
|
"spendable": spendable.toJsonString(),
|
|
|
|
"blockedTotal": blockedTotal.toJsonString(),
|
|
|
|
"pendingSpendable": pendingSpendable.toJsonString(),
|
|
|
|
});
|
2023-01-12 18:15:28 +00:00
|
|
|
|
2023-04-05 22:06:31 +00:00
|
|
|
// need to fall back to parsing from in due to cached balances being previously
|
|
|
|
// stored as int values instead of Amounts
|
2023-01-12 18:15:28 +00:00
|
|
|
factory Balance.fromJson(String json, Coin coin) {
|
|
|
|
final decoded = jsonDecode(json);
|
|
|
|
return Balance(
|
|
|
|
coin: coin,
|
2023-04-05 22:06:31 +00:00
|
|
|
total: decoded["total"] is String
|
|
|
|
? Amount.fromSerializedJsonString(decoded["total"] as String)
|
|
|
|
: Amount(
|
|
|
|
rawValue: BigInt.from(decoded["total"] as int),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
),
|
|
|
|
spendable: decoded["spendable"] is String
|
|
|
|
? Amount.fromSerializedJsonString(decoded["spendable"] as String)
|
|
|
|
: Amount(
|
|
|
|
rawValue: BigInt.from(decoded["spendable"] as int),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
),
|
|
|
|
blockedTotal: decoded["blockedTotal"] is String
|
|
|
|
? Amount.fromSerializedJsonString(decoded["blockedTotal"] as String)
|
|
|
|
: Amount(
|
|
|
|
rawValue: BigInt.from(decoded["blockedTotal"] as int),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
),
|
|
|
|
pendingSpendable: decoded["pendingSpendable"] is String
|
|
|
|
? Amount.fromSerializedJsonString(
|
|
|
|
decoded["pendingSpendable"] as String)
|
|
|
|
: Amount(
|
|
|
|
rawValue: BigInt.from(decoded["pendingSpendable"] as int),
|
|
|
|
fractionDigits: coin.decimals,
|
|
|
|
),
|
2023-01-12 18:15:28 +00:00
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|