Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get UTXO ordinal data in batches #291

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 Expand Up @@ -97,8 +101,8 @@
timeout: 30000,
transformResponse: [(data) => parseOrdinalTextContentData(data)],
})
.then((response) => response!.data)

Check warning on line 104 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

Forbidden non-null assertion
.catch((error) => {

Check warning on line 105 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

'error' is defined but never used
return '';
});
}
Expand Down Expand Up @@ -128,9 +132,9 @@
})
.then((response) => {
if (response.data) {
const responseTokensList = response!.data;

Check warning on line 135 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

Forbidden non-null assertion
const tokensList: Array<FungibleToken> = [];
responseTokensList.forEach((responseToken: any) => {

Check warning on line 137 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const token: FungibleToken = {
name: responseToken.ticker,
balance: responseToken.overallBalance,
Expand All @@ -153,7 +157,7 @@
return [];
}
})
.catch((error) => {

Check warning on line 160 in api/ordinals.ts

View workflow job for this annotation

GitHub Actions / test

'error' is defined but never used
return [];
});
}
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
Loading