mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-23 11:04:33 +00:00
use balance caching in all wallets
This commit is contained in:
parent
61a1ad551f
commit
1170f742e9
11 changed files with 49 additions and 10 deletions
|
@ -248,6 +248,7 @@ class DB {
|
|||
|
||||
abstract class DBKeys {
|
||||
static const String cachedBalance = "cachedBalance";
|
||||
static const String cachedBalanceSecondary = "cachedBalanceSecondary";
|
||||
static const String isFavorite = "isFavorite";
|
||||
static const String id = "id";
|
||||
static const String storedChainHeight = "storedChainHeight";
|
||||
|
|
|
@ -1810,6 +1810,7 @@ class BitcoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: satoshiBalanceBlocked,
|
||||
pendingSpendable: satoshiBalancePending,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
} catch (e, s) {
|
||||
Logging.instance
|
||||
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
|
||||
|
@ -1817,7 +1818,7 @@ class BitcoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
// /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list)
|
||||
|
|
|
@ -1552,6 +1552,7 @@ class DogecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: satoshiBalanceBlocked,
|
||||
pendingSpendable: satoshiBalancePending,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
} catch (e, s) {
|
||||
Logging.instance
|
||||
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
|
||||
|
@ -1559,7 +1560,7 @@ class DogecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
// /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list)
|
||||
|
|
|
@ -2284,10 +2284,12 @@ class EpicCashWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
coin,
|
||||
),
|
||||
);
|
||||
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
@override
|
||||
|
|
|
@ -2459,6 +2459,7 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: 0,
|
||||
pendingSpendable: unconfirmedLelantusBalance + balance.total,
|
||||
);
|
||||
await updateCachedBalanceSecondary(walletId, _balancePrivate!);
|
||||
// _balance = Balance(
|
||||
// coin: coin,
|
||||
// total: utxos.satoshiBalance,
|
||||
|
@ -3660,6 +3661,7 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: satoshiBalanceBlocked,
|
||||
pendingSpendable: satoshiBalancePending,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
} catch (e, s) {
|
||||
Logging.instance
|
||||
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
|
||||
|
@ -4843,10 +4845,11 @@ class FiroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
Balance get balancePrivate => _balancePrivate!;
|
||||
Balance get balancePrivate =>
|
||||
_balancePrivate ??= getCachedBalanceSecondary(walletId, coin);
|
||||
Balance? _balancePrivate;
|
||||
|
||||
@override
|
||||
|
|
|
@ -1796,6 +1796,7 @@ class LitecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: satoshiBalanceBlocked,
|
||||
pendingSpendable: satoshiBalancePending,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
} catch (e, s) {
|
||||
Logging.instance
|
||||
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
|
||||
|
@ -1803,7 +1804,7 @@ class LitecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
// /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list)
|
||||
|
|
|
@ -734,6 +734,7 @@ class MoneroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: 0,
|
||||
pendingSpendable: total - available,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
}
|
||||
|
||||
Future<int> get _availableBalance async {
|
||||
|
@ -1214,7 +1215,7 @@ class MoneroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
int get storedChainHeight => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
@override
|
||||
|
|
|
@ -1776,6 +1776,7 @@ class NamecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: satoshiBalanceBlocked,
|
||||
pendingSpendable: satoshiBalancePending,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
} catch (e, s) {
|
||||
Logging.instance
|
||||
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
|
||||
|
@ -1783,7 +1784,7 @@ class NamecoinWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
// /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list)
|
||||
|
|
|
@ -1665,6 +1665,7 @@ class ParticlWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: satoshiBalanceBlocked,
|
||||
pendingSpendable: satoshiBalancePending,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
} catch (e, s) {
|
||||
Logging.instance
|
||||
.log("Output fetch unsuccessful: $e\n$s", level: LogLevel.Error);
|
||||
|
@ -1672,7 +1673,7 @@ class ParticlWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
}
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
// /// Takes in a list of UtxoObjects and adds a name (dependent on object index within list)
|
||||
|
|
|
@ -744,6 +744,7 @@ class WowneroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
blockedTotal: 0,
|
||||
pendingSpendable: total - available,
|
||||
);
|
||||
await updateCachedBalance(walletId, _balance!);
|
||||
}
|
||||
|
||||
Future<int> get _availableBalance async {
|
||||
|
@ -1284,7 +1285,7 @@ class WowneroWallet extends CoinServiceAPI with WalletCache, WalletDB {
|
|||
int get storedChainHeight => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
Balance get balance => _balance!;
|
||||
Balance get balance => _balance ??= getCachedBalance(walletId, coin);
|
||||
Balance? _balance;
|
||||
|
||||
@override
|
||||
|
|
|
@ -27,4 +27,30 @@ mixin WalletCache {
|
|||
value: balance.toJsonIgnoreCoin(),
|
||||
);
|
||||
}
|
||||
|
||||
Balance getCachedBalanceSecondary(String walletId, Coin coin) {
|
||||
final jsonString = DB.instance.get<dynamic>(
|
||||
boxName: walletId,
|
||||
key: DBKeys.cachedBalanceSecondary,
|
||||
) as String?;
|
||||
if (jsonString == null) {
|
||||
return Balance(
|
||||
coin: coin,
|
||||
total: 0,
|
||||
spendable: 0,
|
||||
blockedTotal: 0,
|
||||
pendingSpendable: 0,
|
||||
);
|
||||
}
|
||||
return Balance.fromJson(jsonString, coin);
|
||||
}
|
||||
|
||||
Future<void> updateCachedBalanceSecondary(
|
||||
String walletId, Balance balance) async {
|
||||
await DB.instance.put<dynamic>(
|
||||
boxName: walletId,
|
||||
key: DBKeys.cachedBalanceSecondary,
|
||||
value: balance.toJsonIgnoreCoin(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue