From e3c0f58abc8fabf9fe24b72bc76c1862d3128bc5 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 4 May 2023 14:33:19 -0600 Subject: [PATCH] custom block explorer isar model/schema --- lib/db/isar/main_db.dart | 21 +++++++++++++ lib/models/isar/models/block_explorer.dart | 35 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 lib/models/isar/models/block_explorer.dart diff --git a/lib/db/isar/main_db.dart b/lib/db/isar/main_db.dart index 76a60676f..33b7e606f 100644 --- a/lib/db/isar/main_db.dart +++ b/lib/db/isar/main_db.dart @@ -2,6 +2,7 @@ import 'package:decimal/decimal.dart'; import 'package:flutter_native_splash/cli_commands.dart'; import 'package:isar/isar.dart'; import 'package:stackwallet/exceptions/main_db/main_db_exception.dart'; +import 'package:stackwallet/models/isar/models/block_explorer.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; @@ -33,6 +34,7 @@ class MainDB { AddressSchema, AddressLabelSchema, EthContractSchema, + TransactionBlockExplorerSchema, ], directory: (await StackFileSystem.applicationIsarDirectory()).path, // inspector: kDebugMode, @@ -43,6 +45,25 @@ class MainDB { return true; } + // tx block explorers + TransactionBlockExplorer? getTransactionBlockExplorer({required Coin coin}) { + return isar.transactionBlockExplorers + .where() + .tickerEqualTo(coin.ticker) + .findFirstSync(); + } + + Future putTransactionBlockExplorer( + TransactionBlockExplorer explorer) async { + try { + return await isar.writeTxn(() async { + return await isar.transactionBlockExplorers.put(explorer); + }); + } catch (e) { + throw MainDBException("failed putTransactionBlockExplorer: $explorer", e); + } + } + // addresses QueryBuilder getAddresses( String walletId) => diff --git a/lib/models/isar/models/block_explorer.dart b/lib/models/isar/models/block_explorer.dart new file mode 100644 index 000000000..cf01fa5e2 --- /dev/null +++ b/lib/models/isar/models/block_explorer.dart @@ -0,0 +1,35 @@ +import 'package:isar/isar.dart'; +import 'package:stackwallet/utilities/enums/coin_enum.dart'; + +part 'block_explorer.g.dart'; + +@collection +class TransactionBlockExplorer { + TransactionBlockExplorer({ + required this.ticker, + required this.url, + }); + + Id id = Isar.autoIncrement; + + @Index(unique: true, replace: true) + late final String ticker; + + late final String url; + + @ignore + Coin? get coin { + try { + return coinFromTickerCaseInsensitive(ticker); + } catch (_) { + return null; + } + } + + Uri? getUrlFor({required String txid}) => Uri.tryParse( + url.replaceFirst( + "%5BTXID%5D", + txid, + ), + ); +}