Skip to content

Commit 4c3add3

Browse files
committed
refactor: use isDefined utility for better null/undefined checks in event filter and transaction classes
1 parent ee096ab commit 4c3add3

5 files changed

Lines changed: 30 additions & 39 deletions

File tree

core/event/event-filter/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { BoundedArray } from "@/common/helpers/bounded-array.ts";
99
import type { ContractId } from "@/strkeys/types.ts";
1010
import { assert } from "@/common/assert/assert.ts";
1111
import * as E from "@/event/event-filter/error.ts";
12+
import { isDefined } from "@/common/type-guards/is-defined.ts";
1213
export class EventFilter {
1314
private _type?: EventType;
1415
private _contractIds?: BoundedArray<ContractId, 0, 5>;
@@ -36,14 +37,9 @@ export class EventFilter {
3637
return {
3738
type: this._type,
3839
contractIds: this._contractIds,
39-
topics:
40-
this._topics === undefined
41-
? undefined
42-
: [
43-
...this._topics.map((topicFilter) =>
44-
this.encodeTopics(topicFilter)
45-
),
46-
],
40+
topics: isDefined(this._topics)
41+
? [...this._topics.map((topicFilter) => this.encodeTopics(topicFilter))]
42+
: undefined,
4743
};
4844
}
4945

@@ -77,7 +73,7 @@ export class EventFilter {
7773
}
7874
const eventTopicsMatchFilterTopic = (
7975
topicFilter: TopicFilter,
80-
eventTopics: xdr.ScVal[]
76+
eventTopics: xdr.ScVal[],
8177
): boolean => {
8278
assert(eventTopics.length > 0, new E.EVENT_HAS_NO_TOPICS());
8379

@@ -101,7 +97,7 @@ const eventTopicsMatchFilterTopic = (
10197
throw new E.FAILED_TO_CHECK_FILTER_SEGMENT(
10298
filterSegment,
10399
eventSegment,
104-
e as Error
100+
e as Error,
105101
);
106102
}
107103
return false; // No match for this segment

core/event/parsing/ledger-close-meta.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ import { assert } from "@/common/assert/assert.ts";
1010
import { isDefined } from "@/common/type-guards/is-defined.ts";
1111

1212
export const isLedgerCloseMetaV1 = (
13-
metadataXdr: xdr.LedgerCloseMeta
13+
metadataXdr: xdr.LedgerCloseMeta,
1414
): boolean => {
1515
return metadataXdr.switch() === 1;
1616
};
1717

1818
export const isLedgerCloseMetaV2 = (
19-
metadataXdr: xdr.LedgerCloseMeta
19+
metadataXdr: xdr.LedgerCloseMeta,
2020
): boolean => {
2121
return metadataXdr.switch() === 2;
2222
};
2323

2424
export const parseEventsFromLedgerCloseMeta = async (
2525
metadataXdr: xdr.LedgerCloseMeta,
2626
onEvent: EventHandler,
27-
filters?: EventFilter[]
27+
filters?: EventFilter[],
2828
): Promise<void> => {
2929
assert(
3030
xdr.LedgerCloseMeta.isValid(metadataXdr),
31-
new E.INVALID_LEDGER_CLOSE_META_XDR()
31+
new E.INVALID_LEDGER_CLOSE_META_XDR(),
3232
);
3333

3434
let ledgerCloseMeta:
@@ -46,7 +46,7 @@ export const parseEventsFromLedgerCloseMeta = async (
4646

4747
assert(
4848
isDefined(ledgerCloseMeta),
49-
new E.UNSUPPORTED_LEDGER_CLOSE_META_VERSION(metadataXdr.switch())
49+
new E.UNSUPPORTED_LEDGER_CLOSE_META_VERSION(metadataXdr.switch()),
5050
);
5151

5252
const ledgerSequence = ledgerCloseMeta.ledgerHeader().header().ledgerSeq();
@@ -84,7 +84,7 @@ export const parseEventsFromLedgerCloseMeta = async (
8484
const contractId =
8585
contractIdXdr !== null
8686
? (Address.fromScAddress(
87-
xdr.ScAddress.scAddressTypeContract(contractIdXdr)
87+
xdr.ScAddress.scAddressTypeContract(contractIdXdr),
8888
).toString() as ContractId)
8989
: undefined;
9090

@@ -100,7 +100,7 @@ export const parseEventsFromLedgerCloseMeta = async (
100100
ledgerSequence,
101101
transactionIndex,
102102
operationIndex,
103-
eventIndex
103+
eventIndex,
104104
);
105105

106106
await onEvent(
@@ -116,7 +116,7 @@ export const parseEventsFromLedgerCloseMeta = async (
116116
topic: topic,
117117
value: value,
118118
inSuccessfulContractCall: inSuccessfulContractCall,
119-
})
119+
}),
120120
);
121121
}
122122
}
@@ -135,7 +135,7 @@ export const isIncludedInFilters = ({
135135
type?: EventType;
136136
topics?: xdr.ScVal[];
137137
}): boolean => {
138-
if (filters === undefined || filters.length === 0) return true;
138+
if (!isDefined(filters) || filters.length === 0) return true;
139139

140140
for (const filter of filters) {
141141
if (type !== undefined && !filter.matchesType(type)) continue;

core/ledger-parser/transaction/index.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { Operation } from "@/ledger-parser/operation/index.ts";
1515
import { INVALID_TRANSACTION_INDEX } from "@/ledger-parser/error.ts";
1616
import type { Ledger } from "@/ledger-parser/ledger/index.ts";
17+
import { isDefined } from "@/common/type-guards/is-defined.ts";
1718

1819
/**
1920
* Transaction class for lazy operation parsing
@@ -41,7 +42,7 @@ export class Transaction {
4142
txResult: xdr.TransactionResult,
4243
txMeta: xdr.TransactionMeta,
4344
txHash: Uint8Array,
44-
index: number
45+
index: number,
4546
) {
4647
this.ledger = ledger;
4748
this.txEnvelope = txEnvelope;
@@ -54,13 +55,13 @@ export class Transaction {
5455
/**
5556
* Factory method to create a Transaction from transaction result metadata
5657
*
57-
* Note: Only supports TransactionMeta v4 (Lightsail archive normalizes all to v4).
58+
* Note: Only supports TransactionMeta v4.
5859
* Envelope is NOT available in v4 meta - use fromMetaWithEnvelope for LedgerCloseMeta v2.
5960
*/
6061
static fromMeta(
6162
ledger: Ledger,
6263
txResultMeta: xdr.TransactionResultMeta,
63-
index: number
64+
index: number,
6465
): Transaction {
6566
if (index < 0) {
6667
throw new INVALID_TRANSACTION_INDEX(index, ledger.sequence, -1);
@@ -82,15 +83,12 @@ export class Transaction {
8283

8384
/**
8485
* Factory method for V2 where envelope comes from txSet separately
85-
*
86-
* In V2, envelopes are stored in txSet.phases, not in TransactionMeta.
87-
* The Ledger class extracts them and passes them here.
8886
*/
8987
static fromMetaWithEnvelope(
9088
ledger: Ledger,
9189
txResultMeta: xdr.TransactionResultMeta,
9290
envelope: xdr.TransactionEnvelope,
93-
index: number
91+
index: number,
9492
): Transaction {
9593
if (index < 0) {
9694
throw new INVALID_TRANSACTION_INDEX(index, ledger.sequence, -1);
@@ -106,13 +104,9 @@ export class Transaction {
106104

107105
/**
108106
* Check if transaction envelope is available
109-
*
110-
* Envelope availability:
111-
* - LedgerCloseMeta v2: ✅ Available (from txSet)
112-
* - LedgerCloseMeta v0/v1: ❌ Not available (TransactionMeta v4 doesn't include envelope)
113107
*/
114108
get hasEnvelope(): boolean {
115-
return this.txEnvelope !== undefined;
109+
return isDefined(this.txEnvelope);
116110
}
117111

118112
/**
@@ -134,7 +128,7 @@ export class Transaction {
134128
@memoize()
135129
get hash(): string {
136130
return Array.from(this.txHash, (b) => b.toString(16).padStart(2, "0")).join(
137-
""
131+
"",
138132
);
139133
}
140134

@@ -186,7 +180,7 @@ export class Transaction {
186180
get sourceAccount(): string {
187181
if (!this.txEnvelope) {
188182
throw new Error(
189-
`Cannot get source account for transaction ${this.hash} - envelope not available`
183+
`Cannot get source account for transaction ${this.hash} - envelope not available`,
190184
);
191185
}
192186

@@ -235,7 +229,7 @@ export class Transaction {
235229
get sequence(): bigint {
236230
if (!this.txEnvelope) {
237231
throw new Error(
238-
`Cannot get sequence for transaction ${this.hash} - envelope not available`
232+
`Cannot get sequence for transaction ${this.hash} - envelope not available`,
239233
);
240234
}
241235

@@ -267,7 +261,7 @@ export class Transaction {
267261
get operations(): Operation[] {
268262
if (!this.txEnvelope) {
269263
throw new Error(
270-
`Cannot get operations for transaction ${this.hash} - envelope not available`
264+
`Cannot get operations for transaction ${this.hash} - envelope not available`,
271265
);
272266
}
273267

rpc-streamer/src/streamer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export class RPCStreamer<T> {
352352
// Don't wait if we're about to stop
353353
if (
354354
shouldWait &&
355-
(options.stopLedger === undefined ||
355+
(!isDefined(options.stopLedger) ||
356356
currentLedger <= options.stopLedger)
357357
) {
358358
await this.waitFor("ledger");

sep10/src/client/jwt.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import * as E from "@/client/error.ts";
9+
import { isDefined } from "@colibri/core";
910

1011
/**
1112
* Standard JWT claims
@@ -83,7 +84,7 @@ export class Sep10Jwt {
8384
if (parts.length !== 3) {
8485
throw new E.INVALID_JWT(
8586
token,
86-
`JWT must have 3 parts, got ${parts.length}`
87+
`JWT must have 3 parts, got ${parts.length}`,
8788
);
8889
}
8990

@@ -130,7 +131,7 @@ export class Sep10Jwt {
130131
* When the token expires.
131132
*/
132133
get expiresAt(): Date | undefined {
133-
if (this._claims.exp === undefined) {
134+
if (!isDefined(this._claims.exp)) {
134135
return undefined;
135136
}
136137
return new Date(this._claims.exp * 1000);
@@ -140,7 +141,7 @@ export class Sep10Jwt {
140141
* When the token was issued.
141142
*/
142143
get issuedAt(): Date | undefined {
143-
if (this._claims.iat === undefined) {
144+
if (!isDefined(this._claims.iat)) {
144145
return undefined;
145146
}
146147
return new Date(this._claims.iat * 1000);

0 commit comments

Comments
 (0)