stack_wallet/lib/models/balance.dart

95 lines
2.8 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:convert';
2023-04-06 21:24:56 +00:00
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2023-04-05 22:06:31 +00:00
class Balance {
2023-04-05 22:06:31 +00:00
final Amount total;
final Amount spendable;
final Amount blockedTotal;
final Amount pendingSpendable;
Balance({
required this.total,
required this.spendable,
required this.blockedTotal,
required this.pendingSpendable,
});
factory Balance.zeroForCoin({required Coin coin}) {
final amount = Amount(
rawValue: BigInt.zero,
fractionDigits: coin.decimals,
);
return Balance(
total: amount,
spendable: amount,
blockedTotal: amount,
pendingSpendable: amount,
);
}
2023-04-05 22:06:31 +00:00
String toJsonIgnoreCoin() => jsonEncode({
"total": total.toJsonString(),
"spendable": spendable.toJsonString(),
"blockedTotal": blockedTotal.toJsonString(),
"pendingSpendable": pendingSpendable.toJsonString(),
});
2023-04-10 16:02:19 +00:00
// need to fall back to parsing from int due to cached balances being previously
2023-04-05 22:06:31 +00:00
// stored as int values instead of Amounts
2023-04-10 16:02:19 +00:00
factory Balance.fromJson(String json, int deprecatedValue) {
final decoded = jsonDecode(json);
return Balance(
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),
2023-04-10 16:02:19 +00:00
fractionDigits: deprecatedValue,
2023-04-05 22:06:31 +00:00
),
spendable: decoded["spendable"] is String
? Amount.fromSerializedJsonString(decoded["spendable"] as String)
: Amount(
rawValue: BigInt.from(decoded["spendable"] as int),
2023-04-10 16:02:19 +00:00
fractionDigits: deprecatedValue,
2023-04-05 22:06:31 +00:00
),
blockedTotal: decoded["blockedTotal"] is String
? Amount.fromSerializedJsonString(decoded["blockedTotal"] as String)
: Amount(
rawValue: BigInt.from(decoded["blockedTotal"] as int),
2023-04-10 16:02:19 +00:00
fractionDigits: deprecatedValue,
2023-04-05 22:06:31 +00:00
),
pendingSpendable: decoded["pendingSpendable"] is String
? Amount.fromSerializedJsonString(
decoded["pendingSpendable"] as String)
: Amount(
rawValue: BigInt.from(decoded["pendingSpendable"] as int),
2023-04-10 16:02:19 +00:00
fractionDigits: deprecatedValue,
2023-04-05 22:06:31 +00:00
),
);
}
Map<String, dynamic> toMap() => {
"total": total,
"spendable": spendable,
"blockedTotal": blockedTotal,
"pendingSpendable": pendingSpendable,
};
@override
String toString() {
return toMap().toString();
}
}