2023-03-01 21:52:13 +00:00
|
|
|
import 'package:stackwallet/db/hive/db.dart';
|
2023-01-12 18:15:28 +00:00
|
|
|
import 'package:stackwallet/models/balance.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
|
|
|
|
mixin WalletCache {
|
2023-01-12 20:57:07 +00:00
|
|
|
late final String _walletId;
|
|
|
|
late final Coin _coin;
|
|
|
|
|
|
|
|
void initCache(String walletId, Coin coin) {
|
|
|
|
_walletId = walletId;
|
|
|
|
_coin = coin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cached wallet id
|
|
|
|
String? getCachedId() {
|
|
|
|
return DB.instance.get<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.id,
|
|
|
|
) as String?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateCachedId(String? id) async {
|
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.id,
|
|
|
|
value: id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cached Chain Height
|
|
|
|
int getCachedChainHeight() {
|
|
|
|
return DB.instance.get<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.storedChainHeight,
|
|
|
|
) as int? ??
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateCachedChainHeight(int height) async {
|
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.storedChainHeight,
|
|
|
|
value: height,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// wallet favorite flag
|
|
|
|
bool getCachedIsFavorite() {
|
|
|
|
return DB.instance.get<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.isFavorite,
|
|
|
|
) as bool? ??
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateCachedIsFavorite(bool isFavorite) async {
|
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.isFavorite,
|
|
|
|
value: isFavorite,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// main balance cache
|
|
|
|
Balance getCachedBalance() {
|
2023-01-12 18:15:28 +00:00
|
|
|
final jsonString = DB.instance.get<dynamic>(
|
2023-01-12 20:57:07 +00:00
|
|
|
boxName: _walletId,
|
2023-01-12 18:15:28 +00:00
|
|
|
key: DBKeys.cachedBalance,
|
|
|
|
) as String?;
|
|
|
|
if (jsonString == null) {
|
|
|
|
return Balance(
|
2023-01-12 20:57:07 +00:00
|
|
|
coin: _coin,
|
2023-01-12 18:15:28 +00:00
|
|
|
total: 0,
|
|
|
|
spendable: 0,
|
|
|
|
blockedTotal: 0,
|
|
|
|
pendingSpendable: 0,
|
|
|
|
);
|
|
|
|
}
|
2023-01-12 20:57:07 +00:00
|
|
|
return Balance.fromJson(jsonString, _coin);
|
2023-01-12 18:15:28 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 20:57:07 +00:00
|
|
|
Future<void> updateCachedBalance(Balance balance) async {
|
2023-01-12 18:15:28 +00:00
|
|
|
await DB.instance.put<dynamic>(
|
2023-01-12 20:57:07 +00:00
|
|
|
boxName: _walletId,
|
2023-01-12 18:15:28 +00:00
|
|
|
key: DBKeys.cachedBalance,
|
|
|
|
value: balance.toJsonIgnoreCoin(),
|
|
|
|
);
|
|
|
|
}
|
2023-01-12 19:21:03 +00:00
|
|
|
|
2023-01-12 20:57:07 +00:00
|
|
|
// secondary balance cache for coins such as firo
|
|
|
|
Balance getCachedBalanceSecondary() {
|
2023-01-12 19:21:03 +00:00
|
|
|
final jsonString = DB.instance.get<dynamic>(
|
2023-01-12 20:57:07 +00:00
|
|
|
boxName: _walletId,
|
2023-01-12 19:21:03 +00:00
|
|
|
key: DBKeys.cachedBalanceSecondary,
|
|
|
|
) as String?;
|
|
|
|
if (jsonString == null) {
|
|
|
|
return Balance(
|
2023-01-12 20:57:07 +00:00
|
|
|
coin: _coin,
|
2023-01-12 19:21:03 +00:00
|
|
|
total: 0,
|
|
|
|
spendable: 0,
|
|
|
|
blockedTotal: 0,
|
|
|
|
pendingSpendable: 0,
|
|
|
|
);
|
|
|
|
}
|
2023-01-12 20:57:07 +00:00
|
|
|
return Balance.fromJson(jsonString, _coin);
|
2023-01-12 19:21:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 20:57:07 +00:00
|
|
|
Future<void> updateCachedBalanceSecondary(Balance balance) async {
|
2023-01-12 19:21:03 +00:00
|
|
|
await DB.instance.put<dynamic>(
|
2023-01-12 20:57:07 +00:00
|
|
|
boxName: _walletId,
|
2023-01-12 19:21:03 +00:00
|
|
|
key: DBKeys.cachedBalanceSecondary,
|
|
|
|
value: balance.toJsonIgnoreCoin(),
|
|
|
|
);
|
|
|
|
}
|
2023-03-23 15:39:06 +00:00
|
|
|
|
|
|
|
// Ethereum specific
|
|
|
|
List<String> getWalletTokenContractAddresses() {
|
|
|
|
return DB.instance.get<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.ethTokenContracts,
|
|
|
|
) as List<String>? ??
|
|
|
|
[];
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateWalletTokenContractAddresses(
|
|
|
|
List<String> contractAddresses) async {
|
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: _walletId,
|
|
|
|
key: DBKeys.ethTokenContracts,
|
|
|
|
value: contractAddresses,
|
|
|
|
);
|
|
|
|
}
|
2023-01-12 18:15:28 +00:00
|
|
|
}
|