2023-03-01 21:52:13 +00:00
|
|
|
import 'package:stackwallet/db/hive/db.dart';
|
2023-03-03 00:40:12 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart';
|
2023-02-27 16:01:06 +00:00
|
|
|
import 'package:stackwallet/models/token_balance.dart';
|
|
|
|
|
|
|
|
abstract class _Keys {
|
|
|
|
static String tokenBalance(String contractAddress) {
|
|
|
|
return "tokenBalanceCache_$contractAddress";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mixin EthTokenCache {
|
|
|
|
late final String _walletId;
|
2023-03-03 00:40:12 +00:00
|
|
|
late final EthContract _token;
|
2023-02-27 16:01:06 +00:00
|
|
|
|
2023-03-03 00:40:12 +00:00
|
|
|
void initCache(String walletId, EthContract token) {
|
2023-02-27 16:01:06 +00:00
|
|
|
_walletId = walletId;
|
|
|
|
_token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
// token balance cache
|
|
|
|
TokenBalance getCachedBalance() {
|
|
|
|
final jsonString = DB.instance.get<dynamic>(
|
|
|
|
boxName: _walletId,
|
2023-03-03 00:40:12 +00:00
|
|
|
key: _Keys.tokenBalance(_token.address),
|
2023-02-27 16:01:06 +00:00
|
|
|
) as String?;
|
|
|
|
if (jsonString == null) {
|
|
|
|
return TokenBalance(
|
2023-03-03 00:40:12 +00:00
|
|
|
contractAddress: _token.address,
|
2023-02-27 16:01:06 +00:00
|
|
|
decimalPlaces: _token.decimals,
|
|
|
|
total: 0,
|
|
|
|
spendable: 0,
|
|
|
|
blockedTotal: 0,
|
|
|
|
pendingSpendable: 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return TokenBalance.fromJson(
|
|
|
|
jsonString,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateCachedBalance(TokenBalance balance) async {
|
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: _walletId,
|
2023-03-03 00:40:12 +00:00
|
|
|
key: _Keys.tokenBalance(_token.address),
|
2023-02-27 16:01:06 +00:00
|
|
|
value: balance.toJsonIgnoreCoin(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|