@sentio/sdk 2.59.0-rc.24 → 2.59.0-rc.26

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.
Files changed (200) hide show
  1. package/lib/aptos/aptos-plugin.d.ts +6 -5
  2. package/lib/aptos/aptos-plugin.d.ts.map +1 -1
  3. package/lib/aptos/aptos-plugin.js +31 -33
  4. package/lib/aptos/aptos-plugin.js.map +1 -1
  5. package/lib/aptos/aptos-processor.d.ts +18 -18
  6. package/lib/aptos/aptos-processor.d.ts.map +1 -1
  7. package/lib/aptos/aptos-processor.js +83 -38
  8. package/lib/aptos/aptos-processor.js.map +1 -1
  9. package/lib/aptos/aptos-resource-processor-template.d.ts +5 -4
  10. package/lib/aptos/aptos-resource-processor-template.d.ts.map +1 -1
  11. package/lib/aptos/aptos-resource-processor-template.js +8 -8
  12. package/lib/aptos/aptos-resource-processor-template.js.map +1 -1
  13. package/lib/aptos/builtin/0x1.d.ts +334 -333
  14. package/lib/aptos/builtin/0x1.d.ts.map +1 -1
  15. package/lib/aptos/builtin/0x1.js +348 -348
  16. package/lib/aptos/builtin/0x1.js.map +1 -1
  17. package/lib/aptos/builtin/0x3.d.ts +67 -66
  18. package/lib/aptos/builtin/0x3.d.ts.map +1 -1
  19. package/lib/aptos/builtin/0x3.js +28 -28
  20. package/lib/aptos/builtin/0x3.js.map +1 -1
  21. package/lib/aptos/builtin/0x4.d.ts +30 -29
  22. package/lib/aptos/builtin/0x4.d.ts.map +1 -1
  23. package/lib/aptos/builtin/0x4.js +34 -34
  24. package/lib/aptos/builtin/0x4.js.map +1 -1
  25. package/lib/aptos/codegen/codegen.js +19 -2
  26. package/lib/aptos/codegen/codegen.js.map +1 -1
  27. package/lib/aptos/data.d.ts +28 -3
  28. package/lib/aptos/data.d.ts.map +1 -1
  29. package/lib/aptos/data.js +61 -0
  30. package/lib/aptos/data.js.map +1 -1
  31. package/lib/aptos/models.d.ts +1 -5
  32. package/lib/aptos/models.d.ts.map +1 -1
  33. package/lib/btc/btc-plugin.d.ts +4 -1
  34. package/lib/btc/btc-plugin.d.ts.map +1 -1
  35. package/lib/btc/btc-plugin.js +27 -0
  36. package/lib/btc/btc-plugin.js.map +1 -1
  37. package/lib/btc/btc-processor.d.ts +6 -5
  38. package/lib/btc/btc-processor.d.ts.map +1 -1
  39. package/lib/btc/btc-processor.js +35 -9
  40. package/lib/btc/btc-processor.js.map +1 -1
  41. package/lib/btc/types.d.ts +2 -1
  42. package/lib/btc/types.d.ts.map +1 -1
  43. package/lib/core/handler-options.d.ts +25 -0
  44. package/lib/core/handler-options.d.ts.map +1 -0
  45. package/lib/core/handler-options.js +21 -0
  46. package/lib/core/handler-options.js.map +1 -0
  47. package/lib/core/index.d.ts +2 -0
  48. package/lib/core/index.d.ts.map +1 -1
  49. package/lib/core/index.js +2 -0
  50. package/lib/core/index.js.map +1 -1
  51. package/lib/core/partition-handler-manager.d.ts +44 -0
  52. package/lib/core/partition-handler-manager.d.ts.map +1 -0
  53. package/lib/core/partition-handler-manager.js +76 -0
  54. package/lib/core/partition-handler-manager.js.map +1 -0
  55. package/lib/cosmos/cosmos-processor.d.ts +3 -2
  56. package/lib/cosmos/cosmos-processor.d.ts.map +1 -1
  57. package/lib/cosmos/cosmos-processor.js +11 -1
  58. package/lib/cosmos/cosmos-processor.js.map +1 -1
  59. package/lib/cosmos/types.d.ts +1 -0
  60. package/lib/cosmos/types.d.ts.map +1 -1
  61. package/lib/eth/base-processor.d.ts +18 -17
  62. package/lib/eth/base-processor.d.ts.map +1 -1
  63. package/lib/eth/base-processor.js +147 -26
  64. package/lib/eth/base-processor.js.map +1 -1
  65. package/lib/eth/builtin/internal/eacaggregatorproxy-processor.d.ts +32 -31
  66. package/lib/eth/builtin/internal/eacaggregatorproxy-processor.d.ts.map +1 -1
  67. package/lib/eth/builtin/internal/eacaggregatorproxy-processor.js +62 -62
  68. package/lib/eth/builtin/internal/eacaggregatorproxy-processor.js.map +1 -1
  69. package/lib/eth/builtin/internal/erc1155-processor.d.ts +17 -16
  70. package/lib/eth/builtin/internal/erc1155-processor.d.ts.map +1 -1
  71. package/lib/eth/builtin/internal/erc1155-processor.js +32 -32
  72. package/lib/eth/builtin/internal/erc1155-processor.js.map +1 -1
  73. package/lib/eth/builtin/internal/erc20-processor.d.ts +25 -24
  74. package/lib/eth/builtin/internal/erc20-processor.d.ts.map +1 -1
  75. package/lib/eth/builtin/internal/erc20-processor.js +48 -48
  76. package/lib/eth/builtin/internal/erc20-processor.js.map +1 -1
  77. package/lib/eth/builtin/internal/erc20bytes-processor.d.ts +14 -13
  78. package/lib/eth/builtin/internal/erc20bytes-processor.d.ts.map +1 -1
  79. package/lib/eth/builtin/internal/erc20bytes-processor.js +26 -26
  80. package/lib/eth/builtin/internal/erc20bytes-processor.js.map +1 -1
  81. package/lib/eth/builtin/internal/erc721-processor.d.ts +21 -20
  82. package/lib/eth/builtin/internal/erc721-processor.d.ts.map +1 -1
  83. package/lib/eth/builtin/internal/erc721-processor.js +40 -40
  84. package/lib/eth/builtin/internal/erc721-processor.js.map +1 -1
  85. package/lib/eth/builtin/internal/weth9-processor.d.ts +20 -19
  86. package/lib/eth/builtin/internal/weth9-processor.d.ts.map +1 -1
  87. package/lib/eth/builtin/internal/weth9-processor.js +38 -38
  88. package/lib/eth/builtin/internal/weth9-processor.js.map +1 -1
  89. package/lib/eth/codegen/event-handler.js +2 -2
  90. package/lib/eth/codegen/event-handler.js.map +1 -1
  91. package/lib/eth/codegen/file.d.ts.map +1 -1
  92. package/lib/eth/codegen/file.js +1 -0
  93. package/lib/eth/codegen/file.js.map +1 -1
  94. package/lib/eth/codegen/functions-handler.js +2 -2
  95. package/lib/eth/codegen/functions-handler.js.map +1 -1
  96. package/lib/eth/eth-plugin.d.ts +4 -1
  97. package/lib/eth/eth-plugin.d.ts.map +1 -1
  98. package/lib/eth/eth-plugin.js +42 -1
  99. package/lib/eth/eth-plugin.js.map +1 -1
  100. package/lib/eth/eth.d.ts +2 -7
  101. package/lib/eth/eth.d.ts.map +1 -1
  102. package/lib/eth/eth.js +8 -1
  103. package/lib/eth/eth.js.map +1 -1
  104. package/lib/fuel/codegen/codegen.js +5 -5
  105. package/lib/fuel/codegen/codegen.js.map +1 -1
  106. package/lib/fuel/fuel-plugin.d.ts +4 -1
  107. package/lib/fuel/fuel-plugin.d.ts.map +1 -1
  108. package/lib/fuel/fuel-plugin.js +41 -0
  109. package/lib/fuel/fuel-plugin.js.map +1 -1
  110. package/lib/fuel/fuel-processor-template.d.ts +7 -4
  111. package/lib/fuel/fuel-processor-template.d.ts.map +1 -1
  112. package/lib/fuel/fuel-processor-template.js +11 -9
  113. package/lib/fuel/fuel-processor-template.js.map +1 -1
  114. package/lib/fuel/fuel-processor.d.ts +7 -7
  115. package/lib/fuel/fuel-processor.d.ts.map +1 -1
  116. package/lib/fuel/fuel-processor.js +90 -19
  117. package/lib/fuel/fuel-processor.js.map +1 -1
  118. package/lib/fuel/types.d.ts +3 -0
  119. package/lib/fuel/types.d.ts.map +1 -1
  120. package/lib/move/filter.d.ts +4 -0
  121. package/lib/move/filter.d.ts.map +1 -1
  122. package/lib/move/filter.js +4 -0
  123. package/lib/move/filter.js.map +1 -1
  124. package/lib/move/shared-network-codegen.js +5 -5
  125. package/lib/move/shared-network-codegen.js.map +1 -1
  126. package/lib/solana/solana-processor.d.ts +10 -4
  127. package/lib/solana/solana-processor.d.ts.map +1 -1
  128. package/lib/solana/solana-processor.js +13 -4
  129. package/lib/solana/solana-processor.js.map +1 -1
  130. package/lib/stark/starknet-processor.d.ts +3 -1
  131. package/lib/stark/starknet-processor.d.ts.map +1 -1
  132. package/lib/stark/starknet-processor.js +27 -2
  133. package/lib/stark/starknet-processor.js.map +1 -1
  134. package/lib/sui/builtin/0x1.d.ts +7 -6
  135. package/lib/sui/builtin/0x1.d.ts.map +1 -1
  136. package/lib/sui/builtin/0x1.js +12 -12
  137. package/lib/sui/builtin/0x1.js.map +1 -1
  138. package/lib/sui/builtin/0x2.d.ts +61 -60
  139. package/lib/sui/builtin/0x2.d.ts.map +1 -1
  140. package/lib/sui/builtin/0x2.js +120 -120
  141. package/lib/sui/builtin/0x2.js.map +1 -1
  142. package/lib/sui/builtin/0x3.d.ts +49 -48
  143. package/lib/sui/builtin/0x3.d.ts.map +1 -1
  144. package/lib/sui/builtin/0x3.js +96 -96
  145. package/lib/sui/builtin/0x3.js.map +1 -1
  146. package/lib/sui/models.d.ts +1 -5
  147. package/lib/sui/models.d.ts.map +1 -1
  148. package/lib/sui/sui-plugin.d.ts +6 -3
  149. package/lib/sui/sui-plugin.d.ts.map +1 -1
  150. package/lib/sui/sui-plugin.js +59 -20
  151. package/lib/sui/sui-plugin.js.map +1 -1
  152. package/lib/sui/sui-processor.d.ts +5 -4
  153. package/lib/sui/sui-processor.d.ts.map +1 -1
  154. package/lib/sui/sui-processor.js +47 -11
  155. package/lib/sui/sui-processor.js.map +1 -1
  156. package/package.json +3 -3
  157. package/src/aptos/aptos-plugin.ts +35 -39
  158. package/src/aptos/aptos-processor.ts +94 -54
  159. package/src/aptos/aptos-resource-processor-template.ts +9 -8
  160. package/src/aptos/builtin/0x1.ts +1407 -507
  161. package/src/aptos/builtin/0x3.ts +251 -80
  162. package/src/aptos/builtin/0x4.ts +112 -46
  163. package/src/aptos/codegen/codegen.ts +23 -2
  164. package/src/aptos/data.ts +78 -3
  165. package/src/aptos/models.ts +1 -6
  166. package/src/btc/btc-plugin.ts +38 -0
  167. package/src/btc/btc-processor.ts +35 -10
  168. package/src/btc/types.ts +2 -1
  169. package/src/core/handler-options.ts +40 -0
  170. package/src/core/index.ts +5 -0
  171. package/src/core/partition-handler-manager.ts +94 -0
  172. package/src/cosmos/cosmos-processor.ts +12 -1
  173. package/src/cosmos/types.ts +1 -0
  174. package/src/eth/base-processor.ts +139 -33
  175. package/src/eth/builtin/internal/eacaggregatorproxy-processor.ts +118 -62
  176. package/src/eth/builtin/internal/erc1155-processor.ts +76 -32
  177. package/src/eth/builtin/internal/erc20-processor.ts +79 -48
  178. package/src/eth/builtin/internal/erc20bytes-processor.ts +47 -26
  179. package/src/eth/builtin/internal/erc721-processor.ts +77 -40
  180. package/src/eth/builtin/internal/weth9-processor.ts +79 -38
  181. package/src/eth/codegen/event-handler.ts +2 -2
  182. package/src/eth/codegen/file.ts +1 -0
  183. package/src/eth/codegen/functions-handler.ts +2 -2
  184. package/src/eth/eth-plugin.ts +55 -3
  185. package/src/eth/eth.ts +12 -1
  186. package/src/fuel/codegen/codegen.ts +5 -5
  187. package/src/fuel/fuel-plugin.ts +56 -0
  188. package/src/fuel/fuel-processor-template.ts +17 -10
  189. package/src/fuel/fuel-processor.ts +89 -13
  190. package/src/fuel/types.ts +3 -0
  191. package/src/move/filter.ts +4 -0
  192. package/src/move/shared-network-codegen.ts +5 -5
  193. package/src/solana/solana-processor.ts +28 -6
  194. package/src/stark/starknet-processor.ts +28 -2
  195. package/src/sui/builtin/0x1.ts +22 -13
  196. package/src/sui/builtin/0x2.ts +220 -121
  197. package/src/sui/builtin/0x3.ts +241 -97
  198. package/src/sui/models.ts +1 -6
  199. package/src/sui/sui-plugin.ts +68 -22
  200. package/src/sui/sui-processor.ts +48 -11
