litescribe api and demo

This commit is contained in:
sneurlax 2023-07-20 14:38:56 -05:00
parent d02b7f7ad4
commit f046912c89
6 changed files with 176 additions and 13 deletions

View file

@ -0,0 +1,91 @@
import 'package:stackwallet/dto/ordinals/litescribe_response.dart';
class AddressInscriptionResponse extends LitescribeResponse<AddressInscriptionResponse> {
final int status;
final String message;
final AddressInscriptionResult result;
AddressInscriptionResponse({
required this.status,
required this.message,
required this.result,
});
factory AddressInscriptionResponse.fromJson(Map<String, dynamic> json) {
return AddressInscriptionResponse(
status: json['status'] as int,
message: json['message'] as String,
result: AddressInscriptionResult.fromJson(json['result'] as Map<String, dynamic>),
);
}
}
class AddressInscriptionResult {
final List<AddressInscription> list;
final int total;
AddressInscriptionResult({
required this.list,
required this.total,
});
factory AddressInscriptionResult.fromJson(Map<String, dynamic> json) {
return AddressInscriptionResult(
list: (json['list'] as List).map((item) => AddressInscription.fromJson(item as Map<String, dynamic>)).toList(),
total: json['total'] as int,
);
}
}
class AddressInscription {
final String inscriptionId;
final int inscriptionNumber;
final String address;
final String preview;
final String content;
final int contentLength;
final String contentType;
final String contentBody;
final int timestamp;
final String genesisTransaction;
final String location;
final String output;
final int outputValue;
final int offset;
AddressInscription({
required this.inscriptionId,
required this.inscriptionNumber,
required this.address,
required this.preview,
required this.content,
required this.contentLength,
required this.contentType,
required this.contentBody,
required this.timestamp,
required this.genesisTransaction,
required this.location,
required this.output,
required this.outputValue,
required this.offset,
});
factory AddressInscription.fromJson(Map<String, dynamic> json) {
return AddressInscription(
inscriptionId: json['inscriptionId'] as String,
inscriptionNumber: json['inscriptionNumber'] as int,
address: json['address'] as String,
preview: json['preview'] as String,
content: json['content'] as String,
contentLength: json['contentLength'] as int,
contentType: json['contentType'] as String,
contentBody: json['contentBody'] as String,
timestamp: json['timestamp'] as int,
genesisTransaction: json['genesisTransaction'] as String,
location: json['location'] as String,
output: json['output'] as String,
outputValue: json['outputValue'] as int,
offset: json['offset'] as int,
);
}
}

View file

@ -0,0 +1,6 @@
class LitescribeResponse<T> {
final T? data;
final String? error;
LitescribeResponse({this.data, this.error});
}

View file

