stack_wallet/lib/services/mixins/eth_token_cache.dart

71 lines
1.8 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
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';
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';
abstract class TokenCacheKeys {
static String tokenBalance(String contractAddress) {
return "tokenBalanceCache_$contractAddress";
}
}
mixin EthTokenCache {
late final String _walletId;
late final EthContract _token;
void initCache(String walletId, EthContract token) {
_walletId = walletId;
_token = token;
}
// token balance cache
2023-04-10 16:02:19 +00:00
Balance getCachedBalance() {
final jsonString = DB.instance.get<dynamic>(
boxName: _walletId,
key: TokenCacheKeys.tokenBalance(_token.address),
) 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-04-10 16:02:19 +00:00
return Balance.fromJson(
jsonString,
2023-04-05 22:06:31 +00:00
_token.decimals,
);
}
2023-04-10 16:02:19 +00:00
Future<void> updateCachedBalance(Balance balance) async {
await DB.instance.put<dynamic>(
boxName: _walletId,
key: TokenCacheKeys.tokenBalance(_token.address),
value: balance.toJsonIgnoreCoin(),
);
}
}