@ocap/types 1.28.9 → 1.29.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.
@@ -0,0 +1,444 @@
1
+ // core/types/interfaces/indexdb.ts
2
+
3
+ import type { Promisable } from './base';
4
+ import type {
5
+ IAccountState,
6
+ IAssetFactoryState,
7
+ IAssetState,
8
+ IDelegateState,
9
+ IRollupBlock,
10
+ IRollupState,
11
+ IStakeState,
12
+ ITokenFactoryState,
13
+ ITokenState,
14
+ TokenBalanceMap,
15
+ } from './state';
16
+
17
+ // Re-export from trace-type_pb
18
+ export type { TPage, TPageOrder, TPageInfo } from '../lib/trace-type_pb';
19
+ export type {
20
+ TAddressFilter,
21
+ TTimeFilter,
22
+ TTypeFilter,
23
+ TValidityFilter,
24
+ TAssetFilter,
25
+ TTokenFilter,
26
+ TAccountFilter,
27
+ TFactoryFilter,
28
+ TRollupFilter,
29
+ TStakeFilter,
30
+ TDelegationFilter,
31
+ TTxFilter,
32
+ TTokenFactoryFilter,
33
+ TValidatorFilter,
34
+ TRangeFilter,
35
+ DirectionMap,
36
+ ValidityMap,
37
+ } from '../lib/trace-type_pb';
38
+ export type { TTokenMeta, TAccountToken } from '../lib/trace-type_pb';
39
+ export type {
40
+ TIndexedTransaction,
41
+ TIndexedAccountState,
42
+ TIndexedAssetState,
43
+ TIndexedTokenState,
44
+ TIndexedFactoryState,
45
+ TIndexedStakeState,
46
+ TIndexedDelegationState,
47
+ TIndexedRollupState,
48
+ TIndexedRollupBlock,
49
+ TIndexedRollupValidator,
50
+ TIndexedTokenFactoryState,
51
+ TTokenDistribution,
52
+ TSearchResult,
53
+ } from '../lib/trace-type_pb';
54
+
55
+ // Re-export from rpc_pb
56
+ export type {
57
+ TRequestListTransactions,
58
+ TRequestListAssets,
59
+ TRequestListTopAccounts,
60
+ TRequestListTokens,
61
+ TRequestListFactories,
62
+ TRequestListStakes,
63
+ TRequestListRollups,
64
+ TRequestListRollupBlocks,
65
+ TRequestListRollupValidators,
66
+ TRequestListDelegations,
67
+ TRequestListTokenFactories,
68
+ } from '../lib/rpc_pb';
69
+ export type {
70
+ TResponseListTransactions,
71
+ TResponseListAssets,
72
+ TResponseListTopAccounts,
73
+ TResponseListTokens,
74
+ TResponseListFactories,
75
+ TResponseListStakes,
76
+ TResponseListRollups,
77
+ TResponseListRollupBlocks,
78
+ TResponseListRollupValidators,
79
+ TResponseListDelegations,
80
+ TResponseListTokenFactories,
81
+ } from '../lib/rpc_pb';
82
+
83
+ // IndexDB types
84
+ import type {
85
+ TIndexedAccountState,
86
+ TIndexedAssetState,
87
+ TIndexedDelegationState,
88
+ TIndexedFactoryState,
89
+ TIndexedRollupBlock,
90
+ TIndexedRollupState,
91
+ TIndexedRollupValidator,
92
+ TIndexedStakeState,
93
+ TIndexedTokenFactoryState,
94
+ TIndexedTokenState,
95
+ TIndexedTransaction,
96
+ TPageInfo,
97
+ TTokenDistribution,
98
+ } from '../lib/trace-type_pb';
99
+
100
+ import type {
101
+ TRequestListAssets,
102
+ TRequestListDelegations,
103
+ TRequestListFactories,
104
+ TRequestListRollupBlocks,
105
+ TRequestListRollupValidators,
106
+ TRequestListRollups,
107
+ TRequestListStakes,
108
+ TRequestListTokenFactories,
109
+ TRequestListTokens,
110
+ TRequestListTopAccounts,
111
+ TRequestListTransactions,
112
+ } from '../lib/rpc_pb';
113
+
114
+ // ============================================================================
115
+ // Token Format Conversion Types
116
+ // ============================================================================
117
+ // StateDB stores tokens as Record<address, balance> for O(1) lookup
118
+ // IndexDB stores tokens as Array<TTokenMeta> for GraphQL/protobuf compatibility
119
+ // These types help with explicit conversion between formats
120
+
121
+ /**
122
+ * Token balance in StateDB format: address -> balance mapping
123
+ * Used by: IAccountState, IAssetFactoryState, IStakeState
124
+ * @example { "z35n6...": "1000000", "z35n7...": "500" }
125
+ */
126
+ export type { TokenBalanceMap } from './state';
127
+
128
+ /**
129
+ * Input for converting StateDB tokens to IndexDB format
130
+ * Requires token metadata lookup to populate symbol/decimal/unit
131
+ */
132
+ export interface TokenConversionInput {
133
+ /** Token balances from StateDB (address -> balance) */
134
+ tokens: TokenBalanceMap;
135
+ /** Token states for metadata lookup */
136
+ tokenStates: Array<{ address: string; symbol?: string; decimal?: number; unit?: string; icon?: string }>;
137
+ }
138
+
139
+ // ============================================================================
140
+ // Unified Indexed State Type
141
+ // ============================================================================
142
+
143
+ /**
144
+ * Union of all indexed state types stored in IndexDB
145
+ * Useful for generic document handling
146
+ */
147
+ export type TIndexedState =
148
+ | TIndexedTransaction
149
+ | TIndexedAccountState
150
+ | TIndexedAssetState
151
+ | TIndexedTokenState
152
+ | TIndexedFactoryState
153
+ | TIndexedStakeState
154
+ | TIndexedDelegationState
155
+ | TIndexedRollupState
156
+ | TIndexedRollupBlock
157
+ | TIndexedRollupValidator
158
+ | TIndexedTokenFactoryState
159
+ | TTokenDistribution;
160
+
161
+ // ============================================================================
162
+ // Index Table Type Mappings
163
+ // ============================================================================
164
+
165
+ /** Maps table name to its indexed document type */
166
+ export type IndexTableTypeMap = {
167
+ tx: TIndexedTransaction;
168
+ account: TIndexedAccountState;
169
+ asset: TIndexedAssetState;
170
+ token: TIndexedTokenState;
171
+ factory: TIndexedFactoryState;
172
+ stake: TIndexedStakeState;
173
+ delegation: TIndexedDelegationState;
174
+ rollup: TIndexedRollupState;
175
+ rollupBlock: TIndexedRollupBlock;
176
+ rollupValidator: TIndexedRollupValidator;
177
+ tokenFactory: TIndexedTokenFactoryState;
178
+ tokenDistribution: TTokenDistribution;
179
+ };
180
+
181
+ /** Valid index table names */
182
+ export type IndexTableType = keyof IndexTableTypeMap;
183
+
184
+ /** Get indexed type for a table name */
185
+ export type IndexedTypeFor<K extends IndexTableType> = IndexTableTypeMap[K];
186
+
187
+ // ============================================================================
188
+ // State to Indexed Type Mapping
189
+ // ============================================================================
190
+ // Maps StateDB state types to their IndexDB indexed counterparts
191
+ // Note: The main difference is token format (Record vs Array) and context structure
192
+
193
+ /** Maps StateDB types to IndexDB types for conversion */
194
+ export type StateToIndexedMap = {
195
+ account: { state: IAccountState; indexed: TIndexedAccountState };
196
+ asset: { state: IAssetState; indexed: TIndexedAssetState };
197
+ token: { state: ITokenState; indexed: TIndexedTokenState };
198
+ factory: { state: IAssetFactoryState; indexed: TIndexedFactoryState };
199
+ stake: { state: IStakeState; indexed: TIndexedStakeState };
200
+ delegation: { state: IDelegateState; indexed: TIndexedDelegationState };
201
+ rollup: { state: IRollupState; indexed: TIndexedRollupState };
202
+ rollupBlock: { state: IRollupBlock; indexed: TIndexedRollupBlock };
203
+ tokenFactory: { state: ITokenFactoryState; indexed: TIndexedTokenFactoryState };
204
+ };
205
+
206
+ /** Get StateDB state type for a table name */
207
+ export type StateTypeOf<K extends keyof StateToIndexedMap> = StateToIndexedMap[K]['state'];
208
+
209
+ /** Get IndexDB indexed type for a table name */
210
+ export type IndexedTypeOf<K extends keyof StateToIndexedMap> = StateToIndexedMap[K]['indexed'];
211
+
212
+ // ============================================================================
213
+ // IIndexTable Interface
214
+ // ============================================================================
215
+
216
+ /** Hook function type for pre/post operation hooks */
217
+ export type IndexTableHookFn = (...args: unknown[]) => void;
218
+
219
+ /**
220
+ * Interface for index table operations
221
+ * Implemented by memory, fs, and elasticsearch adapters
222
+ */
223
+ export interface IIndexTable<T> {
224
+ /** Table name (e.g., 'tx', 'account', 'asset') */
225
+ readonly name: string;
226
+
227
+ /** Primary key field name (e.g., 'hash', 'address') */
228
+ readonly primaryKey: string;
229
+
230
+ /** Unique index definition - single field or composite */
231
+ readonly uniqIndex: string | string[];
232
+
233
+ /**
234
+ * Get a document by primary key
235
+ * @param key - Primary key value or composite key object
236
+ * @returns Document or null if not found
237
+ */
238
+ get(key: string | Record<string, unknown>): Promisable<T | null>;
239
+
240
+ /**
241
+ * Insert a new document
242
+ * @param doc - Document to insert (must include primary key)
243
+ * @returns Inserted document (may include generated fields like $loki)
244
+ */
245
+ insert(doc: T | Record<string, unknown>): Promisable<T>;
246
+
247
+ /**
248
+ * Update an existing document
249
+ * @param key - Primary key value or composite key object
250
+ * @param updates - Partial document with fields to update
251
+ * @returns Updated document
252
+ * @throws Error if document not found
253
+ */
254
+ update(key: string | Record<string, unknown>, updates: Partial<T>): Promisable<T>;
255
+
256
+ /**
257
+ * Count documents matching query
258
+ * @param query - Query object (implementation-specific)
259
+ * @returns Number of matching documents
260
+ */
261
+ count(query?: unknown): Promisable<number>;
262
+
263
+ /**
264
+ * Reset/clear all documents in the table
265
+ */
266
+ reset(): Promisable<void>;
267
+
268
+ /**
269
+ * Register a callback for when the table is ready
270
+ * @param cb - Callback function
271
+ */
272
+ onReady(cb: () => void): void;
273
+
274
+ /**
275
+ * Mark the table as ready (called by implementations after initialization)
276
+ */
277
+ markReady(): void;
278
+
279
+ /**
280
+ * Register a pre-operation hook
281
+ * @param hookName - Operation name ('insert', 'get', 'update', 'reset')
282
+ * @param fn - Hook function called before the operation
283
+ */
284
+ pre(hookName: string, fn: IndexTableHookFn): void;
285
+
286
+ /**
287
+ * Register a post-operation hook
288
+ * @param hookName - Operation name ('insert', 'get', 'update', 'reset')
289
+ * @param fn - Hook function called after the operation
290
+ */
291
+ post(hookName: string, fn: IndexTableHookFn): void;
292
+
293
+ /**
294
+ * Generate primary key from document or key object
295
+ * @param doc - String key or document/key object
296
+ * @returns Primary key string (may be MD5 hash for composite keys)
297
+ */
298
+ generatePrimaryKey(doc: string | Record<string, unknown>): string;
299
+
300
+ /**
301
+ * Optional: Direct access to underlying collection (LokiJS-based implementations)
302
+ * Type is unknown to avoid coupling to specific implementations
303
+ */
304
+ collection?: unknown;
305
+ }
306
+
307
+ // ============================================================================
308
+ // List Result Types
309
+ // ============================================================================
310
+
311
+ export interface IListTransactionsResult {
312
+ transactions: TIndexedTransaction[];
313
+ paging: TPageInfo;
314
+ }
315
+
316
+ export interface IListAssetsResult {
317
+ assets: TIndexedAssetState[];
318
+ account?: TIndexedAccountState | null;
319
+ paging: TPageInfo;
320
+ }
321
+
322
+ export interface IListAccountsResult {
323
+ accounts: TIndexedAccountState[];
324
+ paging: TPageInfo;
325
+ }
326
+
327
+ export interface IListTokensResult {
328
+ tokens: TIndexedTokenState[];
329
+ paging: TPageInfo;
330
+ }
331
+
332
+ export interface IListFactoriesResult {
333
+ factories: TIndexedFactoryState[];
334
+ paging: TPageInfo;
335
+ }
336
+
337
+ export interface IListStakesResult {
338
+ stakes: TIndexedStakeState[];
339
+ paging: TPageInfo;
340
+ }
341
+
342
+ export interface IListDelegationsResult {
343
+ delegations: TIndexedDelegationState[];
344
+ paging: TPageInfo;
345
+ }
346
+
347
+ export interface IListRollupsResult {
348
+ rollups: TIndexedRollupState[];
349
+ paging: TPageInfo;
350
+ }
351
+
352
+ export interface IListRollupBlocksResult {
353
+ blocks: TIndexedRollupBlock[];
354
+ paging: TPageInfo;
355
+ }
356
+
357
+ export interface IListRollupValidatorsResult {
358
+ validators: TIndexedRollupValidator[];
359
+ paging: TPageInfo;
360
+ }
361
+
362
+ export interface IListTokenFactoriesResult {
363
+ tokenFactories: TIndexedTokenFactoryState[];
364
+ paging: TPageInfo;
365
+ }
366
+
367
+ /** Maps table name to its list result type */
368
+ export type ListResultTypeMap = {
369
+ tx: IListTransactionsResult;
370
+ account: IListAccountsResult;
371
+ asset: IListAssetsResult;
372
+ token: IListTokensResult;
373
+ factory: IListFactoriesResult;
374
+ stake: IListStakesResult;
375
+ delegation: IListDelegationsResult;
376
+ rollup: IListRollupsResult;
377
+ rollupBlock: IListRollupBlocksResult;
378
+ rollupValidator: IListRollupValidatorsResult;
379
+ tokenFactory: IListTokenFactoriesResult;
380
+ };
381
+
382
+ /** Get list result type for a table name */
383
+ export type ListResultFor<K extends keyof ListResultTypeMap> = ListResultTypeMap[K];
384
+
385
+ // ============================================================================
386
+ // IIndexDB Interface
387
+ // ============================================================================
388
+
389
+ /**
390
+ * Main IndexDB interface implemented by all adapters (memory, fs, elasticsearch)
391
+ */
392
+ export interface IIndexDB {
393
+ /** Adapter name (e.g., '@ocap/indexdb-memory') */
394
+ name: string;
395
+
396
+ /** Adapter version */
397
+ version: string;
398
+
399
+ // Index tables
400
+ tx: IIndexTable<IndexTableTypeMap['tx']>;
401
+ account: IIndexTable<IndexTableTypeMap['account']>;
402
+ asset: IIndexTable<IndexTableTypeMap['asset']>;
403
+ token: IIndexTable<IndexTableTypeMap['token']>;
404
+ factory: IIndexTable<IndexTableTypeMap['factory']>;
405
+ stake: IIndexTable<IndexTableTypeMap['stake']>;
406
+ delegation: IIndexTable<IndexTableTypeMap['delegation']>;
407
+ rollup: IIndexTable<IndexTableTypeMap['rollup']>;
408
+ rollupBlock: IIndexTable<IndexTableTypeMap['rollupBlock']>;
409
+ rollupValidator: IIndexTable<IndexTableTypeMap['rollupValidator']>;
410
+ tokenDistribution: IIndexTable<IndexTableTypeMap['tokenDistribution']>;
411
+ tokenFactory: IIndexTable<IndexTableTypeMap['tokenFactory']>;
412
+
413
+ // List methods
414
+ listTransactions(params?: Partial<TRequestListTransactions>): Promisable<IListTransactionsResult>;
415
+ listAssets(params?: Partial<TRequestListAssets>): Promisable<IListAssetsResult>;
416
+ listTopAccounts(params?: Partial<TRequestListTopAccounts>): Promisable<IListAccountsResult>;
417
+ listTokens(params?: Partial<TRequestListTokens>): Promisable<IListTokensResult>;
418
+ listFactories(params?: Partial<TRequestListFactories>): Promisable<IListFactoriesResult>;
419
+ listStakes(params?: Partial<TRequestListStakes>): Promisable<IListStakesResult>;
420
+ listDelegations(params?: Partial<TRequestListDelegations>): Promisable<IListDelegationsResult>;
421
+ listRollups(params?: Partial<TRequestListRollups>): Promisable<IListRollupsResult>;
422
+ listRollupBlocks(params?: Partial<TRequestListRollupBlocks>): Promisable<IListRollupBlocksResult>;
423
+ listRollupValidators(params?: Partial<TRequestListRollupValidators>): Promisable<IListRollupValidatorsResult>;
424
+ listTokenFactories(params?: Partial<TRequestListTokenFactories>): Promisable<IListTokenFactoriesResult>;
425
+
426
+ /**
427
+ * Get related addresses for account migration tracking
428
+ * @param address - Account address
429
+ * @returns Array of related addresses (migratedFrom chain)
430
+ */
431
+ getRelatedAddresses?(address: string): Promisable<string[]>;
432
+
433
+ /**
434
+ * Register a callback for when all tables are ready
435
+ * @param cb - Callback function
436
+ */
437
+ onReady(cb: () => void): void;
438
+
439
+ /**
440
+ * Attach ready listeners to all tables
441
+ * Should be called after all tables are initialized
442
+ */
443
+ attachReadyListeners(): void;
444
+ }