From a322c1395432a18f1fc1f53eff53037ec7e3499b Mon Sep 17 00:00:00 2001 From: sneurlax Date: Thu, 20 Jul 2023 14:46:15 -0500 Subject: [PATCH] return inscriptions in batches of 1000 --- lib/services/litescribe_api.dart | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/services/litescribe_api.dart b/lib/services/litescribe_api.dart index 507492ee6..edb46f1d2 100644 --- a/lib/services/litescribe_api.dart +++ b/lib/services/litescribe_api.dart @@ -34,12 +34,29 @@ class LitescribeAPI { } } - Future 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 + Future getInscriptionsByAddress(String address, {int cursor = 0, int size = 1000}) async { + // size param determines how many inscriptions are returned per response + // default of 1000 is used to cover most addresses (I assume) + // if the total number of inscriptions at the address exceeds the length of the list of inscriptions returned, another call with a higher size is made + final int defaultLimit = 1000; final response = await _getResponse('/address/inscriptions?address=$address&cursor=$cursor&size=$size'); - try { - return AddressInscriptionResponse.fromJson(response.data as Map); - } catch(e) { - throw const FormatException('LitescribeAPI getInscriptionsByAddress exception: AddressInscriptionResponse.fromJson failure'); + + // Check if the number of returned inscriptions equals the limit + final list = response.data['result']['list'] as List; + final int total = response.data['result']['total'] as int; + final int currentSize = list.length; + + if (currentSize == size && currentSize < total) { + // If the number of returned inscriptions equals the limit and there are more inscriptions available, + // increase the size to fetch all inscriptions. + return getInscriptionsByAddress(address, cursor: cursor, size: total+1); // potential off-by-one error, but should be safe + // TODO don't re-request the same inscriptions previously returned; increment cursor (probably) by size and only request the rest. ex: cursor=0 size=1000 probably returns inscriptions 0-999, so set cursor=size (or size-1?) to get 1000-1999 + } else { + try { + return AddressInscriptionResponse.fromJson(response.data as Map); + } catch (e) { + throw const FormatException('LitescribeAPI getInscriptionsByAddress exception: AddressInscriptionResponse.fromJson failure'); + } } } } \ No newline at end of file