stack_wallet/lib/services/ethereum/cached_eth_token_balance.dart

64 lines
1.7 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
*
*/
import 'package:isar/isar.dart';
import '../../db/isar/main_db.dart';
import '../../models/balance.dart';
import '../../models/isar/models/ethereum/eth_contract.dart';
import 'ethereum_api.dart';
import '../../utilities/amount/amount.dart';
import '../../utilities/logger.dart';
import '../../wallets/isar/models/token_wallet_info.dart';
2023-03-01 21:27:10 +00:00
class CachedEthTokenBalance {
2023-03-01 21:27:10 +00:00
final String walletId;
final EthContract token;
2023-03-01 21:27:10 +00:00
CachedEthTokenBalance(this.walletId, this.token);
2023-03-01 21:27:10 +00:00
Future<void> fetchAndUpdateCachedBalance(
String address,
MainDB mainDB,
) async {
2023-03-01 21:27:10 +00:00
final response = await EthereumAPI.getWalletTokenBalance(
address: address,
contractAddress: token.address,
2023-03-01 21:27:10 +00:00
);
final info = await mainDB.isar.tokenWalletInfo
.where()
.walletIdTokenAddressEqualTo(walletId, token.address)
.findFirst();
if (response.value != null && info != null) {
await info.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
),
isar: mainDB.isar,
2023-03-01 21:27:10 +00:00
);
} else {
Logging.instance.log(
"CachedEthTokenBalance.fetchAndUpdateCachedBalance failed: ${response.exception}",
level: LogLevel.Warning,
);
}
}
}