@@ -7,10 +7,12 @@ import {
7
7
  HandlerType,
8
8
  ProcessConfigResponse,
9
9
  ProcessResult,
10
+ ProcessStreamResponse_Partitions,
10
11
  StartRequest
11
12
  } from '@sentio/protos'
12
13
 
13
14
  import { ServerError, Status } from 'nice-grpc'
15
+ import { PartitionHandlerManager } from '../core/index.js'
14
16
  import { TemplateInstanceState } from '../core/template.js'
15
17
  import { BTCProcessorState } from './btc-processor.js'
16
18
  import { filters2Proto, TransactionFilter } from './filter.js'
@@ -27,6 +29,8 @@ export class BTCPlugin extends Plugin {
27
29
  blockHandlers: []
28
30
  }
29
31
 
32
+ partitionManager = new PartitionHandlerManager()
33
+
30
34
  async configure(config: ProcessConfigResponse) {
31
35
  const handlers: Handlers = {
32
36
  txHandlers: [],
@@ -47,6 +51,11 @@ export class BTCPlugin extends Plugin {
47
51
  })
48
52
  for (const callHandler of processor.callHandlers) {
49
53
  const handlerId = handlers.txHandlers.push(callHandler.handler) - 1
54
+ this.partitionManager.registerPartitionHandler(
55
+ HandlerType.BTC_TRANSACTION,
56
+ handlerId,
57
+ callHandler.partitionHandler
58
+ )
50
59
  const handlerName = callHandler.handlerName
51
60
 
52
61
  if (callHandler.filter) {
@@ -70,6 +79,7 @@ export class BTCPlugin extends Plugin {
70
79
 
71
80
  for (const blockHandler of processor.blockHandlers) {
72
81
  const handlerId = handlers.blockHandlers.push(blockHandler.handler) - 1
82
+ this.partitionManager.registerPartitionHandler(HandlerType.BTC_BLOCK, handlerId, blockHandler.partitionHandler)
73
83
  contractConfig.intervalConfigs.push({
74
84
  slot: 0,
75
85
  slotInterval: blockHandler.blockInterval,
@@ -107,6 +117,34 @@ export class BTCPlugin extends Plugin {
107
117
  }
108
118
  }
109
119
 
120
+ async partition(request: DataBinding): Promise<ProcessStreamResponse_Partitions> {
121
+ let data: any
122
+ switch (request.handlerType) {
123
+ case HandlerType.BTC_TRANSACTION:
124
+ if (!request.data?.btcTransaction) {
125
+ throw new ServerError(Status.INVALID_ARGUMENT, "btcTransaction can't be empty")
126
+ }
127
+ data = request.data.btcTransaction
128
+ break
129
+ case HandlerType.BTC_BLOCK:
130
+ if (!request.data?.btcBlock) {
131
+ throw new ServerError(Status.INVALID_ARGUMENT, "btcBlock can't be empty")
132
+ }
133
+ data = request.data.btcBlock
134
+ break
135
+ default:
136
+ throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
137
+ }
138
+ const partitions = await this.partitionManager.processPartitionForHandlerType(
139
+ request.handlerType,
140
+ request.handlerIds,
141
+ data
142
+ )
143
+ return {
144
+ partitions
145
+ }
146
+ }
147
+
110
148
  async start(request: StartRequest) {}
111
149
 
112
150
  stateDiff(config: ProcessConfigResponse): boolean {
@@ -2,7 +2,7 @@ import { ListStateStorage } from '@sentio/runtime'
2
2
  import { BlockHandler, BTCBlock, BTCBlockContext, BTCContext, BTCOnIntervalFetchConfig, Transaction } from './types.js'
3
3
  import { Data_BTCBlock, Data_BTCTransaction, HandleInterval, ProcessResult } from '@sentio/protos'
4
4
  import { TransactionFilters } from './filter.js'
5
- import { PromiseOrVoid } from '../core/index.js'
5
+ import { HandlerOptions, PromiseOrVoid } from '../core/index.js'
6
6
  import { ServerError, Status } from 'nice-grpc'
7
7
  import { getHandlerName, proxyProcessor } from '../utils/metrics.js'
8
8
 
@@ -26,7 +26,8 @@ export class BTCProcessor {
26
26
 
27
27
  public onTransaction(
28
28
  handler: (transaction: Transaction, ctx: BTCContext) => void | Promise<void>,
29
- filter?: TransactionFilters
29
+ filter?: TransactionFilters,
30
+ handlerOptions?: HandlerOptions<object, Transaction>
30
31
  ) {
31
32
  const callHandler = {
32
33
  handlerName: getHandlerName(),
@@ -42,7 +43,16 @@ export class BTCProcessor {
42
43
  await handler(tx, ctx)
43
44
  return ctx.stopAndGetResult()
44
45
  },
45
- filter
46
+ filter,
47
+ partitionHandler: async (call: Data_BTCTransaction): Promise<string | undefined> => {
48
+ const p = handlerOptions?.partitionKey
49
+ if (!p) return undefined
50
+ if (typeof p === 'function') {
51
+ const tx = call.transaction as Transaction
52
+ return p(tx)
53
+ }
54
+ return p
55
+ }
46
56
  }
47
57
  this.callHandlers.push(callHandler)
48
58
  return this
@@ -52,7 +62,7 @@ export class BTCProcessor {
52
62
  handler: (block: BTCBlock, ctx: BTCBlockContext) => PromiseOrVoid,
53
63
  timeInterval: HandleInterval | undefined,
54
64
  blockInterval: HandleInterval | undefined,
55
- fetchConfig?: BTCOnIntervalFetchConfig
65
+ handlerOptions?: HandlerOptions<BTCOnIntervalFetchConfig, BTCBlock>
56
66
  ): this {
57
67
  if (timeInterval) {
58
68
  if (timeInterval.backfillInterval < timeInterval.recentInterval) {
@@ -75,7 +85,7 @@ export class BTCProcessor {
75
85
  const block = {
76
86
  ...header
77
87
  } as BTCBlock
78
- if (fetchConfig?.getTransactions) {
88
+ if (handlerOptions?.getTransactions) {
79
89
  block.tx = header.rawtx?.map((tx: any) => tx as Transaction)
80
90
  }
81
91
 
@@ -88,7 +98,21 @@ export class BTCProcessor {
88
98
  await handler(block, ctx)
89
99
  return ctx.stopAndGetResult()
90
100
  },
91
- fetchConfig
101
+ fetchConfig: handlerOptions,
102
+ partitionHandler: async (data: Data_BTCBlock): Promise<string | undefined> => {
103
+ const p = handlerOptions?.partitionKey
104
+ if (!p) return undefined
105
+ if (typeof p === 'function') {
106
+ const header = data.block
107
+ if (!header) return undefined
108
+ const block = { ...header } as BTCBlock
109
+ if (handlerOptions?.getTransactions) {
110
+ block.tx = header.rawtx?.map((tx: any) => tx as Transaction)
111
+ }
112
+ return p(block)
113
+ }
114
+ return p
115
+ }
92
116
  })
93
117
  return this
94
118
  }
@@ -97,7 +121,7 @@ export class BTCProcessor {
97
121
  handler: (block: BTCBlock, ctx: BTCBlockContext) => PromiseOrVoid,
98
122
  blockInterval = 250,
99
123
  backfillBlockInterval = 1000,
100
- fetchConfig?: BTCOnIntervalFetchConfig
124
+ handlerOptions?: HandlerOptions<BTCOnIntervalFetchConfig, BTCBlock>
101
125
  ): this {
102
126
  return this.onInterval(
103
127
  handler,
@@ -106,7 +130,7 @@ export class BTCProcessor {
106
130
  recentInterval: blockInterval,
107
131
  backfillInterval: backfillBlockInterval
108
132
  },
109
- fetchConfig
133
+ handlerOptions
110
134
  )
111
135
  }
112
136
 
@@ -114,13 +138,13 @@ export class BTCProcessor {
114
138
  handler: (block: BTCBlock, ctx: BTCBlockContext) => PromiseOrVoid,
115
139
  timeIntervalInMinutes = 60,
116
140
  backfillTimeIntervalInMinutes = 240,
117
- fetchConfig?: BTCOnIntervalFetchConfig
141
+ handlerOptions?: HandlerOptions<BTCOnIntervalFetchConfig, BTCBlock>
118
142
  ): this {
119
143
  return this.onInterval(
120
144
  handler,
121
145
  { recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },
122
146
  undefined,
123
- fetchConfig
147
+ handlerOptions
124
148
  )
125
149
  }
126
150
  }
@@ -137,4 +161,5 @@ export type CallHandler<T> = {
137
161
  handlerName: string
138
162
  handler: (call: T) => Promise<ProcessResult>
139
163
  filter?: TransactionFilters
164
+ partitionHandler?: (call: T) => Promise<string | undefined>
140
165
  }
package/src/btc/types.ts CHANGED
@@ -126,8 +126,9 @@ export type BlockHandler = {
126
126
  handler: (block: Data_BTCBlock) => Promise<ProcessResult>
127
127
  handlerName: string
128
128
  fetchConfig?: BTCOnIntervalFetchConfig
129
+ partitionHandler?: (block: Data_BTCBlock) => Promise<string | undefined>
129
130
  }
130
131
 
131
132
  export type BTCOnIntervalFetchConfig = {
132
- getTransactions: boolean
133
+ getTransactions?: boolean
133
134
  }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Function type that extracts a partition key from data.
3
+ * @template D The data type to process
4
+ */
5
+ export type PartitionHandler<D> = (data: D) => string | Promise<string>
6
+
7
+ /**
8
+ * Handler options that extend fetch configuration with optional partitioning support.
9
+ * @template F The fetch configuration type (e.g., EthFetchConfig, MoveFetchConfig)
10
+ * @template D The data type that will be processed (e.g., Event, Transaction, Block)
11
+ */
12
+ export type HandlerOptions<F, D> = Partial<F> & {
13
+ /**
14
+ * Optional partition key for data partitioning.
15
+ * Can be a static string or a function that computes the key from the data.
16
+ */
17
+ partitionKey?: string | PartitionHandler<D>
18
+ }
19
+
20
+ /**
21
+ * Merge two handler options, with the second options taking precedence over the first.
22
+ * @param options1 First handler options
23
+ * @param options2 Second handler options (takes precedence)
24
+ * @returns Merged handler options
25
+ */
26
+ export function mergeHandlerOptions<F, D>(
27
+ options1?: HandlerOptions<F, D>,
28
+ options2?: HandlerOptions<F, D>
29
+ ): HandlerOptions<F, D> | undefined {
30
+ if (!options1 && !options2) return undefined
31
+ if (!options1) return options2
32
+ if (!options2) return options1
33
+
34
+ return {
35
+ ...options1,
36
+ ...options2,
37
+ // For partitionKey, the second option takes precedence
38
+ partitionKey: options2.partitionKey ?? options1.partitionKey
39
+ }
40
+ }
package/src/core/index.ts CHANGED
@@ -22,3 +22,8 @@ export { type Numberish, toBigInteger, toMetricValue } from './numberish.js'
22
22
  export { CorePlugin } from './core-plugin.js'
23
23
  export { DatabaseSchema } from './database-schema.js'
24
24
  export * from './constants.js'
25
+ export { type HandlerOptions, type PartitionHandler, mergeHandlerOptions } from './handler-options.js'
26
+ export {
27
+ PartitionHandlerManager,
28
+ type PartitionHandler as PartitionHandlerFunction
29
+ } from './partition-handler-manager.js'
@@ -0,0 +1,94 @@
1
+ import {
2
+ HandlerType,
3
+ ProcessStreamResponse_Partitions_Partition,
4
+ ProcessStreamResponse_Partitions_Partition_SysValue
5
+ } from '@sentio/protos'
6
+ import { ServerError, Status } from 'nice-grpc'
7
+
8
+ /**
9
+ * Type for partition handler functions that can process any data type
10
+ */
11
+ export type PartitionHandler = (request: any) => Promise<string | undefined>
12
+
13
+ /**
14
+ * Generic manager for handling partition logic across all chain plugins.
15
+ * Provides a unified interface for registering, storing, and processing partition handlers.
16
+ * Chain-specific logic should remain in individual plugins.
17
+ */
18
+ export class PartitionHandlerManager {
19
+ private partitionHandlers: Map<HandlerType, Record<number, PartitionHandler>> = new Map()
20
+
21
+ /**
22
+ * Register a partition handler for a specific handler type and ID
23
+ * @param handlerType The type of handler (e.g., HandlerType.ETH_LOG, HandlerType.APT_EVENT)
24
+ * @param handlerId The unique ID for this handler instance
25
+ * @param partitionHandler The partition handler function (optional)
26
+ */
27
+ registerPartitionHandler(handlerType: HandlerType, handlerId: number, partitionHandler?: PartitionHandler): void {
28
+ if (partitionHandler) {
29
+ const existingHandlers = this.partitionHandlers.get(handlerType) || {}
30
+ existingHandlers[handlerId] = partitionHandler
31
+ this.partitionHandlers.set(handlerType, existingHandlers)
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Process partition logic for a specific handler type
37
+ * @param handlerType The type of handler being processed
38
+ * @param handlerIds Array of handler IDs to process
39
+ * @param data The data to pass to partition handlers
40
+ * @returns Record mapping handler IDs to partition results
41
+ */
42
+ async processPartitionForHandlerType(
43
+ handlerType: HandlerType,
44
+ handlerIds: number[],
45
+ data: any
46
+ ): Promise<Record<number, ProcessStreamResponse_Partitions_Partition>> {
47
+ const result: Record<number, ProcessStreamResponse_Partitions_Partition> = {}
48
+
49
+ for (const handlerId of handlerIds) {
50
+ const partitionHandler = this.partitionHandlers.get(handlerType)?.[handlerId]
51
+ if (partitionHandler && data) {
52
+ try {
53
+ const partitionValue = await partitionHandler(data)
54
+ result[handlerId] = {
55
+ userValue: partitionValue
56
+ }
57
+ } catch (error) {
58
+ // If partition handler fails, fall back to unrecognized
59
+ throw new ServerError(Status.INVALID_ARGUMENT, 'compute partition key failed, error:' + error.message)
60
+ }
61
+ } else {
62
+ result[handlerId] = {
63
+ sysValue: ProcessStreamResponse_Partitions_Partition_SysValue.UNRECOGNIZED
64
+ }
65
+ }
66
+ }
67
+
68
+ return result
69
+ }
70
+
71
+ /**
72
+ * Clear all partition handlers (useful for testing or reinitialization)
73
+ */
74
+ clear(): void {
75
+ this.partitionHandlers.clear()
76
+ }
77
+
78
+ /**
79
+ * Get all registered handler types
80
+ * @returns Array of handler types that have registered partition handlers
81
+ */
82
+ getRegisteredHandlerTypes(): HandlerType[] {
83
+ return Array.from(this.partitionHandlers.keys())
84
+ }
85
+
86
+ /**
87
+ * Get the number of registered handlers for a specific handler type
88
+ * @param handlerType The handler type to check
89
+ * @returns Number of registered handlers for this type
90
+ */
91
+ getHandlerCount(handlerType: HandlerType): number {
92
+ return Object.keys(this.partitionHandlers.get(handlerType) || {}).length
93
+ }
94
+ }
@@ -3,6 +3,7 @@ import { Data_CosmosCall } from '@sentio/protos'
3
3
  import { CosmosContext } from './context.js'
4
4
  import { CosmosEvent, CosmosTransaction, CosmosTxLog } from './transaction.js'
5
5
  import { getHandlerName, proxyProcessor } from '../utils/metrics.js'
6
+ import { HandlerOptions } from '../core/handler-options.js'
6
7
 
7
8
  export class CosmosProcessor {
8
9
  callHandlers: CallHandler<Data_CosmosCall>[] = []
@@ -19,7 +20,8 @@ export class CosmosProcessor {
19
20
 
20
21
  public onLogEvent(
21
22
  logFilters: string[] | string,
22
- handler: (log: CosmosTxLog, event: CosmosEvent, context: CosmosContext) => void | Promise<void>
23
+ handler: (log: CosmosTxLog, event: CosmosEvent, context: CosmosContext) => void | Promise<void>,
24
+ handlerOptions?: HandlerOptions<object, CosmosTransaction>
23
25
  ) {
24
26
  const filter = Array.isArray(logFilters) ? logFilters : [logFilters]
25
27
  const callHandler = {
@@ -39,6 +41,15 @@ export class CosmosProcessor {
39
41
  },
40
42
  logConfig: {
41
43
  logFilters: filter
44
+ },
45
+ partitionHandler: async (call: Data_CosmosCall): Promise<string | undefined> => {
46
+ const p = handlerOptions?.partitionKey
47
+ if (!p) return undefined
48
+ if (typeof p === 'function') {
49
+ const transaction = call.transaction as CosmosTransaction
50
+ return p(transaction)
51
+ }
52
+ return p
42
53
  }
43
54
  }
44
55
  this.callHandlers.push(callHandler)
@@ -13,6 +13,7 @@ export type CallHandler<T> = {
13
13
  logConfig?: {
14
14
  logFilters: string[]
15
15
  }
16
+ partitionHandler?: (call: T) => Promise<string | undefined>
16
17
  }
17
18
 
18
19
  export type CosmosProcessorConfig = {