@sentio/sdk 1.25.3 → 1.26.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 (55) hide show
  1. package/lib/aptos/aptos-processor.d.ts +24 -3
  2. package/lib/aptos/aptos-processor.js +70 -20
  3. package/lib/aptos/aptos-processor.js.map +1 -1
  4. package/lib/aptos/context.d.ts +10 -3
  5. package/lib/aptos/context.js +34 -2
  6. package/lib/aptos/context.js.map +1 -1
  7. package/lib/aptos/index.d.ts +1 -1
  8. package/lib/aptos/index.js +2 -1
  9. package/lib/aptos/index.js.map +1 -1
  10. package/lib/aptos/network.d.ts +1 -2
  11. package/lib/aptos/network.js +1 -1
  12. package/lib/aptos/network.js.map +1 -1
  13. package/lib/core/context.d.ts +2 -5
  14. package/lib/core/context.js +8 -11
  15. package/lib/core/context.js.map +1 -1
  16. package/lib/core/event-tracker.js +1 -1
  17. package/lib/core/event-tracker.js.map +1 -1
  18. package/lib/core/exporter.d.ts +14 -0
  19. package/lib/core/exporter.js +27 -0
  20. package/lib/core/exporter.js.map +1 -0
  21. package/lib/core/logger.js +1 -1
  22. package/lib/core/logger.js.map +1 -1
  23. package/lib/core/meter.js +2 -2
  24. package/lib/core/meter.js.map +1 -1
  25. package/lib/gen/processor/protos/processor.d.ts +54 -9
  26. package/lib/gen/processor/protos/processor.js +317 -25
  27. package/lib/gen/processor/protos/processor.js.map +1 -1
  28. package/lib/processor-state.d.ts +4 -0
  29. package/lib/processor-state.js +2 -0
  30. package/lib/processor-state.js.map +1 -1
  31. package/lib/service.d.ts +4 -0
  32. package/lib/service.js +58 -17
  33. package/lib/service.js.map +1 -1
  34. package/lib/tests/aptos.test.js +25 -0
  35. package/lib/tests/aptos.test.js.map +1 -1
  36. package/lib/tests/erc20.js +7 -0
  37. package/lib/tests/erc20.js.map +1 -1
  38. package/lib/tests/souffl3.js +5 -1
  39. package/lib/tests/souffl3.js.map +1 -1
  40. package/package.json +1 -1
  41. package/src/aptos/aptos-processor.ts +98 -19
  42. package/src/aptos/context.ts +33 -3
  43. package/src/aptos/index.ts +1 -1
  44. package/src/aptos/network.ts +2 -4
  45. package/src/core/context.ts +10 -20
  46. package/src/core/event-tracker.ts +1 -1
  47. package/src/core/exporter.ts +33 -0
  48. package/src/core/logger.ts +1 -1
  49. package/src/core/meter.ts +2 -2
  50. package/src/gen/processor/protos/processor.ts +403 -38
  51. package/src/processor-state.ts +5 -0
  52. package/src/service.ts +65 -19
  53. package/src/tests/aptos.test.ts +28 -0
  54. package/src/tests/erc20.ts +7 -0
  55. package/src/tests/souffl3.ts +6 -1
package/src/service.ts CHANGED
@@ -3,12 +3,14 @@ import { CallContext, ServerError, Status } from 'nice-grpc'
3
3
  import { SOL_MAINMET_ID, SUI_DEVNET_ID } from './utils/chain'
4
4
 
