- Fix haven and litecoin fee transaction priority issue

- Revert removing fiat rate update and use it to update fiat rate periodically if Fiat API is not disabled
This commit is contained in:
OmarHatem 2022-12-08 17:23:17 +02:00
parent 6395d6b2f7
commit ce19be56a0
6 changed files with 104 additions and 24 deletions

View file

@ -51,6 +51,10 @@ class CWBitcoin extends Bitcoin {
TransactionPriority deserializeBitcoinTransactionPriority(int raw)
=> BitcoinTransactionPriority.deserialize(raw: raw);
@override
TransactionPriority deserializeLitecoinTransactionPriority(int raw)
=> LitecoinTransactionPriority.deserialize(raw: raw);
@override
int getFeeRate(Object wallet, TransactionPriority priority) {
final bitcoinWallet = wallet as ElectrumWallet;

View file

@ -22,6 +22,8 @@ class PreferencesKey {
'current_default_settings_migration_version';
static const moneroTransactionPriority = 'current_fee_priority_monero';
static const bitcoinTransactionPriority = 'current_fee_priority_bitcoin';
static const havenTransactionPriority = 'current_fee_priority_haven';
static const litecoinTransactionPriority = 'current_fee_priority_litecoin';
static const shouldShowReceiveWarning = 'should_show_receive_warning';
static const shouldShowYatPopup = 'should_show_yat_popup';
static const moneroWalletPasswordUpdateV1Base = 'monero_wallet_update_v1';

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'package:cake_wallet/reactions/fiat_rate_update.dart';
import 'package:cake_wallet/reactions/on_current_node_change.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
@ -31,4 +32,5 @@ Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
appStore, settingsStore, fiatConversionStore);
startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore);
startOnCurrentNodeChangeReaction(appStore);
startFiatRateUpdate(appStore, settingsStore, fiatConversionStore);
}

View file

@ -0,0 +1,40 @@
import 'dart:async';
import 'package:cake_wallet/core/fiat_conversion_service.dart';
import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:cake_wallet/entities/update_haven_rate.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cw_core/wallet_type.dart';
Timer? _timer;
Future<void> startFiatRateUpdate(
AppStore appStore, SettingsStore settingsStore, FiatConversionStore fiatConversionStore) async {
if (_timer != null) {
return;
}
if (appStore.wallet != null && settingsStore.fiatApiMode == FiatApiMode.enabled) {
fiatConversionStore.prices[appStore.wallet!.currency] = await FiatConversionService.fetchPrice(
appStore.wallet!.currency, settingsStore.fiatCurrency);
}
_timer = Timer.periodic(Duration(seconds: 30), (_) async {
if (settingsStore.fiatApiMode == FiatApiMode.disabled) {
return;
}
try {
if (appStore.wallet!.type == WalletType.haven) {
await updateHavenRate(fiatConversionStore);
} else {
fiatConversionStore.prices[appStore.wallet!.currency] =
await FiatConversionService.fetchPrice(
appStore.wallet!.currency, settingsStore.fiatCurrency);
}
} catch (e) {
print(e);
}
});
}

View file

