Skip to content

Commit

Permalink
fix: Get UTXO ordinal data in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
victorkirov committed Nov 22, 2023
1 parent 5b9b539 commit d7ca530
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
42 changes: 23 additions & 19 deletions api/ordinals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios';
import BitcoinEsploraApiProvider from '../api/esplora/esploraAPiProvider';
import XordApiProvider from '../api/ordinals/provider';
import { INSCRIPTION_REQUESTS_SERVICE_URL, ORDINALS_URL, XVERSE_API_BASE_URL, XVERSE_INSCRIBE_URL } from '../constant';
import {
Account,
Expand Down Expand Up @@ -47,28 +48,31 @@ export async function fetchBtcOrdinalsData(btcAddress: string, network: NetworkT
const btcClient = new BitcoinEsploraApiProvider({
network,
});
const addressUTXOs = await btcClient.getUnspentUtxos(btcAddress);
const ordinals: BtcOrdinal[] = [];
const xordClient = new XordApiProvider({
network,
});

await Promise.all(
addressUTXOs
.filter((utxo) => utxo.status.confirmed) // we can only detect ordinals from confirmed utxos
.map(async (utxo: UTXO) => {
const ordinalContentUrl = `${XVERSE_INSCRIBE_URL(network)}/v1/inscriptions/utxo/${utxo.txid}/${utxo.vout}`;
const [addressUTXOs, inscriptions] = await Promise.all([
btcClient.getUnspentUtxos(btcAddress),
xordClient.getAllInscriptions(btcAddress),
]);
const ordinals: BtcOrdinal[] = [];

const ordinalIds = await axios.get<string[]>(ordinalContentUrl);
const utxoMap = addressUTXOs.reduce((acc, utxo) => {
acc[`${utxo.txid}:${utxo.vout}`] = utxo;
return acc;
}, {} as Record<string, UTXO>);

if (ordinalIds.data.length > 0) {
ordinalIds.data.forEach((ordinalId) => {
ordinals.push({
id: ordinalId,
confirmationTime: utxo.status.block_time || 0,
utxo,
});
});
}
}),
);
inscriptions.forEach((inscription) => {
const utxo = utxoMap[inscription.output];
if (utxo) {
ordinals.push({
id: inscription.id,
confirmationTime: utxo.status.block_time || 0,
utxo,
});
}
});

return ordinals.sort(sortOrdinalsByConfirmationTime);
}
Expand Down
13 changes: 13 additions & 0 deletions api/ordinals/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ export default class OrdinalsApi implements OrdinalsApiProvider {
}
}

async getAllInscriptions(address: string): Promise<ordinalsType.Inscription[]> {
const firstPage = await this.getInscriptions(address, 0, 100);
const results = [...firstPage.results];

// we do this sequentially to avoid rate limiting
while (results.length < firstPage.total) {
const nextPage = await this.getInscriptions(address, results.length, firstPage.limit);
results.push(...nextPage.results);
}

return results;
}

async getInscriptions(address: string, offset: number, limit: number): Promise<ordinalsType.InscriptionsList> {
const url = 'inscriptions';
const params = {
Expand Down

0 comments on commit d7ca530

Please sign in to comment.