5
5
  import {
6
+ AccountConfig,
6
7
  AptosCallHandlerConfig,
7
8
  AptosEventHandlerConfig,
8
9
  BlockBinding,
9
10
  ContractConfig,
10
11
  DataBinding,
11
12
  EventTrackingConfig,
13
+ ExportConfig,
12
14
  HandlerType,
13
15
  LogFilter,
14
16
  LogHandlerConfig,
@@ -31,6 +33,7 @@ import Long from 'long'
31
33
  import { TextDecoder } from 'util'
32
34
  import { Trace } from './core'
33
35
  import { Instruction } from '@project-serum/anchor'
36
+ import { MoveResourcesWithVersionPayload } from './aptos/aptos-processor'
34
37
 
35
38
  const DEFAULT_MAX_BLOCK = Long.ZERO
36
39
 
@@ -42,6 +45,8 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
42
45
  private blockHandlers: ((block: Block) => Promise<ProcessResult>)[] = []
43
46
  private aptosEventHandlers: ((event: any) => Promise<ProcessResult>)[] = []
44
47
  private aptosCallHandlers: ((func: any) => Promise<ProcessResult>)[] = []
48
+ private aptosResourceHandlers: ((resourceWithVersion: MoveResourcesWithVersionPayload) => Promise<ProcessResult>)[] =
49
+ []
45
50
 
46
51
  // map from chain id to list of processors
47
52
  // private blockHandlers = new Map<string, ((block: Block) => Promise<ProcessResult>)[]>()
@@ -49,9 +54,11 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
49
54
 
50
55
  private started = false
51
56
  private contractConfigs: ContractConfig[]
57
+ private accountConfigs: AccountConfig[]
52
58
  private templateInstances: TemplateInstance[]
53
59
  private metricConfigs: MetricConfig[]
54
60
  private eventTrackingConfigs: EventTrackingConfig[]
61
+ private exportConfigs: ExportConfig[]
55
62
  private readonly loader: () => void
56
63
 
57
64
  private readonly shutdownHandler?: () => void
@@ -69,10 +76,11 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
69
76
  // TODO project setting
70
77
  config: undefined,
71
78
  contractConfigs: this.contractConfigs,
79
+ accountConfigs: this.accountConfigs,
72
80
  templateInstances: this.templateInstances,
73
81
  eventTrackingConfigs: this.eventTrackingConfigs,
74
82
  metricConfigs: this.metricConfigs,
75
- accountConfigs: [],
83
+ exportConfigs: this.exportConfigs,
76
84
  }
77
85
  }
78
86
 
@@ -81,10 +89,12 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
81
89
  this.templateInstances = []
82
90
  // this.processorsByChainId.clear()
83
91
  this.contractConfigs = []
92
+ this.accountConfigs = []
84
93
 
85
94
  this.templateInstances = [...global.PROCESSOR_STATE.templatesInstances]
86
95
  this.eventTrackingConfigs = []
87
96
  this.metricConfigs = []
97
+ this.exportConfigs = []
88
98
 
89
99
  // part 0, prepare metrics and event tracking configs
90
100
  for (const metric of global.PROCESSOR_STATE.metrics) {
@@ -104,6 +114,14 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
104
114
  })
105
115
  }
106
116
 
117
+ for (const exporter of global.PROCESSOR_STATE.exporters) {
118
+ this.exportConfigs.push({
119
+ exportName: exporter.exportName,
120
+ exportType: exporter.options.exportType,
121
+ exportUrl: exporter.options.exportUrl,
122
+ })
123
+ }
124
+
107
125
  // Part 1, prepare EVM processors
108
126
  for (const processor of global.PROCESSOR_STATE.processors) {
109
127
  // If server favor incremental update this need to change
@@ -247,7 +265,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
247
265
  blockConfigs: [],
248
266
  logConfigs: [],
249
267
  traceConfigs: [],
250
- startBlock: aptosProcessor.config.startVersion,
268
+ startBlock: Long.fromString(aptosProcessor.config.startVersion.toString()),
251
269
  endBlock: DEFAULT_MAX_BLOCK,
252
270
  instructionConfig: undefined,
253
271
  aptosEventConfigs: [],
@@ -286,6 +304,28 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
286
304
  }
287
305
  this.contractConfigs.push(contractConfig)
288
306
  }
