Skip to content

Commit 84252a7

Browse files
authored
fix: ignore events from failed transactions (#264)
1 parent 353fb7b commit 84252a7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/pg/chainhook/block-cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class BlockCache {
4747
}
4848

4949
transaction(tx: StacksTransaction) {
50+
if (!tx.metadata.success) return;
5051
if (tx.metadata.kind.type === 'ContractDeployment' && tx.metadata.contract_abi) {
5152
const abi = tx.metadata.contract_abi as ClarityAbi;
5253
const sip = getSmartContractSip(abi);

tests/chainhook/smart-contracts.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ describe('contract deployments', () => {
4343
await expect(db.getPendingJobBatch({ limit: 1 })).resolves.toHaveLength(1);
4444
});
4545

46+
test('ignores token contract from a failed transaction', async () => {
47+
await db.chainhook.processPayload(
48+
new TestChainhookPayloadBuilder()
49+
.apply()
50+
.block({ height: 100 })
51+
.transaction({
52+
hash: '0x01',
53+
sender: 'SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60',
54+
success: false, // Failed
55+
})
56+
.contractDeploy('SP1K1A1PMGW2ZJCNF46NWZWHG8TS1D23EGH1KNK60.friedger-pool-nft', SIP_009_ABI)
57+
.build()
58+
);
59+
await expect(db.getSmartContract({ id: 1 })).resolves.toBeUndefined();
60+
await expect(db.getPendingJobBatch({ limit: 1 })).resolves.toHaveLength(0);
61+
});
62+
4663
test('ignores non-token contract', async () => {
4764
await db.chainhook.processPayload(
4865
new TestChainhookPayloadBuilder()

tests/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ export class TestChainhookPayloadBuilder {
13411341
return this;
13421342
}
13431343

1344-
transaction(args: { hash: string; sender?: string }): this {
1344+
transaction(args: { hash: string; sender?: string; success?: boolean }): this {
13451345
this.lastBlock.transactions.push({
13461346
metadata: {
13471347
contract_abi: null,
@@ -1367,7 +1367,7 @@ export class TestChainhookPayloadBuilder {
13671367
},
13681368
result: '(ok true)',
13691369
sender: args.sender ?? 'SP3HXJJMJQ06GNAZ8XWDN1QM48JEDC6PP6W3YZPZJ',
1370-
success: true,
1370+
success: args.success ?? true,
13711371
},
13721372
operations: [],
13731373
transaction_identifier: {

0 commit comments

Comments
 (0)