From a337bc1f9eaf2ee3cd66d4479677a14b07e5e086 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Fri, 22 Nov 2024 10:56:51 -0700 Subject: [PATCH] unspent coins info handling improvements --- cw_bitcoin/lib/bitcoin_address_record.dart | 4 ++-- cw_bitcoin/lib/electrum_wallet.dart | 24 +++++++++---------- cw_bitcoin/lib/litecoin_wallet.dart | 6 ++--- cw_bitcoin/lib/litecoin_wallet_addresses.dart | 2 ++ 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cw_bitcoin/lib/bitcoin_address_record.dart b/cw_bitcoin/lib/bitcoin_address_record.dart index 7e4b5f58f..0d273dfa6 100644 --- a/cw_bitcoin/lib/bitcoin_address_record.dart +++ b/cw_bitcoin/lib/bitcoin_address_record.dart @@ -6,7 +6,7 @@ abstract class BaseBitcoinAddressRecord { BaseBitcoinAddressRecord( this.address, { required this.index, - this.isHidden = false, + required this.isHidden, int txCount = 0, int balance = 0, String name = '', @@ -56,7 +56,7 @@ class BitcoinAddressRecord extends BaseBitcoinAddressRecord { BitcoinAddressRecord( super.address, { required super.index, - super.isHidden = false, + required super.isHidden, super.txCount = 0, super.balance = 0, super.name = '', diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index e58fad00f..8b71d8268 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -924,13 +924,13 @@ abstract class ElectrumWalletBase ); } else { // Here, lastOutput already is change, return the amount left without the fee to the user's address. - updatedOutputs[updatedOutputs.length - 1] = BitcoinOutput( + updatedOutputs.last = BitcoinOutput( address: lastOutput.address, value: BigInt.from(amountLeftForChange), isSilentPayment: lastOutput.isSilentPayment, isChange: true, ); - outputs[outputs.length - 1] = BitcoinOutput( + outputs.last = BitcoinOutput( address: lastOutput.address, value: BigInt.from(amountLeftForChange), isSilentPayment: lastOutput.isSilentPayment, @@ -1377,20 +1377,16 @@ abstract class ElectrumWalletBase updatedUnspentCoins.addAll(await fetchUnspent(address)); })); - unspentCoins = updatedUnspentCoins; - - if (unspentCoinsInfo.length != updatedUnspentCoins.length) { - unspentCoins.forEach((coin) => addCoinInfo(coin)); - return; - } - - await updateCoins(unspentCoins); + unspentCoins = await updateCoinsWithInfoFromBox(updatedUnspentCoins); await _refreshUnspentCoinsInfo(); } - Future updateCoins(List newUnspentCoins) async { + Future> updateCoinsWithInfoFromBox(List newUnspentCoins) async { + // this function updates and returns unspent coins list (freshly fetched from the server) + // with info from the box, if the box doesn't have info for some of the unspents, it adds them to the box + if (newUnspentCoins.isEmpty) { - return; + return newUnspentCoins; } newUnspentCoins.forEach((coin) { @@ -1413,12 +1409,14 @@ abstract class ElectrumWalletBase addCoinInfo(coin); } }); + + return newUnspentCoins; } @action Future updateUnspentsForAddress(BitcoinAddressRecord address) async { final newUnspentCoins = await fetchUnspent(address); - await updateCoins(newUnspentCoins); + unspentCoins = await updateCoinsWithInfoFromBox(newUnspentCoins); } @action diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index 86228fc83..d6ee87a3e 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -837,11 +837,11 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { mwebUnspentCoins.add(unspent); }); - // copy coin control attributes to mwebCoins: - await updateCoins(mwebUnspentCoins); + // copy coin control attributes to coinsInfo: + mwebUnspentCoins = await updateCoinsWithInfoFromBox(mwebUnspentCoins); // get regular ltc unspents (this resets unspentCoins): await super.updateAllUnspents(); - // add the mwebCoins: + // add back the mwebCoins: unspentCoins.addAll(mwebUnspentCoins); } diff --git a/cw_bitcoin/lib/litecoin_wallet_addresses.dart b/cw_bitcoin/lib/litecoin_wallet_addresses.dart index 062c590ba..bb76dc2c3 100644 --- a/cw_bitcoin/lib/litecoin_wallet_addresses.dart +++ b/cw_bitcoin/lib/litecoin_wallet_addresses.dart @@ -106,6 +106,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with index: e.key, type: SegwitAddresType.mweb, network: network, + isHidden: false, )) .toList(); addMwebAddresses(addressRecords); @@ -195,6 +196,7 @@ abstract class LitecoinWalletAddressesBase extends ElectrumWalletAddresses with index: 0, type: SegwitAddresType.mweb, network: network, + isHidden: true, ); }