From 096282032b3ba2be2fdf1161e31c2097aa080f56 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Thu, 21 Nov 2024 12:09:28 -0700 Subject: [PATCH 1/6] consistently order unspent coins / prevent coins jumping around --- .../unspent_coins/unspent_coins_list_view_model.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart b/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart index f16b8390f6..d01d97efb8 100644 --- a/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart +++ b/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart @@ -146,6 +146,15 @@ abstract class UnspentCoinsListViewModelBase with Store { } }); + // sort by hash so that even if the addresses are the same, they don't jump around when selected (because that calls updateBalance) + unspents.sort((a, b) => a.hash.compareTo(b.hash)); + + // sort unspents by address so that if something changes it's easier to see: + unspents.sort((a, b) => a.address.compareTo(b.address)); + + // sort change addresses to the end: + // unspents.sort((a, b) => a.isChange ? 1 : -1); + _items.addAll(unspents); } } From e37adaece05a5823986e9a8e7c9911e233a70c69 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Thu, 21 Nov 2024 12:10:07 -0700 Subject: [PATCH 2/6] sort change addresses to the end --- lib/view_model/unspent_coins/unspent_coins_list_view_model.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart b/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart index d01d97efb8..3baa68e470 100644 --- a/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart +++ b/lib/view_model/unspent_coins/unspent_coins_list_view_model.dart @@ -153,7 +153,7 @@ abstract class UnspentCoinsListViewModelBase with Store { unspents.sort((a, b) => a.address.compareTo(b.address)); // sort change addresses to the end: - // unspents.sort((a, b) => a.isChange ? 1 : -1); + unspents.sort((a, b) => a.isChange ? 1 : -1); _items.addAll(unspents); } From a337bc1f9eaf2ee3cd66d4479677a14b07e5e086 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Fri, 22 Nov 2024 10:56:51 -0700 Subject: [PATCH 3/6] 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 7e4b5f58f0..0d273dfa61 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 e58fad00f7..8b71d8268a 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 86228fc833..d6ee87a3e4 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 062c590baf..bb76dc2c39 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, ); } From 80a55245fd7ccf4426de5e6743e0e974bccd11f2 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Mon, 25 Nov 2024 10:24:01 -0700 Subject: [PATCH 4/6] experimental fix --- cw_bitcoin/lib/electrum_wallet.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 9b3baf6690..62f5f115e7 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1379,7 +1379,7 @@ abstract class ElectrumWalletBase })); unspentCoins = await updateCoinsWithInfoFromBox(updatedUnspentCoins); - await _refreshUnspentCoinsInfo(); + // await _refreshUnspentCoinsInfo(); } Future> updateCoinsWithInfoFromBox(List newUnspentCoins) async { From c521b9ceaf60c181eae987881a4c02e77b1c6d10 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Tue, 26 Nov 2024 09:43:28 -0700 Subject: [PATCH 5/6] undo experimental change [skip ci] --- cw_bitcoin/lib/electrum_wallet.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 5cac8392a0..490cb0846c 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1379,7 +1379,7 @@ abstract class ElectrumWalletBase })); unspentCoins = await updateCoinsWithInfoFromBox(updatedUnspentCoins); - // await _refreshUnspentCoinsInfo(); + await _refreshUnspentCoinsInfo(); } Future> updateCoinsWithInfoFromBox(List newUnspentCoins) async { From 12038c4285caa606222a3f8eb8aa7fc66ef80c3c Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Tue, 26 Nov 2024 10:01:11 -0700 Subject: [PATCH 6/6] [skip ci] add comment --- cw_bitcoin/lib/electrum_wallet.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart index 490cb0846c..2572ffe97c 100644 --- a/cw_bitcoin/lib/electrum_wallet.dart +++ b/cw_bitcoin/lib/electrum_wallet.dart @@ -1459,6 +1459,7 @@ abstract class ElectrumWalletBase await unspentCoinsInfo.add(newInfo); } + // delete unspent coins info entries that don't have a corresponding unspent coin: Future _refreshUnspentCoinsInfo() async { try { final List keys = [];