@sentio/sdk 2.15.7-rc.2 → 2.16.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 (35) hide show
  1. package/lib/aptos/aptos-plugin.js +3 -14
  2. package/lib/aptos/aptos-plugin.js.map +1 -1
  3. package/lib/core/template.d.ts +5 -0
  4. package/lib/core/template.js +6 -0
  5. package/lib/core/template.js.map +1 -0
  6. package/lib/eth/base-processor-template.d.ts +1 -4
  7. package/lib/eth/base-processor-template.js +2 -7
  8. package/lib/eth/base-processor-template.js.map +1 -1
  9. package/lib/eth/eth-plugin.js +6 -1
  10. package/lib/eth/eth-plugin.js.map +1 -1
  11. package/lib/sui/index.d.ts +3 -1
  12. package/lib/sui/index.js +5 -1
  13. package/lib/sui/index.js.map +1 -1
  14. package/lib/sui/sui-object-processor.d.ts +54 -0
  15. package/lib/sui/sui-object-processor.js +93 -0
  16. package/lib/sui/sui-object-processor.js.map +1 -0
  17. package/lib/sui/sui-objects-processor-template.d.ts +34 -0
  18. package/lib/sui/sui-objects-processor-template.js +88 -0
  19. package/lib/sui/sui-objects-processor-template.js.map +1 -0
  20. package/lib/sui/sui-plugin.d.ts +1 -1
  21. package/lib/sui/sui-plugin.js +21 -2
  22. package/lib/sui/sui-plugin.js.map +1 -1
  23. package/lib/sui/sui-processor.d.ts +8 -56
  24. package/lib/sui/sui-processor.js +9 -122
  25. package/lib/sui/sui-processor.js.map +1 -1
  26. package/package.json +4 -3
  27. package/src/aptos/aptos-plugin.ts +3 -15
  28. package/src/core/template.ts +6 -0
  29. package/src/eth/base-processor-template.ts +2 -7
  30. package/src/eth/eth-plugin.ts +8 -1
  31. package/src/sui/index.ts +13 -1
  32. package/src/sui/sui-object-processor.ts +186 -0
  33. package/src/sui/sui-objects-processor-template.ts +153 -0
  34. package/src/sui/sui-plugin.ts +29 -2
  35. package/src/sui/sui-processor.ts +14 -196
