Skip to content

Commit

Permalink
review fixes, additional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fossephate committed Sep 24, 2024
1 parent 6f462aa commit 840287c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 75 deletions.
7 changes: 3 additions & 4 deletions cw_bitcoin/lib/electrum_balance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ class ElectrumBalance extends Balance {
}

@override
String get formattedSecondAvailableBalance => bitcoinAmountToString(amount: secondConfirmed ?? 0);
String get formattedSecondAvailableBalance => bitcoinAmountToString(amount: secondConfirmed);

@override
String get formattedSecondAdditionalBalance =>
bitcoinAmountToString(amount: secondUnconfirmed ?? 0);
String get formattedSecondAdditionalBalance => bitcoinAmountToString(amount: secondUnconfirmed);

@override
String get formattedFullAvailableBalance =>
bitcoinAmountToString(amount: confirmed + (secondConfirmed ?? 0) - frozen);
bitcoinAmountToString(amount: confirmed + secondConfirmed - frozen);

String toJSON() => json.encode({
'confirmed': confirmed,
Expand Down
8 changes: 1 addition & 7 deletions cw_bitcoin/lib/electrum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1357,13 +1357,7 @@ abstract class ElectrumWalletBase
List<Map<String, dynamic>> unspents = [];
List<BitcoinUnspent> updatedUnspentCoins = [];

try {
unspents = await electrumClient.getListUnspent(address.getScriptHash(network));
} catch (e, s) {
print(e);
print(s);
return [];
}
unspents = await electrumClient.getListUnspent(address.getScriptHash(network));

await Future.wait(unspents.map((unspent) async {
try {
Expand Down
70 changes: 26 additions & 44 deletions cw_bitcoin/lib/litecoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
late final Bip32Slip10Secp256k1 mwebHd;
late final Box<MwebUtxo> mwebUtxosBox;
Timer? _syncTimer;
Timer? _stuckSyncTimer;
Timer? _feeRatesTimer;
StreamSubscription<Utxo>? _utxoStream;
late RpcClient _stub;
Expand Down Expand Up @@ -233,6 +232,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {

Future<void> waitForMwebAddresses() async {
// ensure that we have the full 1000 mweb addresses generated before continuing:
// should no longer be needed, but leaving here just in case
final mwebAddrs = (walletAddresses as LitecoinWalletAddresses).mwebAddrs;
while (mwebAddrs.length < 1000) {
print("waiting for mweb addresses to finish generating...");
Expand All @@ -243,31 +243,35 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@action
@override
Future<void> startSync() async {
print("STARTING SYNC @@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
if (syncStatus is SyncronizingSyncStatus) {
return;
}
print("STARTING SYNC - MWEB ENABLED: $mwebEnabled");
syncStatus = SyncronizingSyncStatus();
await subscribeForUpdates();
try {
syncStatus = SyncronizingSyncStatus();
await subscribeForUpdates();

await updateFeeRates();
_feeRatesTimer?.cancel();
_feeRatesTimer =
Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates());
await updateFeeRates();
_feeRatesTimer?.cancel();
_feeRatesTimer =
Timer.periodic(const Duration(minutes: 1), (timer) async => await updateFeeRates());

if (!mwebEnabled) {
try {
await updateAllUnspents();
await updateTransactions();
await updateBalance();
syncStatus = SyncedSyncStatus();
} catch (e, s) {
print(e);
print(s);
syncStatus = FailedSyncStatus();
if (!mwebEnabled) {
try {
await updateAllUnspents();
await updateTransactions();
await updateBalance();
syncStatus = SyncedSyncStatus();
} catch (e, s) {
print(e);
print(s);
syncStatus = FailedSyncStatus();
}
return;
}
return;
}

await waitForMwebAddresses();
try {
await waitForMwebAddresses();
await getStub();
await processMwebUtxos();
await updateTransactions();
Expand Down Expand Up @@ -318,34 +322,13 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
return;
}
});

// setup a watch dog to restart the sync process if it gets stuck:
List<double> lastFewProgresses = [];
_stuckSyncTimer?.cancel();
_stuckSyncTimer = Timer.periodic(const Duration(seconds: 10), (timer) async {
if (syncStatus is! SyncingSyncStatus) return;
if (syncStatus.progress() > 0.98) return; // don't check if we're close to synced
lastFewProgresses.add(syncStatus.progress());
if (lastFewProgresses.length < 10) return;
// limit list size to 10:
while (lastFewProgresses.length > 10) {
lastFewProgresses.removeAt(0);
}
// if the progress is the same over the last 100 seconds, restart the sync:
if (lastFewProgresses.every((p) => p == lastFewProgresses.first)) {
print("mweb syncing is stuck, restarting...");
syncStatus = LostConnectionSyncStatus();
await stopSync();
}
});
}

@action
@override
Future<void> stopSync() async {
_syncTimer?.cancel();
_utxoStream?.cancel();
_stuckSyncTimer?.cancel();
_feeRatesTimer?.cancel();
await CwMweb.stop();
}
Expand Down Expand Up @@ -913,7 +896,7 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
final addresses = <String>{};
transaction.inputAddresses?.forEach((id) async {
final utxo = mwebUtxosBox.get(id);
// await mwebUtxosBox.delete(id);
// await mwebUtxosBox.delete(id);// gets deleted in checkMwebUtxosSpent
if (utxo == null) return;
final addressRecord = walletAddresses.allAddresses
.firstWhere((addressRecord) => addressRecord.address == utxo.address);
Expand Down Expand Up @@ -946,7 +929,6 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store {
@override
Future<void> close() async {
_utxoStream?.cancel();
_stuckSyncTimer?.cancel();
_feeRatesTimer?.cancel();
_syncTimer?.cancel();
await stopSync();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/screens/receive/widgets/address_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AddressList extends StatelessWidget {
separatorBuilder: (context, _) => const HorizontalSectionDivider(),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: min(addressListViewModel.items.length, 100),// TODO: don't show all 1000 mweb addresses
itemCount: addressListViewModel.items.length,
itemBuilder: (context, index) {
final item = addressListViewModel.items[index];
Widget cell = Container();
Expand Down
11 changes: 0 additions & 11 deletions lib/view_model/dashboard/balance_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,6 @@ abstract class BalanceViewModelBase with Store {
return walletBalance.formattedAdditionalBalance;
}

@computed
String get secondAdditionalBalance {
final walletBalance = _walletBalance;

if (displayMode == BalanceDisplayMode.hiddenBalance) {
return '---';
}

return walletBalance.formattedSecondAdditionalBalance;
}

@computed
String get availableFiatBalance {
final walletBalance = _walletBalance;
Expand Down
11 changes: 10 additions & 1 deletion lib/view_model/send/send_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,16 @@ abstract class SendViewModelBase extends WalletChangeListenerViewModel with Stor
PendingTransaction? pendingTransaction;

@computed
String get balance => wallet.balance[selectedCryptoCurrency]!.formattedFullAvailableBalance;
String get balance {
String fullFormattedBalance =
wallet.balance[selectedCryptoCurrency]!.formattedFullAvailableBalance;
String formattedAvailableBalance =
wallet.balance[selectedCryptoCurrency]!.formattedAvailableBalance;
if (fullFormattedBalance.isNotEmpty) {
return fullFormattedBalance;
}
return formattedAvailableBalance;
}

@computed
bool get isFiatDisabled => balanceViewModel.isFiatDisabled;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/wallet_change_listener_view_model.dart';
import 'package:cake_wallet/entities/auto_generate_subaddress_status.dart';
Expand Down Expand Up @@ -217,8 +219,9 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo
}) : _baseItems = <ListItem>[],
selectedCurrency = walletTypeToCryptoCurrency(appStore.wallet!.type),
_cryptoNumberFormat = NumberFormat(_cryptoNumberPattern),
hasAccounts =
appStore.wallet!.type == WalletType.monero || appStore.wallet!.type == WalletType.wownero || appStore.wallet!.type == WalletType.haven,
hasAccounts = appStore.wallet!.type == WalletType.monero ||
appStore.wallet!.type == WalletType.wownero ||
appStore.wallet!.type == WalletType.haven,
amount = '',
_settingsStore = appStore.settingsStore,
super(appStore: appStore) {
Expand All @@ -230,7 +233,9 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo
_init();

selectedCurrency = walletTypeToCryptoCurrency(wallet.type);
hasAccounts = wallet.type == WalletType.monero || wallet.type == WalletType.wownero || wallet.type == WalletType.haven;
hasAccounts = wallet.type == WalletType.monero ||
wallet.type == WalletType.wownero ||
wallet.type == WalletType.haven;
}

static const String _cryptoNumberPattern = '0.00000000';
Expand Down Expand Up @@ -446,7 +451,7 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo
address: wallet.walletAddresses.address,
));
}

if (wallet.type == WalletType.tron) {
final primaryAddress = tron!.getAddress(wallet);

Expand Down Expand Up @@ -519,16 +524,17 @@ abstract class WalletAddressListViewModelBase extends WalletChangeListenerViewMo

@action
Future<void> setAddressType(dynamic option) async {
if (wallet.type == WalletType.bitcoin ||
wallet.type == WalletType.litecoin) {
if (wallet.type == WalletType.bitcoin || wallet.type == WalletType.litecoin) {
await bitcoin!.setAddressType(wallet, option);
}
}

void _init() {
_baseItems = [];

if (wallet.type == WalletType.monero || wallet.type == WalletType.wownero || wallet.type == WalletType.haven) {
if (wallet.type == WalletType.monero ||
wallet.type == WalletType.wownero ||
wallet.type == WalletType.haven) {
_baseItems.add(WalletAccountListHeader());
}

Expand Down

0 comments on commit 840287c

Please sign in to comment.