2023-07-20 21:49:26 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-07-20 20:56:11 +00:00
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:stackwallet/db/isar/main_db.dart';
|
2023-07-21 16:48:31 +00:00
|
|
|
import 'package:stackwallet/dto/ordinals/inscription_data.dart';
|
2023-07-20 20:56:11 +00:00
|
|
|
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
|
2023-07-21 23:51:04 +00:00
|
|
|
import 'package:stackwallet/models/isar/ordinal.dart';
|
2023-07-20 19:38:56 +00:00
|
|
|
import 'package:stackwallet/services/litescribe_api.dart';
|
2023-07-21 16:48:31 +00:00
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
2023-07-19 18:03:17 +00:00
|
|
|
|
2023-07-18 16:15:05 +00:00
|
|
|
mixin OrdinalsInterface {
|
2023-07-20 20:56:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-07-21 23:51:04 +00:00
|
|
|
|
|
|
|
final LitescribeAPI litescribeAPI =
|
|
|
|
LitescribeAPI(baseUrl: 'https://litescribe.io/api');
|
2023-07-20 19:38:56 +00:00
|
|
|
|
2023-07-22 00:43:59 +00:00
|
|
|
Future<void> refreshInscriptions() async {
|
|
|
|
final uniqueAddresses = await _db
|
|
|
|
.getUTXOs(_walletId)
|
|
|
|
.filter()
|
|
|
|
.addressIsNotNull()
|
|
|
|
.distinctByAddress()
|
|
|
|
.addressProperty()
|
|
|
|
.findAll();
|
|
|
|
final inscriptions =
|
|
|
|
await _getInscriptionDataFromAddresses(uniqueAddresses.cast<String>());
|
2023-07-20 20:32:39 +00:00
|
|
|
|
2023-07-22 00:43:59 +00:00
|
|
|
final ords = inscriptions
|
|
|
|
.map((e) => Ordinal.fromInscriptionData(e, _walletId))
|
|
|
|
.toList();
|
2023-07-21 16:48:31 +00:00
|
|
|
|
2023-07-22 00:43:59 +00:00
|
|
|
await _db.isar.writeTxn(() async {
|
|
|
|
await _db.isar.ordinals
|
|
|
|
.where()
|
|
|
|
.filter()
|
|
|
|
.walletIdEqualTo(_walletId)
|
|
|
|
.deleteAll();
|
|
|
|
await _db.isar.ordinals.putAll(ords);
|
|
|
|
});
|
2023-07-21 16:48:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 00:43:59 +00:00
|
|
|
Future<List<InscriptionData>> _getInscriptionDataFromAddresses(
|
2023-07-21 23:51:04 +00:00
|
|
|
List<String> addresses) async {
|
2023-07-20 21:30:39 +00:00
|
|
|
List<InscriptionData> allInscriptions = [];
|
|
|
|
for (String address in addresses) {
|
|
|
|
try {
|
2023-07-21 23:51:04 +00:00
|
|
|
var inscriptions =
|
|
|
|
await litescribeAPI.getInscriptionsByAddress(address);
|
2023-07-20 21:30:39 +00:00
|
|
|
allInscriptions.addAll(inscriptions);
|
|
|
|
} catch (e) {
|
2023-07-23 02:09:42 +00:00
|
|
|
throw Exception("Error fetching inscriptions for address $address: $e");
|
2023-07-20 21:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return allInscriptions;
|
|
|
|
}
|
2023-07-23 03:14:18 +00:00
|
|
|
|
|
|
|
// check if an inscription is in a given <UTXO> output
|
|
|
|
Future<bool> inscriptionInOutput(UTXO output) async {
|
|
|
|
if (output.address != null) {
|
|
|
|
var inscriptions =
|
|
|
|
await litescribeAPI.getInscriptionsByAddress("${output.address}");
|
|
|
|
if (inscriptions.isNotEmpty) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw UnimplementedError(
|
|
|
|
'TODO look up utxo without address. utxo->txid:output->address');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if an inscription is in a given <UTXO> output
|
|
|
|
Future<bool> inscriptionInAddress(String address) async {
|
|
|
|
var inscriptions = await litescribeAPI.getInscriptionsByAddress(address);
|
|
|
|
if (inscriptions.isNotEmpty) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-07-21 23:51:04 +00:00
|
|
|
}
|