cake_wallet/cw_ethereum/lib/ethereum_balance.dart
OmarHatem c6a26cac16 - Fix balance display
- Fix parsing of Ethereum amount
- Add more Ethereum Nodes [skip ci]
2023-04-14 14:05:24 +02:00

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());
}
}
}