fix sending with mweb amounts

This commit is contained in:
Matthew Fosse 2024-09-13 11:49:03 -07:00
parent 245f4d665d
commit 3a969acc6d
8 changed files with 38 additions and 20 deletions

View file

@ -7,8 +7,8 @@ class ElectrumBalance extends Balance {
required this.confirmed,
required this.unconfirmed,
required this.frozen,
this.secondConfirmed,
this.secondUnconfirmed,
this.secondConfirmed = 0,
this.secondUnconfirmed = 0,
}) : super(
confirmed,
unconfirmed,
@ -32,8 +32,8 @@ class ElectrumBalance extends Balance {
int confirmed;
int unconfirmed;
final int frozen;
int? secondConfirmed;
int? secondUnconfirmed;
int secondConfirmed = 0;
int secondUnconfirmed = 0;
@override
String get formattedAvailableBalance => bitcoinAmountToString(amount: confirmed - frozen);
@ -51,7 +51,12 @@ class ElectrumBalance extends Balance {
String get formattedSecondAvailableBalance => bitcoinAmountToString(amount: secondConfirmed ?? 0);
@override
String get formattedSecondAdditionalBalance => bitcoinAmountToString(amount: secondUnconfirmed ?? 0);
String get formattedSecondAdditionalBalance =>
bitcoinAmountToString(amount: secondUnconfirmed ?? 0);
@override
String get formattedFullAvailableBalance =>
bitcoinAmountToString(amount: confirmed + (secondConfirmed ?? 0) - frozen);
String toJSON() => json.encode({
'confirmed': confirmed,

View file

@ -869,7 +869,7 @@ abstract class ElectrumWalletBase
final totalAmount = amount + fee;
if (totalAmount > balance[currency]!.confirmed) {
if (totalAmount > (balance[currency]!.confirmed + balance[currency]!.secondConfirmed)) {
throw BitcoinTransactionWrongBalanceException();
}

View file

@ -676,8 +676,10 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
try {
mwebUtxosBox.values.forEach((utxo) {
if (utxo.height > 0) {
// confirmed += utxo.value.toInt();
confirmedMweb += utxo.value.toInt();
} else {
// unconfirmed += utxo.value.toInt();
unconfirmedMweb += utxo.value.toInt();
}
});

View file

@ -122,6 +122,10 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
Future<String> getChangeAddress({List<BitcoinOutput>? outputs, UtxoDetails? utxoDetails}) async {
// use regular change address on peg in, otherwise use mweb for change address:
if (!mwebEnabled) {
return super.getChangeAddress();
}
if (outputs != null && utxoDetails != null) {
// check if this is a PEGIN:
bool outputsToMweb = false;
@ -134,6 +138,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
outputsToMweb = true;
}
}
// TODO: this doesn't respect coin control because it doesn't know which available inputs are selected
utxoDetails.availableInputs.forEach((element) {
if (element.address.contains("mweb")) {
comesFromMweb = true;
@ -144,6 +149,11 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with
if (isPegIn && mwebEnabled) {
return super.getChangeAddress();
}
// use regular change address if it's not an mweb tx:
if (!comesFromMweb && !outputsToMweb) {
return super.getChangeAddress();
}
}
if (mwebEnabled) {

View file

@ -13,4 +13,6 @@ abstract class Balance {
String get formattedUnAvailableBalance => '';
String get formattedSecondAvailableBalance => '';
String get formattedSecondAdditionalBalance => '';
String get formattedFullAvailableBalance => '';
String get formattedFullUnAvailableBalance => '';
}

View file

@ -1,3 +1,6 @@
import 'dart:math';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart';
@ -35,7 +38,7 @@ class AddressList extends StatelessWidget {
separatorBuilder: (context, _) => const HorizontalSectionDivider(),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: addressListViewModel.items.length,
itemCount: min(addressListViewModel.items.length, 100),// TODO: don't show all 1000 mweb addresses
itemBuilder: (context, index) {
final item = addressListViewModel.items[index];
Widget cell = Container();

View file

@ -369,22 +369,18 @@ abstract class BalanceViewModelBase with Store {
}
bool _hasSecondAdditionalBalanceForWalletType(WalletType type) {
// return _walletBalance.secondAdditional != null && _walletBalance.secondAdditional! != 0;
// if (wallet.type == WalletType.litecoin && settingsStore.mwebEnabled) {
// return true;
// }
// return false;
if (wallet.type == WalletType.litecoin /*&& settingsStore.mwebEnabled*/) {
return true;
}
return false;
}
bool _hasSecondAvailableBalanceForWalletType(WalletType type) {
// return _walletBalance.secondAdditional != null && _walletBalance.secondAdditional! != 0;
// if (wallet.type == WalletType.litecoin && settingsStore.mwebEnabled) {
// return true;
// }
// return false;
if (wallet.type == WalletType.litecoin /*&& settingsStore.mwebEnabled*/) {
return true;
}
return false;
}
@computed
List<BalanceRecord> get formattedBalances {

View file

@ -217,7 +217,7 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
PendingTransaction? pendingTransaction;
@computed
String get balance => wallet.balance[selectedCryptoCurrency]!.formattedAvailableBalance;
String get balance => wallet.balance[selectedCryptoCurrency]!.formattedFullAvailableBalance;
@computed
bool get isFiatDisabled => balanceViewModel.isFiatDisabled;