Skip to content

Commit

Permalink
Fix Erc20 send all feature (#1030)
Browse files Browse the repository at this point in the history
* Fix Erc20 send all feature

* Remove debug prints

* Add user connection issues and certificate issues to ignored errors [skip ci]
  • Loading branch information
OmarHatem28 authored Aug 11, 2023
1 parent 07844a6 commit f4fad4d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions cw_ethereum/lib/ethereum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,15 @@ abstract class EthereumWalletBase
}
} else {
final output = outputs.first;
final BigInt allAmount =
_erc20Balance.balance - BigInt.from(calculateEstimatedFee(_credentials.priority!, null));
// since the fees are taken from Ethereum
// then no need to subtract the fees from the amount if send all
final BigInt allAmount;
if (transactionCurrency is Erc20Token) {
allAmount = _erc20Balance.balance;
} else {
allAmount = _erc20Balance.balance -
BigInt.from(calculateEstimatedFee(_credentials.priority!, null));
}
final totalOriginalAmount =
EthereumFormatter.parseEthereumAmountToDouble(output.formattedCryptoAmount ?? 0);
totalAmount = output.sendAll
Expand All @@ -196,7 +203,9 @@ abstract class EthereumWalletBase

final pendingEthereumTransaction = await _client.signTransaction(
privateKey: _privateKey,
toAddress: _credentials.outputs.first.address,
toAddress: _credentials.outputs.first.isParsedAddress
? _credentials.outputs.first.extractedAddress!
: _credentials.outputs.first.address,
amount: totalAmount.toString(),
gas: _estimatedGas!,
priority: _credentials.priority!,
Expand Down
10 changes: 8 additions & 2 deletions cw_ethereum/lib/pending_ethereum_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ class PendingEthereumTransaction with PendingTransaction {
});

@override
String get amountFormatted => (BigInt.parse(amount) / BigInt.from(pow(10, exponent))).toString();
String get amountFormatted {
final _amount = BigInt.parse(amount) / BigInt.from(pow(10, exponent));
return _amount.toStringAsFixed(min(15, _amount.toString().length));
}

@override
Future<void> commit() async => await sendTransaction();

@override
String get feeFormatted => (fee / BigInt.from(pow(10, 18))).toString();
String get feeFormatted {
final _fee = fee / BigInt.from(pow(10, 18));
return _fee.toStringAsFixed(min(15, _fee.toString().length));
}

@override
String get hex => bytesToHex(signedTransaction, include0x: true);
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/exception_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ class ExceptionHandler {
"Connection closed before full header was received",
"Connection terminated during handshake",
"PERMISSION_NOT_GRANTED",
"Failed host lookup: ",
"Failed host lookup:",
"CERTIFICATE_VERIFY_FAILED",
"Handshake error in client",
"Error while launching http",
];

static Future<void> _addDeviceInfo(File file) async {
Expand Down

0 comments on commit f4fad4d

Please sign in to comment.