@secondlayer/sdk 6.38.0 → 6.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +107 -6
- package/dist/index.js +67 -36
- package/dist/index.js.map +7 -7
- package/dist/streams/index.d.ts +69 -3
- package/dist/streams/index.js +65 -35
- package/dist/streams/index.js.map +6 -6
- package/dist/subgraphs/index.d.ts +106 -5
- package/dist/subgraphs/index.js +67 -36
- package/dist/subgraphs/index.js.map +6 -6
- package/package.json +4 -2
|
@@ -453,6 +453,12 @@ type StreamsEventBase = {
|
|
|
453
453
|
* Optional for back-compat; the API always sets it on Streams responses.
|
|
454
454
|
*/
|
|
455
455
|
finalized?: boolean
|
|
456
|
+
/**
|
|
457
|
+
* Labels whose filter group this event satisfied, present only when the
|
|
458
|
+
* request used a labelled `filters` map. The SDK dispatches on it for you
|
|
459
|
+
* (`on.<label>`), so handlers rarely read it directly.
|
|
460
|
+
*/
|
|
461
|
+
matched?: string[]
|
|
456
462
|
};
|
|
457
463
|
type StreamsEventOf<
|
|
458
464
|
T extends StreamsEventType,
|
|
@@ -522,6 +528,34 @@ type StreamsReorgsListEnvelope = {
|
|
|
522
528
|
};
|
|
523
529
|
/** A filter that matches a single value or any value in a list. */
|
|
524
530
|
type StreamsFilterValue = string | readonly string[];
|
|
531
|
+
/**
|
|
532
|
+
* One labelled filter group. Fields inside a group AND together; groups in a
|
|
533
|
+
* map OR together server-side.
|
|
534
|
+
*/
|
|
535
|
+
type StreamsLabelledFilter = {
|
|
536
|
+
types?: readonly StreamsEventType[]
|
|
537
|
+
contractId?: StreamsFilterValue
|
|
538
|
+
sender?: StreamsFilterValue
|
|
539
|
+
recipient?: StreamsFilterValue
|
|
540
|
+
assetIdentifier?: string
|
|
541
|
+
};
|
|
542
|
+
/** A map of label → filter group. Labels are yours; the server echoes them. */
|
|
543
|
+
type StreamsFilterMap = Record<string, StreamsLabelledFilter>;
|
|
544
|
+
/**
|
|
545
|
+
* The events one label can yield. A group that declares `types` narrows to
|
|
546
|
+
* exactly those variants, so a per-label handler needs no `event_type` guard;
|
|
547
|
+
* a group without `types` keeps the full union.
|
|
548
|
+
*/
|
|
549
|
+
type StreamsEventForFilter<
|
|
550
|
+
F,
|
|
551
|
+
D extends boolean = false
|
|
552
|
+
> = F extends {
|
|
553
|
+
types: readonly (infer T extends StreamsEventType)[]
|
|
554
|
+
} ? D extends true ? Extract<IndexEvent, {
|
|
555
|
+
event_type: T
|
|
556
|
+
}> : Extract<StreamsEvent, {
|
|
557
|
+
event_type: T
|
|
558
|
+
}> : D extends true ? IndexEvent : StreamsEvent;
|
|
525
559
|
type StreamsEventsListParams = {
|
|
526
560
|
cursor?: string | null
|
|
527
561
|
fromHeight?: number
|
|
@@ -533,6 +567,12 @@ type StreamsEventsListParams = {
|
|
|
533
567
|
sender?: StreamsFilterValue
|
|
534
568
|
recipient?: StreamsFilterValue
|
|
535
569
|
assetIdentifier?: string
|
|
570
|
+
/**
|
|
571
|
+
* Labelled filter groups. The groups OR together in one scan and each
|
|
572
|
+
* returned event carries the labels it matched (`event.matched`) — two
|
|
573
|
+
* unrelated concerns share one page, one cursor, one checkpoint.
|
|
574
|
+
*/
|
|
575
|
+
filters?: StreamsFilterMap
|
|
536
576
|
limit?: number
|
|
537
577
|
};
|
|
538
578
|
type StreamsEventsStreamParams = {
|
|
@@ -543,6 +583,8 @@ type StreamsEventsStreamParams = {
|
|
|
543
583
|
sender?: StreamsFilterValue
|
|
544
584
|
recipient?: StreamsFilterValue
|
|
545
585
|
assetIdentifier?: string
|
|
586
|
+
/** Labelled filter groups; see {@link StreamsEventsListParams.filters}. */
|
|
587
|
+
filters?: StreamsFilterMap
|
|
546
588
|
batchSize?: number
|
|
547
589
|
emptyBackoffMs?: number
|
|
548
590
|
maxPages?: number
|
|
@@ -558,6 +600,8 @@ type StreamsEventsSubscribeParams = {
|
|
|
558
600
|
sender?: StreamsFilterValue
|
|
559
601
|
recipient?: StreamsFilterValue
|
|
560
602
|
assetIdentifier?: string
|
|
603
|
+
/** Labelled filter groups; see {@link StreamsEventsListParams.filters}. */
|
|
604
|
+
filters?: StreamsFilterMap
|
|
561
605
|
/** Abort to unsubscribe (the returned function does the same). */
|
|
562
606
|
signal?: AbortSignal
|
|
563
607
|
/** Called for each pushed event, in order. */
|
|
@@ -609,7 +653,8 @@ type StreamsReorgContext = {
|
|
|
609
653
|
};
|
|
610
654
|
type StreamsEventsConsumeParams<
|
|
611
655
|
TTx = never,
|
|
612
|
-
D extends boolean = false
|
|
656
|
+
D extends boolean = false,
|
|
657
|
+
F extends StreamsFilterMap = Record<never, never>
|
|
613
658
|
> = {
|
|
614
659
|
fromCursor?: string | null
|
|
615
660
|
/**
|
|
@@ -643,13 +688,33 @@ type StreamsEventsConsumeParams<
|
|
|
643
688
|
sender?: StreamsFilterValue
|
|
644
689
|
recipient?: StreamsFilterValue
|
|
645
690
|
assetIdentifier?: string
|
|
691
|
+
/**
|
|
692
|
+
* Labelled filter groups. The groups OR together in ONE server-side scan,
|
|
693
|
+
* so two unrelated concerns share one page, one cursor, and one checkpoint
|
|
694
|
+
* instead of two consume loops. Pair with `on` to handle each label.
|
|
695
|
+
*/
|
|
696
|
+
filters?: F
|
|
697
|
+
/**
|
|
698
|
+
* One handler per label in `filters`. Every label must be handled, so
|
|
699
|
+
* adding a label is a compile error until its handler exists.
|
|
700
|
+
*
|
|
701
|
+
* A label's declared `types` narrows its handler's events, so `payload` is
|
|
702
|
+
* typed without an `event_type` guard. Labels are dispatched in declaration
|
|
703
|
+
* order: a label's own events stay in cursor order, but events of different
|
|
704
|
+
* labels do not interleave within a page — add `onBatch` when you need
|
|
705
|
+
* strict global order across labels.
|
|
706
|
+
*/
|
|
707
|
+
on?: { [K in keyof F] : (events: StreamsEventForFilter<F[K], D>[], ctx: StreamsBatchContext & WithSinkTx<TTx>) => void | Promise<void> }
|
|
646
708
|
batchSize?: number
|
|
647
709
|
/**
|
|
648
710
|
* Apply a page of canonical events. Persist `ctx.cursor` in the same
|
|
649
711
|
* transaction as your writes. Returning a cursor overrides `ctx.cursor` as
|
|
650
712
|
* the resume point (advanced manual control); returning nothing uses it.
|
|
713
|
+
*
|
|
714
|
+
* Optional only when `on` handles the page instead; a consume call with
|
|
715
|
+
* neither throws.
|
|
651
716
|
*/
|
|
652
|
-
onBatch
|
|
717
|
+
onBatch?: (events: D extends true ? IndexEvent[] : StreamsEvent[], envelope: StreamsEventsEnvelope, ctx: StreamsBatchContext & WithSinkTx<TTx>) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>
|
|
653
718
|
/**
|
|
654
719
|
* Roll your projection back to `reorg.fork_point_height`, persisting
|
|
655
720
|
* `ctx.cursor` in the same transaction. Called once per *new* reorg
|
|
@@ -807,9 +872,10 @@ type StreamsClient = {
|
|
|
807
872
|
* stops it.
|
|
808
873
|
*/
|
|
809
874
|
consume<
|
|
875
|
+
const F extends StreamsFilterMap = Record<never, never>,
|
|
810
876
|
TTx = never,
|
|
811
877
|
D extends boolean = false
|
|
812
|
-
>(params: StreamsEventsConsumeParams<TTx, D> & {
|
|
878
|
+
>(params: StreamsEventsConsumeParams<TTx, D, F> & {
|
|
813
879
|
sink?: ConsumerSink<TTx>
|
|
814
880
|
}): Promise<StreamsEventsConsumeResult>
|
|
815
881
|
/**
|
|
@@ -1153,8 +1219,19 @@ type IndexPrint = IndexEventBase & {
|
|
|
1153
1219
|
/** Decoded chain event, discriminated by `event_type`. */
|
|
1154
1220
|
type IndexEvent = IndexFtTransfer | IndexNftTransfer | IndexStxTransfer | IndexStxMint | IndexStxBurn | IndexStxLock | IndexFtMint | IndexFtBurn | IndexNftMint | IndexNftBurn | IndexPrint;
|
|
1155
1221
|
type IndexEventType = IndexEvent["event_type"];
|
|
1156
|
-
|
|
1157
|
-
|
|
1222
|
+
/** Fields every read returns regardless of `fields` — the consume contract
|
|
1223
|
+
* (`cursor`, `block_height`) plus the union discriminant. */
|
|
1224
|
+
type IndexAlwaysFields = "cursor" | "block_height" | "event_type";
|
|
1225
|
+
/** An event row narrowed to a `fields` selection (plus what always ships). */
|
|
1226
|
+
type IndexEventFields<
|
|
1227
|
+
T extends IndexEventType,
|
|
1228
|
+
F extends keyof IndexEventOf<T> & string
|
|
1229
|
+
> = Pick<IndexEventOf<T>, (F | IndexAlwaysFields) & keyof IndexEventOf<T>>;
|
|
1230
|
+
type EventsEnvelope<
|
|
1231
|
+
T extends IndexEventType = IndexEventType,
|
|
1232
|
+
TRow = IndexEventOf<T>
|
|
1233
|
+
> = {
|
|
1234
|
+
events: TRow[]
|
|
1158
1235
|
next_cursor: string | null
|
|
1159
1236
|
tip: IndexTip
|
|
1160
1237
|
reorgs: IndexReorg[]
|
|
@@ -1187,6 +1264,21 @@ type EventsListParams<T extends IndexEventType = IndexEventType> = {
|
|
|
1187
1264
|
* Avoids a `/v1/index/transactions` call per event; for `print` events it's
|
|
1188
1265
|
* the only source of the submitting sender. */
|
|
1189
1266
|
txContext?: boolean
|
|
1267
|
+
/**
|
|
1268
|
+
* Return only these columns. The server projects the SELECT, so an
|
|
1269
|
+
* unrequested field is physically absent — and the returned row type
|
|
1270
|
+
* narrows to match, making a read of one a compile error rather than
|
|
1271
|
+
* `undefined`.
|
|
1272
|
+
*
|
|
1273
|
+
* `cursor`, `block_height`, and `event_type` come back regardless: the
|
|
1274
|
+
* first two are the consume contract, the third carries the discriminant.
|
|
1275
|
+
* Don't list them.
|
|
1276
|
+
*
|
|
1277
|
+
* Cost note: this does NOT change your bill — Index meters per row read,
|
|
1278
|
+
* not per field. What it buys is wire bytes, plus skipping the `blocks`
|
|
1279
|
+
* join when `block_time` is omitted.
|
|
1280
|
+
*/
|
|
1281
|
+
fields?: readonly (keyof IndexEventOf<T> & string)[]
|
|
1190
1282
|
};
|
|
1191
1283
|
type EventsWalkParams<T extends IndexEventType = IndexEventType> = Omit<EventsListParams<T>, "limit"> & {
|
|
1192
1284
|
batchSize?: number
|
|
@@ -1887,6 +1979,15 @@ interface NftTransfersResource {
|
|
|
1887
1979
|
/** `index.events` — callable shorthand for `.list()`; `eventType` is required. */
|
|
1888
1980
|
interface IndexEventsResource {
|
|
1889
1981
|
<T extends IndexEventType>(params: EventsListParams<T>): Promise<EventsEnvelope<T>>;
|
|
1982
|
+
/** Narrowing overload: `fields` shrinks the returned row type to exactly
|
|
1983
|
+
* the requested columns plus {@link IndexAlwaysFields}. `const F` means no
|
|
1984
|
+
* `as const` at the call site. */
|
|
1985
|
+
list<
|
|
1986
|
+
T extends IndexEventType,
|
|
1987
|
+
const F extends keyof IndexEventOf<T> & string
|
|
1988
|
+
>(params: EventsListParams<T> & {
|
|
1989
|
+
fields: readonly F[]
|
|
1990
|
+
}): Promise<EventsEnvelope<T, IndexEventFields<T, F>>>;
|
|
1890
1991
|
list<T extends IndexEventType>(params: EventsListParams<T>): Promise<EventsEnvelope<T>>;
|
|
1891
1992
|
walk<T extends IndexEventType>(params: EventsWalkParams<T>): AsyncIterable<IndexEventOf<T>>;
|
|
1892
1993
|
consume<
|
package/dist/subgraphs/index.js
CHANGED
|
@@ -669,7 +669,8 @@ async function consumeStreamsEvents(opts) {
|
|
|
669
669
|
contractId: opts.contractId,
|
|
670
670
|
sender: opts.sender,
|
|
671
671
|
recipient: opts.recipient,
|
|
672
|
-
assetIdentifier: opts.assetIdentifier
|
|
672
|
+
assetIdentifier: opts.assetIdentifier,
|
|
673
|
+
filters: opts.filters
|
|
673
674
|
}), {
|
|
674
675
|
retryCount: opts.retryCount,
|
|
675
676
|
retryDelay: opts.retryDelay,
|
|
@@ -746,7 +747,8 @@ async function* iterateStreamsBatches(opts) {
|
|
|
746
747
|
contractId: opts.contractId,
|
|
747
748
|
sender: opts.sender,
|
|
748
749
|
recipient: opts.recipient,
|
|
749
|
-
assetIdentifier: opts.assetIdentifier
|
|
750
|
+
assetIdentifier: opts.assetIdentifier,
|
|
751
|
+
filters: opts.filters
|
|
750
752
|
});
|
|
751
753
|
const checkpoint = envelope.next_cursor ?? cursor;
|
|
752
754
|
if (envelope.events.length > 0 || envelope.reorgs.length > 0) {
|
|
@@ -783,7 +785,8 @@ async function* streamStreamsEvents(opts) {
|
|
|
783
785
|
contractId: opts.contractId,
|
|
784
786
|
sender: opts.sender,
|
|
785
787
|
recipient: opts.recipient,
|
|
786
|
-
assetIdentifier: opts.assetIdentifier
|
|
788
|
+
assetIdentifier: opts.assetIdentifier,
|
|
789
|
+
filters: opts.filters
|
|
787
790
|
});
|
|
788
791
|
pages++;
|
|
789
792
|
for (const event of envelope.events) {
|
|
@@ -1113,7 +1116,8 @@ class Index extends BaseClient {
|
|
|
1113
1116
|
from_height: params.fromHeight,
|
|
1114
1117
|
to_height: params.toHeight,
|
|
1115
1118
|
trait: params.trait,
|
|
1116
|
-
tx_context: params.txContext ? "true" : undefined
|
|
1119
|
+
tx_context: params.txContext ? "true" : undefined,
|
|
1120
|
+
fields: params.fields?.join(",")
|
|
1117
1121
|
})}`);
|
|
1118
1122
|
}
|
|
1119
1123
|
walkEvents(params) {
|
|
@@ -1597,7 +1601,8 @@ function subscribeStreamsEvents(opts) {
|
|
|
1597
1601
|
contract_id: params.contractId,
|
|
1598
1602
|
sender: params.sender,
|
|
1599
1603
|
recipient: params.recipient,
|
|
1600
|
-
asset_identifier: params.assetIdentifier
|
|
1604
|
+
asset_identifier: params.assetIdentifier,
|
|
1605
|
+
filters: params.filters ? JSON.stringify(params.filters) : undefined
|
|
1601
1606
|
})}`;
|
|
1602
1607
|
const res = await opts.fetchImpl(url, {
|
|
1603
1608
|
headers: {
|
|
@@ -1841,7 +1846,8 @@ function createStreamsClient(options) {
|
|
|
1841
1846
|
contractId,
|
|
1842
1847
|
sender,
|
|
1843
1848
|
recipient,
|
|
1844
|
-
assetIdentifier
|
|
1849
|
+
assetIdentifier,
|
|
1850
|
+
filters
|
|
1845
1851
|
}) => {
|
|
1846
1852
|
return listEvents({
|
|
1847
1853
|
cursor,
|
|
@@ -1851,9 +1857,13 @@ function createStreamsClient(options) {
|
|
|
1851
1857
|
contractId,
|
|
1852
1858
|
sender,
|
|
1853
1859
|
recipient,
|
|
1854
|
-
assetIdentifier
|
|
1860
|
+
assetIdentifier,
|
|
1861
|
+
filters
|
|
1855
1862
|
});
|
|
1856
1863
|
};
|
|
1864
|
+
function encodeFilters(filters) {
|
|
1865
|
+
return filters ? JSON.stringify(filters) : undefined;
|
|
1866
|
+
}
|
|
1857
1867
|
async function listEvents(params = {}) {
|
|
1858
1868
|
return request(`/v1/streams/events${buildQuery({
|
|
1859
1869
|
cursor: params.cursor,
|
|
@@ -1865,9 +1875,56 @@ function createStreamsClient(options) {
|
|
|
1865
1875
|
recipient: params.recipient,
|
|
1866
1876
|
asset_identifier: params.assetIdentifier,
|
|
1867
1877
|
types: params.types,
|
|
1868
|
-
not_types: params.notTypes
|
|
1878
|
+
not_types: params.notTypes,
|
|
1879
|
+
filters: encodeFilters(params.filters)
|
|
1869
1880
|
})}`);
|
|
1870
1881
|
}
|
|
1882
|
+
function consumeEvents(params) {
|
|
1883
|
+
const labels = params.filters ? Object.keys(params.filters) : [];
|
|
1884
|
+
if (labels.length === 0 && !params.onBatch) {
|
|
1885
|
+
throw new ValidationError("consume requires onBatch, or filters + on handlers.", 400);
|
|
1886
|
+
}
|
|
1887
|
+
if (labels.length > 0 && !params.on && !params.onBatch) {
|
|
1888
|
+
throw new ValidationError("consume with filters requires on handlers (or onBatch).", 400);
|
|
1889
|
+
}
|
|
1890
|
+
const dispatch = params.on;
|
|
1891
|
+
const project = (events) => params.decoded ? events.map(decode) : events;
|
|
1892
|
+
const onBatch = async (events, envelope, ctx) => {
|
|
1893
|
+
for (const label of labels) {
|
|
1894
|
+
const forLabel = events.filter((event) => event.matched?.includes(label));
|
|
1895
|
+
if (forLabel.length === 0)
|
|
1896
|
+
continue;
|
|
1897
|
+
await dispatch?.[label]?.(project(forLabel), ctx);
|
|
1898
|
+
}
|
|
1899
|
+
const userOnBatch = params.onBatch;
|
|
1900
|
+
return await userOnBatch?.(project(events), envelope, ctx);
|
|
1901
|
+
};
|
|
1902
|
+
return consumeStreamsEvents({
|
|
1903
|
+
fromCursor: params.fromCursor,
|
|
1904
|
+
sink: params.sink,
|
|
1905
|
+
onProgress: params.onProgress,
|
|
1906
|
+
mode: params.mode,
|
|
1907
|
+
finalizedOnly: params.finalizedOnly,
|
|
1908
|
+
types: params.types,
|
|
1909
|
+
notTypes: params.notTypes,
|
|
1910
|
+
contractId: params.contractId,
|
|
1911
|
+
sender: params.sender,
|
|
1912
|
+
recipient: params.recipient,
|
|
1913
|
+
assetIdentifier: params.assetIdentifier,
|
|
1914
|
+
filters: params.filters,
|
|
1915
|
+
batchSize: params.batchSize ?? 100,
|
|
1916
|
+
fetchEvents,
|
|
1917
|
+
onBatch,
|
|
1918
|
+
onReorg: params.onReorg,
|
|
1919
|
+
emptyBackoffMs: params.emptyBackoffMs,
|
|
1920
|
+
maxPages: params.maxPages,
|
|
1921
|
+
maxEmptyPolls: params.maxEmptyPolls,
|
|
1922
|
+
signal: params.signal,
|
|
1923
|
+
retryCount: params.retryCount,
|
|
1924
|
+
retryDelay: params.retryDelay,
|
|
1925
|
+
onError: params.onError
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1871
1928
|
return {
|
|
1872
1929
|
consume(params = {}) {
|
|
1873
1930
|
return iterateStreamsBatches({
|
|
@@ -1889,33 +1946,7 @@ function createStreamsClient(options) {
|
|
|
1889
1946
|
byTxId(txId) {
|
|
1890
1947
|
return request(`/v1/streams/events/${encodeURIComponent(txId)}`);
|
|
1891
1948
|
},
|
|
1892
|
-
consume
|
|
1893
|
-
const onBatch = params.decoded ? (events, envelope, ctx) => params.onBatch(events.map(decode), envelope, ctx) : params.onBatch;
|
|
1894
|
-
return consumeStreamsEvents({
|
|
1895
|
-
fromCursor: params.fromCursor,
|
|
1896
|
-
sink: params.sink,
|
|
1897
|
-
onProgress: params.onProgress,
|
|
1898
|
-
mode: params.mode,
|
|
1899
|
-
finalizedOnly: params.finalizedOnly,
|
|
1900
|
-
types: params.types,
|
|
1901
|
-
notTypes: params.notTypes,
|
|
1902
|
-
contractId: params.contractId,
|
|
1903
|
-
sender: params.sender,
|
|
1904
|
-
recipient: params.recipient,
|
|
1905
|
-
assetIdentifier: params.assetIdentifier,
|
|
1906
|
-
batchSize: params.batchSize ?? 100,
|
|
1907
|
-
fetchEvents,
|
|
1908
|
-
onBatch,
|
|
1909
|
-
onReorg: params.onReorg,
|
|
1910
|
-
emptyBackoffMs: params.emptyBackoffMs,
|
|
1911
|
-
maxPages: params.maxPages,
|
|
1912
|
-
maxEmptyPolls: params.maxEmptyPolls,
|
|
1913
|
-
signal: params.signal,
|
|
1914
|
-
retryCount: params.retryCount,
|
|
1915
|
-
retryDelay: params.retryDelay,
|
|
1916
|
-
onError: params.onError
|
|
1917
|
-
});
|
|
1918
|
-
},
|
|
1949
|
+
consume: consumeEvents,
|
|
1919
1950
|
stream(params = {}) {
|
|
1920
1951
|
return streamStreamsEvents({
|
|
1921
1952
|
fromCursor: params.fromCursor,
|
|
@@ -2148,5 +2179,5 @@ export {
|
|
|
2148
2179
|
Subgraphs
|
|
2149
2180
|
};
|
|
2150
2181
|
|
|
2151
|
-
//# debugId=
|
|
2182
|
+
//# debugId=4B9D9DF481DBB85464756E2164756E21
|
|
2152
2183
|
//# sourceMappingURL=index.js.map
|