307
+
308
+ for (const aptosProcessor of global.PROCESSOR_STATE.aptosAccountProcessors) {
309
+ const accountConfig: AccountConfig = {
310
+ address: aptosProcessor.config.address,
311
+ chainId: aptosProcessor.getChainId(),
312
+ startBlock: Long.fromValue(aptosProcessor.config.startVersion.toString()),
313
+ onAptosIntervalConfigs: [],
314
+ onIntervalConfigs: [],
315
+ }
316
+ for (const handler of aptosProcessor.resourcesHandlers) {
317
+ const handlerId = this.aptosResourceHandlers.push(handler.handler) - 1
318
+ accountConfig.onAptosIntervalConfigs.push({
319
+ intervalConfig: {
320
+ handlerId: handlerId,
321
+ minutes: handler.timeIntervalInMinutes || 0,
322
+ slot: handler.versionInterval || 0,
323
+ },
324
+ type: handler.type || '',
325
+ })
326
+ }
327
+ this.accountConfigs.push(accountConfig)
328
+ }
289
329
  }
290
330
 
291
331
  async start(request: StartRequest, context: CallContext): Promise<Empty> {
@@ -361,6 +401,8 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
361
401
  return this.processAptosFunctionCall(request)
362
402
  case HandlerType.APT_EVENT:
363
403
  return this.processAptosEvent(request)
404
+ case HandlerType.APT_RESOURCE:
405
+ return this.processAptosResource(request)
364
406
  default:
365
407
  throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
366
408
  }
@@ -613,6 +655,19 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
613
655
  return result
614
656
  }
615
657
 
658
+ async processAptosResource(binding: DataBinding): Promise<ProcessResult> {
659
+ if (!binding.data) {
660
+ throw new ServerError(Status.INVALID_ARGUMENT, "Event can't be empty")
661
+ }
662
+ const jsonString = Utf8ArrayToStr(binding.data.raw)
663
+ const json = JSON.parse(jsonString) as MoveResourcesWithVersionPayload
664
+ const result = await this.aptosResourceHandlers[binding.handlerId](json).catch((e) => {
665
+ throw new ServerError(Status.INTERNAL, 'error processing event: ' + jsonString + '\n' + errorString(e))
666
+ })
667
+ recordRuntimeInfo(result, HandlerType.APT_RESOURCE)
668
+ return result
669
+ }
670
+
616
671
  async processAptosFunctionCall(binding: DataBinding): Promise<ProcessResult> {
617
672
  if (!binding.data) {
618
673
  throw new ServerError(Status.INVALID_ARGUMENT, "Event can't be empty")
@@ -677,28 +732,19 @@ function mergeProcessResults(results: ProcessResult[]): ProcessResult {
677
732
  res.gauges = res.gauges.concat(r.gauges)
678
733
  res.logs = res.logs.concat(r.logs)
679
734
  res.events = res.events.concat(r.events)
735
+ res.exports = res.exports.concat(r.exports)
680
736
  }
681
737
  return res
682
738
  }
683
739
 
684
740
  function recordRuntimeInfo(results: ProcessResult, handlerType: HandlerType) {
685
- results.gauges.forEach((e) => {
686
- e.runtimeInfo = {
687
- from: handlerType,
688
- }
689
- })
690
-
691
- results.counters.forEach((e) => {
692
- e.runtimeInfo = {
693
- from: handlerType,
694
- }
695
- })
696
-
697
- results.logs.forEach((e) => {
698
- e.runtimeInfo = {
699
- from: handlerType,
700
- }
701
- })
741
+ for (const list of [results.gauges, results.counters, results.logs, results.events, results.exports]) {
742
+ list.forEach((e) => {
743
+ e.runtimeInfo = {
744
+ from: handlerType,
745
+ }
746
+ })
747
+ }
702
748
  }
703
749
 
704
750
  function errorString(e: Error): string {
@@ -115,6 +115,34 @@ describe('Test Aptos Example', () => {
115
115
  const res = await service.processBindings(request)
116
116
  expect(firstGaugeValue(res.result, 'size')).equal(2n)
117
117
  })
118
+
119
+ test('check on timer', async () => {
120
+ const request: ProcessBindingsRequest = {
121
+ bindings: [
122
+ {
123
+ data: {
124
+ raw: new TextEncoder().encode(
125
+ JSON.stringify({
126
+ version: '12345',
127
+ resources: [
128
+ {
129
+ type: '0x1::coin::SupplyConfig',
130
+ data: {
131
+ allow_upgrades: false,
132
+ },
133
+ },
134
+ ],
135
+ })
136
+ ),
137
+ },
138
+ handlerId: 0,
139
+ handlerType: HandlerType.APT_RESOURCE,
140
+ },
141
+ ],
142
+ }
143
+ const res = await service.processBindings(request)
144
+ expect(firstCounterValue(res.result, 'onTimer')).equal(1n)
145
+ })
118
146
  })
