mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-26 13:39:39 +00:00
c6a26cac16
- Fix parsing of Ethereum amount - Add more Ethereum Nodes [skip ci]
41 lines
1.2 KiB
Dart
41 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:cw_core/balance.dart';
|
|
import 'package:web3dart/web3dart.dart';
|
|
|
|
class EthereumBalance extends Balance {
|
|
EthereumBalance(this.balance)
|
|
: super(balance.getValueInUnit(EtherUnit.wei).toInt(),
|
|
balance.getValueInUnit(EtherUnit.wei).toInt());
|
|
|
|
final EtherAmount balance;
|
|
|
|
@override
|
|
String get formattedAdditionalBalance {
|
|
final String formattedBalance = balance.getValueInUnit(EtherUnit.ether).toString();
|
|
return formattedBalance.substring(0, min(12, formattedBalance.length));
|
|
}
|
|
|
|
@override
|
|
String get formattedAvailableBalance {
|
|
final String formattedBalance = balance.getValueInUnit(EtherUnit.ether).toString();
|
|
return formattedBalance.substring(0, min(12, formattedBalance.length));
|
|
}
|
|
|
|
String toJSON() => json.encode({'balanceInWei': balance.getInWei.toString()});
|
|
|
|
static EthereumBalance? fromJSON(String? jsonSource) {
|
|
if (jsonSource == null) {
|
|
return null;
|
|
}
|
|
|
|
final decoded = json.decode(jsonSource) as Map;
|
|
|
|
try {
|
|
return EthereumBalance(EtherAmount.inWei(BigInt.parse(decoded['balanceInWei'])));
|
|
} catch (e) {
|
|
return EthereumBalance(EtherAmount.zero());
|
|
}
|
|
}
|
|
}
|