stack_wallet/lib/services/mixins/ordinals_interface.dart

160 lines
5.6 KiB
Dart
Raw Normal View History

2023-07-20 20:56:11 +00:00
import 'package:isar/isar.dart';
import 'package:stackwallet/db/isar/main_db.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/utxo.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2023-07-20 19:38:56 +00:00
// ord-litecoin-specific imports
// import 'package:stackwallet/dto/ordinals/feed_response.dart';
// import 'package:stackwallet/dto/ordinals/inscription_response.dart';
// import 'package:stackwallet/dto/ordinals/sat_response.dart';
// import 'package:stackwallet/dto/ordinals/transaction_response.dart';
// import 'package:stackwallet/dto/ordinals/output_response.dart';
// import 'package:stackwallet/dto/ordinals/address_response.dart';
// import 'package:stackwallet/dto/ordinals/block_response.dart';
// import 'package:stackwallet/dto/ordinals/content_response.dart';
// import 'package:stackwallet/dto/ordinals/preview_response.dart';
// import 'package:stackwallet/services/ordinals_api.dart';
import 'package:stackwallet/dto/ordinals/address_inscription_response.dart'; // verbose due to Litescribe being the 2nd API
import 'package:stackwallet/services/litescribe_api.dart';
2023-07-20 20:56:11 +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-20 19:38:56 +00:00
final LitescribeAPI litescribeAPI = LitescribeAPI(baseUrl: 'https://litescribe.io/api');
Future<List<AddressInscription>> getInscriptionsByAddress(String address) async {
try {
var response = await litescribeAPI.getInscriptionsByAddress(address);
print("Found ${response.result.total} inscription${response.result.total > 1 ? 's' : ''} at address $address"); // TODO disable (POC)
2023-07-20 19:38:56 +00:00
return response.result.list;
} catch (e) {
throw Exception('Error in OrdinalsInterface getInscriptionsByAddress: $e');
}
}
void refreshInscriptions() async {
2023-07-20 20:56:11 +00:00
List<dynamic> _inscriptions;
final utxos = await _db.getUTXOs(_walletId).findAll();
final uniqueAddresses = getUniqueAddressesFromUTXOs(utxos);
for (String address in uniqueAddresses) {
// TODO fetch all inscriptions from all addresses
// TODO save those inscriptions to isar, which a StreamBuilder will be "subscribed"-to
}
// TODO get all inscriptions at all addresses in wallet
var inscriptions = await getInscriptionsByAddress('ltc1qk4e8hdq5w6rvk5xvkxajjak78v45pkul8a2cg9');
for (var inscription in inscriptions) {
print(inscription);
print(inscription.address);
print(inscription.content);
print(inscription.inscriptionId);
print(inscription.inscriptionNumber);
}
}
2023-07-20 20:56:11 +00:00
List<String> getUniqueAddressesFromUTXOs(List<UTXO> utxos) {
final Set<String> uniqueAddresses = <String>{};
for (var utxo in utxos) {
if (utxo.address != null) {
uniqueAddresses.add(utxo.address!);
}
}
return uniqueAddresses.toList();
}
2023-07-20 19:38:56 +00:00
/* // ord-litecoin interface
2023-07-19 22:13:54 +00:00
final OrdinalsAPI ordinalsAPI = OrdinalsAPI(baseUrl: 'https://ord-litecoin.stackwallet.com');
Future<FeedResponse> fetchLatestInscriptions() async {
try {
final feedResponse = await ordinalsAPI.getLatestInscriptions();
// Process the feedResponse data as needed
2023-07-19 22:13:54 +00:00
// print('Latest Inscriptions:');
// for (var inscription in feedResponse.inscriptions) {
// print('Title: ${inscription.title}, Href: ${inscription.href}');
// }
return feedResponse;
} catch (e) {
// Handle errors
2023-07-19 22:13:54 +00:00
throw Exception('Error in OrdinalsInterface fetchLatestInscriptions: $e');
}
}
Future<InscriptionResponse> getInscriptionDetails(String inscriptionId) async {
try {
return await ordinalsAPI.getInscriptionDetails(inscriptionId);
} catch (e) {
throw Exception('Error in OrdinalsInterface getInscriptionDetails: $e');
}
}
Future<SatResponse> getSatDetails(int satNumber) async {
try {
return await ordinalsAPI.getSatDetails(satNumber);
} catch (e) {
throw Exception('Error in OrdinalsInterface getSatDetails: $e');
}
}
Future<TransactionResponse> getTransaction(String transactionId) async {
try {
print(1);
return await ordinalsAPI.getTransaction(transactionId);
} catch (e) {
throw Exception('Error in OrdinalsInterface getTransaction: $e');
}
}
Future<OutputResponse> getTransactionOutputs(String transactionId) async {
try {
return await ordinalsAPI.getTransactionOutputs(transactionId);
} catch (e) {
throw Exception('Error in OrdinalsInterface getTransactionOutputs: $e');
}
}
Future<AddressResponse> getInscriptionsByAddress(String address) async {
try {
return await ordinalsAPI.getInscriptionsByAddress(address);
} catch (e) {
throw Exception('Error in OrdinalsInterface getInscriptionsByAddress: $e');
}
}
Future<BlockResponse> getBlock(int blockNumber) async {
try {
return await ordinalsAPI.getBlock(blockNumber);
} catch (e) {
throw Exception('Error in OrdinalsInterface getBlock: $e');
}
}
Future<ContentResponse> getInscriptionContent(String inscriptionId) async {
try {
return await ordinalsAPI.getInscriptionContent(inscriptionId);
} catch (e) {
throw Exception('Error in OrdinalsInterface getInscriptionContent: $e');
}
}
Future<PreviewResponse> getInscriptionPreview(String inscriptionId) async {
try {
return await ordinalsAPI.getInscriptionPreview(inscriptionId);
} catch (e) {
throw Exception('Error in OrdinalsInterface getInscriptionPreview: $e');
}
}
2023-07-20 19:38:56 +00:00
*/ // /ord-litecoin interface
}