@ocap/indexdb 1.28.8 → 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.
package/lib/util.cjs ADDED
@@ -0,0 +1,579 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ let _ocap_util = require("@ocap/util");
3
+ let _ocap_util_lib_create_sorted_list = require("@ocap/util/lib/create-sorted-list");
4
+ let lodash_get = require("lodash/get");
5
+ lodash_get = require_rolldown_runtime.__toESM(lodash_get);
6
+ let lodash_pick = require("lodash/pick");
7
+ lodash_pick = require_rolldown_runtime.__toESM(lodash_pick);
8
+ let lodash_uniq = require("lodash/uniq");
9
+ lodash_uniq = require_rolldown_runtime.__toESM(lodash_uniq);
10
+ let lodash_uniqBy = require("lodash/uniqBy");
11
+ lodash_uniqBy = require_rolldown_runtime.__toESM(lodash_uniqBy);
12
+
13
+ //#region src/util.ts
14
+ /**
15
+ * Format token with metadata from token states
16
+ * @param token - Token with address
17
+ * @param tokenStates - Array of token states for metadata lookup
18
+ * @param rest - Additional args for debug logging
19
+ */
20
+ const formatTokenMeta = (token, tokenStates, ...rest) => {
21
+ if (!tokenStates) return token;
22
+ const tokenState = tokenStates.filter(Boolean).find((x) => x.address === token.address);
23
+ if (tokenState) {
24
+ token.decimal = tokenState.decimal;
25
+ token.unit = tokenState.unit;
26
+ token.symbol = tokenState.symbol;
27
+ token.icon = tokenState.icon;
28
+ } else console.warn("Invalid token state on formatting", token, tokenStates, ...rest);
29
+ return token;
30
+ };
31
+ /**
32
+ * Create indexed account from account state
33
+ */
34
+ const createIndexedAccount = async (x, getTokenFn) => {
35
+ const tasks = Object.keys(x.tokens || {}).map((address) => getTokenFn(address));
36
+ const tokenStates = await Promise.all(tasks);
37
+ return {
38
+ address: x.address,
39
+ balance: x.balance || "0",
40
+ moniker: x.moniker || "",
41
+ nonce: x.nonce ?? 0,
42
+ numAssets: x.numAssets ?? 0,
43
+ numTxs: x.numTxs ?? 0,
44
+ recentNumTxs: [],
45
+ renaissanceTime: x.context.renaissanceTime,
46
+ tokens: Object.keys(x.tokens || {}).map((address) => formatTokenMeta({
47
+ address,
48
+ balance: x.tokens[address]
49
+ }, tokenStates)),
50
+ genesisTime: x.context.genesisTime,
51
+ migratedTo: x.migratedTo?.[0] || "",
52
+ migratedFrom: x.migratedFrom?.[0] || ""
53
+ };
54
+ };
55
+ /**
56
+ * Create indexed asset from asset state
57
+ * Note: display/endpoint/data types from IAssetState may differ from TIndexedAssetState,
58
+ * they are passed through without transformation
59
+ */
60
+ const createIndexedAsset = (x) => ({
61
+ address: x.address,
62
+ issuer: x.issuer,
63
+ moniker: x.moniker,
64
+ owner: x.owner,
65
+ parent: x.parent,
66
+ readonly: x.readonly,
67
+ transferrable: x.transferrable,
68
+ ttl: String(x.ttl || 0),
69
+ consumedTime: x.consumedTime || "",
70
+ genesisTime: x.context.genesisTime,
71
+ renaissanceTime: x.context.renaissanceTime,
72
+ data: x.data,
73
+ display: x.display,
74
+ endpoint: x.endpoint,
75
+ tags: x.tags || []
76
+ });
77
+ /**
78
+ * Create indexed delegation from delegation state
79
+ */
80
+ const createIndexedDelegation = (x, ctx) => {
81
+ const result = {
82
+ address: x.address,
83
+ data: x.data,
84
+ genesisTime: x.context.genesisTime,
85
+ renaissanceTime: x.context.renaissanceTime,
86
+ ops: [x.ops]
87
+ };
88
+ if (x.from) result.from = x.from;
89
+ else if (ctx.senderState) result.from = (0, lodash_get.default)(ctx, "senderState.address", "");
90
+ else result.from = (0, lodash_get.default)(ctx, "tx.from", "");
91
+ if (x.to) result.to = x.to;
92
+ else if (ctx.receiverState) result.to = (0, lodash_get.default)(ctx, "receiverState.address", "");
93
+ else result.to = (0, lodash_get.default)(ctx, "itx.to", "");
94
+ return result;
95
+ };
96
+ /**
97
+ * Create indexed token from token state
98
+ * Note: foreignToken/metadata/data types from ITokenState may differ from TIndexedTokenState,
99
+ * they are passed through without transformation
100
+ */
101
+ const createIndexedToken = (x) => ({
102
+ address: x.address,
103
+ issuer: x.issuer || "",
104
+ name: x.name,
105
+ description: x.description,
106
+ symbol: x.symbol,
107
+ unit: x.unit,
108
+ decimal: x.decimal,
109
+ icon: x.icon || "",
110
+ website: x.website || "",
111
+ totalSupply: x.totalSupply,
112
+ initialSupply: x.initialSupply,
113
+ maxTotalSupply: x.maxTotalSupply || "",
114
+ foreignToken: x.foreignToken,
115
+ tokenFactoryAddress: x.tokenFactoryAddress || "",
116
+ metadata: x.metadata,
117
+ spenders: x.spenders || [],
118
+ minters: x.minters || [],
119
+ type: x.type || "",
120
+ data: x.data,
121
+ genesisTime: x.context.genesisTime,
122
+ renaissanceTime: x.context.renaissanceTime
123
+ });
124
+ /**
125
+ * Create indexed token factory from token factory state
126
+ * Note: token/reserveToken may not have all TIndexedTokenInput fields filled,
127
+ * they are populated with available metadata from context
128
+ */
129
+ const createIndexedTokenFactory = (tokenFactory, context) => {
130
+ return {
131
+ ...(0, lodash_pick.default)(tokenFactory, [
132
+ "address",
133
+ "owner",
134
+ "tokenAddress",
135
+ "reserveAddress",
136
+ "curve",
137
+ "feeRate",
138
+ "currentSupply",
139
+ "reserveBalance",
140
+ "status",
141
+ "data"
142
+ ]),
143
+ token: formatTokenMeta({ address: tokenFactory.tokenAddress }, context.itx?.token ? [context.itx.token] : []),
144
+ reserveToken: formatTokenMeta({ address: tokenFactory.reserveAddress }, context.config?.token ? [context.config.token] : []),
145
+ genesisTime: tokenFactory.context.genesisTime,
146
+ renaissanceTime: tokenFactory.context.renaissanceTime
147
+ };
148
+ };
149
+ /**
150
+ * Create indexed factory from asset factory state
151
+ * Note: tokens array may not have all TTokenMeta fields filled,
152
+ * they are populated with available metadata from context
153
+ */
154
+ const createIndexedFactory = (factory, context) => {
155
+ if (context.factoryTokens && context.factoryTokens.length > 0 && factory.input?.tokens) factory.input.tokens = factory.input.tokens.map((token) => formatTokenMeta(token, context.tokenStates));
156
+ return {
157
+ ...(0, lodash_pick.default)(factory, [
158
+ "address",
159
+ "owner",
160
+ "name",
161
+ "description",
162
+ "settlement",
163
+ "limit",
164
+ "trustedIssuers",
165
+ "input",
166
+ "output",
167
+ "hooks",
168
+ "data",
169
+ "numMinted",
170
+ "lastSettlement",
171
+ "balance",
172
+ "display"
173
+ ]),
174
+ tokens: Object.keys(factory.tokens || {}).map((address) => formatTokenMeta({
175
+ address,
176
+ balance: factory.tokens[address]
177
+ }, context.tokenStates)),
178
+ genesisTime: factory.context.genesisTime,
179
+ renaissanceTime: factory.context.renaissanceTime
180
+ };
181
+ };
182
+ /**
183
+ * Create indexed transaction from transaction context
184
+ * These fields are used internally to filter transactions by some entity
185
+ */
186
+ const createIndexedTransaction = async (tx, ctx, indexdb) => {
187
+ tx.accounts = [
188
+ tx.tx.from,
189
+ tx.sender,
190
+ tx.receiver
191
+ ].filter(Boolean);
192
+ tx.assets = [];
193
+ tx.tokens = [];
194
+ tx.factories = [];
195
+ tx.stakes = [];
196
+ tx.rollups = [];
197
+ tx.delegations = [];
198
+ tx.tokenFactories = [];
199
+ tx.valid = tx.code === "OK";
200
+ if (ctx.senderState) tx.accounts.push(ctx.senderState.address);
201
+ if (ctx.receiverState) tx.accounts.push(ctx.receiverState.address);
202
+ if (ctx.feeVaultState && ctx.feeVaultChange) tx.accounts.push(ctx.feeVaultState.address);
203
+ if (ctx.gasVaultState && ctx.gasVaultChange) tx.accounts.push(ctx.gasVaultState.address);
204
+ if (ctx.updatedAccounts && Array.isArray(ctx.updatedAccounts)) tx.accounts.push(...ctx.updatedAccounts.map((x) => x.address));
205
+ if (ctx.delegatorState) tx.accounts.push(ctx.delegatorState.address);
206
+ if (ctx.delegationState) tx.delegations.push(ctx.delegationState.address);
207
+ if (ctx.delegationState && ctx.txType === "fg:t:revoke_delegate") {
208
+ const indexed = await indexdb.delegation?.get(ctx.delegationState.address);
209
+ if (indexed) tx.accounts.push(indexed.to);
210
+ }
211
+ if (Array.isArray(ctx.delegatorStates)) tx.accounts.push(...ctx.delegatorStates.map((x) => x.address));
212
+ if (Array.isArray(ctx.signerStates)) tx.accounts.push(...ctx.signerStates.map((x) => x.address));
213
+ if (Array.isArray(ctx.receiverStates)) tx.accounts.push(...ctx.receiverStates.map((x) => x.address));
214
+ if (ctx.assetState) {
215
+ tx.assets.push(ctx.assetState.address);
216
+ if (ctx.txType === "fg:t:update_asset") tx.accounts.push(ctx.assetState.owner);
217
+ if (ctx.txType === "fg:t:mint_asset") tx.accounts.push(ctx.ownerAddress);
218
+ }
219
+ if (ctx.assetStates && Array.isArray(ctx.assetStates)) tx.assets.push(...ctx.assetStates.map((x) => x.address));
220
+ if (ctx.tokenState) tx.tokens.push(ctx.tokenState.address);
221
+ if (ctx.tokenStates && Array.isArray(ctx.tokenStates)) tx.tokens.push(...ctx.tokenStates.map((x) => x.address));
222
+ if (ctx.factoryState) tx.factories.push(ctx.factoryState.address);
223
+ if (ctx.stakeState) {
224
+ tx.stakes.push(ctx.stakeState.address);
225
+ if (ctx.txType === "fg:t:slash_stake") tx.accounts.push(ctx.stakeState.sender);
226
+ if (ctx.txType === "fg:t:return_stake") tx.accounts.push(ctx.stakeState.sender);
227
+ }
228
+ if (Array.isArray(ctx.stakeStates)) tx.stakes.push(...ctx.stakeStates.map((x) => x.address));
229
+ if (ctx.rollupState) tx.rollups.push(ctx.rollupState.address);
230
+ if (ctx.tokenFactoryState) tx.tokenFactories.push(ctx.tokenFactoryState.address);
231
+ const pickedFields = [
232
+ "address",
233
+ "symbol",
234
+ "decimal",
235
+ "unit"
236
+ ];
237
+ if (Array.isArray(ctx.tokenStates)) tx.tokenSymbols = ctx.tokenStates.map((t) => (0, lodash_pick.default)(t, pickedFields));
238
+ else if (ctx.tokenState) tx.tokenSymbols = [(0, lodash_pick.default)(ctx.tokenState, pickedFields)];
239
+ else tx.tokenSymbols = [];
240
+ tx.accounts = [...tx.accounts, ...tx.stakes];
241
+ return tx;
242
+ };
243
+ /**
244
+ * Create indexed stake from stake state
245
+ * Note: tokens/revokedTokens arrays may not have all TTokenMeta fields filled,
246
+ * they are populated with available metadata from context
247
+ */
248
+ const createIndexedStake = async (x, ctx, indexdb) => {
249
+ const tokens = (0, lodash_uniq.default)(Object.keys(x.tokens || {}).concat(Object.keys(x.revokedTokens || {})));
250
+ const existTokenStates = ctx.tokenStates || [];
251
+ const missingTokenStates = await Promise.all(tokens.filter((a) => !existTokenStates.find((t) => t.address === a)).map((t) => indexdb.token?.get(t)));
252
+ const tokenStates = [...existTokenStates, ...missingTokenStates.filter(Boolean)];
253
+ return {
254
+ ...(0, lodash_pick.default)(x, [
255
+ "address",
256
+ "sender",
257
+ "receiver",
258
+ "assets",
259
+ "slashers",
260
+ "revocable",
261
+ "data",
262
+ "nonce",
263
+ "message",
264
+ "revokeWaitingPeriod",
265
+ "revokedAssets"
266
+ ]),
267
+ tokens: Object.keys(x.tokens || {}).map((address) => formatTokenMeta({
268
+ address,
269
+ balance: x.tokens[address]
270
+ }, tokenStates, x)),
271
+ revokedTokens: Object.keys(x.revokedTokens || {}).map((address) => formatTokenMeta({
272
+ address,
273
+ balance: x.revokedTokens[address]
274
+ }, tokenStates)),
275
+ genesisTime: x.context.genesisTime,
276
+ renaissanceTime: x.context.renaissanceTime
277
+ };
278
+ };
279
+ /**
280
+ * Create indexed rollup from rollup state
281
+ * Note: foreignToken from TokenStateInfo is passed through without transformation
282
+ */
283
+ const createIndexedRollup = (x, ctx) => {
284
+ const rollup = {
285
+ ...(0, lodash_pick.default)(x, [
286
+ "address",
287
+ "paused",
288
+ "closed",
289
+ "tokenAddress",
290
+ "vaultAddress",
291
+ "contractAddress",
292
+ "seedValidators",
293
+ "validators",
294
+ "minStakeAmount",
295
+ "maxStakeAmount",
296
+ "minSignerCount",
297
+ "maxSignerCount",
298
+ "minBlockSize",
299
+ "maxBlockSize",
300
+ "minBlockInterval",
301
+ "minBlockConfirmation",
302
+ "minDepositAmount",
303
+ "maxDepositAmount",
304
+ "minWithdrawAmount",
305
+ "maxWithdrawAmount",
306
+ "depositFeeRate",
307
+ "withdrawFeeRate",
308
+ "proposerFeeShare",
309
+ "publisherFeeShare",
310
+ "minDepositFee",
311
+ "maxDepositFee",
312
+ "minWithdrawFee",
313
+ "maxWithdrawFee",
314
+ "issuer",
315
+ "blockHeight",
316
+ "blockHash",
317
+ "leaveWaitingPeriod",
318
+ "publishWaitingPeriod",
319
+ "publishSlashRate",
320
+ "migrateHistory",
321
+ "vaultHistory",
322
+ "data"
323
+ ]),
324
+ genesisTime: x.context.genesisTime,
325
+ renaissanceTime: x.context.renaissanceTime
326
+ };
327
+ if (ctx.tokenStates) {
328
+ const tokenState = ctx.tokenStates.find((t) => t.address === x.tokenAddress);
329
+ rollup.tokenInfo = formatTokenMeta({
330
+ address: x.tokenAddress,
331
+ value: "0"
332
+ }, ctx.tokenStates);
333
+ if (tokenState) rollup.foreignToken = tokenState.foreignToken;
334
+ }
335
+ return rollup;
336
+ };
337
+ /**
338
+ * Create indexed rollup block from rollup block state
339
+ * Note: tokenInfo may not have all TIndexedTokenInput fields filled,
340
+ * they are populated with available metadata from context
341
+ */
342
+ const createIndexedRollupBlock = (x, ctx) => {
343
+ const signatures = x.signaturesList || x.signatures || [];
344
+ return {
345
+ ...(0, lodash_pick.default)(x, [
346
+ "hash",
347
+ "height",
348
+ "merkleRoot",
349
+ "previousHash",
350
+ "txsHash",
351
+ "txs",
352
+ "proposer",
353
+ "signatures",
354
+ "rollup",
355
+ "mintedAmount",
356
+ "burnedAmount",
357
+ "rewardAmount",
358
+ "minReward",
359
+ "governance",
360
+ "data"
361
+ ]),
362
+ genesisTime: x.context.genesisTime,
363
+ renaissanceTime: x.context.renaissanceTime,
364
+ tokenInfo: ctx.rollupState ? formatTokenMeta({
365
+ address: ctx.rollupState.tokenAddress,
366
+ value: "0"
367
+ }, ctx.tokenStates) : void 0,
368
+ validators: signatures.map((v) => v.signer)
369
+ };
370
+ };
371
+ /**
372
+ * Create indexed token distribution
373
+ */
374
+ const createIndexedTokenDistribution = (x) => {
375
+ return {
376
+ tokenAddress: x.tokenAddress,
377
+ txTime: x.txTime,
378
+ account: x.account.toString(),
379
+ gas: x.gas.toString(),
380
+ fee: x.fee.toString(),
381
+ slashedVault: x.slashedVault.toString(),
382
+ stake: x.stake.toString(),
383
+ revokedStake: x.revokedStake.toString(),
384
+ gasStake: x.gasStake.toString()
385
+ };
386
+ };
387
+ /**
388
+ * Format transaction before inserting to index
389
+ */
390
+ const formatTxBeforeInsert = (row) => {
391
+ const tx = { ...row };
392
+ tx.sender = tx.sender || "";
393
+ tx.receiver = tx.receiver || "";
394
+ if (!tx.tx || !tx.tx.itxJson) return tx;
395
+ const itx = tx.tx.itxJson;
396
+ tx.assets = tx.assets || [];
397
+ if (["transfer", "transfer_v2"].includes(tx.type || "") && itx.assets) tx.assets.push(...itx.assets);
398
+ else if (["transfer_v3", "stake"].includes(tx.type || "") && itx.inputs) tx.assets.push(...(0, _ocap_util_lib_create_sorted_list.createSortedList)(itx.inputs.map((x) => x.assets || [])));
399
+ else if ([
400
+ "revoke_stake",
401
+ "slash_stake",
402
+ "return_stake"
403
+ ].includes(tx.type || "") && itx.outputs) tx.assets.push(...(0, _ocap_util_lib_create_sorted_list.createSortedList)(itx.outputs.map((x) => x.assets || [])));
404
+ else if (["exchange", "exchange_v2"].includes(tx.type || "")) tx.assets.push(...itx.sender?.assets || [], ...itx.receiver?.assets || []);
405
+ else if ([
406
+ "create_asset",
407
+ "update_asset",
408
+ "acquire_asset_v2",
409
+ "mint_asset"
410
+ ].includes(tx.type || "") && itx.address) tx.assets.push(itx.address);
411
+ tx.factories = tx.factories || [];
412
+ if ([
413
+ "acquire_asset_v2",
414
+ "acquire_asset_v3",
415
+ "mint_asset",
416
+ "create_factory"
417
+ ].includes(tx.type || "") && itx.factory) tx.factories.push(itx.factory);
418
+ tx.tokens = tx.tokens || [];
419
+ if (tx.type === "transfer_v2" && itx.tokens) tx.tokens.push(...itx.tokens.map((x) => x.address));
420
+ else if (["transfer_v3", "stake"].includes(tx.type || "") && itx.inputs) tx.tokens.push(...(0, _ocap_util_lib_create_sorted_list.createSortedList)(itx.inputs.map((x) => (x.tokens || []).map((t) => t.address))));
421
+ else if ([
422
+ "revoke_stake",
423
+ "slash_stake",
424
+ "return_stake"
425
+ ].includes(tx.type || "") && itx.outputs) tx.tokens.push(...(0, _ocap_util_lib_create_sorted_list.createSortedList)(itx.outputs.map((x) => (x.tokens || []).map((t) => t.address))));
426
+ else if (tx.type === "exchange_v2") tx.tokens.push(...(itx.sender?.tokens || []).map((x) => x.address), ...(itx.receiver?.tokens || []).map((x) => x.address));
427
+ tx.accounts = tx.accounts || [];
428
+ tx.stakes = tx.stakes || [];
429
+ tx.assets = (0, lodash_uniq.default)(tx.assets).filter(Boolean);
430
+ tx.factories = (0, lodash_uniq.default)(tx.factories).filter(Boolean);
431
+ tx.tokens = (0, lodash_uniq.default)(tx.tokens).filter(Boolean);
432
+ tx.accounts = (0, lodash_uniq.default)(tx.accounts).filter(Boolean);
433
+ tx.stakes = (0, lodash_uniq.default)(tx.stakes).filter(Boolean);
434
+ tx.rollups = (0, lodash_uniq.default)(tx.rollups || []).filter(Boolean);
435
+ tx.tokenSymbols = (0, lodash_uniqBy.default)(tx.tokenSymbols || [], "address");
436
+ return tx;
437
+ };
438
+ /**
439
+ * Format transaction after reading from index (remove internal indexing fields)
440
+ */
441
+ const formatTxAfterRead = (tx) => {
442
+ delete tx.assets;
443
+ delete tx.factories;
444
+ delete tx.tokens;
445
+ delete tx.accounts;
446
+ delete tx.stakes;
447
+ delete tx.rollups;
448
+ delete tx.delegations;
449
+ return tx;
450
+ };
451
+ /**
452
+ * Format delegation after reading from index
453
+ */
454
+ const formatDelegationAfterRead = (delegation) => {
455
+ const opsRecord = Array.isArray(delegation.ops) ? delegation.ops[0] : delegation.ops;
456
+ return {
457
+ ...delegation,
458
+ ops: Object.keys(opsRecord).map((x) => ({
459
+ key: x,
460
+ value: opsRecord[x]
461
+ }))
462
+ };
463
+ };
464
+ /**
465
+ * Parse date time string to ISO format
466
+ */
467
+ const parseDateTime = (str) => {
468
+ if (!str) return "";
469
+ const parsed = Date.parse(str);
470
+ if (Number.isNaN(parsed)) return "";
471
+ return new Date(parsed).toISOString();
472
+ };
473
+ const PAGE_SIZE_DEFAULT = 20;
474
+ const PAGE_SIZE_MAX = 100;
475
+ /**
476
+ * Format pagination parameters with defaults
477
+ */
478
+ const formatPagination = ({ paging, defaultSortField, supportedSortFields = [] }) => {
479
+ if (!defaultSortField) throw new Error("argument defaultSortField is required");
480
+ const pagination = {
481
+ size: paging?.size ?? PAGE_SIZE_DEFAULT,
482
+ cursor: typeof paging?.cursor === "number" ? paging.cursor : Number(paging?.cursor) || 0,
483
+ order: {
484
+ field: defaultSortField,
485
+ type: "desc"
486
+ }
487
+ };
488
+ let inputOrder = paging?.order;
489
+ if (Array.isArray(inputOrder)) inputOrder = inputOrder[0];
490
+ if (inputOrder?.field) pagination.order.field = inputOrder.field;
491
+ if (supportedSortFields.length > 0 && !supportedSortFields.includes(pagination.order.field)) pagination.order.field = defaultSortField;
492
+ if (inputOrder?.type) pagination.order.type = inputOrder.type;
493
+ pagination.size = Math.min(pagination.size, PAGE_SIZE_MAX);
494
+ return pagination;
495
+ };
496
+ /**
497
+ * Format next pagination info for response
498
+ */
499
+ const formatNextPagination = (total, pagination) => {
500
+ const nextCursor = pagination.cursor + pagination.size;
501
+ if (total > nextCursor) return {
502
+ cursor: String(nextCursor),
503
+ next: true,
504
+ total
505
+ };
506
+ return {
507
+ cursor: "0",
508
+ next: false,
509
+ total
510
+ };
511
+ };
512
+ /**
513
+ * Check if a transaction changes the default token balance
514
+ */
515
+ const isDefaultTokenChanged = (tx, token) => {
516
+ const itx = tx.tx.itxJson;
517
+ if ([
518
+ "fg:t:transfer",
519
+ "fg:t:transfer_v2",
520
+ "fg:t:transfer_v3",
521
+ "fg:t:acquire_asset_v2",
522
+ "fg:t:acquire_asset_v3",
523
+ "fg:t:exchange",
524
+ "fg:t:exchange_v2",
525
+ "fg:t:deposit_token",
526
+ "fg:t:withdraw_token",
527
+ "fg:t:faucet",
528
+ "fg:t:poke",
529
+ "fg:t:setup_swap",
530
+ "fg:t:retrieve_swap",
531
+ "fg:t:stake",
532
+ "fg:t:revoke_stake"
533
+ ].includes(itx.type_url || "") === false) return false;
534
+ const ZERO = new _ocap_util.BN(0);
535
+ const isDefaultToken = (id) => [token.address, ""].includes(id);
536
+ const isPositive = (v) => new _ocap_util.BN(v || 0).gt(ZERO);
537
+ const isChangedByInput = (input) => isPositive(input.value) || (input.tokens?.some((x) => isDefaultToken(x.address) && isPositive(x.value)) ?? false);
538
+ switch (itx.type_url) {
539
+ case "fg:t:transfer": return isPositive(itx.value);
540
+ case "fg:t:transfer_v2": return isChangedByInput(itx);
541
+ case "fg:t:transfer_v3":
542
+ case "fg:t:acquire_asset_v3": return (itx.inputs || []).some((input) => isChangedByInput(input));
543
+ case "fg:t:acquire_asset_v2": return (tx.receipts || []).some((r) => r.changes.some((c) => isDefaultToken(c.target) && isPositive(c.value)));
544
+ case "fg:t:exchange": return isPositive(itx.sender?.value) || isPositive(itx.receiver?.value);
545
+ case "fg:t:exchange_v2": return isChangedByInput(itx.sender || {}) || isChangedByInput(itx.receiver || {});
546
+ case "fg:t:deposit_token":
547
+ case "fg:t:withdraw_token": return isPositive(itx.value);
548
+ case "fg:t:setup_swap":
549
+ case "fg:t:retrieve_swap": return isPositive(itx.value);
550
+ case "fg:t:poke": return true;
551
+ case "fg:t:faucet": return !itx.token;
552
+ case "fg:t:stake": return (itx.inputs || []).some((input) => isChangedByInput(input));
553
+ case "fg:t:revoke_stake":
554
+ case "fg:t:slash_stake":
555
+ case "fg:t:return_stake": return (itx.outputs || []).some((output) => isChangedByInput(output));
556
+ default: return false;
557
+ }
558
+ };
559
+
560
+ //#endregion
561
+ exports.createIndexedAccount = createIndexedAccount;
562
+ exports.createIndexedAsset = createIndexedAsset;
563
+ exports.createIndexedDelegation = createIndexedDelegation;
564
+ exports.createIndexedFactory = createIndexedFactory;
565
+ exports.createIndexedRollup = createIndexedRollup;
566
+ exports.createIndexedRollupBlock = createIndexedRollupBlock;
567
+ exports.createIndexedStake = createIndexedStake;
568
+ exports.createIndexedToken = createIndexedToken;
569
+ exports.createIndexedTokenDistribution = createIndexedTokenDistribution;
570
+ exports.createIndexedTokenFactory = createIndexedTokenFactory;
571
+ exports.createIndexedTransaction = createIndexedTransaction;
572
+ exports.formatDelegationAfterRead = formatDelegationAfterRead;
573
+ exports.formatNextPagination = formatNextPagination;
574
+ exports.formatPagination = formatPagination;
575
+ exports.formatTokenMeta = formatTokenMeta;
576
+ exports.formatTxAfterRead = formatTxAfterRead;
577
+ exports.formatTxBeforeInsert = formatTxBeforeInsert;
578
+ exports.isDefaultTokenChanged = isDefaultTokenChanged;
579
+ exports.parseDateTime = parseDateTime;