@sentio/sdk 2.59.4 → 2.60.0-rc.1

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 (51) hide show
  1. package/lib/aptos/aptos-plugin.d.ts +5 -11
  2. package/lib/aptos/aptos-plugin.d.ts.map +1 -1
  3. package/lib/aptos/aptos-plugin.js +38 -23
  4. package/lib/aptos/aptos-plugin.js.map +1 -1
  5. package/lib/btc/btc-plugin.d.ts +5 -8
  6. package/lib/btc/btc-plugin.d.ts.map +1 -1
  7. package/lib/btc/btc-plugin.js +22 -14
  8. package/lib/btc/btc-plugin.js.map +1 -1
  9. package/lib/core/core-plugin.d.ts +2 -1
  10. package/lib/core/core-plugin.d.ts.map +1 -1
  11. package/lib/core/core-plugin.js +15 -1
  12. package/lib/core/core-plugin.js.map +1 -1
  13. package/lib/core/handler-register.d.ts +18 -0
  14. package/lib/core/handler-register.d.ts.map +1 -0
  15. package/lib/core/handler-register.js +62 -0
  16. package/lib/core/handler-register.js.map +1 -0
  17. package/lib/cosmos/cosmos-plugin.d.ts +5 -7
  18. package/lib/cosmos/cosmos-plugin.d.ts.map +1 -1
  19. package/lib/cosmos/cosmos-plugin.js +18 -10
  20. package/lib/cosmos/cosmos-plugin.js.map +1 -1
  21. package/lib/eth/eth-plugin.d.ts +5 -40
  22. package/lib/eth/eth-plugin.d.ts.map +1 -1
  23. package/lib/eth/eth-plugin.js +41 -117
  24. package/lib/eth/eth-plugin.js.map +1 -1
  25. package/lib/fuel/fuel-plugin.d.ts +5 -9
  26. package/lib/fuel/fuel-plugin.d.ts.map +1 -1
  27. package/lib/fuel/fuel-plugin.js +26 -18
  28. package/lib/fuel/fuel-plugin.js.map +1 -1
  29. package/lib/solana/solana-plugin.d.ts +3 -2
  30. package/lib/solana/solana-plugin.d.ts.map +1 -1
  31. package/lib/solana/solana-plugin.js +11 -1
  32. package/lib/solana/solana-plugin.js.map +1 -1
  33. package/lib/stark/starknet-plugin.d.ts +5 -7
  34. package/lib/stark/starknet-plugin.d.ts.map +1 -1
  35. package/lib/stark/starknet-plugin.js +18 -10
  36. package/lib/stark/starknet-plugin.js.map +1 -1
  37. package/lib/sui/sui-plugin.d.ts +5 -10
  38. package/lib/sui/sui-plugin.d.ts.map +1 -1
  39. package/lib/sui/sui-plugin.js +37 -24
  40. package/lib/sui/sui-plugin.js.map +1 -1
  41. package/package.json +3 -3
  42. package/src/aptos/aptos-plugin.ts +55 -44
  43. package/src/btc/btc-plugin.ts +33 -32
  44. package/src/core/core-plugin.ts +17 -3
  45. package/src/core/handler-register.ts +79 -0
  46. package/src/cosmos/cosmos-plugin.ts +24 -20
  47. package/src/eth/eth-plugin.ts +62 -182
  48. package/src/fuel/fuel-plugin.ts +43 -44
  49. package/src/solana/solana-plugin.ts +20 -2
  50. package/src/stark/starknet-plugin.ts +24 -20
  51. package/src/sui/sui-plugin.ts +58 -52
