Clean up etches fetching logic

This commit is contained in:
natsoni 2024-10-08 12:38:12 +09:00
parent 040c067aac
commit 177bbc83f3
No known key found for this signature in database
GPG Key ID: C65917583181743B

View File

@ -19,16 +19,13 @@ export class OrdApiService {
decodeRunestone$(tx: Transaction): Observable<{ runestone: Runestone, runeInfo: { [id: string]: { etching: Etching; txid: string; } } }> { decodeRunestone$(tx: Transaction): Observable<{ runestone: Runestone, runeInfo: { [id: string]: { etching: Etching; txid: string; } } }> {
const runestone = decipherRunestone(tx); const runestone = decipherRunestone(tx);
const runeInfo: { [id: string]: { etching: Etching; txid: string; } } = {}; const runeInfo: { [id: string]: { etching: Etching; txid: string; } } = {};
const runesToFetch: Set<string> = new Set();
if (runestone) { if (runestone) {
const runesToFetch: Set<string> = new Set();
if (runestone.mint) { if (runestone.mint) {
if (runestone.mint.toString() === '1:0') {
runeInfo[runestone.mint.toString()] = { etching: UNCOMMON_GOODS, txid: '0000000000000000000000000000000000000000000000000000000000000000' };
} else {
runesToFetch.add(runestone.mint.toString()); runesToFetch.add(runestone.mint.toString());
} }
}
if (runestone.edicts.length) { if (runestone.edicts.length) {
runestone.edicts.forEach(edict => { runestone.edicts.forEach(edict => {
@ -37,18 +34,15 @@ export class OrdApiService {
} }
if (runesToFetch.size) { if (runesToFetch.size) {
const runeEtchingObservables = Array.from(runesToFetch).map(runeId => { const runeEtchingObservables = Array.from(runesToFetch).map(runeId => this.getEtchingFromRuneId$(runeId));
return this.getEtchingFromRuneId$(runeId).pipe(
tap(etching => {
if (etching) {
runeInfo[runeId] = etching;
}
})
);
});
return forkJoin(runeEtchingObservables).pipe( return forkJoin(runeEtchingObservables).pipe(
map(() => { map((etchings) => {
etchings.forEach((el) => {
if (el) {
runeInfo[el.runeId] = { etching: el.etching, txid: el.txid };
}
});
return { runestone: runestone, runeInfo }; return { runestone: runestone, runeInfo };
}) })
); );
@ -60,9 +54,11 @@ export class OrdApiService {
} }
// Get etching from runeId by looking up the transaction that etched the rune // Get etching from runeId by looking up the transaction that etched the rune
getEtchingFromRuneId$(runeId: string): Observable<{ etching: Etching; txid: string; }> { getEtchingFromRuneId$(runeId: string): Observable<{ runeId: string; etching: Etching; txid: string; }> {
if (runeId === '1:0') {
return of({ runeId, etching: UNCOMMON_GOODS, txid: '0000000000000000000000000000000000000000000000000000000000000000' });
} else {
const [blockNumber, txIndex] = runeId.split(':'); const [blockNumber, txIndex] = runeId.split(':');
return this.electrsApiService.getBlockHashFromHeight$(parseInt(blockNumber)).pipe( return this.electrsApiService.getBlockHashFromHeight$(parseInt(blockNumber)).pipe(
switchMap(blockHash => this.electrsApiService.getBlockTxId$(blockHash, parseInt(txIndex))), switchMap(blockHash => this.electrsApiService.getBlockTxId$(blockHash, parseInt(txIndex))),
switchMap(txId => this.electrsApiService.getTransaction$(txId)), switchMap(txId => this.electrsApiService.getTransaction$(txId)),
@ -71,7 +67,7 @@ export class OrdApiService {
if (runestone) { if (runestone) {
const etching = runestone.etching; const etching = runestone.etching;
if (etching) { if (etching) {
return of({ etching, txid: tx.txid }); return of({ runeId, etching, txid: tx.txid });
} }
} }
return of(null); return of(null);
@ -79,6 +75,7 @@ export class OrdApiService {
catchError(() => of(null)) catchError(() => of(null))
); );
} }
}
decodeInscriptions(witness: string): Inscription[] | null { decodeInscriptions(witness: string): Inscription[] | null {