stack_wallet/lib/models/token_balance.dart

88 lines
2.7 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:stackwallet/models/balance.dart';
2023-04-05 22:06:31 +00:00
import 'package:stackwallet/utilities/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
class TokenBalance extends Balance {
TokenBalance({
required this.contractAddress,
required super.total,
required super.spendable,
required super.blockedTotal,
required super.pendingSpendable,
super.coin = Coin.ethereum,
});
final String contractAddress;
2023-04-05 22:06:31 +00:00
// @override
// Decimal getTotal({bool includeBlocked = false}) =>
// Format.satoshisToEthTokenAmount(
// includeBlocked ? total : total - blockedTotal,
// decimalPlaces,
// );
//
// @override
// Decimal getSpendable() => Format.satoshisToEthTokenAmount(
// spendable,
// decimalPlaces,
// );
//
// @override
// Decimal getPending() => Format.satoshisToEthTokenAmount(
// pendingSpendable,
// decimalPlaces,
// );
//
// @override
// Decimal getBlocked() => Format.satoshisToEthTokenAmount(
// blockedTotal,
// decimalPlaces,
// );
@override
String toJsonIgnoreCoin() => jsonEncode({
2023-03-01 20:03:44 +00:00
"contractAddress": contractAddress,
"total": total,
"spendable": spendable,
"blockedTotal": blockedTotal,
"pendingSpendable": pendingSpendable,
});
factory TokenBalance.fromJson(
String json,
2023-04-05 22:06:31 +00:00
int fractionDigits,
) {
final decoded = jsonDecode(json);
return TokenBalance(
2023-03-01 20:03:44 +00:00
contractAddress: decoded["contractAddress"] as String,
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: fractionDigits,
),
spendable: decoded["spendable"] is String
? Amount.fromSerializedJsonString(decoded["spendable"] as String)
: Amount(
rawValue: BigInt.from(decoded["spendable"] as int),
fractionDigits: fractionDigits,
),
blockedTotal: decoded["blockedTotal"] is String
? Amount.fromSerializedJsonString(decoded["blockedTotal"] as String)
: Amount(
rawValue: BigInt.from(decoded["blockedTotal"] as int),
fractionDigits: fractionDigits,
),
pendingSpendable: decoded["pendingSpendable"] is String
? Amount.fromSerializedJsonString(
decoded["pendingSpendable"] as String)
: Amount(
rawValue: BigInt.from(decoded["pendingSpendable"] as int),
fractionDigits: fractionDigits,
),
);
}
}