import 'dart:async'; import 'package:isar/isar.dart'; import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/dto/ordinals/inscription_data.dart'; import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart'; import 'package:stackwallet/models/isar/ordinal.dart'; import 'package:stackwallet/services/litescribe_api.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; mixin OrdinalsInterface { late final String _walletId; late final Coin _coin; late final MainDB _db; void initOrdinalsInterface({ required String walletId, required Coin coin, required MainDB db, }) { _walletId = walletId; _coin = coin; _db = db; } final LitescribeAPI litescribeAPI = LitescribeAPI(baseUrl: 'https://litescribe.io/api'); Future refreshInscriptions() async { final uniqueAddresses = await _db .getUTXOs(_walletId) .filter() .addressIsNotNull() .distinctByAddress() .addressProperty() .findAll(); final inscriptions = await _getInscriptionDataFromAddresses(uniqueAddresses.cast()); final ords = inscriptions .map((e) => Ordinal.fromInscriptionData(e, _walletId)) .toList(); await _db.isar.writeTxn(() async { await _db.isar.ordinals .where() .filter() .walletIdEqualTo(_walletId) .deleteAll(); await _db.isar.ordinals.putAll(ords); }); } // // Future> getInscriptionData() async { // try { // final utxos = await _db.getUTXOs(_walletId).findAll(); // final uniqueAddresses = getUniqueAddressesFromUTXOs(utxos); // return await _getInscriptionDataFromAddresses(uniqueAddresses); // } catch (e) { // throw Exception('Error in OrdinalsInterface getInscriptions: $e'); // } // } // // Future> getOrdinals() async { // try { // final utxos = await _db.getUTXOs(_walletId).findAll(); // final uniqueAddresses = getUniqueAddressesFromUTXOs(utxos); // return await getOrdinalsFromAddresses(uniqueAddresses); // } catch (e) { // throw Exception('Error in OrdinalsInterface getOrdinals: $e'); // } // } // // List getUniqueAddressesFromUTXOs(List utxos) { // final Set uniqueAddresses = {}; // for (var utxo in utxos) { // if (utxo.address != null) { // uniqueAddresses.add(utxo.address!); // } // } // return uniqueAddresses.toList(); // } // // Future> getInscriptionDataFromAddress( // String address) async { // List allInscriptions = []; // try { // var inscriptions = await litescribeAPI.getInscriptionsByAddress(address); // allInscriptions.addAll(inscriptions); // } catch (e) { // throw Exception( // 'Error in OrdinalsInterface getInscriptionsByAddress: $e'); // } // return allInscriptions; // } Future> _getInscriptionDataFromAddresses( List addresses) async { List allInscriptions = []; for (String address in addresses) { try { var inscriptions = await litescribeAPI.getInscriptionsByAddress(address); allInscriptions.addAll(inscriptions); } catch (e) { print("Error fetching inscriptions for address $address: $e"); } } return allInscriptions; } // Future> getOrdinalsFromAddress(String address) async { // try { // var inscriptions = await litescribeAPI.getInscriptionsByAddress(address); // return inscriptions // .map((data) => Ordinal.fromInscriptionData(data, _walletId)) // .toList(); // } catch (e) { // throw Exception('Error in OrdinalsInterface getOrdinalsFromAddress: $e'); // } // } // // Future> getOrdinalsFromAddresses(List addresses) async { // List allOrdinals = []; // for (String address in addresses) { // try { // var inscriptions = // await litescribeAPI.getInscriptionsByAddress(address); // allOrdinals.addAll(inscriptions // .map((data) => Ordinal.fromInscriptionData(data, _walletId))); // } catch (e) { // print("Error fetching inscriptions for address $address: $e"); // } // } // return allOrdinals; // } }