@ -182,7 +182,10 @@ class _OrdinalsViewState extends ConsumerState<OrdinalsView> with OrdinalsInterf
height: 16, height: 16,
), ),
TextButton(onPressed: () async { TextButton(onPressed: () async {
await getTransaction('ed5a5c4e555e204768ec54c049ae0b01c86fdcc8b126a9d100c4dff745e7d3ca'); // await fetchLatestInscriptions();
// await getTransaction('ed5a5c4e555e204768ec54c049ae0b01c86fdcc8b126a9d100c4dff745e7d3ca');
// await getBlock('31278055ba414fe6dbed75e4a77e841da4481972ac09bd2a214c445da1a44aad');
await getInscriptionsByAddress('ltc1qk4e8hdq5w6rvk5xvkxajjak78v45pkul8a2cg9');
}, child: Text( }, child: Text(
"Test", "Test",
style: STextStyles.navBarTitle(context), style: STextStyles.navBarTitle(context),

View file

@ -0,0 +1,45 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:stackwallet/dto/ordinals/address_inscription_response.dart';
import 'package:stackwallet/dto/ordinals/litescribe_response.dart';
class LitescribeAPI {
static final LitescribeAPI _instance = LitescribeAPI._internal();
factory LitescribeAPI({required String baseUrl}) {
_instance.baseUrl = baseUrl;
return _instance;
}
LitescribeAPI._internal();
late String baseUrl;
Future<LitescribeResponse> _getResponse(String endpoint) async {
final response = await http.get(Uri.parse('$baseUrl$endpoint'));
if (response.statusCode == 200) {
return LitescribeResponse(data: _validateJson(response.body));
} else {
throw Exception('LitescribeAPI _getResponse exception: Failed to load data');
}
}
Map<String, dynamic> _validateJson(String responseBody) {
final parsed = jsonDecode(responseBody);
if (parsed is Map<String, dynamic>) {
return parsed;
} else {
throw const FormatException('LitescribeAPI _validateJson exception: Invalid JSON format');
}
}
Future<AddressInscriptionResponse> getInscriptionsByAddress(String address, {int cursor = 0, int size = 1000}) async { // size = 1000 = hardcoded limit as default limit to inscriptions returned from API call, TODO increase limit if returned inscriptions = limit
final response = await _getResponse('/address/inscriptions?address=$address&cursor=$cursor&size=$size');
try {
return AddressInscriptionResponse.fromJson(response.data as Map<String, dynamic>);
} catch(e) {
throw const FormatException('LitescribeAPI getInscriptionsByAddress exception: AddressInscriptionResponse.fromJson failure');
}
}
}

View file

@ -1,15 +1,32 @@
import 'package:stackwallet/dto/ordinals/feed_response.dart'; // ord-litecoin-specific imports
import 'package:stackwallet/dto/ordinals/inscription_response.dart'; // import 'package:stackwallet/dto/ordinals/feed_response.dart';
import 'package:stackwallet/dto/ordinals/sat_response.dart'; // import 'package:stackwallet/dto/ordinals/inscription_response.dart';
import 'package:stackwallet/dto/ordinals/transaction_response.dart'; // import 'package:stackwallet/dto/ordinals/sat_response.dart';
import 'package:stackwallet/dto/ordinals/output_response.dart'; // import 'package:stackwallet/dto/ordinals/transaction_response.dart';
import 'package:stackwallet/dto/ordinals/address_response.dart'; // import 'package:stackwallet/dto/ordinals/output_response.dart';
import 'package:stackwallet/dto/ordinals/block_response.dart'; // import 'package:stackwallet/dto/ordinals/address_response.dart';
import 'package:stackwallet/dto/ordinals/content_response.dart'; // import 'package:stackwallet/dto/ordinals/block_response.dart';
import 'package:stackwallet/dto/ordinals/preview_response.dart'; // import 'package:stackwallet/dto/ordinals/content_response.dart';
import 'package:stackwallet/services/ordinals_api.dart'; // Assuming this import is necessary // 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';
mixin OrdinalsInterface { mixin OrdinalsInterface {
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} inscriptions at address $address"); // TODO disable (POC)
return response.result.list;
} catch (e) {
throw Exception('Error in OrdinalsInterface getInscriptionsByAddress: $e');
}
}
/* // ord-litecoin interface
final OrdinalsAPI ordinalsAPI = OrdinalsAPI(baseUrl: 'https://ord-litecoin.stackwallet.com'); final OrdinalsAPI ordinalsAPI = OrdinalsAPI(baseUrl: 'https://ord-litecoin.stackwallet.com');
Future<FeedResponse> fetchLatestInscriptions() async { Future<FeedResponse> fetchLatestInscriptions() async {
@ -91,4 +108,5 @@ mixin OrdinalsInterface {
throw Exception('Error in OrdinalsInterface getInscriptionPreview: $e'); throw Exception('Error in OrdinalsInterface getInscriptionPreview: $e');
} }
} }
*/ // /ord-litecoin interface
} }

View file

@ -72,8 +72,8 @@ class OrdinalsAPI {
return AddressResponse.fromJson(response); return AddressResponse.fromJson(response);
} }
Future<BlockResponse> getBlock(int blockNumber) async { Future<BlockResponse> getBlock(String blockHash) async {
final response = await _getResponse('/block/$blockNumber'); final response = await _getResponse('/block/$blockHash');
return BlockResponse.fromJson(response); return BlockResponse.fromJson(response);
} }