@sentio/sdk 1.30.1 → 1.30.3

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 (46) hide show
  1. package/lib/aptos/aptos-processor.d.ts +9 -2
  2. package/lib/aptos/aptos-processor.js +12 -3
  3. package/lib/aptos/aptos-processor.js.map +1 -1
  4. package/lib/core/event-tracker.d.ts +5 -1
  5. package/lib/core/event-tracker.js +8 -5
  6. package/lib/core/event-tracker.js.map +1 -1
  7. package/lib/core/exporter.d.ts +4 -0
  8. package/lib/core/exporter.js +7 -3
  9. package/lib/core/exporter.js.map +1 -1
  10. package/lib/core/meter.d.ts +14 -1
  11. package/lib/core/meter.js +39 -10
  12. package/lib/core/meter.js.map +1 -1
  13. package/lib/index.d.ts +0 -2
  14. package/lib/index.js +3 -5
  15. package/lib/index.js.map +1 -1
  16. package/lib/processor-runner.js +1 -1
  17. package/lib/processor-runner.js.map +1 -1
  18. package/lib/service.js +9 -5
  19. package/lib/service.js.map +1 -1
  20. package/lib/{processor-state.d.ts → state/processor-state.d.ts} +3 -10
  21. package/lib/{processor-state.js → state/processor-state.js} +2 -5
  22. package/lib/state/processor-state.js.map +1 -0
  23. package/lib/state/state-storage.d.ts +17 -0
  24. package/lib/state/state-storage.js +60 -0
  25. package/lib/state/state-storage.js.map +1 -0
  26. package/lib/state/state-storage.test.d.ts +1 -0
  27. package/lib/state/state-storage.test.js.map +1 -0
  28. package/lib/testing/test-processor-server.js +1 -1
  29. package/lib/testing/test-processor-server.js.map +1 -1
  30. package/lib/utils/dex-price.test.js.map +1 -1
  31. package/lib/utils/erc20.test.js.map +1 -1
  32. package/lib/utils/price.js +11 -4
  33. package/lib/utils/price.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/aptos/aptos-processor.ts +15 -6
  36. package/src/core/event-tracker.ts +8 -5
  37. package/src/core/exporter.ts +6 -2
  38. package/src/core/meter.ts +41 -9
  39. package/src/index.ts +2 -2
  40. package/src/processor-runner.ts +1 -1
  41. package/src/service.ts +13 -6
  42. package/src/{processor-state.ts → state/processor-state.ts} +4 -13
  43. package/src/state/state-storage.ts +65 -0
  44. package/src/testing/test-processor-server.ts +1 -1
  45. package/src/utils/price.ts +12 -4
  46. package/lib/processor-state.js.map +0 -1
@@ -1,14 +1,15 @@
1
- import { ProcessResult } from '../gen'
2
-
3
- import { TYPE_REGISTRY, TypeRegistry } from './type-registry'
4
- import { AptosBindOptions, AptosNetwork, getChainId } from './network'
5
1
  import {
6
2
  MoveResource,
7
3
  Transaction_UserTransaction,
8
4
  TransactionPayload_EntryFunctionPayload,
9
5
  } from 'aptos-sdk/src/generated'
6
+
7
+ import { TYPE_REGISTRY, TypeRegistry } from './type-registry'
8
+ import { AptosBindOptions, AptosNetwork, getChainId } from './network'
10
9
  import { AptosContext, AptosResourceContext } from './context'
11
10
  import { EventInstance } from './models'
11
+ import { ListStateStorage } from '../state/state-storage'
12
+ import { ProcessResult } from '../gen'
12
13
 
