stack_wallet/lib/services/ethereum/cached_eth_token_balance.dart

45 lines
1.3 KiB
Dart
Raw Normal View History

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-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;
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,
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,
);
}
}
}