mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-01 12:04:21 +01:00
Cw 598 fixes for electrum based wallets (#1344)
* fix: address book addresses, bch builder, exchange all fee estimation, bch coin control * feat: new error framework for Electrum messages * build: cw_bitcoin.dart * feat: error improvements, localization, fix exchange amount mismatch * chore: misc comment & print [skip ci] * feat: refactor & simplify sendAll vs regular tx estimation and creation - Since there were so many conditions inside a single function to alter its behavior if sendAll or not, it is easier and more readable to have separate sendAll and estimateTx functions that behave separately * fix: wrong LTC dust * feat: fee rate confirmation * fix: wrong createTrade value when isSendAll is enabled * fix bitcoin cash address parsing [skip ci] * fix: form no amount validator, address book with multiple entries, exchange all below min error * fix: improve string, fix sending with dust inputs at the top * fix: two change outputs when re-estimating * fix: sendAll with a little dust adds fees * chore: sanity check [skip ci] * fix: if the fee is higher than estimated * Minor enhancement [skip ci] --------- Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
This commit is contained in:
@@ -46,6 +46,8 @@ abstract class ContactListViewModelBase with Store {
|
||||
name,
|
||||
walletTypeToCryptoCurrency(info.type),
|
||||
));
|
||||
// Only one contact address per wallet
|
||||
return;
|
||||
});
|
||||
} else if (info.address != null) {
|
||||
walletContacts.add(WalletContact(
|
||||
|
||||
@@ -105,6 +105,7 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||
output.address = trade.inputAddress ?? '';
|
||||
output.setCryptoAmount(trade.amount);
|
||||
if (_provider is ThorChainExchangeProvider) output.memo = trade.memo;
|
||||
if (trade.isSendAll == true) output.sendAll = true;
|
||||
sendViewModel.selectedCryptoCurrency = trade.from;
|
||||
final pendingTransaction = await sendViewModel.createTransaction(provider: _provider);
|
||||
if (_provider is ThorChainExchangeProvider) {
|
||||
@@ -116,6 +117,8 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||
@action
|
||||
Future<void> _updateTrade() async {
|
||||
try {
|
||||
final agreedAmount = tradesStore.trade!.amount;
|
||||
final isSendAll = tradesStore.trade!.isSendAll;
|
||||
final updatedTrade = await _provider!.findTradeById(id: trade.id);
|
||||
|
||||
if (updatedTrade.createdAt == null && trade.createdAt != null)
|
||||
@@ -124,6 +127,8 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||
if (updatedTrade.amount.isEmpty) updatedTrade.amount = trade.amount;
|
||||
|
||||
trade = updatedTrade;
|
||||
trade.amount = agreedAmount;
|
||||
trade.isSendAll = isSendAll;
|
||||
|
||||
_updateItems();
|
||||
} catch (e) {
|
||||
@@ -137,9 +142,9 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||
final tagTo = tradesStore.trade!.to.tag != null ? '${tradesStore.trade!.to.tag}' + ' ' : '';
|
||||
items.clear();
|
||||
|
||||
if(trade.provider != ExchangeProviderDescription.thorChain)
|
||||
items.add(ExchangeTradeItem(
|
||||
title: "${trade.provider.title} ${S.current.id}", data: '${trade.id}', isCopied: true));
|
||||
if (trade.provider != ExchangeProviderDescription.thorChain)
|
||||
items.add(ExchangeTradeItem(
|
||||
title: "${trade.provider.title} ${S.current.id}", data: '${trade.id}', isCopied: true));
|
||||
|
||||
if (trade.extraId != null) {
|
||||
final title = trade.from == CryptoCurrency.xrp
|
||||
|
||||
@@ -470,6 +470,18 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
||||
|
||||
@action
|
||||
Future<void> createTrade() async {
|
||||
if (isSendAllEnabled) {
|
||||
await calculateDepositAllAmount();
|
||||
final amount = double.tryParse(depositAmount);
|
||||
|
||||
if (limits.min != null && amount != null && amount < limits.min!) {
|
||||
tradeState = TradeIsCreatedFailure(
|
||||
title: S.current.trade_not_created,
|
||||
error: S.current.amount_is_below_minimum_limit(limits.min!.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
for (var provider in _sortedAvailableProviders.values) {
|
||||
if (!(await provider.checkIsAvailable())) continue;
|
||||
@@ -496,8 +508,11 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
||||
else {
|
||||
try {
|
||||
tradeState = TradeIsCreating();
|
||||
final trade =
|
||||
await provider.createTrade(request: request, isFixedRateMode: isFixedRateMode);
|
||||
final trade = await provider.createTrade(
|
||||
request: request,
|
||||
isFixedRateMode: isFixedRateMode,
|
||||
isSendAll: isSendAllEnabled,
|
||||
);
|
||||
trade.walletId = wallet.id;
|
||||
trade.fromWalletAddress = wallet.walletAddresses.address;
|
||||
|
||||
@@ -551,25 +566,24 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
||||
@action
|
||||
void enableSendAllAmount() {
|
||||
isSendAllEnabled = true;
|
||||
isFixedRateMode = false;
|
||||
calculateDepositAllAmount();
|
||||
}
|
||||
|
||||
@action
|
||||
void enableFixedRateMode() {
|
||||
isSendAllEnabled = false;
|
||||
isFixedRateMode = true;
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> calculateDepositAllAmount() async {
|
||||
if (wallet.type == WalletType.litecoin || wallet.type == WalletType.bitcoinCash) {
|
||||
final availableBalance = wallet.balance[wallet.currency]!.available;
|
||||
final priority = _settingsStore.priority[wallet.type]!;
|
||||
final fee = wallet.calculateEstimatedFee(priority, null);
|
||||
|
||||
if (availableBalance < fee || availableBalance == 0) return;
|
||||
|
||||
final amount = availableBalance - fee;
|
||||
changeDepositAmount(amount: bitcoin!.formatterBitcoinAmountToString(amount: amount));
|
||||
} else if (wallet.type == WalletType.bitcoin) {
|
||||
if (wallet.type == WalletType.litecoin ||
|
||||
wallet.type == WalletType.bitcoin ||
|
||||
wallet.type == WalletType.bitcoinCash) {
|
||||
final priority = _settingsStore.priority[wallet.type]!;
|
||||
|
||||
final amount = await bitcoin!.estimateFakeSendAllTxAmount(
|
||||
wallet, bitcoin!.deserializeBitcoinTransactionPriority(priority.raw));
|
||||
final amount = await bitcoin!.estimateFakeSendAllTxAmount(wallet, priority);
|
||||
|
||||
changeDepositAmount(amount: bitcoin!.formatterBitcoinAmountToString(amount: amount));
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import 'package:cake_wallet/solana/solana.dart';
|
||||
import 'package:cake_wallet/store/app_store.dart';
|
||||
import 'package:cake_wallet/view_model/contact_list/contact_list_view_model.dart';
|
||||
import 'package:cake_wallet/view_model/dashboard/balance_view_model.dart';
|
||||
import 'package:cw_core/exceptions.dart';
|
||||
import 'package:cw_core/transaction_priority.dart';
|
||||
import 'package:cake_wallet/view_model/send/output.dart';
|
||||
import 'package:cake_wallet/view_model/send/send_template_view_model.dart';
|
||||
@@ -309,9 +310,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
state = ExecutedSuccessfullyState();
|
||||
return pendingTransaction;
|
||||
} catch (e) {
|
||||
print('Failed with ${e.toString()}');
|
||||
state = FailureState(e.toString());
|
||||
return null;
|
||||
state = FailureState(translateErrorMessage(e, wallet.type, wallet.currency));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,8 +352,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
|
||||
state = TransactionCommitted();
|
||||
} catch (e) {
|
||||
String translatedError = translateErrorMessage(e.toString(), wallet.type, wallet.currency);
|
||||
state = FailureState(translatedError);
|
||||
state = FailureState(translateErrorMessage(e, wallet.type, wallet.currency));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,11 +427,10 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
}
|
||||
}
|
||||
|
||||
ContactRecord? newContactAddress () {
|
||||
|
||||
ContactRecord? newContactAddress() {
|
||||
final Set<String> contactAddresses =
|
||||
Set.from(contactListViewModel.contacts.map((contact) => contact.address))
|
||||
..addAll(contactListViewModel.walletContacts.map((contact) => contact.address));
|
||||
Set.from(contactListViewModel.contacts.map((contact) => contact.address))
|
||||
..addAll(contactListViewModel.walletContacts.map((contact) => contact.address));
|
||||
|
||||
for (var output in outputs) {
|
||||
String address;
|
||||
@@ -444,7 +441,6 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
}
|
||||
|
||||
if (address.isNotEmpty && !contactAddresses.contains(address)) {
|
||||
|
||||
return ContactRecord(
|
||||
contactListViewModel.contactSource,
|
||||
Contact(
|
||||
@@ -458,22 +454,59 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
|
||||
}
|
||||
|
||||
String translateErrorMessage(
|
||||
String error,
|
||||
Object error,
|
||||
WalletType walletType,
|
||||
CryptoCurrency currency,
|
||||
) {
|
||||
String errorMessage = error.toString();
|
||||
|
||||
if (walletType == WalletType.ethereum ||
|
||||
walletType == WalletType.polygon ||
|
||||
walletType == WalletType.solana ||
|
||||
walletType == WalletType.haven) {
|
||||
if (error.contains('gas required exceeds allowance') ||
|
||||
error.contains('insufficient funds')) {
|
||||
if (errorMessage.contains('gas required exceeds allowance') ||
|
||||
errorMessage.contains('insufficient funds')) {
|
||||
return S.current.do_not_have_enough_gas_asset(currency.toString());
|
||||
}
|
||||
|
||||
return error;
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
return error;
|
||||
if (walletType == WalletType.bitcoin ||
|
||||
walletType == WalletType.litecoin ||
|
||||
walletType == WalletType.bitcoinCash) {
|
||||
if (error is TransactionWrongBalanceException) {
|
||||
return S.current.tx_wrong_balance_exception(currency.toString());
|
||||
}
|
||||
if (error is TransactionNoInputsException) {
|
||||
return S.current.tx_not_enough_inputs_exception;
|
||||
}
|
||||
if (error is TransactionNoFeeException) {
|
||||
return S.current.tx_zero_fee_exception;
|
||||
}
|
||||
if (error is TransactionNoDustException) {
|
||||
return S.current.tx_no_dust_exception;
|
||||
}
|
||||
if (error is TransactionCommitFailed) {
|
||||
return S.current.tx_commit_failed;
|
||||
}
|
||||
if (error is TransactionCommitFailedDustChange) {
|
||||
return S.current.tx_rejected_dust_change;
|
||||
}
|
||||
if (error is TransactionCommitFailedDustOutput) {
|
||||
return S.current.tx_rejected_dust_output;
|
||||
}
|
||||
if (error is TransactionCommitFailedDustOutputSendAll) {
|
||||
return S.current.tx_rejected_dust_output_send_all;
|
||||
}
|
||||
if (error is TransactionCommitFailedVoutNegative) {
|
||||
return S.current.tx_rejected_vout_negative;
|
||||
}
|
||||
if (error is TransactionNoDustOnChangeException) {
|
||||
return S.current.tx_commit_exception_no_dust_on_change(error.min, error.max);
|
||||
}
|
||||
}
|
||||
|
||||
return errorMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,5 @@ abstract class UnspentCoinsDetailsViewModelBase with Store {
|
||||
final WalletType _type;
|
||||
List<TransactionDetailsListItem> items;
|
||||
|
||||
String get formattedAddress => WalletType.bitcoinCash == _type
|
||||
? bitcoinCash!.getCashAddrFormat(unspentCoinsItem.address)
|
||||
: unspentCoinsItem.address;
|
||||
String get formattedAddress => unspentCoinsItem.address;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user