@@ -18,11 +18,12 @@ import {
18
18
  import { ServerError, Status } from 'nice-grpc'
19
19
  import { ProcessorState } from './binds.js'
20
20
  import { AccountProcessorState } from './account-processor-state.js'
21
- import { ProcessorTemplateProcessorState, TemplateInstanceState } from './base-processor-template.js'
21
+ import { ProcessorTemplateProcessorState } from './base-processor-template.js'
22
22
  import { GlobalProcessorState } from './base-processor.js'
23
23
  import { validateAndNormalizeAddress } from './eth.js'
24
24
  import { EthChainId } from '../core/chain.js'
25
25
  import { EthContext } from './context.js'
26
+ import { TemplateInstanceState } from '../core/template.js'
26
27
 
27
28
  interface Handlers {
28
29
  eventHandlers: ((event: Data_EthLog) => Promise<ProcessResult>)[]
@@ -244,7 +245,13 @@ export class EthPlugin extends Plugin {
244
245
 
245
246
  async start(request: StartRequest) {
246
247
  const ctx = new NoopContext()
248
+ const allowedChainIds = new Set<string>(Object.values(EthChainId))
249
+
247
250
  for (const instance of request.templateInstances) {
251
+ if (!allowedChainIds.has(instance.contract?.chainId || '')) {
252
+ continue
253
+ }
254
+
248
255
  const template = ProcessorTemplateProcessorState.INSTANCE.getValues()[instance.templateId]
249
256
  if (!template) {
250
257
  throw new ServerError(Status.INVALID_ARGUMENT, 'Invalid template contract:' + instance)
package/src/sui/index.ts CHANGED
@@ -1,4 +1,16 @@
1
- export * from './sui-processor.js'
1
+ export { SuiBaseProcessor, type SuiBindOptions } from './sui-processor.js'
2
+ export {
3
+ SuiAddressProcessor,
4
+ SuiObjectProcessor,
5
+ SuiWrappedObjectProcessor,
6
+ type SuiObjectBindOptions,
7
+ } from './sui-object-processor.js'
8
+
9
+ export {
10
+ // SuiAddressProcessorTemplate,
11
+ SuiObjectsProcessorTemplate,
12
+ SuiWrappedObjectProcessorTemplate,
13
+ } from './sui-objects-processor-template.js'
2
14
 
3
15
  export * from './network.js'
4
16
  export * from './context.js'
@@ -0,0 +1,186 @@
1
+ import { Data_SuiObject, HandleInterval, MoveAccountFetchConfig, MoveOwnerType, ProcessResult } from '@sentio/protos'
2
+ import { ListStateStorage } from '@sentio/runtime'
3
+ import { SuiNetwork } from './network.js'
4
+ import { SuiObjectsContext } from './context.js'
5
+ import { SuiMoveObject } from '@mysten/sui.js'
6
+ import { PromiseOrVoid } from '../core/index.js'
7
+ import { configure, IndexConfigure, SuiBindOptions } from './sui-processor.js'
8
+
9
+ export interface SuiObjectBindOptions {
10
+ objectId: string
11
+ network?: SuiNetwork
12
+ startCheckpoint?: bigint
13
+ baseLabels?: { [key: string]: string }
14
+ }
15
+
16
+ interface ObjectHandler {
17
+ type?: string
18
+ versionInterval?: HandleInterval
19
+ timeIntervalInMinutes?: HandleInterval
20
+ fetchConfig: MoveAccountFetchConfig
21
+ handler: (resource: Data_SuiObject) => Promise<ProcessResult>
22
+ }
23
+
24
+ export const DEFAULT_FETCH_CONFIG: MoveAccountFetchConfig = {
25
+ owned: true,
26
+ }
27
+
28
+ export class SuiAccountProcessorState extends ListStateStorage<SuiBaseObjectsProcessor<any>> {
29
+ static INSTANCE = new SuiAccountProcessorState()
30
+ }
31
+
32
+ export interface SuiInternalObjectsBindOptions extends SuiBindOptions {
33
+ ownerType: MoveOwnerType
34
+ }
35
+
36
+ export abstract class SuiBaseObjectsProcessor<HandlerType> {
37
+ config: IndexConfigure
38
+ ownerType: MoveOwnerType
39
+
40
+ objectHandlers: ObjectHandler[] = []
41
+
42
+ // static bind(options: SuiObjectsBindOptions): SuiBaseObjectsProcessor<any> {
43
+ // return new SuiBaseObjectsProcessor(options)
44
+ // }
45
+
46
+ protected constructor(options: SuiInternalObjectsBindOptions) {
47
+ this.config = configure(options)
48
+ this.ownerType = options.ownerType
49
+ SuiAccountProcessorState.INSTANCE.addValue(this)
50
+ }
51
+
52
+ getChainId(): string {
53
+ return this.config.network
54
+ }
55
+
56
+ // protected abstract transformObjects(objects: SuiMoveObject[]): SuiMoveObject[]
57
+
58
+ protected abstract doHandle(handler: HandlerType, data: Data_SuiObject, ctx: SuiObjectsContext): PromiseOrVoid
59
+
60
+ public onInterval(
61
+ handler: HandlerType, //(resources: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
62
+ timeInterval: HandleInterval | undefined,
63
+ versionInterval: HandleInterval | undefined,
64
+ type: string | undefined,
65
+ fetchConfig: Partial<MoveAccountFetchConfig> | undefined
66
+ ): this {
67
+ const processor = this
68
+ this.objectHandlers.push({
69
+ handler: async function (data) {
70
+ const ctx = new SuiObjectsContext(
71
+ processor.config.network,
72
+ processor.config.address,
73
+ data.slot,
74
+ data.timestamp || new Date(0),
75
+ processor.config.baseLabels
76
+ )
77
+ await processor.doHandle(handler, data, ctx)
78
+ return ctx.getProcessResult()
79
+ },
80
+ timeIntervalInMinutes: timeInterval,
81
+ versionInterval: versionInterval,
82
+ type,
83
+ fetchConfig: { ...DEFAULT_FETCH_CONFIG, ...fetchConfig },
84
+ })
85
+ return this
86
+ }
87
+
88
+ public onTimeInterval(
89
+ handler: HandlerType,
90
+ timeIntervalInMinutes = 60,
91
+ backfillTimeIntervalInMinutes = 240,
92
+ type?: string,
93
+ fetchConfig?: Partial<MoveAccountFetchConfig>
94
+ ): this {
95
+ return this.onInterval(
96
+ handler,
97
+ {
98
+ recentInterval: timeIntervalInMinutes,
99
+ backfillInterval: backfillTimeIntervalInMinutes,
100
+ },
101
+ undefined,
102
+ type,
103
+ fetchConfig
104
+ )
105
+ }
106
+
107
+ public onSlotInterval(
108
+ handler: HandlerType,
109
+ slotInterval = 100000,
110
+ backfillSlotInterval = 400000,
111
+ type?: string,
112
+ fetchConfig?: Partial<MoveAccountFetchConfig>
113
+ ): this {
114
+ return this.onInterval(
115
+ handler,
116
+ undefined,
117
+ { recentInterval: slotInterval, backfillInterval: backfillSlotInterval },
118
+ type,
119
+ fetchConfig
120
+ )
121
+ }
122
+ }
123
+
124
+ export class SuiAddressProcessor extends SuiBaseObjectsProcessor<
125
+ (objects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid
126
+ > {
127
+ static bind(options: SuiBindOptions): SuiAddressProcessor {
128
+ return new SuiAddressProcessor({ ...options, ownerType: MoveOwnerType.ADDRESS })
129
+ }
130
+
131
+ protected doHandle(
132
+ handler: (objects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
133
+ data: Data_SuiObject,
134
+ ctx: SuiObjectsContext
135
+ ): PromiseOrVoid {
136
+ return handler(data.objects as SuiMoveObject[], ctx)
137
+ }
138
+ }
139
+
140
+ export class SuiObjectProcessor extends SuiBaseObjectsProcessor<
141
+ (self: SuiMoveObject, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid
142
+ > {
143
+ static bind(options: SuiObjectBindOptions): SuiObjectProcessor {
144
+ return new SuiObjectProcessor({
145
+ address: options.objectId,
146
+ network: options.network,
147
+ startCheckpoint: options.startCheckpoint,
148
+ ownerType: MoveOwnerType.OBJECT,
149
+ baseLabels: options.baseLabels,
150
+ })
151
+ }
152
+
153
+ protected doHandle(
154
+ handler: (self: SuiMoveObject, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
155
+ data: Data_SuiObject,
156
+ ctx: SuiObjectsContext
157
+ ): PromiseOrVoid {
158
+ if (!data.self) {
159
+ console.log(`Sui object not existed in ${ctx.slot}, please specific a start time`)
160
+ return
161
+ }
162
+ return handler(data.self as SuiMoveObject, data.objects as SuiMoveObject[], ctx)
163
+ }
164
+ }
165
+
166
+ export class SuiWrappedObjectProcessor extends SuiBaseObjectsProcessor<
167
+ (dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid
168
+ > {
169
+ static bind(options: SuiObjectBindOptions): SuiWrappedObjectProcessor {
170
+ return new SuiWrappedObjectProcessor({
171
+ address: options.objectId,
172
+ network: options.network,
173
+ startCheckpoint: options.startCheckpoint,
174
+ ownerType: MoveOwnerType.WRAPPED_OBJECT,
175
+ baseLabels: options.baseLabels,
176
+ })
177
+ }
178
+
179
+ protected doHandle(
180
+ handler: (dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
181
+ data: Data_SuiObject,
182
+ ctx: SuiObjectsContext
183
+ ): PromiseOrVoid {
184
+ return handler(data.objects as SuiMoveObject[], ctx)
185
+ }
186
+ }
@@ -0,0 +1,153 @@
1
+ import { HandleInterval, MoveAccountFetchConfig } from '@sentio/protos'
2
+ import { ListStateStorage } from '@sentio/runtime'
3
+ import { SuiContext, SuiObjectsContext } from './context.js'
4
+ import { SuiMoveObject } from '@mysten/sui.js'
5
+ import { PromiseOrVoid } from '../core/index.js'
6
+ import {
7
+ DEFAULT_FETCH_CONFIG,
8
+ SuiBaseObjectsProcessor,
9
+ SuiObjectBindOptions,
10
+ SuiObjectProcessor,
11
+ SuiWrappedObjectProcessor,
12
+ } from './sui-object-processor.js'
13
+ import { TemplateInstanceState } from '../core/template.js'
14
+
15
+ class ObjectHandler<HandlerType> {
16
+ type?: string
17
+ versionInterval?: HandleInterval
18
+ timeIntervalInMinutes?: HandleInterval
19
+ handler: HandlerType
20
+ fetchConfig: MoveAccountFetchConfig
21
+ }
22
+
23
+ export class SuiAccountProcessorTemplateState extends ListStateStorage<SuiBaseObjectsProcessorTemplate<any, any>> {
24
+ static INSTANCE = new SuiAccountProcessorTemplateState()
25
+ }
26
+
27
+ export abstract class SuiBaseObjectsProcessorTemplate<
28
+ HandlerType,
29
+ // OptionType,
30
+ ProcessorType extends SuiBaseObjectsProcessor<HandlerType>
31
+ > {
32
+ id: number
33
+ objectHandlers: ObjectHandler<HandlerType>[] = []
34
+ binds = new Set<string>()
35
+
36
+ protected constructor() {
37
+ this.id = SuiAccountProcessorTemplateState.INSTANCE.getValues().length
38
+ SuiAccountProcessorTemplateState.INSTANCE.addValue(this)
39
+ }
40
+
41
+ abstract createProcessor(options: SuiObjectBindOptions): ProcessorType
42
+
43
+ bind(options: SuiObjectBindOptions, ctx: SuiContext): void {
44
+ options.network = options.network || ctx.network
45
+ const sig = [options.network, options.objectId].join('_')
46
+ if (this.binds.has(sig)) {
47
+ console.log(`Same object id can be bind to one template only once, ignore duplicate bind: ${sig}`)
48
+ return
49
+ }
50
+ this.binds.add(sig)
51
+
52
+ const processor = this.createProcessor(options)
53
+ for (const h of this.objectHandlers) {
54
+ processor.onInterval(h.handler, h.timeIntervalInMinutes, h.versionInterval, h.type, h.fetchConfig)
55
+ }
56
+ const config = processor.config
57
+
58
+ ctx._res.states.configUpdated = true
59
+ TemplateInstanceState.INSTANCE.addValue({
60
+ templateId: this.id,
61
+ contract: {
62
+ name: '',
63
+ chainId: config.network,
64
+ address: config.address,
65
+ abi: '',
66
+ },
67
+ startBlock: config.startCheckpoint,
68
+ endBlock: 0n,
69
+ })
70
+ }
71
+
72
+ protected onInterval(
73
+ handler: HandlerType,
74
+ timeInterval: HandleInterval | undefined,
75
+ versionInterval: HandleInterval | undefined,
76
+ type: string | undefined,
77
+ fetchConfig: Partial<MoveAccountFetchConfig> | undefined
78
+ ): this {
79
+ this.objectHandlers.push({
80
+ handler: handler,
81
+ timeIntervalInMinutes: timeInterval,
82
+ versionInterval: versionInterval,
83
+ type,
84
+ fetchConfig: { ...DEFAULT_FETCH_CONFIG, ...fetchConfig },
85
+ })
86
+ return this
87
+ }
88
+
89
+ public onTimeInterval(
90
+ handler: HandlerType,
91
+ timeIntervalInMinutes = 60,
92
+ backfillTimeIntervalInMinutes = 240,
93
+ type?: string,
94
+ fetchConfig?: Partial<MoveAccountFetchConfig>
95
+ ): this {
96
+ return this.onInterval(
97
+ handler,
98
+ {
99
+ recentInterval: timeIntervalInMinutes,
100
+ backfillInterval: backfillTimeIntervalInMinutes,
101
+ },
102
+ undefined,
103
+ type,
104
+ fetchConfig
105
+ )
106
+ }
107
+
108
+ public onSlotInterval(
109
+ handler: HandlerType,
110
+ slotInterval = 100000,
111
+ backfillSlotInterval = 400000,
112
+ type?: string,
113
+ fetchConfig?: Partial<MoveAccountFetchConfig>
114
+ ): this {
115
+ return this.onInterval(
116
+ handler,
117
+ undefined,
118
+ { recentInterval: slotInterval, backfillInterval: backfillSlotInterval },
119
+ type,
120
+ fetchConfig
121
+ )
122
+ }
123
+ }
124
+
125
+ // export class SuiAddressProcessorTemplate extends SuiBaseObjectsProcessorTemplate<
126
+ // (objects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
127
+ // SuiBindOptions,
128
+ // SuiAddressProcessor
129
+ // > {
130
+ // bind(options: SuiBindOptions, ctx: SuiContext): SuiAddressProcessor {
131
+ // return this.bindInternal(SuiAddressProcessorTemplate.bind(options), ctx)
132
+ // }
133
+ // }
134
+
135
+ export class SuiObjectsProcessorTemplate extends SuiBaseObjectsProcessorTemplate<
136
+ (self: SuiMoveObject, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
137
+ // SuiObjectBindOptions,
138
+ SuiObjectProcessor
139
+ > {
140
+ createProcessor(options: SuiObjectBindOptions): SuiObjectProcessor {
141
+ return SuiObjectProcessor.bind(options)
142
+ }
143
+ }
144
+
145
+ export class SuiWrappedObjectProcessorTemplate extends SuiBaseObjectsProcessorTemplate<
146
+ (dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
147
+ // SuiObjectBindOptions,
148
+ SuiWrappedObjectProcessor
149
+ > {
150
+ createProcessor(options: SuiObjectBindOptions): SuiWrappedObjectProcessor {
151
+ return SuiWrappedObjectProcessor.bind(options)
152
+ }
153
+ }
@@ -16,9 +16,14 @@ import {
16
16
 
17
17
  import { ServerError, Status } from 'nice-grpc'
18
18
 
19
- import { SuiAccountProcessorState, SuiProcessorState } from './sui-processor.js'
19
+ import { SuiProcessorState } from './sui-processor.js'
20
+ import { SuiAccountProcessorState } from './sui-object-processor.js'
20
21
  import { validateAndNormalizeAddress } from './utils.js'
21
22
  import { initCoinList } from './ext/coin.js'
23
+ import { SuiChainId } from '../core/chain.js'
24
+ import { SuiAccountProcessorTemplateState, SuiBaseObjectsProcessorTemplate } from './sui-objects-processor-template.js'
25
+ import { SuiNetwork } from './network.js'
26
+ import { SuiContext } from './context.js'
22
27
 
23
28
  interface Handlers {
24
29
  suiEventHandlers: ((event: Data_SuiEvent) => Promise<ProcessResult>)[]
@@ -33,8 +38,27 @@ export class SuiPlugin extends Plugin {
33
38
  suiEventHandlers: [],
34
39
  suiObjectHandlers: [],
35
40
  }
36
- async start(start: StartRequest): Promise<void> {
41
+ async start(request: StartRequest): Promise<void> {
37
42
  await initCoinList()
43
+
44
+ const allowedChainIds = new Set<string>(Object.values(SuiChainId))
45
+ for (const instance of request.templateInstances) {
46
+ if (!allowedChainIds.has(instance.contract?.chainId || '')) {
47
+ continue
48
+ }
49
+
50
+ const template: SuiBaseObjectsProcessorTemplate<any, any> =
51
+ SuiAccountProcessorTemplateState.INSTANCE.getValues()[instance.templateId]
52
+
53
+ template.bind(
54
+ {
55
+ objectId: instance.contract?.address || '',
56
+ network: <SuiNetwork>instance.contract?.chainId || SuiNetwork.MAIN_NET,
57
+ startCheckpoint: instance.startBlock || 0n,
58
+ },
59
+ NoopContext
60
+ )
61
+ }
38
62
  }
39
63
 
40
64
  async configure(config: ProcessConfigResponse) {
@@ -107,6 +131,7 @@ export class SuiPlugin extends Plugin {
107
131
  },
108
132
  type: handler.type || '',
109
133
  ownerType: processor.ownerType,
134
+ fetchConfig: undefined,
110
135
  })
111
136
  }
112
137
  config.accountConfigs.push(accountConfig)
@@ -187,3 +212,5 @@ export class SuiPlugin extends Plugin {
187
212
  }
188
213
 
189
214
  PluginManager.INSTANCE.register(new SuiPlugin())
215
+
216
+ const NoopContext = new SuiContext('', SuiChainId.SUI_MAINNET, '', new Date(), 0n, {} as any, 0, {})
@@ -1,22 +1,13 @@
1
- import {
2
- Data_SuiCall,
3
- Data_SuiEvent,
4
- Data_SuiObject,
5
- HandleInterval,
6
- MoveFetchConfig,
7
- MoveOnIntervalConfig_OwnerType,
8
- ProcessResult,
9
- } from '@sentio/protos'
1
+ import { Data_SuiCall, Data_SuiEvent, MoveFetchConfig } from '@sentio/protos'
10
2
  import { ListStateStorage, mergeProcessResults } from '@sentio/runtime'
11
3
  import { SuiNetwork } from './network.js'
12
4
  import { ServerError, Status } from 'nice-grpc'
13
- import { SuiContext, SuiObjectsContext } from './context.js'
5
+ import { SuiContext } from './context.js'
14
6
  import {
15
7
  getProgrammableTransaction,
16
8
  getTransactionKind,
17
9
  MoveCallSuiTransaction,
18
10
  SuiEvent,
19
- SuiMoveObject,
20
11
  SuiTransactionBlockResponse,
21
12
  } from '@mysten/sui.js'
22
13
  import {
@@ -29,35 +20,32 @@ import {
29
20
  } from '../move/index.js'
30
21
  import { getMoveCalls } from './utils.js'
31
22
  import { defaultMoveCoder, MoveCoder } from './move-coder.js'
32
- import { Labels, PromiseOrVoid } from '../core/index.js'
33
- // import { dynamic_field } from './builtin/0x2.js'
23
+ import { Labels } from '../core/index.js'
24
+ import { Required } from 'utility-types'
34
25
 
35
26
  const DEFAULT_FETCH_CONFIG: MoveFetchConfig = {
36
27
  resourceChanges: false,
37
28
  allEvents: true,
38
29
  }
39
30
 
40
- class IndexConfigure {
41
- address: string
42
- network: SuiNetwork
43
- startCheckpoint: bigint
44
- baseLabels?: Labels
31
+ export type IndexConfigure = Required<SuiBindOptions, 'startCheckpoint' | 'network'>
32
+
33
+ export function configure(options: SuiBindOptions): IndexConfigure {
34
+ return {
35
+ startCheckpoint: options.startCheckpoint || 0n,
36
+ address: options.address,
37
+ network: options.network || SuiNetwork.MAIN_NET,
38
+ baseLabels: options.baseLabels,
39
+ }
45
40
  }
46
41
 
47
- export class SuiBindOptions {
42
+ export interface SuiBindOptions {
48
43
  address: string
49
44
  network?: SuiNetwork
50
45
  startCheckpoint?: bigint
51
46
  baseLabels?: Labels
52
47
  }
53
48
 
54
- export class SuiObjectBindOptions {
55
- objectId: string
56
- network?: SuiNetwork
57
- startCheckpoint?: bigint
58
- baseLabels?: { [key: string]: string }
59
- }
60
-
61
49
  export class SuiProcessorState extends ListStateStorage<SuiBaseProcessor> {
62
50
  static INSTANCE = new SuiProcessorState()
63
51
  }
@@ -206,173 +194,3 @@ export class SuiBaseProcessor {
206
194
  return this
207
195
  }
208
196
  }
209
-
210
- class ObjectHandler {
211
- type?: string
212
- versionInterval?: HandleInterval
213
- timeIntervalInMinutes?: HandleInterval
214
- handler: (resource: Data_SuiObject) => Promise<ProcessResult>
215
- }
216
-
217
- export class SuiAccountProcessorState extends ListStateStorage<SuiBaseObjectsProcessor<any>> {
218
- static INSTANCE = new SuiAccountProcessorState()
219
- }
220
-
221
- class SuiObjectsBindOptions extends SuiBindOptions {
222
- ownerType: MoveOnIntervalConfig_OwnerType
223
- }
224
-
225
- abstract class SuiBaseObjectsProcessor<HandlerType> {
226
- config: IndexConfigure
227
- ownerType: MoveOnIntervalConfig_OwnerType
228
-
229
- objectHandlers: ObjectHandler[] = []
230
-
231
- // static bind(options: SuiObjectsBindOptions): SuiBaseObjectsProcessor<any> {
232
- // return new SuiBaseObjectsProcessor(options)
233
- // }
234
-
235
- protected constructor(options: SuiObjectsBindOptions) {
236
- this.config = configure(options)
237
- this.ownerType = options.ownerType
238
- SuiAccountProcessorState.INSTANCE.addValue(this)
239
- }
240
-
241
- getChainId(): string {
242
- return this.config.network
243
- }
244
-
245
- // protected abstract transformObjects(objects: SuiMoveObject[]): SuiMoveObject[]
246
-
247
- protected abstract doHandle(handler: HandlerType, data: Data_SuiObject, ctx: SuiObjectsContext): PromiseOrVoid
248
-
249
- protected onInterval(
250
- handler: HandlerType, //(resources: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
251
- timeInterval: HandleInterval | undefined,
252
- versionInterval: HandleInterval | undefined,
253
- type: string | undefined
254
- ): this {
255
- const processor = this
256
- this.objectHandlers.push({
257
- handler: async function (data) {
258
- const ctx = new SuiObjectsContext(
259
- processor.config.network,
260
- processor.config.address,
261
- data.slot,
262
- data.timestamp || new Date(0),
263
- processor.config.baseLabels
264
- )
265
- await processor.doHandle(handler, data, ctx)
266
- return ctx.getProcessResult()
267
- },
268
- timeIntervalInMinutes: timeInterval,
269
- versionInterval: versionInterval,
270
- type,
271
- })
272
- return this
273
- }
274
-
275
- public onTimeInterval(
276
- handler: HandlerType,
277
- timeIntervalInMinutes = 60,
278
- backfillTimeIntervalInMinutes = 240,
279
- type?: string
280
- ): this {
281
- return this.onInterval(
282
- handler,
283
- {
284
- recentInterval: timeIntervalInMinutes,
285
- backfillInterval: backfillTimeIntervalInMinutes,
286
- },
287
- undefined,
288
- type
289
- )
290
- }
291
-
292
- public onSlotInterval(
293
- handler: HandlerType,
294
- slotInterval = 100000,
295
- backfillSlotInterval = 400000,
296
- type?: string
297
- ): this {
298
- return this.onInterval(
299
- handler,
300
- undefined,
301
- { recentInterval: slotInterval, backfillInterval: backfillSlotInterval },
302
- type
303
- )
304
- }
305
- }
306
-
307
- function configure(options: SuiBindOptions): IndexConfigure {
308
- return {
309
- startCheckpoint: options.startCheckpoint || 0n,
310
- address: options.address,
311
- network: options.network || SuiNetwork.MAIN_NET,
312
- baseLabels: options.baseLabels,
313
- }
314
- }
315
-
316
- export class SuiAddressProcessor extends SuiBaseObjectsProcessor<
317
- (objects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid
318
- > {
319
- static bind(options: SuiBindOptions): SuiAddressProcessor {
320
- return new SuiAddressProcessor({ ...options, ownerType: MoveOnIntervalConfig_OwnerType.ADDRESS })
321
- }
322
-
323
- protected doHandle(
324
- handler: (objects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
325
- data: Data_SuiObject,
326
- ctx: SuiObjectsContext
327
- ): PromiseOrVoid {
328
- return handler(data.objects as SuiMoveObject[], ctx)
329
- }
330
- }
331
- // export class SuiDynamicFieldObjectsProcessor extends SuiBaseObjectsProcessor<dynamic_field.Field<any, any>> {
332
- export class SuiObjectProcessor extends SuiBaseObjectsProcessor<
333
- (self: SuiMoveObject, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid
334
- > {
335
- static bind(options: SuiObjectBindOptions): SuiObjectProcessor {
336
- return new SuiObjectProcessor({
337
- address: options.objectId,
338
- network: options.network,
339
- startCheckpoint: options.startCheckpoint,
340
- ownerType: MoveOnIntervalConfig_OwnerType.OBJECT,
341
- baseLabels: options.baseLabels,
342
- })
343
- }
344
-
345
- protected doHandle(
346
- handler: (self: SuiMoveObject, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
347
- data: Data_SuiObject,
348
- ctx: SuiObjectsContext
349
- ): PromiseOrVoid {
350
- if (!data.self) {
351
- console.log(`Sui object not existed in ${ctx.slot}, please specific a start time`)
352
- return
353
- }
354
- return handler(data.self as SuiMoveObject, data.objects as SuiMoveObject[], ctx)
355
- }
356
- }
357
-
358
- export class SuiWrappedObjectProcessor extends SuiBaseObjectsProcessor<
359
- (dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid
360
- > {
361
- static bind(options: SuiObjectBindOptions): SuiWrappedObjectProcessor {
362
- return new SuiWrappedObjectProcessor({
363
- address: options.objectId,
364
- network: options.network,
365
- startCheckpoint: options.startCheckpoint,
366
- ownerType: MoveOnIntervalConfig_OwnerType.WRAPPED_OBJECT,
367
- baseLabels: options.baseLabels,
368
- })
369
- }
370
-
371
- protected doHandle(
372
- handler: (dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectsContext) => PromiseOrVoid,
373
- data: Data_SuiObject,
374
- ctx: SuiObjectsContext
375
- ): PromiseOrVoid {
376
- return handler(data.objects as SuiMoveObject[], ctx)
377
- }
378
- }