2023-02-27 16:01:06 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:decimal/decimal.dart';
|
|
|
|
import 'package:stackwallet/models/balance.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/format.dart';
|
|
|
|
|
|
|
|
class TokenBalance extends Balance {
|
|
|
|
TokenBalance({
|
|
|
|
required this.contractAddress,
|
|
|
|
required this.decimalPlaces,
|
|
|
|
required super.total,
|
|
|
|
required super.spendable,
|
|
|
|
required super.blockedTotal,
|
|
|
|
required super.pendingSpendable,
|
|
|
|
super.coin = Coin.ethereum,
|
|
|
|
});
|
|
|
|
|
|
|
|
final String contractAddress;
|
|
|
|
final int decimalPlaces;
|
|
|
|
|
|
|
|
@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,
|
2023-02-27 16:01:06 +00:00
|
|
|
"decimalPlaces": decimalPlaces,
|
|
|
|
"total": total,
|
|
|
|
"spendable": spendable,
|
|
|
|
"blockedTotal": blockedTotal,
|
|
|
|
"pendingSpendable": pendingSpendable,
|
|
|
|
});
|
|
|
|
|
|
|
|
factory TokenBalance.fromJson(
|
|
|
|
String json,
|
|
|
|
) {
|
|
|
|
final decoded = jsonDecode(json);
|
|
|
|
return TokenBalance(
|
2023-03-01 20:03:44 +00:00
|
|
|
contractAddress: decoded["contractAddress"] as String,
|
2023-02-27 16:01:06 +00:00
|
|
|
decimalPlaces: decoded["decimalPlaces"] as int,
|
|
|
|
total: decoded["total"] as int,
|
|
|
|
spendable: decoded["spendable"] as int,
|
|
|
|
blockedTotal: decoded["blockedTotal"] as int,
|
|
|
|
pendingSpendable: decoded["pendingSpendable"] as int,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|