@@ -1,11 +1,12 @@
1
1
  import { errorString, GLOBAL_CONFIG, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime'
2
2
  import { PartitionHandlerManager } from '../core/index.js'
3
+ import { HandlerRegister } from '../core/handler-register.js'
3
4
  import {
4
5
  AccountConfig,
5
6
  ContractConfig,
6
- Data_AptCall,
7
7
  DataBinding,
8
8
  HandlerType,
9
+ InitResponse,
9
10
  MoveCallHandlerConfig,
10
11
  MoveEventHandlerConfig,
11
12
  MoveOwnerType,
@@ -27,26 +28,21 @@ import {
27
28
  AptosResourceProcessorTemplateState
28
29
  } from './aptos-resource-processor-template.js'
29
30
  import { AptosNetwork } from './network.js'
30
- import { AptEvent, AptCall, AptResource } from './data.js'
31
-
32
- interface Handlers {
33
- aptosEventHandlers: ((event: AptEvent) => Promise<ProcessResult>)[]
34
- aptosCallHandlers: ((func: AptCall) => Promise<ProcessResult>)[]
35
- aptosResourceHandlers: ((resourceWithVersion: AptResource) => Promise<ProcessResult>)[]
36
- aptosTransactionIntervalHandlers: ((txn: Data_AptCall) => Promise<ProcessResult>)[]
37
- }
31
+ import { AptCall, AptEvent, AptResource } from './data.js'
38
32
 
39
33
  export class AptosPlugin extends Plugin {
40
34
  name: string = 'AptosPlugin'
41
- handlers: Handlers = {
42
- aptosEventHandlers: [],
43
- aptosCallHandlers: [],
44
- aptosResourceHandlers: [],
45
- aptosTransactionIntervalHandlers: []
46
- }
47
-
35
+ handlerRegister = new HandlerRegister()
48
36
  partitionManager = new PartitionHandlerManager()
49
37
 
38
+ async init(config: InitResponse) {
39
+ for (const state of [AptosProcessorState.INSTANCE, AptosResourceProcessorState.INSTANCE]) {
40
+ for (const aptosProcessor of state.getValues()) {
41
+ config.chainIds.push(aptosProcessor.getChainId())
42
+ }
43
+ }
44
+ }
45
+
50
46
  async start(request: StartRequest) {
51
47
  await initTokenList()
52
48
 
@@ -72,14 +68,13 @@ export class AptosPlugin extends Plugin {
72
68
  }
73
69
  }
74
70
 
75
- async configure(config: ProcessConfigResponse) {
76
- const handlers: Handlers = {
77
- aptosTransactionIntervalHandlers: [],
78
- aptosEventHandlers: [],
79
- aptosCallHandlers: [],
80
- aptosResourceHandlers: []
81
- }
71
+ async configure(config: ProcessConfigResponse, forChainId?: string) {
72
+ this.handlerRegister.clear(forChainId as any)
82
73
  for (const aptosProcessor of AptosProcessorState.INSTANCE.getValues()) {
74
+ const chainId = aptosProcessor.getChainId()
75
+ if (forChainId !== undefined && forChainId !== chainId.toString()) {
76
+ continue
77
+ }
83
78
  const contractConfig = ContractConfig.fromPartial({
84
79
  processorType: USER_PROCESSOR,
85
80
  contract: {
@@ -93,7 +88,7 @@ export class AptosPlugin extends Plugin {
93
88
  })
94
89
  // 1. Prepare event handlers
95
90
  for (const handler of aptosProcessor.eventHandlers) {
96
- const handlerId = handlers.aptosEventHandlers.push(handler.handler) - 1
91
+ const handlerId = this.handlerRegister.register(handler.handler, chainId)
97
92
  this.partitionManager.registerPartitionHandler(HandlerType.APT_EVENT, handlerId, handler.partitionHandler)
98
93
  const eventHandlerConfig: MoveEventHandlerConfig = {
99
94
  filters: handler.filters.map((f) => {
@@ -112,7 +107,7 @@ export class AptosPlugin extends Plugin {
112
107
 
113
108
  // 2. Prepare function handlers
114
109
  for (const handler of aptosProcessor.callHandlers) {
115
- const handlerId = handlers.aptosCallHandlers.push(handler.handler) - 1
110
+ const handlerId = this.handlerRegister.register(handler.handler, chainId)
116
111
  this.partitionManager.registerPartitionHandler(HandlerType.APT_CALL, handlerId, handler.partitionHandler)
117
112
  const functionHandlerConfig: MoveCallHandlerConfig = {
118
113
  filters: handler.filters.map((filter) => {
@@ -137,6 +132,10 @@ export class AptosPlugin extends Plugin {
137
132
 
138
133
  // Prepare resource handlers
139
134
  for (const aptosProcessor of AptosProcessorState.INSTANCE.getValues()) {
135
+ const chainId = aptosProcessor.getChainId()
136
+ if (forChainId !== undefined && forChainId !== chainId.toString()) {
137
+ continue
138
+ }
140
139
  const accountConfig = AccountConfig.fromPartial({
141
140
  address: aptosProcessor.config.address,
142
141
  chainId: aptosProcessor.getChainId(),
@@ -144,7 +143,7 @@ export class AptosPlugin extends Plugin {
144
143
  endBlock: aptosProcessor.config.endVersion
145
144
  })
146
145
  for (const handler of aptosProcessor.resourceChangeHandlers) {
147
- const handlerId = handlers.aptosResourceHandlers.push(handler.handler) - 1
146
+ const handlerId = this.handlerRegister.register(handler.handler, chainId)
148
147
  this.partitionManager.registerPartitionHandler(HandlerType.APT_RESOURCE, handlerId, handler.partitionHandler)
149
148
  accountConfig.moveResourceChangeConfigs.push({
150
149
  handlerId: handlerId,
@@ -156,7 +155,7 @@ export class AptosPlugin extends Plugin {
156
155
 
157
156
  // Prepare interval handlers
158
157
  for (const handler of aptosProcessor.transactionIntervalHandlers) {
159
- const handlerId = handlers.aptosTransactionIntervalHandlers.push(handler.handler) - 1
158
+ const handlerId = this.handlerRegister.register(handler.handler, chainId)
160
159
  this.partitionManager.registerPartitionHandler(HandlerType.APT_CALL, handlerId, handler.partitionHandler)
161
160
  accountConfig.moveIntervalConfigs.push({
162
161
  intervalConfig: {
@@ -179,6 +178,10 @@ export class AptosPlugin extends Plugin {
179
178
  }
180
179
 
181
180
  for (const aptosProcessor of AptosResourceProcessorState.INSTANCE.getValues()) {
181
+ const chainId = aptosProcessor.getChainId()
182
+ if (forChainId !== undefined && forChainId !== chainId.toString()) {
183
+ continue
184
+ }
182
185
  const accountConfig = AccountConfig.fromPartial({
183
186
  address: aptosProcessor.config.address,
184
187
  chainId: aptosProcessor.getChainId(),
@@ -186,7 +189,7 @@ export class AptosPlugin extends Plugin {
186
189
  endBlock: aptosProcessor.config.endVersion
187
190
  })
188
191
  for (const handler of aptosProcessor.resourceIntervalHandlers) {
189
- const handlerId = handlers.aptosResourceHandlers.push(handler.handler) - 1
192
+ const handlerId = this.handlerRegister.register(handler.handler, chainId)
190
193
  this.partitionManager.registerPartitionHandler(HandlerType.APT_RESOURCE, handlerId, handler.partitionHandler)
191
194
  if (handler.timeIntervalInMinutes || handler.versionInterval) {
192
195
  accountConfig.moveIntervalConfigs.push({
@@ -216,7 +219,6 @@ export class AptosPlugin extends Plugin {
216
219
  }
217
220
  config.accountConfigs.push(accountConfig)
218
221
  }
219
- this.handlers = handlers
220
222
  }
221
223
 
222
224
  supportedHandlers = [HandlerType.APT_CALL, HandlerType.APT_RESOURCE, HandlerType.APT_EVENT]
@@ -276,12 +278,14 @@ export class AptosPlugin extends Plugin {
276
278
  const event = new AptEvent(binding.data.aptEvent)
277
279
 
278
280
  for (const handlerId of binding.handlerIds) {
279
- const promise = this.handlers.aptosEventHandlers[handlerId](event).catch((e) => {
280
- throw new ServerError(
281
- Status.INTERNAL,
282
- 'error processing event: ' + JSON.stringify(event) + '\n' + errorString(e)
283
- )
284
- })
281
+ const promise = this.handlerRegister
282
+ .getHandlerById(handlerId)(event)
283
+ .catch((e: any) => {
284
+ throw new ServerError(
285
+ Status.INTERNAL,
286
+ 'error processing event: ' + JSON.stringify(event) + '\n' + errorString(e)
287
+ )
288
+ })
285
289
  if (GLOBAL_CONFIG.execution.sequential) {
286
290
  await promise
287
291
  }
@@ -298,12 +302,14 @@ export class AptosPlugin extends Plugin {
298
302
 
299
303
  const promises: Promise<ProcessResult>[] = []
300
304
  for (const handlerId of binding.handlerIds) {
301
- const promise = this.handlers.aptosResourceHandlers[handlerId](resource).catch((e) => {
302
- throw new ServerError(
303
- Status.INTERNAL,
304
- 'error processing resource: ' + JSON.stringify(resource) + '\n' + errorString(e)
305
- )
306
- })
305
+ const promise = this.handlerRegister
306
+ .getHandlerById(handlerId)(resource)
307
+ .catch((e: any) => {
308
+ throw new ServerError(
309
+ Status.INTERNAL,
310
+ 'error processing resource: ' + JSON.stringify(resource) + '\n' + errorString(e)
311
+ )
312
+ })
307
313
  if (GLOBAL_CONFIG.execution.sequential) {
308
314
  await promise
309
315
  }
@@ -321,9 +327,14 @@ export class AptosPlugin extends Plugin {
321
327
  const promises: Promise<ProcessResult>[] = []
322
328
  for (const handlerId of binding.handlerIds) {
323
329
  // only support aptos call for now
324
- const promise = this.handlers.aptosCallHandlers[handlerId](call).catch((e) => {
325
- throw new ServerError(Status.INTERNAL, 'error processing call: ' + JSON.stringify(call) + '\n' + errorString(e))
326
- })
330
+ const promise = this.handlerRegister
331
+ .getHandlerById(handlerId)(call)
332
+ .catch((e: any) => {
333
+ throw new ServerError(
334
+ Status.INTERNAL,
335
+ 'error processing call: ' + JSON.stringify(call) + '\n' + errorString(e)
336
+ )
337
+ })
327
338
  if (GLOBAL_CONFIG.execution.sequential) {
328
339
  await promise
329
340
  }
@@ -1,10 +1,9 @@
1
1
  import { errorString, GLOBAL_CONFIG, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime'
2
2
  import {
3
3
  ContractConfig,
4
- Data_BTCBlock,
5
- Data_BTCTransaction,
6
4
  DataBinding,
7
5
  HandlerType,
6
+ InitResponse,
8
7
  ProcessConfigResponse,
9
8
  ProcessResult,
10
9
  ProcessStreamResponse_Partitions,
@@ -13,31 +12,31 @@ import {
13
12
 
14
13
  import { ServerError, Status } from 'nice-grpc'
15
14
  import { PartitionHandlerManager } from '../core/index.js'
15
+ import { HandlerRegister } from '../core/handler-register.js'
16
16
  import { TemplateInstanceState } from '../core/template.js'
17
17
  import { BTCProcessorState } from './btc-processor.js'
18
18
  import { filters2Proto, TransactionFilter } from './filter.js'
19
19
 
20
- interface Handlers {
21
- txHandlers: ((trace: Data_BTCTransaction) => Promise<ProcessResult>)[]
22
- blockHandlers: ((trace: Data_BTCBlock) => Promise<ProcessResult>)[]
23
- }
24
-
25
20
  export class BTCPlugin extends Plugin {
26
21
  name: string = 'BTCPlugin'
27
- handlers: Handlers = {
28
- txHandlers: [],
29
- blockHandlers: []
30
- }
31
-
22
+ handlerRegister = new HandlerRegister()
32
23
  partitionManager = new PartitionHandlerManager()
33
24
 
34
- async configure(config: ProcessConfigResponse) {
35
- const handlers: Handlers = {
36
- txHandlers: [],
37
- blockHandlers: []
25
+ async init(config: InitResponse) {
26
+ for (const aptosProcessor of BTCProcessorState.INSTANCE.getValues()) {
27
+ const chainId = aptosProcessor.config.chainId
28
+ config.chainIds.push(chainId)
38
29
  }
30
+ }
31
+
32
+ async configure(config: ProcessConfigResponse, forChainId?: string) {
33
+ this.handlerRegister.clear(forChainId as any)
39
34
 
40
35
  for (const processor of BTCProcessorState.INSTANCE.getValues()) {
36
+ const chainId = processor.config.chainId
37
+ if (forChainId !== undefined && forChainId !== chainId.toString()) {
38
+ continue
39
+ }
41
40
  const contractConfig = ContractConfig.fromPartial({
42
41
  processorType: USER_PROCESSOR,
43
42
  contract: {
@@ -50,7 +49,7 @@ export class BTCPlugin extends Plugin {
50
49
  endBlock: processor.config.endBlock
51
50
  })
52
51
  for (const callHandler of processor.callHandlers) {
53
- const handlerId = handlers.txHandlers.push(callHandler.handler) - 1
52
+ const handlerId = this.handlerRegister.register(callHandler.handler, chainId)
54
53
  this.partitionManager.registerPartitionHandler(
55
54
  HandlerType.BTC_TRANSACTION,
56
55
  handlerId,
@@ -78,7 +77,7 @@ export class BTCPlugin extends Plugin {
78
77
  }
79
78
 
80
79
  for (const blockHandler of processor.blockHandlers) {
81
- const handlerId = handlers.blockHandlers.push(blockHandler.handler) - 1
80
+ const handlerId = this.handlerRegister.register(blockHandler.handler, chainId)
82
81
  this.partitionManager.registerPartitionHandler(HandlerType.BTC_BLOCK, handlerId, blockHandler.partitionHandler)
83
82
  contractConfig.intervalConfigs.push({
84
83
  slot: 0,
@@ -100,8 +99,6 @@ export class BTCPlugin extends Plugin {
100
99
  // Finish up a contract
101
100
  config.contractConfigs.push(contractConfig)
102
101
  }
103
-
104
- this.handlers = handlers
105
102
  }
106
103
 
107
104
  supportedHandlers = [HandlerType.BTC_TRANSACTION, HandlerType.BTC_BLOCK]
@@ -161,12 +158,14 @@ export class BTCPlugin extends Plugin {
161
158
  const result = binding.data?.btcTransaction
162
159
 
163
160
  for (const handlerId of binding.handlerIds) {
164
- const promise = this.handlers.txHandlers[handlerId](binding.data?.btcTransaction).catch((e) => {
165
- throw new ServerError(
166
- Status.INTERNAL,
167
- 'error processing transaction: ' + JSON.stringify(result) + '\n' + errorString(e)
168
- )
169
- })
161
+ const promise = this.handlerRegister
162
+ .getHandlerById(handlerId)(binding.data?.btcTransaction)
163
+ .catch((e: any) => {
164
+ throw new ServerError(
165
+ Status.INTERNAL,
166
+ 'error processing transaction: ' + JSON.stringify(result) + '\n' + errorString(e)
167
+ )
168
+ })
170
169
  if (GLOBAL_CONFIG.execution.sequential) {
171
170
  await promise
172
171
  }
@@ -184,12 +183,14 @@ export class BTCPlugin extends Plugin {
184
183
 
185
184
  const promises: Promise<ProcessResult>[] = []
186
185
  for (const handlerId of request.handlerIds) {
187
- const promise = this.handlers.blockHandlers[handlerId](block).catch((e) => {
188
- throw new ServerError(
189
- Status.INTERNAL,
190
- 'error processing block: ' + JSON.stringify(block) + '\n' + errorString(e)
191
- )
192
- })
186
+ const promise = this.handlerRegister
187
+ .getHandlerById(handlerId)(block)
188
+ .catch((e: any) => {
189
+ throw new ServerError(
190
+ Status.INTERNAL,
191
+ 'error processing block: ' + JSON.stringify(block) + '\n' + errorString(e)
192
+ )
193
+ })
193
194
  if (GLOBAL_CONFIG.execution.sequential) {
194
195
  await promise
195
196
  }
@@ -1,7 +1,7 @@
1
1
  import { Plugin, PluginManager } from '@sentio/runtime'
2
- import { ProcessConfigResponse } from '@sentio/protos'
2
+ import { InitResponse, ProcessConfigResponse } from '@sentio/protos'
3
3
 
4
- import { MetricState } from './meter.js'
4
+ import { MetricState, MetricStateNew } from './meter.js'
5
5
  import { ExporterState } from './exporter.js'
6
6
  import { TemplateInstanceState } from './template.js'
7
7
  import { EventLoggerState } from './event-logger.js'
@@ -19,7 +19,11 @@ export class CorePlugin extends Plugin {
19
19
  ...metric.config
20
20
  })
21
21
  }
22
-
22
+ for (const metric of MetricStateNew.INSTANCE.getValues()) {
23
+ config.metricConfigs.push({
24
+ ...metric.config
25
+ })
26
+ }
23
27
  for (const event of EventLoggerState.INSTANCE.getValues()) {
24
28
  config.eventLogConfigs.push({
25
29
  ...event.config
@@ -41,6 +45,16 @@ export class CorePlugin extends Plugin {
41
45
  }
42
46
  }
43
47
  }
48
+
49
+ async init(config: InitResponse): Promise<void> {
50
+ if (DatabaseSchemaState.INSTANCE.getValues().length > 0) {
51
+ config.dbSchema = {
52
+ gqlSchema: DatabaseSchemaState.INSTANCE.getValues()
53
+ .map((e) => e.source)
54
+ .join('\n\n')
55
+ }
56
+ }
57
+ }
44
58
  }
45
59
 
46
60
  PluginManager.INSTANCE.register(new CorePlugin())
@@ -0,0 +1,79 @@
1
+ import { ServerError, Status } from 'nice-grpc'
2
+ import { ChainId } from '@sentio/chain'
3
+ import { ProcessResult } from '@sentio/protos'
4
+
5
+ export type HandlerFunction = (...args: any[]) => Promise<ProcessResult>
6
+
7
+ interface HandlerEntry {
8
+ id: number
9
+ handler: HandlerFunction
10
+ chainId: ChainId | string
11
+ }
12
+
13
+ const chainToNumber: Map<string, number> = new Map()
14
+ const numberToChain: Map<number, string> = new Map()
15
+
16
+ for (const chainId of Object.values(ChainId)) {
17
+ const num = chainToNumber.size + 1
18
+ chainToNumber.set(chainId, num)
19
+ numberToChain.set(num, chainId)
20
+ }
21
+
22
+ const MAX_HANDLER_PER_CHAIN = 1000000 // Maximum handlers per chain
23
+
24
+ export class HandlerRegister {
25
+ private handlerByChain: Map<number, HandlerEntry[]> = new Map()
26
+ private handlers: Map<number, HandlerEntry> = new Map()
27
+
28
+ /**
29
+ * Register a handler function with chain ID and handle type
30
+ * @returns handler ID
31
+ */
32
+ register(handler: HandlerFunction, chainId: ChainId | string): number {
33
+ const chainNum = chainToNumber.get(chainId) || 0
34
+ const entries = this.handlerByChain.get(chainNum) || []
35
+
36
+ const len = entries.length
37
+ const id = chainNum * MAX_HANDLER_PER_CHAIN + len + 1 // Use the first 32 bits for chain ID, next bits for index
38
+
39
+ const entry: HandlerEntry = {
40
+ id,
41
+ handler,
42
+ chainId
43
+ }
44
+ entries.push(entry)
45
+ this.handlerByChain.set(chainNum, entries)
46
+ this.handlers.set(id, entry)
47
+
48
+ return id
49
+ }
50
+
51
+ /**
52
+ * Get handler function by ID
53
+ */
54
+ getHandlerById(id: number): HandlerFunction {
55
+ const entry = this.handlers.get(id)
56
+ if (!entry) {
57
+ throw new ServerError(Status.INTERNAL, `Handler with ID ${id} not found.`)
58
+ }
59
+ return entry.handler
60
+ }
61
+
62
+ clear(chainId?: ChainId): void {
63
+ if (chainId) {
64
+ const chainNum = chainToNumber.get(chainId)
65
+ if (chainNum !== undefined) {
66
+ const chainHandlers = this.handlerByChain.get(chainNum)
67
+ if (chainHandlers) {
68
+ for (const entry of chainHandlers) {
69
+ this.handlers.delete(entry.id)
70
+ }
71
+ }
72
+ this.handlerByChain.delete(chainNum)
73
+ }
74
+ } else {
75
+ this.handlerByChain.clear()
76
+ this.handlers.clear()
77
+ }
78
+ }
79
+ }
@@ -1,33 +1,37 @@
1
1
  import { errorString, GLOBAL_CONFIG, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime'
2
2
  import {
3
3
  ContractConfig,
4
- Data_CosmosCall,
5
4
  DataBinding,
6
5
  HandlerType,
6
+ InitResponse,
7
7
  ProcessConfigResponse,
8
8
  ProcessResult,
9
9
  StartRequest
10
10
  } from '@sentio/protos'
11
11
  import { ServerError, Status } from 'nice-grpc'
12
12
  import { TemplateInstanceState } from '../core/template.js'
13
+ import { HandlerRegister } from '../core/handler-register.js'
13
14
  import { CosmosProcessorState } from './types.js'
14
15
 
15
- interface Handlers {
16
- callHandlers: ((trace: Data_CosmosCall) => Promise<ProcessResult>)[]
17
- }
18
-
19
16
  export class CosmosPlugin extends Plugin {
20
17
  name: string = 'CosmosPlugin'
21
- handlers: Handlers = {
22
- callHandlers: []
23
- }
18
+ handlerRegister = new HandlerRegister()
24
19
 
25
- async configure(config: ProcessConfigResponse) {
26
- const handlers: Handlers = {
27
- callHandlers: []
20
+ async init(config: InitResponse) {
21
+ for (const aptosProcessor of CosmosProcessorState.INSTANCE.getValues()) {
22
+ const chainId = aptosProcessor.config.chainId
23
+ config.chainIds.push(chainId)
28
24
  }
25
+ }
26
+
27
+ async configure(config: ProcessConfigResponse, forChainId?: string) {
28
+ this.handlerRegister.clear(forChainId as any)
29
29
 
30
30
  for (const processor of CosmosProcessorState.INSTANCE.getValues()) {
31
+ const chainId = processor.config.chainId
32
+ if (forChainId !== undefined && forChainId !== chainId.toString()) {
33
+ continue
34
+ }
31
35
  const contractConfig = ContractConfig.fromPartial({
32
36
  processorType: USER_PROCESSOR,
33
37
  contract: {
@@ -41,7 +45,7 @@ export class CosmosPlugin extends Plugin {
41
45
  })
42
46
 
43
47
  for (const callHandler of processor.callHandlers) {
44
- const handlerId = handlers.callHandlers.push(callHandler.handler) - 1
48
+ const handlerId = this.handlerRegister.register(callHandler.handler, chainId)
45
49
 
46
50
  contractConfig.cosmosLogConfigs.push({
47
51
  handlerId,
@@ -53,8 +57,6 @@ export class CosmosPlugin extends Plugin {
53
57
  // Finish up a contract
54
58
  config.contractConfigs.push(contractConfig)
55
59
  }
56
-
57
- this.handlers = handlers
58
60
  }
59
61
 
60
62
  supportedHandlers = [HandlerType.COSMOS_CALL]
@@ -83,12 +85,14 @@ export class CosmosPlugin extends Plugin {
83
85
  const promises: Promise<ProcessResult>[] = []
84
86
 
85
87
  for (const handlerId of binding.handlerIds) {
86
- const promise = this.handlers.callHandlers[handlerId](call).catch((e) => {
87
- throw new ServerError(
88
- Status.INTERNAL,
89
- 'error processing transaction: ' + JSON.stringify(call.transaction) + '\n' + errorString(e)
90
- )
91
- })
88
+ const promise = this.handlerRegister
89
+ .getHandlerById(handlerId)(call)
90
+ .catch((e) => {
91
+ throw new ServerError(
92
+ Status.INTERNAL,
93
+ 'error processing transaction: ' + JSON.stringify(call.transaction) + '\n' + errorString(e)
94
+ )
95
+ })
92
96
  if (GLOBAL_CONFIG.execution.sequential) {
93
97
  await promise
94
98
  }