token wallet info cache refactor from hive to isar

This commit is contained in:
julian 2024-01-10 11:58:22 -06:00
parent 6d9be31e76
commit 61e687585e
3 changed files with 1257 additions and 0 deletions

View file

@ -22,6 +22,7 @@ import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
import 'package:stackwallet/wallets/isar/models/spark_coin.dart';
import 'package:stackwallet/wallets/isar/models/token_wallet_info.dart';
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
import 'package:stackwallet/wallets/isar/models/wallet_info_meta.dart';
import 'package:tuple/tuple.dart';
@ -65,6 +66,7 @@ class MainDB {
TransactionV2Schema,
SparkCoinSchema,
WalletInfoMetaSchema,
TokenWalletInfoSchema,
],
directory: (await StackFileSystem.applicationIsarDirectory()).path,
// inspector: kDebugMode,

View file

@ -0,0 +1,93 @@
import 'package:isar/isar.dart';
import 'package:stackwallet/models/balance.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/wallets/isar/isar_id_interface.dart';
part 'token_wallet_info.g.dart';
@Collection(accessor: "tokenWalletInfo", inheritance: false)
class TokenWalletInfo implements IsarId {
@override
Id id = Isar.autoIncrement;
@Index(
unique: true,
replace: false,
composite: [
CompositeIndex("tokenAddress"),
],
)
final String walletId;
final String tokenAddress;
final int tokenFractionDigits;
final String? cachedBalanceJsonString;
TokenWalletInfo({
required this.walletId,
required this.tokenAddress,
required this.tokenFractionDigits,
this.cachedBalanceJsonString,
});
EthContract getContract(Isar isar) =>
isar.ethContracts.where().addressEqualTo(tokenAddress).findFirstSync()!;
// token balance cache
Balance getCachedBalance() {
if (cachedBalanceJsonString == null) {
return Balance(
total: Amount.zeroWith(
fractionDigits: tokenFractionDigits,
),
spendable: Amount.zeroWith(
fractionDigits: tokenFractionDigits,
),
blockedTotal: Amount.zeroWith(
fractionDigits: tokenFractionDigits,
),
pendingSpendable: Amount.zeroWith(
fractionDigits: tokenFractionDigits,
),
);
}
return Balance.fromJson(
cachedBalanceJsonString!,
tokenFractionDigits,
);
}
Future<void> updateCachedBalance(
Balance balance, {
required Isar isar,
}) async {
// // ensure we are updating using the latest entry of this in the db
final thisEntry = await isar.tokenWalletInfo
.where()
.walletIdEqualToTokenAddressNotEqualTo(walletId, tokenAddress)
.findFirst();
if (thisEntry == null) {
throw Exception(
"Attempted to update cached token balance before object was saved in db",
);
} else {
await isar.writeTxn(() async {
await isar.tokenWalletInfo.deleteByWalletIdTokenAddress(
walletId,
tokenAddress,
);
await isar.tokenWalletInfo.put(
TokenWalletInfo(
walletId: walletId,
tokenAddress: tokenAddress,
tokenFractionDigits: tokenFractionDigits,
cachedBalanceJsonString: balance.toJsonIgnoreCoin(),
),
);
});
}
}
}

File diff suppressed because it is too large Load diff