stack_wallet/lib/models/token_balance.dart

63 lines
2.1 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:stackwallet/models/balance.dart';
2023-04-06 21:24:56 +00:00
import 'package:stackwallet/utilities/amount/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;
@override
String toJsonIgnoreCoin() => jsonEncode({
2023-03-01 20:03:44 +00:00
"contractAddress": contractAddress,
2023-04-07 20:13:28 +00:00
"total": total.toJsonString(),
"spendable": spendable.toJsonString(),
"blockedTotal": blockedTotal.toJsonString(),
"pendingSpendable": pendingSpendable.toJsonString(),
});
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,
),
);
}
}