2023-03-01 21:52:13 +00:00
|
|
|
import 'package:stackwallet/db/hive/db.dart';
|
2023-04-10 16:02:19 +00:00
|
|
|
import 'package:stackwallet/models/balance.dart';
|
2023-03-03 00:40:12 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart';
|
2023-04-06 21:24:56 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-02-27 16:01:06 +00:00
|
|
|
|
2023-03-28 22:18:11 +00:00
|
|
|
abstract class TokenCacheKeys {
|
2023-02-27 16:01:06 +00:00
|
|
|
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
|
2023-04-10 16:02:19 +00:00
|
|
|
Balance getCachedBalance() {
|
2023-02-27 16:01:06 +00:00
|
|
|
final jsonString = DB.instance.get<dynamic>(
|
|
|
|
boxName: _walletId,
|
2023-03-28 22:18:11 +00:00
|
|
|
key: TokenCacheKeys.tokenBalance(_token.address),
|
2023-02-27 16:01:06 +00:00
|
|
|
) as String?;
|
|
|
|
if (jsonString == null) {
|
2023-04-10 16:02:19 +00:00
|
|
|
return Balance(
|
2023-04-05 22:06:31 +00:00
|
|
|
total: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: _token.decimals,
|
|
|
|
),
|
|
|
|
spendable: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: _token.decimals,
|
|
|
|
),
|
|
|
|
blockedTotal: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: _token.decimals,
|
|
|
|
),
|
|
|
|
pendingSpendable: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: _token.decimals,
|
|
|
|
),
|
2023-02-27 16:01:06 +00:00
|
|
|
);
|
|
|
|
}
|
2023-04-10 16:02:19 +00:00
|
|
|
return Balance.fromJson(
|
2023-02-27 16:01:06 +00:00
|
|
|
jsonString,
|
2023-04-05 22:06:31 +00:00
|
|
|
_token.decimals,
|
2023-02-27 16:01:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-10 16:02:19 +00:00
|
|
|
Future<void> updateCachedBalance(Balance balance) async {
|
2023-02-27 16:01:06 +00:00
|
|
|
await DB.instance.put<dynamic>(
|
|
|
|
boxName: _walletId,
|
2023-03-28 22:18:11 +00:00
|
|
|
key: TokenCacheKeys.tokenBalance(_token.address),
|
2023-02-27 16:01:06 +00:00
|
|
|
value: balance.toJsonIgnoreCoin(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|