119
147
 
120
148
  const testData = {
@@ -1,5 +1,7 @@
1
1
  import { ERC20Processor, ERC20ProcessorTemplate } from '../builtin/erc20'
2
2
  import { EventTracker } from '../core/event-tracker'
3
+ import { Exporter } from '../core/exporter'
4
+ import { ExportConfig_ExportType } from '@sentio/sdk'
3
5
 
4
6
  export const filter = ERC20Processor.filters.Transfer(
5
7
  '0x0000000000000000000000000000000000000000',
@@ -7,6 +9,10 @@ export const filter = ERC20Processor.filters.Transfer(
7
9
  )
8
10
 
9
11
  const tracker = EventTracker.register('sdf')
12
+ const exporter = Exporter.register('transfer', {
13
+ exportType: ExportConfig_ExportType.WEBHOOK,
14
+ exportUrl: 'http://localhost',
15
+ })
10
16
 
11
17
  const processorTemplate = new ERC20ProcessorTemplate().onEventTransfer(async function (event, ctx) {
12
18
  console.log('')
@@ -34,6 +40,7 @@ ERC20Processor.bind({ address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', net
34
40
  .onEventTransfer(async function (event, ctx) {
35
41
  ctx.meter.Counter('c2').add(2)
36
42
  tracker.trackEvent(ctx, { distinctId: event.args.from })
43
+ exporter.emit(ctx, event)
37
44
  }, filter)
38
45
  .onBlock(async function (block, ctx) {
39
46
  ctx.meter.Gauge('g2').record(20, { k: 'v' })
@@ -3,10 +3,11 @@ import { token } from '../builtin/aptos/0x3'
3
3
  import { voting } from '../builtin/aptos/0x1'
4
4
  import { TYPE_REGISTRY } from '../aptos/types'
5
5
  import { AccountEventTracker } from '@sentio/sdk'
6
+ import { AptosAccountProcessor } from '../aptos/aptos-processor'
6
7
 
7
8
  const accountTracker = AccountEventTracker.register('pull')
8
9
 
9
- SouffleChefCampaign.bind({ startVersion: 3212312 })
10
+ SouffleChefCampaign.bind({ startVersion: 3212312n })
10
11
  .onEntryPullTokenV2((call: SouffleChefCampaign.PullTokenV2Payload, ctx) => {
11
12
  ctx.meter.Counter('call_num').add(1)
12
13
  ctx.meter.Counter('pulled').add(call.arguments_typed[3])
@@ -45,3 +46,7 @@ voting.bind().onEventCreateProposalEvent((evt, ctx) => {
45
46
  evt.data_typed.expiration_secs + evt.data_typed.expiration_secs
46
47
  ctx.meter.Gauge('size').record(evt.data_typed.metadata.data.length)
47
48
  })
49
+
50
+ AptosAccountProcessor.bind({ address: '0x1' }).onTimeInterval((resources, ctx) => {
51
+ ctx.meter.Counter('onTimer').add(1)
52
+ }, 10000)