Skip to content

Commit

Permalink
expand spent outputs to utxos
Browse files Browse the repository at this point in the history
  • Loading branch information
me-rob committed Apr 21, 2024
1 parent d5da162 commit 17f9192
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/indexer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ export namespace RuneLocation {
}
}

export type RuneOutput = {
txid: string;
vout: number;
};
export type SpentRuneUtxoBalance = RuneUtxoBalance & { mempoolTxid: string };

export type RuneUtxoBalance = {
txid: string;
Expand Down Expand Up @@ -156,6 +153,6 @@ export type RuneBlockIndex = {
etchings: RuneEtching[];
mintCounts: RuneMintCount[];
utxoBalances: RuneUtxoBalance[];
spentOutputs: RuneOutput[];
spentOutputs: SpentRuneUtxoBalance[];
burnedBalances: RuneBalance[];
};
19 changes: 14 additions & 5 deletions src/indexer/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import { BitcoinRpcClient } from '../rpcclient';
import { Rune } from '../rune';
import { Runestone } from '../runestone';
import { script } from '../script';
import { SpacedRune } from '../spacedrune';
import {
BlockInfo,
RuneBlockIndex,
RuneBalance,
RuneBlockIndex,
RuneEtching,
RuneLocation,
RuneMintCount,
RuneOutput,
RuneUtxoBalance,
RunestoneStorage,
SpentRuneUtxoBalance,
} from './types';
import { SpacedRune } from '../spacedrune';

function isScriptPubKeyHexOpReturn(scriptPubKeyHex: string) {
return scriptPubKeyHex && Buffer.from(scriptPubKeyHex, 'hex')[0] === OP_RETURN;
Expand All @@ -39,7 +39,7 @@ export class RuneUpdater implements RuneBlockIndex {
block: BlockInfo;
etchings: RuneEtching[] = [];
utxoBalances: RuneUtxoBalance[] = [];
spentOutputs: RuneOutput[] = [];
spentOutputs: SpentRuneUtxoBalance[] = [];

private _minimum: Rune;
private _mintCountsByRuneLocation: Map<string, RuneMintCount> = new Map();
Expand Down Expand Up @@ -446,13 +446,22 @@ export class RuneUpdater implements RuneBlockIndex {
const utxoBalance =
utxoBalancesByOutputLocation.get(`${input.txid}:${input.vout}`) ??
(await this._storage.getUtxoBalance(input.txid, input.vout));
this.spentOutputs.push({ txid: input.txid, vout: input.vout });
for (const additionalBalance of utxoBalance) {
const runeId = additionalBalance.runeId;
const runeLocation = RuneLocation.toString(runeId);
const balance = unallocated.get(runeLocation) ?? { runeId, amount: 0n };
unallocated.set(runeLocation, balance);
balance.amount = u128.checkedAddThrow(u128(balance.amount), u128(additionalBalance.amount));
this.spentOutputs.push({
txid: input.txid,
vout: input.vout,
address: additionalBalance.address,
scriptPubKey: additionalBalance.scriptPubKey,
runeId: additionalBalance.runeId,
runeTicker: additionalBalance.runeTicker,
amount: additionalBalance.amount,
mempoolTxid: tx.txid,
});
}
}

Expand Down
30 changes: 26 additions & 4 deletions test/updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ describe('edict', () => {
await runeUpdater.indexRunes(tx2, 89);
expect(runeUpdater.etchings.length).toBe(0);
expect(runeUpdater.utxoBalances.length).toBe(2);
expect(runeUpdater.spentOutputs.length).toBe(2);
expect(runeUpdater.utxoBalances[0]).toMatchObject({
txid: 'txid',
vout: 1,
Expand All @@ -902,10 +903,31 @@ describe('edict', () => {
},
amount: 400n,
});
expect(runeUpdater.spentOutputs).toEqual([
{ txid: 'parenttxid', vout: 0 },
{ txid: 'txid', vout: 1 },
]);
expect(runeUpdater.spentOutputs[0]).toMatchObject({
txid: 'parenttxid',
vout: 0,
address: '3P4WqXDbSLRhzo2H6MT6YFbvBKBDPLbVtQ',
scriptPubKey: Buffer.from('a914ea6b832a05c6ca578baa3836f3f25553d41068a587', 'hex'),
runeId: {
block: 888,
tx: 8,
},
runeTicker: 'TESTRUNE',
amount: 400n,
mempoolTxid: 'txid',
});
expect(runeUpdater.spentOutputs[1]).toMatchObject({
txid: 'txid',
vout: 1,
address: '3P4WqXDbSLRhzo2H6MT6YFbvBKBDPLbVtQ',
runeId: {
block: 888,
tx: 8,
},
runeTicker: 'TESTRUNE',
amount: 400n,
mempoolTxid: 'childtxid',
});
});

test('edict with invalid output is cenotaph', async () => {
Expand Down

0 comments on commit 17f9192

Please sign in to comment.