@ -44,7 +44,9 @@ abstract class SettingsStoreBase with Store {
required this.isBitcoinBuyEnabled,
required this.actionlistDisplayMode,
TransactionPriority? initialBitcoinTransactionPriority,
TransactionPriority? initialMoneroTransactionPriority})
TransactionPriority? initialMoneroTransactionPriority,
TransactionPriority? initialHavenTransactionPriority,
TransactionPriority? initialLitecoinTransactionPriority})
: nodes = ObservableMap<WalletType, Node>.of(nodes),
_sharedPreferences = sharedPreferences,
fiatCurrency = initialFiatCurrency,
@ -67,6 +69,14 @@ abstract class SettingsStoreBase with Store {
priority[WalletType.bitcoin] = initialBitcoinTransactionPriority;
}
if (initialHavenTransactionPriority != null) {
priority[WalletType.haven] = initialHavenTransactionPriority;
}
if (initialLitecoinTransactionPriority != null) {
priority[WalletType.litecoin] = initialLitecoinTransactionPriority;
}
reaction(
(_) => fiatCurrency,
(FiatCurrency fiatCurrency) => sharedPreferences.setString(
@ -78,11 +88,25 @@ abstract class SettingsStoreBase with Store {
.setBool(PreferencesKey.shouldShowYatPopup, shouldShowYatPopup));
priority.observe((change) {
final key = change.key == WalletType.monero
? PreferencesKey.moneroTransactionPriority
: PreferencesKey.bitcoinTransactionPriority;
final String? key;
switch (change.key) {
case WalletType.monero:
key = PreferencesKey.moneroTransactionPriority;
break;
case WalletType.bitcoin:
key = PreferencesKey.bitcoinTransactionPriority;
break;
case WalletType.litecoin:
key = PreferencesKey.litecoinTransactionPriority;
break;
case WalletType.haven:
key = PreferencesKey.havenTransactionPriority;
break;
default:
key = null;
}
if (change.newValue != null) {
if (change.newValue != null && key != null) {
sharedPreferences.setInt(key, change.newValue!.serialize());
}
});
@ -202,33 +226,39 @@ abstract class SettingsStoreBase with Store {
static Future<SettingsStore> load(
{required Box<Node> nodeSource,
required bool isBitcoinBuyEnabled,
TransactionPriority? initialMoneroTransactionPriority,
TransactionPriority? initialBitcoinTransactionPriority,
FiatCurrency initialFiatCurrency = FiatCurrency.usd,
BalanceDisplayMode initialBalanceDisplayMode =
BalanceDisplayMode.availableBalance}) async {
if (initialBitcoinTransactionPriority == null) {
initialBitcoinTransactionPriority = bitcoin?.getMediumTransactionPriority();
}
if (initialMoneroTransactionPriority == null) {
initialMoneroTransactionPriority = monero?.getDefaultTransactionPriority();
}
final sharedPreferences = await getIt.getAsync<SharedPreferences>();
final currentFiatCurrency = FiatCurrency.deserialize(raw:
sharedPreferences.getString(PreferencesKey.currentFiatCurrencyKey)!);
final savedMoneroTransactionPriority =
TransactionPriority? moneroTransactionPriority =
monero?.deserializeMoneroTransactionPriority(
raw: sharedPreferences
.getInt(PreferencesKey.moneroTransactionPriority)!);
final savedBitcoinTransactionPriority =
TransactionPriority? bitcoinTransactionPriority =
bitcoin?.deserializeBitcoinTransactionPriority(sharedPreferences
.getInt(PreferencesKey.bitcoinTransactionPriority)!);
final moneroTransactionPriority =
savedMoneroTransactionPriority ?? initialMoneroTransactionPriority;
final bitcoinTransactionPriority =
savedBitcoinTransactionPriority ?? initialBitcoinTransactionPriority;
TransactionPriority? havenTransactionPriority;
TransactionPriority? litecoinTransactionPriority;
if (sharedPreferences.getInt(PreferencesKey.havenTransactionPriority) != null) {
havenTransactionPriority = monero?.deserializeMoneroTransactionPriority(
raw: sharedPreferences.getInt(PreferencesKey.havenTransactionPriority)!);
}
if (sharedPreferences.getInt(PreferencesKey.litecoinTransactionPriority) != null) {
litecoinTransactionPriority = bitcoin?.deserializeLitecoinTransactionPriority(
sharedPreferences.getInt(PreferencesKey.litecoinTransactionPriority)!);
}
moneroTransactionPriority ??= monero?.getDefaultTransactionPriority();
bitcoinTransactionPriority ??= bitcoin?.getMediumTransactionPriority();
havenTransactionPriority ??= monero?.getDefaultTransactionPriority();
litecoinTransactionPriority ??= bitcoin?.getLitecoinTransactionPriorityMedium();
final currentBalanceDisplayMode = BalanceDisplayMode.deserialize(
raw: sharedPreferences
.getInt(PreferencesKey.currentBalanceDisplayModeKey)!);
@ -249,8 +279,7 @@ abstract class SettingsStoreBase with Store {
: ThemeType.bright.index;
final savedTheme = ThemeList.deserialize(
raw: sharedPreferences.getInt(PreferencesKey.currentTheme) ??
legacyTheme ??
0);
legacyTheme);
final actionListDisplayMode = ObservableList<ActionListDisplayMode>();
actionListDisplayMode.addAll(deserializeActionlistDisplayModes(
sharedPreferences.getInt(PreferencesKey.displayActionListModeKey) ??
@ -296,7 +325,7 @@ abstract class SettingsStoreBase with Store {
if (havenNode != null) {
nodes[WalletType.haven] = havenNode;
}
return SettingsStore(
sharedPreferences: sharedPreferences,
nodes: nodes,
@ -314,6 +343,8 @@ abstract class SettingsStoreBase with Store {
initialLanguageCode: savedLanguageCode,
initialMoneroTransactionPriority: moneroTransactionPriority,
initialBitcoinTransactionPriority: bitcoinTransactionPriority,
initialHavenTransactionPriority: havenTransactionPriority,
initialLitecoinTransactionPriority: litecoinTransactionPriority,
shouldShowYatPopup: shouldShowYatPopup);
}
@ -326,7 +357,7 @@ abstract class SettingsStoreBase with Store {
// TransactionPriority? initialBitcoinTransactionPriority,
// BalanceDisplayMode initialBalanceDisplayMode =
// BalanceDisplayMode.availableBalance}) async {
// if (initialBitcoinTransactionPriority == null) {
// initialBitcoinTransactionPriority = bitcoin?.getMediumTransactionPriority();
// }

View file

@ -75,6 +75,7 @@ abstract class Bitcoin {
List<TransactionPriority> getTransactionPriorities();
List<TransactionPriority> getLitecoinTransactionPriorities();
TransactionPriority deserializeBitcoinTransactionPriority(int raw);
TransactionPriority deserializeLitecoinTransactionPriority(int raw);
int getFeeRate(Object wallet, TransactionPriority priority);
Future<void> generateNewAddress(Object wallet);
Object createBitcoinTransactionCredentials(List<Output> outputs, {required TransactionPriority priority, int? feeRate});