13
14
  type IndexConfigure = {
14
15
  address: string
@@ -60,6 +61,10 @@ class ResourceHandlder {
60
61
  handler: (resource: MoveResourcesWithVersionPayload) => Promise<ProcessResult>
61
62
  }
62
63
 
64
+ export class AptosProcessorState extends ListStateStorage<AptosBaseProcessor> {
65
+ static INSTANCE = new AptosProcessorState()
66
+ }
67
+
63
68
  export class AptosBaseProcessor {
64
69
  readonly moduleName: string
65
70
  config: IndexConfigure
@@ -69,7 +74,7 @@ export class AptosBaseProcessor {
69
74
  constructor(moduleName: string, options: AptosBindOptions) {
70
75
  this.moduleName = moduleName
71
76
  this.config = configure(options)
72
- global.PROCESSOR_STATE.aptosProcessors.push(this)
77
+ AptosProcessorState.INSTANCE.addValue(this)
73
78
  this.loadTypes(TYPE_REGISTRY)
74
79
  }
75
80
 
@@ -202,6 +207,10 @@ export class AptosBaseProcessor {
202
207
  }
203
208
  }
204
209
 
210
+ export class AptosAccountProcessorState extends ListStateStorage<AptosAccountProcessor> {
211
+ static INSTANCE = new AptosAccountProcessorState()
212
+ }
213
+
205
214
  export class AptosAccountProcessor {
206
215
  config: IndexConfigure
207
216
 
@@ -213,7 +222,7 @@ export class AptosAccountProcessor {
213
222
 
214
223
  protected constructor(options: AptosBindOptions) {
215
224
  this.config = configure(options)
216
- global.PROCESSOR_STATE.aptosAccountProcessors.push(this)
225
+ AptosAccountProcessorState.INSTANCE.addValue(this)
217
226
  }
218
227
 
219
228
  getChainId(): string {
@@ -1,6 +1,7 @@
1
1
  import { BaseContext } from './base-context'
2
- import { DataDescriptor, EventTrackingResult } from '../gen'
2
+ import { EventTrackingResult } from '../gen'
3
3
  import { NamedResultDescriptor } from './metadata'
4
+ import { MapStateStorage } from '../state/state-storage'
4
5
 
5
6
  export interface Event {
6
7
  // The unique identifier of main identity associate with an event
@@ -16,6 +17,10 @@ export interface TrackerOptions {
16
17
  distinctByDays?: number[]
17
18
  }
18
19
 
20
+ export class EventTrackerState extends MapStateStorage<EventTracker> {
21
+ static INSTANCE = new EventTrackerState()
22
+ }
23
+
19
24
  // Track Event with an identity associate with it
20
25
  export class EventTracker extends NamedResultDescriptor {
21
26
  static DEFAULT_OPTIONS: TrackerOptions = {
@@ -25,8 +30,7 @@ export class EventTracker extends NamedResultDescriptor {
25
30
 
26
31
  static register(eventName: string, options?: TrackerOptions) {
27
32
  const tracker = new EventTracker(eventName, { ...EventTracker.DEFAULT_OPTIONS, ...options })
28
- global.PROCESSOR_STATE.eventTrackers.push(tracker)
29
- return tracker
33
+ return EventTrackerState.INSTANCE.getOrSetValue(eventName, tracker)
30
34
  }
31
35
 
32
36
  options: TrackerOptions
@@ -60,7 +64,6 @@ export class AccountEventTracker extends EventTracker {
60
64
  eventName = 'user'
61
65
  }
62
66
  const tracker = new AccountEventTracker(eventName, { ...AccountEventTracker.DEFAULT_OPTIONS, ...options })
63
- global.PROCESSOR_STATE.eventTrackers.push(tracker)
64
- return tracker
67
+ return EventTrackerState.INSTANCE.getOrSetValue(eventName, tracker)
65
68
  }
66
69
  }
@@ -1,14 +1,18 @@
1
1
  import { BaseContext } from './base-context'
2
2
  import { ExportResult } from '@sentio/sdk'
3
3
  import { NamedResultDescriptor } from './metadata'
4
+ import { MapStateStorage } from '../state/state-storage'
4
5
 
5
6
  export type Export = Record<string, any>
6
7
 
8
+ export class ExporterState extends MapStateStorage<Exporter> {
9
+ static INSTANCE = new ExporterState()
10
+ }
11
+
7
12
  export class Exporter extends NamedResultDescriptor {
8
13
  static register(name: string, channel: string) {
9
14
  const exporter = new Exporter(name, channel)
10
- global.PROCESSOR_STATE.exporters.push(exporter)
11
- return exporter
15
+ return ExporterState.INSTANCE.getOrSetValue(name, exporter)
12
16
  }
13
17
 
14
18
  channel: string
package/src/core/meter.ts CHANGED
@@ -2,6 +2,7 @@ import { BaseContext } from './base-context'
2
2
  import { toMetricValue, Numberish } from './numberish'
3
3
  import { Labels, NamedResultDescriptor } from './metadata'
4
4
  import { AggregationConfig, MetricConfig } from '../gen'
5
+ import { MapStateStorage } from '../state/state-storage'
5
6
 
6
7
  export function normalizeName(name: string): string {
7
8
  const regex = new RegExp('![_.a-zA-Z0-9]')
@@ -40,20 +41,50 @@ export class CounterOptions {
40
41
  sparse?: boolean
41
42
  }
42
43
 
44
+ enum MetricType {
45
+ Counter = 0,
46
+ Gauge = 1,
47
+ }
48
+
43
49
  export class Metric extends NamedResultDescriptor {
50
+ type: MetricType
44
51
  descriptor: MetricConfig
45
- constructor(name: string, option?: MetricOptions) {
52
+ constructor(type: MetricType, name: string, option?: MetricOptions) {
46
53
  super(name)
54
+ this.type = type
47
55
  this.descriptor = MetricConfig.fromPartial({ name: this.name, ...option })
48
56
  }
49
57
  }
50
58
 
59
+ export class MetricState extends MapStateStorage<Metric> {
60
+ static INSTANCE = new MetricState()
61
+
62
+ getOrRegisterMetric(type: MetricType, name: string, option?: CounterOptions | MetricOptions): Metric {
63
+ const metricMap = this.getOrRegister()
64
+ let metric = metricMap.get(name)
65
+ if (metric && metric.type !== type) {
66
+ throw Error(`redefine ${name} of metric type ${type} that is previously ${metric.type}`)
67
+ }
68
+
69
+ if (!metric) {
70
+ if (type === MetricType.Counter) {
71
+ metric = new Counter(name, option)
72
+ } else {
73
+ metric = new Gauge(name, option)
74
+ }
75
+ }
76
+ metricMap.set(name, metric)
77
+ return metric
78
+ }
79
+ }
80
+
51
81
  export class Counter extends Metric {
52
82
  static register(name: string, option?: CounterOptions): Counter {
53
- // TODO also dedup
54
- const metric = new Counter(name, option)
55
- global.PROCESSOR_STATE.metrics.push(metric)
56
- return metric
83
+ return MetricState.INSTANCE.getOrRegisterMetric(MetricType.Counter, name, option) as Counter
84
+ }
85
+
86
+ constructor(name: string, option?: MetricOptions) {
87
+ super(MetricType.Counter, name, option)
57
88
  }
58
89
 
59
90
  add(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
@@ -94,10 +125,11 @@ export class CounterBinding {
94
125
 
95
126
  export class Gauge extends Metric {
96
127
  static register(name: string, option?: MetricOptions): Gauge {
97
- // TODO also dedup
98
- const metric = new Gauge(name, option)
99
- global.PROCESSOR_STATE.metrics.push(metric)
100
- return metric
128
+ return MetricState.INSTANCE.getOrRegisterMetric(MetricType.Gauge, name, option) as Gauge
129
+ }
130
+
131
+ constructor(name: string, option?: MetricOptions) {
132
+ super(MetricType.Counter, name, option)
101
133
  }
102
134
 
103
135
  record(ctx: BaseContext, value: Numberish, labels: Labels = {}) {
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { ProcessorServiceImpl } from './service'
1
+ // export { ProcessorServiceImpl } from './service'
2
2
  export { getProvider, setProvider, DummyProvider } from './provider'
3
3
  export { transformEtherError } from './error'
4
- export { ProcessorState } from './processor-state'
4
+ // export { ProcessorState } from './state/processor-state'
5
5
  export { EthersError } from './error'
6
6
 
7
7
  export { getProcessor, addProcessor, getContractByABI, addContractByABI } from './binds'
@@ -8,7 +8,7 @@ import { setProvider } from './provider'
8
8
 
9
9
  import path from 'path'
10
10
  import fs from 'fs-extra'
11
- import { ProcessorState } from './processor-state'
11
+ import { ProcessorState } from './state/processor-state'
12
12
  import { load } from './loader'
13
13
  import { CompressionAlgorithms } from '@grpc/grpc-js/build/src/compression-algorithms'
14
14
  import { Endpoints } from './endpoints'
package/src/service.ts CHANGED
@@ -33,7 +33,14 @@ import Long from 'long'
33
33
  import { TextDecoder } from 'util'
34
34
  import { Trace } from './core'
35
35
  import { Instruction } from '@project-serum/anchor'
36
- import { MoveResourcesWithVersionPayload } from './aptos/aptos-processor'
36
+ import { MetricState } from './core/meter'
37
+ import { ExporterState } from './core/exporter'
38
+ import { EventTrackerState } from './core/event-tracker'
39
+ import {
40
+ AptosAccountProcessorState,
41
+ AptosProcessorState,
42
+ MoveResourcesWithVersionPayload,
43
+ } from './aptos/aptos-processor'
37
44
  ;(BigInt.prototype as any).toJSON = function () {
38
45
  return this.toString()
39
46
  }
@@ -100,13 +107,13 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
100
107
  this.exportConfigs = []
101
108
 
102
109
  // part 0, prepare metrics and event tracking configs
103
- for (const metric of global.PROCESSOR_STATE.metrics) {
110
+ for (const metric of MetricState.INSTANCE.getValues()) {
104
111
  this.metricConfigs.push({
105
112
  ...metric.descriptor,
106
113
  })
107
114
  }
108
115
 
109
- for (const eventTracker of global.PROCESSOR_STATE.eventTrackers) {
116
+ for (const eventTracker of EventTrackerState.INSTANCE.getValues()) {
110
117
  this.eventTrackingConfigs.push({
111
118
  distinctAggregationByDays: eventTracker.options.distinctByDays || [],
112
119
  eventName: eventTracker.name,
@@ -117,7 +124,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
117
124
  })
118
125
  }
119
126
 
120
- for (const exporter of global.PROCESSOR_STATE.exporters) {
127
+ for (const exporter of ExporterState.INSTANCE.getValues()) {
121
128
  this.exportConfigs.push({
122
129
  name: exporter.name,
123
130
  channel: exporter.channel,
@@ -260,7 +267,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
260
267
  }
261
268
 
262
269
  // Part 4, prepare aptos constractors
263
- for (const aptosProcessor of global.PROCESSOR_STATE.aptosProcessors) {
270
+ for (const aptosProcessor of AptosProcessorState.INSTANCE.getValues()) {
264
271
  const contractConfig: ContractConfig = {
265
272
  processorType: USER_PROCESSOR,
266
273
  contract: {
@@ -313,7 +320,7 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
313
320
  this.contractConfigs.push(contractConfig)
314
321
  }
315
322
 
316
- for (const aptosProcessor of global.PROCESSOR_STATE.aptosAccountProcessors) {
323
+ for (const aptosProcessor of AptosAccountProcessorState.INSTANCE.getValues()) {
317
324
  const accountConfig: AccountConfig = {
318
325
  address: aptosProcessor.config.address,
319
326
  chainId: aptosProcessor.getChainId(),
@@ -5,14 +5,10 @@ import {
5
5
  BaseProcessorTemplate,
6
6
  SolanaBaseProcessor,
7
7
  SuiBaseProcessor,
8
- EventTracker,
9
- } from './core'
8
+ } from '../core'
10
9
 
11
10
  import { BaseContract } from 'ethers'
12
- import { TemplateInstance } from './gen'
13
- import { Metric } from './core/meter'
14
- import { Exporter } from './core/exporter'
15
- import { AptosBaseProcessor, AptosAccountProcessor } from './aptos/aptos-processor'
11
+ import { TemplateInstance } from '../gen'
16
12
 
17
13
  export class ProcessorState {
18
14
  // from abiName_address_chainId => contract wrapper
@@ -30,12 +26,7 @@ export class ProcessorState {
30
26
 
31
27
  suiProcessors: SuiBaseProcessor[] = []
32
28
 
33
- aptosProcessors: AptosBaseProcessor[] = []
34
- aptosAccountProcessors: AptosAccountProcessor[] = []
29
+ // TODO move above to state map
35
30
 
36
- eventTrackers: EventTracker[] = []
37
-
38
- exporters: Exporter[] = []
39
-
40
- metrics: Metric[] = []
31
+ stateMap = new Map<string, any>()
41
32
  }
@@ -0,0 +1,65 @@
1
+ export abstract class StateStorage<T> {
2
+ // TODO learn how to define single instance for all subclasses
3
+
4
+ protected constructor() {
5
+ //
6
+ }
7
+
8
+ abstract initValue(): T
9
+
10
+ key(): string {
11
+ return this.constructor.name
12
+ }
13
+
14
+ getOrRegister(): T {
15
+ let metricState: T = global.PROCESSOR_STATE.stateMap.get(this.key())
16
+ if (!metricState) {
17
+ metricState = this.initValue()
18
+ global.PROCESSOR_STATE.stateMap.set(this.key(), metricState)
19
+ }
20
+ return metricState
21
+ }
22
+ }
23
+
24
+ export abstract class MapStateStorage<T> extends StateStorage<Map<string, T>> {
25
+ initValue() {
26
+ return new Map<string, T>()
27
+ }
28
+
29
+ getValue(key: string): T | undefined {
30
+ const m = this.getOrRegister()
31
+ return m.get(key)
32
+ }
33
+
34
+ getValues(): T[] {
35
+ const m = this.getOrRegister()
36
+ return Array.from(m.values())
37
+ }
38
+
39
+ getOrSetValue(key: string, value: T): T {
40
+ const m = this.getOrRegister()
41
+ const oldValue = m.get(key)
42
+ if (oldValue) {
43
+ console.warn(key, 'has been registered twice, use the previous one')
44
+ return oldValue
45
+ }
46
+ m.set(key, value)
47
+ return value
48
+ }
49
+ }
50
+
51
+ export abstract class ListStateStorage<T> extends StateStorage<T[]> {
52
+ initValue() {
53
+ return []
54
+ }
55
+
56
+ getValues(): T[] {
57
+ return this.getOrRegister()
58
+ }
59
+
60
+ addValue(value: T): T {
61
+ const m = this.getOrRegister()
62
+ m.push(value)
63
+ return value
64
+ }
65
+ }
@@ -21,7 +21,7 @@ import { Block, Log } from '@ethersproject/abstract-provider'
21
21
  import Long from 'long'
22
22
  import { getNetwork, Networkish } from '@ethersproject/providers'
23
23
  import { Endpoints } from '../endpoints'
24
- import { ProcessorState } from '../processor-state'
24
+ import { ProcessorState } from '../state/processor-state'
25
25
  import { ProcessorServiceImpl } from '../service'
26
26
  import { Trace } from '../core/trace'
27
27
  import { setProvider } from '../provider'
@@ -25,7 +25,7 @@ export async function getPriceByType(chainId: string, coinType: string, date: Da
25
25
  priceClient = getPriceClient()
26
26
  }
27
27
 
28
- const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')
28
+ const dateStr = dateString(date)
29
29
  const key = `${coinType}-${dateStr}`
30
30
  let price = priceMap.get(key)
31
31
  if (price) {
@@ -48,7 +48,9 @@ export async function getPriceByType(chainId: string, coinType: string, date: Da
48
48
  }
49
49
  )
50
50
  price = response.price
51
- priceMap.set(key, price)
51
+ if (response.timestamp && dateString(response.timestamp) === dateStr) {
52
+ priceMap.set(key, price)
53
+ }
52
54
  return price
53
55
  }
54
56
 
@@ -62,7 +64,7 @@ export async function getPriceBySymbol(symbol: string, date: Date): Promise<numb
62
64
  priceClient = getPriceClient()
63
65
  }
64
66
 
65
- const dateStr = [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')
67
+ const dateStr = dateString(date)
66
68
  const key = `${symbol}-${dateStr}`
67
69
  let price = priceMap.get(key)
68
70
  if (price) {
@@ -82,6 +84,12 @@ export async function getPriceBySymbol(symbol: string, date: Date): Promise<numb
82
84
  }
83
85
  )
84
86
  price = response.price
85
- priceMap.set(key, price)
87
+ if (response.timestamp && dateString(response.timestamp) === dateStr) {
88
+ priceMap.set(key, price)
89
+ }
86
90
  return price
87
91
  }
92
+
93
+ function dateString(date: Date) {
94
+ return [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')
95
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"processor-state.js","sourceRoot":"","sources":["../src/processor-state.ts"],"names":[],"mappings":";;;AAgBA,MAAa,cAAc;IACzB,mDAAmD;IACnD,SAAS,GAAG,IAAI,GAAG,EAAsC,CAAA;IACzD,qBAAqB;IACrB,UAAU,GAAwE,EAAE,CAAA;IACpF,oCAAoC;IACpC,YAAY,GAAG,IAAI,GAAG,EAAmC,CAAA;IACzD,0BAA0B;IAC1B,SAAS,GAAgF,EAAE,CAAA;IAC3F,wCAAwC;IACxC,kBAAkB,GAAuB,EAAE,CAAA;IAE3C,gBAAgB,GAA0B,EAAE,CAAA;IAE5C,aAAa,GAAuB,EAAE,CAAA;IAEtC,eAAe,GAAyB,EAAE,CAAA;IAC1C,sBAAsB,GAA4B,EAAE,CAAA;IAEpD,aAAa,GAAmB,EAAE,CAAA;IAElC,SAAS,GAAe,EAAE,CAAA;IAE1B,OAAO,GAAa,EAAE,CAAA;CACvB;AAxBD,wCAwBC","sourcesContent":["import {\n BaseProcessor,\n BoundContractView,\n ContractView,\n BaseProcessorTemplate,\n SolanaBaseProcessor,\n SuiBaseProcessor,\n EventTracker,\n} from './core'\n\nimport { BaseContract } from 'ethers'\nimport { TemplateInstance } from './gen'\nimport { Metric } from './core/meter'\nimport { Exporter } from './core/exporter'\nimport { AptosBaseProcessor, AptosAccountProcessor } from './aptos/aptos-processor'\n\nexport class ProcessorState {\n // from abiName_address_chainId => contract wrapper\n contracts = new Map<string, ContractView<BaseContract>>()\n // all evm processors\n processors: BaseProcessor<BaseContract, BoundContractView<BaseContract, any>>[] = []\n // from abiName_options to contracts\n processorMap = new Map<string, BaseProcessor<any, any>>()\n // evm processor templates\n templates: BaseProcessorTemplate<BaseContract, BoundContractView<BaseContract, any>>[] = []\n // evm processor template instances spec\n templatesInstances: TemplateInstance[] = []\n\n solanaProcessors: SolanaBaseProcessor[] = []\n\n suiProcessors: SuiBaseProcessor[] = []\n\n aptosProcessors: AptosBaseProcessor[] = []\n aptosAccountProcessors: AptosAccountProcessor[] = []\n\n eventTrackers: EventTracker[] = []\n\n exporters: Exporter[] = []\n\n metrics: Metric[] = []\n}\n"]}