Cw 870 ethereum enhancements (#1951)

* fix evm balance display issues

* fix adding token

* fix tab controller issue

* Update cw_evm/lib/evm_chain_client.dart [skip ci]

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
Serhii 2025-02-17 15:12:18 +02:00 committed by GitHub
parent f35c20203e
commit 7cc9e36016
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 60 additions and 24 deletions

View file

@ -268,12 +268,18 @@ abstract class EVMChainClient {
Future<EVMChainERC20Balance> fetchERC20Balances(
EthereumAddress userAddress, String contractAddress) async {
final erc20 = ERC20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
final balance = await erc20.balanceOf(userAddress);
try {
final erc20 = ERC20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
final balance = await erc20.balanceOf(userAddress);
int exponent = (await erc20.decimals()).toInt();
return EVMChainERC20Balance(balance, exponent: exponent);
return EVMChainERC20Balance(balance, exponent: exponent);
} on RangeError catch (_) {
throw Exception('Invalid token contract for this network.');
} catch (e) {
throw Exception('Could not fetch balances: ${e.toString()}');
}
}
Future<Erc20Token?> getErc20Token(String contractAddress, String chainName) async {

View file

@ -634,14 +634,21 @@ abstract class EVMChainWalletBase
final newToken = createNewErc20TokenObject(token, iconPath);
await evmChainErc20TokensBox.put(newToken.contractAddress, newToken);
if (newToken.enabled) {
balance[newToken] = await _client.fetchERC20Balances(
_evmChainPrivateKey.address,
newToken.contractAddress,
);
try {
final erc20Balance = await _client.fetchERC20Balances(
_evmChainPrivateKey.address,
newToken.contractAddress,
);
balance[newToken] = erc20Balance;
await evmChainErc20TokensBox.put(newToken.contractAddress, newToken);
} on Exception catch (_) {
rethrow;
}
} else {
await evmChainErc20TokensBox.put(newToken.contractAddress, newToken);
balance.remove(newToken);
}
}

View file

@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:math';
import 'package:intl/intl.dart';
import 'package:cw_core/balance.dart';
@ -17,8 +18,10 @@ class EVMChainERC20Balance extends Balance {
String get formattedAvailableBalance => _balance();
String _balance() {
final String formattedBalance = (balance / BigInt.from(10).pow(exponent)).toString();
return formattedBalance.substring(0, min(12, formattedBalance.length));
NumberFormat formatter = NumberFormat('0.00##########', 'en_US');
double numBalance = (balance / BigInt.from(10).pow(exponent)).toDouble();
String formattedBalance = formatter.format(numBalance);
return formattedBalance;
}
String toJSON() => json.encode({