cake_wallet/cw_ethereum/lib/ethereum_balance.dart

35 lines
979 B
Dart
Raw Normal View History

2023-01-04 14:51:23 +00:00
import 'dart:convert';
2022-12-28 15:02:04 +00:00
import 'package:cw_core/balance.dart';
2023-01-04 14:51:23 +00:00
import 'package:web3dart/web3dart.dart';
2022-12-28 15:02:04 +00:00
class EthereumBalance extends Balance {
2023-01-05 19:05:44 +00:00
EthereumBalance({required int available, required int additional}) : super(available, additional);
2022-12-28 15:02:04 +00:00
@override
2023-01-04 14:51:23 +00:00
String get formattedAdditionalBalance {
return EtherAmount.fromUnitAndValue(EtherUnit.ether, additional.toString())
.getInEther
.toString();
}
2022-12-28 15:02:04 +00:00
@override
2023-01-04 14:51:23 +00:00
String get formattedAvailableBalance =>
EtherAmount.fromUnitAndValue(EtherUnit.ether, available.toString()).getInEther.toString();
String toJSON() => json.encode({'available': available, 'additional': additional});
2023-01-05 19:05:44 +00:00
static EthereumBalance? fromJSON(String? jsonSource) {
if (jsonSource == null) {
return null;
}
final decoded = json.decode(jsonSource) as Map;
return EthereumBalance(
available: decoded['available'] as int? ?? 0,
additional: decoded['additional'] as int? ?? 0,
);
}
2023-01-04 14:51:23 +00:00
}