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-03-01 21:27:10 +00:00
|
|
|
import 'package:stackwallet/services/ethereum/ethereum_api.dart';
|
|
|
|
import 'package:stackwallet/services/mixins/eth_token_cache.dart';
|
2023-04-06 21:24:56 +00:00
|
|
|
import 'package:stackwallet/utilities/amount/amount.dart';
|
2023-03-01 21:27:10 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
|
|
|
|
class CachedEthTokenBalance with EthTokenCache {
|
|
|
|
final String walletId;
|
2023-03-03 00:40:12 +00:00
|
|
|
final EthContract token;
|
2023-03-01 21:27:10 +00:00
|
|
|
|
|
|
|
CachedEthTokenBalance(this.walletId, this.token) {
|
|
|
|
initCache(walletId, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> fetchAndUpdateCachedBalance(String address) async {
|
|
|
|
final response = await EthereumAPI.getWalletTokenBalance(
|
|
|
|
address: address,
|
2023-03-03 00:40:12 +00:00
|
|
|
contractAddress: token.address,
|
2023-03-01 21:27:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response.value != null) {
|
|
|
|
await updateCachedBalance(
|
2023-04-10 16:02:19 +00:00
|
|
|
Balance(
|
2023-04-07 22:02:28 +00:00
|
|
|
total: response.value!,
|
|
|
|
spendable: response.value!,
|
2023-04-05 22:06:31 +00:00
|
|
|
blockedTotal: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: token.decimals,
|
|
|
|
),
|
|
|
|
pendingSpendable: Amount(
|
|
|
|
rawValue: BigInt.zero,
|
|
|
|
fractionDigits: token.decimals,
|
|
|
|
),
|
2023-03-01 21:27:10 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Logging.instance.log(
|
|
|
|
"CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|