mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 20:39:51 +00:00
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:
parent
f35c20203e
commit
7cc9e36016
6 changed files with 60 additions and 24 deletions
|
@ -268,12 +268,18 @@ abstract class EVMChainClient {
|
||||||
|
|
||||||
Future<EVMChainERC20Balance> fetchERC20Balances(
|
Future<EVMChainERC20Balance> fetchERC20Balances(
|
||||||
EthereumAddress userAddress, String contractAddress) async {
|
EthereumAddress userAddress, String contractAddress) async {
|
||||||
final erc20 = ERC20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
|
try {
|
||||||
final balance = await erc20.balanceOf(userAddress);
|
final erc20 = ERC20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
|
||||||
|
final balance = await erc20.balanceOf(userAddress);
|
||||||
|
|
||||||
int exponent = (await erc20.decimals()).toInt();
|
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 {
|
Future<Erc20Token?> getErc20Token(String contractAddress, String chainName) async {
|
||||||
|
|
|
@ -634,14 +634,21 @@ abstract class EVMChainWalletBase
|
||||||
|
|
||||||
final newToken = createNewErc20TokenObject(token, iconPath);
|
final newToken = createNewErc20TokenObject(token, iconPath);
|
||||||
|
|
||||||
await evmChainErc20TokensBox.put(newToken.contractAddress, newToken);
|
|
||||||
|
|
||||||
if (newToken.enabled) {
|
if (newToken.enabled) {
|
||||||
balance[newToken] = await _client.fetchERC20Balances(
|
try {
|
||||||
_evmChainPrivateKey.address,
|
final erc20Balance = await _client.fetchERC20Balances(
|
||||||
newToken.contractAddress,
|
_evmChainPrivateKey.address,
|
||||||
);
|
newToken.contractAddress,
|
||||||
|
);
|
||||||
|
|
||||||
|
balance[newToken] = erc20Balance;
|
||||||
|
|
||||||
|
await evmChainErc20TokensBox.put(newToken.contractAddress, newToken);
|
||||||
|
} on Exception catch (_) {
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
await evmChainErc20TokensBox.put(newToken.contractAddress, newToken);
|
||||||
balance.remove(newToken);
|
balance.remove(newToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
import 'package:cw_core/balance.dart';
|
import 'package:cw_core/balance.dart';
|
||||||
|
|
||||||
|
@ -17,8 +18,10 @@ class EVMChainERC20Balance extends Balance {
|
||||||
String get formattedAvailableBalance => _balance();
|
String get formattedAvailableBalance => _balance();
|
||||||
|
|
||||||
String _balance() {
|
String _balance() {
|
||||||
final String formattedBalance = (balance / BigInt.from(10).pow(exponent)).toString();
|
NumberFormat formatter = NumberFormat('0.00##########', 'en_US');
|
||||||
return formattedBalance.substring(0, min(12, formattedBalance.length));
|
double numBalance = (balance / BigInt.from(10).pow(exponent)).toDouble();
|
||||||
|
String formattedBalance = formatter.format(numBalance);
|
||||||
|
return formattedBalance;
|
||||||
}
|
}
|
||||||
|
|
||||||
String toJSON() => json.encode({
|
String toJSON() => json.encode({
|
||||||
|
|
|
@ -212,15 +212,34 @@ class _EditTokenPageBodyState extends State<EditTokenPageBody> {
|
||||||
_contractAddressController.text,
|
_contractAddressController.text,
|
||||||
);
|
);
|
||||||
final actionCall = () async {
|
final actionCall = () async {
|
||||||
await widget.homeSettingsViewModel.addToken(
|
try {
|
||||||
token: CryptoCurrency(
|
await widget.homeSettingsViewModel.addToken(
|
||||||
name: _tokenNameController.text,
|
token: CryptoCurrency(
|
||||||
title: _tokenSymbolController.text.toUpperCase(),
|
name: _tokenNameController.text,
|
||||||
decimals: int.parse(_tokenDecimalController.text),
|
title: _tokenSymbolController.text.toUpperCase(),
|
||||||
iconPath: _tokenIconPathController.text,
|
decimals: int.parse(_tokenDecimalController.text),
|
||||||
),
|
iconPath: _tokenIconPathController.text,
|
||||||
contractAddress: _contractAddressController.text,
|
),
|
||||||
);
|
contractAddress: _contractAddressController.text,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.pop(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
showPopUp<void>(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) {
|
||||||
|
return AlertWithOneAction(
|
||||||
|
alertTitle: S.current.warning,
|
||||||
|
alertContent: e.toString(),
|
||||||
|
buttonText: S.of(context).ok,
|
||||||
|
buttonAction: () => Navigator.of(dialogContext).pop(),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (hasPotentialError) {
|
if (hasPotentialError) {
|
||||||
|
@ -235,9 +254,6 @@ class _EditTokenPageBodyState extends State<EditTokenPageBody> {
|
||||||
actionRightButton: () async {
|
actionRightButton: () async {
|
||||||
Navigator.of(dialogContext).pop();
|
Navigator.of(dialogContext).pop();
|
||||||
await actionCall();
|
await actionCall();
|
||||||
if (mounted) {
|
|
||||||
Navigator.pop(context);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
actionLeftButton: () => Navigator.of(dialogContext).pop(),
|
actionLeftButton: () => Navigator.of(dialogContext).pop(),
|
||||||
);
|
);
|
||||||
|
|
|
@ -25,6 +25,7 @@ class BalancePage extends StatelessWidget {
|
||||||
builder: (context) {
|
builder: (context) {
|
||||||
final isEVMCompatible = isEVMCompatibleChain(dashboardViewModel.type);
|
final isEVMCompatible = isEVMCompatibleChain(dashboardViewModel.type);
|
||||||
return DefaultTabController(
|
return DefaultTabController(
|
||||||
|
key: ValueKey<bool>(isEVMCompatible),
|
||||||
length: isEVMCompatible ? 2 : 1,
|
length: isEVMCompatible ? 2 : 1,
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
|
|
|
@ -117,7 +117,10 @@ abstract class HomeSettingsViewModelBase with Store {
|
||||||
|
|
||||||
_updateTokensList();
|
_updateTokensList();
|
||||||
_updateFiatPrices(token);
|
_updateFiatPrices(token);
|
||||||
} finally {
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
isAddingToken = false;
|
isAddingToken = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue