@sentio/sdk 2.0.0-rc.14 → 2.0.0-rc.15

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 (199) hide show
  1. package/lib/aptos/api.d.ts +5 -0
  2. package/lib/aptos/api.js +21 -0
  3. package/lib/aptos/api.js.map +1 -0
  4. package/lib/aptos/aptos-plugin.d.ts +14 -0
  5. package/lib/aptos/aptos-plugin.js +146 -0
  6. package/lib/aptos/aptos-plugin.js.map +1 -0
  7. package/lib/aptos/aptos-processor.d.ts +72 -0
  8. package/lib/aptos/aptos-processor.js +201 -0
  9. package/lib/aptos/aptos-processor.js.map +1 -0
  10. package/lib/aptos/builtin/0x1.d.ts +2140 -0
  11. package/lib/aptos/builtin/0x1.js +2727 -0
  12. package/lib/aptos/builtin/0x1.js.map +1 -0
  13. package/lib/aptos/builtin/0x3.d.ts +573 -0
  14. package/lib/aptos/builtin/0x3.js +496 -0
  15. package/lib/aptos/builtin/0x3.js.map +1 -0
  16. package/lib/aptos/builtin/index.d.ts +2 -0
  17. package/lib/aptos/builtin/index.js +6 -0
  18. package/lib/aptos/builtin/index.js.map +1 -0
  19. package/lib/aptos/codegen/codegen.d.ts +23 -0
  20. package/lib/aptos/codegen/codegen.js +388 -0
  21. package/lib/aptos/codegen/codegen.js.map +1 -0
  22. package/lib/aptos/codegen/index.d.ts +1 -0
  23. package/lib/aptos/codegen/index.js +2 -0
  24. package/lib/aptos/codegen/index.js.map +1 -0
  25. package/lib/aptos/codegen/typegen.d.ts +18 -0
  26. package/lib/aptos/codegen/typegen.js +145 -0
  27. package/lib/aptos/codegen/typegen.js.map +1 -0
  28. package/lib/aptos/codegen/typegen.test.d.ts +1 -0
  29. package/lib/aptos/codegen/typegen.test.js.map +1 -0
  30. package/lib/aptos/context.d.ts +21 -0
  31. package/lib/aptos/context.js +59 -0
  32. package/lib/aptos/context.js.map +1 -0
  33. package/lib/aptos/index.d.ts +9 -0
  34. package/lib/aptos/index.js +8 -0
  35. package/lib/aptos/index.js.map +1 -0
  36. package/lib/aptos/models.d.ts +37 -0
  37. package/lib/aptos/models.js +2 -0
  38. package/lib/aptos/models.js.map +1 -0
  39. package/lib/aptos/move-coder.d.ts +22 -0
  40. package/lib/aptos/move-coder.js +164 -0
  41. package/lib/aptos/move-coder.js.map +1 -0
  42. package/lib/aptos/move-types.d.ts +10 -0
  43. package/lib/aptos/move-types.js +2 -0
  44. package/lib/aptos/move-types.js.map +1 -0
  45. package/lib/aptos/network.d.ts +11 -0
  46. package/lib/aptos/network.js +27 -0
  47. package/lib/aptos/network.js.map +1 -0
  48. package/lib/aptos/run-codegen.d.ts +1 -0
  49. package/lib/aptos/run-codegen.js +12 -0
  50. package/lib/aptos/run-codegen.js.map +1 -0
  51. package/lib/aptos/tests/aptos.test.d.ts +1 -0
  52. package/lib/aptos/tests/aptos.test.js.map +1 -0
  53. package/lib/aptos/tests/souffl3.d.ts +1 -0
  54. package/lib/aptos/tests/souffl3.js +44 -0
  55. package/lib/aptos/tests/souffl3.js.map +1 -0
  56. package/lib/aptos/tests/types/index.d.ts +3 -0
  57. package/lib/aptos/tests/types/index.js +7 -0
  58. package/lib/aptos/tests/types/index.js.map +1 -0
  59. package/lib/aptos/tests/types/reserved.d.ts +445 -0
  60. package/lib/aptos/tests/types/reserved.js +301 -0
  61. package/lib/aptos/tests/types/reserved.js.map +1 -0
  62. package/lib/aptos/tests/types/soffl3.d.ts +1065 -0
  63. package/lib/aptos/tests/types/soffl3.js +548 -0
  64. package/lib/aptos/tests/types/soffl3.js.map +1 -0
  65. package/lib/aptos/tests/types/souffle.d.ts +440 -0
  66. package/lib/aptos/tests/types/souffle.js +295 -0
  67. package/lib/aptos/tests/types/souffle.js.map +1 -0
  68. package/lib/aptos/types.d.ts +9 -0
  69. package/lib/aptos/types.js +130 -0
  70. package/lib/aptos/types.js.map +1 -0
  71. package/lib/aptos/types.test.d.ts +1 -0
  72. package/lib/aptos/types.test.js.map +1 -0
  73. package/lib/aptos/utils.d.ts +7 -0
  74. package/lib/aptos/utils.js +20 -0
  75. package/lib/aptos/utils.js.map +1 -0
  76. package/lib/{utils/chain.d.ts → chain.d.ts} +0 -0
  77. package/lib/{utils/chain.js → chain.js} +0 -0
  78. package/lib/chain.js.map +1 -0
  79. package/lib/core/context.js +1 -1
  80. package/lib/core/context.js.map +1 -1
  81. package/lib/core/sui-plugin.js +1 -1
  82. package/lib/core/sui-plugin.js.map +1 -1
  83. package/lib/index.d.ts +1 -1
  84. package/lib/index.js +1 -1
  85. package/lib/index.js.map +1 -1
  86. package/lib/solana/builtin/index.d.ts +1 -0
  87. package/lib/solana/builtin/index.js +2 -0
  88. package/lib/solana/builtin/index.js.map +1 -0
  89. package/lib/solana/builtin/spl-token-processor.d.ts +39 -0
  90. package/lib/solana/builtin/spl-token-processor.js +254 -0
  91. package/lib/solana/builtin/spl-token-processor.js.map +1 -0
  92. package/lib/solana/builtin/types.d.ts +427 -0
  93. package/lib/solana/builtin/types.js +200 -0
  94. package/lib/solana/builtin/types.js.map +1 -0
  95. package/lib/solana/codegen/codegen.d.ts +1 -0
  96. package/lib/solana/codegen/codegen.js +122 -0
  97. package/lib/solana/codegen/codegen.js.map +1 -0
  98. package/lib/solana/codegen/index.d.ts +1 -0
  99. package/lib/solana/codegen/index.js +2 -0
  100. package/lib/solana/codegen/index.js.map +1 -0
  101. package/lib/solana/index.d.ts +4 -0
  102. package/lib/solana/index.js +5 -0
  103. package/lib/solana/index.js.map +1 -0
  104. package/lib/solana/run-codegen.d.ts +2 -0
  105. package/lib/solana/run-codegen.js +12 -0
  106. package/lib/solana/run-codegen.js.map +1 -0
  107. package/lib/solana/solana-context.d.ts +9 -0
  108. package/lib/solana/solana-context.js +28 -0
  109. package/lib/solana/solana-context.js.map +1 -0
  110. package/lib/solana/solana-options.d.ts +10 -0
  111. package/lib/solana/solana-options.js +10 -0
  112. package/lib/solana/solana-options.js.map +1 -0
  113. package/lib/solana/solana-plugin.d.ts +9 -0
  114. package/lib/solana/solana-plugin.js +87 -0
  115. package/lib/solana/solana-plugin.js.map +1 -0
  116. package/lib/solana/solana-processor.d.ts +43 -0
  117. package/lib/solana/solana-processor.js +72 -0
  118. package/lib/solana/solana-processor.js.map +1 -0
  119. package/lib/solana/tests/solana.test.d.ts +1 -0
  120. package/lib/solana/tests/solana.test.js.map +1 -0
  121. package/lib/solana/tests/types/basic_1.d.ts +26 -0
  122. package/lib/solana/tests/types/basic_1.js +63 -0
  123. package/lib/solana/tests/types/basic_1.js.map +1 -0
  124. package/lib/solana/tests/types/basic_1_processor.d.ts +21 -0
  125. package/lib/solana/tests/types/basic_1_processor.js +35 -0
  126. package/lib/solana/tests/types/basic_1_processor.js.map +1 -0
  127. package/lib/solana/tests/types/token_bridge.d.ts +29 -0
  128. package/lib/solana/tests/types/token_bridge.js +938 -0
  129. package/lib/solana/tests/types/token_bridge.js.map +1 -0
  130. package/lib/solana/tests/types/token_bridge_processor.d.ts +212 -0
  131. package/lib/solana/tests/types/token_bridge_processor.js +134 -0
  132. package/lib/solana/tests/types/token_bridge_processor.js.map +1 -0
  133. package/lib/solana/tests/wormhole-token-bridge.d.ts +15 -0
  134. package/lib/solana/tests/wormhole-token-bridge.js +79 -0
  135. package/lib/solana/tests/wormhole-token-bridge.js.map +1 -0
  136. package/lib/testing/test-processor-server.js +1 -1
  137. package/lib/testing/test-processor-server.js.map +1 -1
  138. package/lib/testing/test-provider.js +1 -1
  139. package/lib/testing/test-provider.js.map +1 -1
  140. package/lib/utils/index.d.ts +0 -1
  141. package/lib/utils/index.js +0 -1
  142. package/lib/utils/index.js.map +1 -1
  143. package/package.json +33 -11
  144. package/src/aptos/abis/0x1.json +9205 -0
  145. package/src/aptos/abis/0x3.json +1515 -0
  146. package/src/aptos/api.ts +23 -0
  147. package/src/aptos/aptos-plugin.ts +183 -0
  148. package/src/aptos/aptos-processor.ts +331 -0
  149. package/src/aptos/builtin/0x1.ts +4911 -0
  150. package/src/aptos/builtin/0x3.ts +1092 -0
  151. package/src/aptos/builtin/index.ts +5 -0
  152. package/src/aptos/codegen/codegen.ts +470 -0
  153. package/src/aptos/codegen/index.ts +1 -0
  154. package/src/aptos/codegen/tsconfig.json +8 -0
  155. package/src/aptos/codegen/typegen.ts +165 -0
  156. package/src/aptos/context.ts +72 -0
  157. package/src/aptos/index.ts +10 -0
  158. package/src/aptos/models.ts +47 -0
  159. package/src/aptos/move-coder.ts +201 -0
  160. package/src/aptos/move-types.ts +11 -0
  161. package/src/aptos/network.ts +29 -0
  162. package/src/aptos/run-codegen.ts +13 -0
  163. package/src/aptos/tests/abis/reserved.json +402 -0
  164. package/src/aptos/tests/abis/soffl3.json +1411 -0
  165. package/src/aptos/tests/abis/souffle.json +389 -0
  166. package/src/aptos/tests/souffl3.ts +57 -0
  167. package/src/aptos/tests/types/index.ts +6 -0
  168. package/src/aptos/tests/types/reserved.ts +881 -0
  169. package/src/aptos/tests/types/soffl3.ts +1820 -0
  170. package/src/aptos/tests/types/souffle.ts +880 -0
  171. package/src/aptos/types.ts +149 -0
  172. package/src/aptos/utils.ts +26 -0
  173. package/src/{utils/chain.ts → chain.ts} +0 -0
  174. package/src/core/context.ts +1 -1
  175. package/src/core/sui-plugin.ts +1 -1
  176. package/src/index.ts +1 -1
  177. package/src/solana/builtin/index.ts +1 -0
  178. package/src/solana/builtin/spl-token-processor.ts +298 -0
  179. package/src/solana/builtin/types.ts +279 -0
  180. package/src/solana/codegen/codegen.ts +140 -0
  181. package/src/solana/codegen/index.ts +1 -0
  182. package/src/solana/codegen/tsconfig.json +8 -0
  183. package/src/solana/index.ts +4 -0
  184. package/src/solana/run-codegen.ts +13 -0
  185. package/src/solana/solana-context.ts +30 -0
  186. package/src/solana/solana-options.ts +11 -0
  187. package/src/solana/solana-plugin.ts +103 -0
  188. package/src/solana/solana-processor.ts +102 -0
  189. package/src/solana/tests/abis/basic_1.json +62 -0
  190. package/src/solana/tests/abis/token_bridge.json +937 -0
  191. package/src/solana/tests/types/basic_1.ts +62 -0
  192. package/src/solana/tests/types/basic_1_processor.ts +42 -0
  193. package/src/solana/tests/types/token_bridge.ts +937 -0
  194. package/src/solana/tests/types/token_bridge_processor.ts +150 -0
  195. package/src/solana/tests/wormhole-token-bridge.ts +96 -0
  196. package/src/testing/test-processor-server.ts +1 -1
  197. package/src/testing/test-provider.ts +1 -1
  198. package/src/utils/index.ts +0 -1
  199. package/lib/utils/chain.js.map +0 -1
@@ -0,0 +1,23 @@
1
+ import { AptosQueryClient, AptosQueryDefinition } from '@sentio/protos/chainquery'
2
+ import { createChannel, createClientFactory } from 'nice-grpc'
3
+ import { Endpoints } from '@sentio/runtime'
4
+ import { retryMiddleware } from 'nice-grpc-client-middleware-retry'
5
+ import { AptosClient } from 'aptos-sdk'
6
+ import { AptosNetwork, getChainId } from './network.js'
7
+
8
+ export function getChainQueryClient(address?: string): AptosQueryClient {
9
+ if (!address) {
10
+ address = Endpoints.INSTANCE.chainQueryAPI
11
+ }
12
+ const channel = createChannel(address)
13
+
14
+ return createClientFactory().use(retryMiddleware).create(AptosQueryDefinition, channel)
15
+ }
16
+
17
+ export function getAptosClient(network = AptosNetwork.MAIN_NET): AptosClient | undefined {
18
+ const chainServer = Endpoints.INSTANCE.chainServer.get(getChainId(network))
19
+ if (chainServer) {
20
+ return new AptosClient(chainServer)
21
+ }
22
+ return undefined
23
+ }
@@ -0,0 +1,183 @@
1
+ import { Plugin, PluginManager, errorString, mergeProcessResults, USER_PROCESSOR } from '@sentio/runtime'
2
+ import {
3
+ AccountConfig,
4
+ AptosCallHandlerConfig,
5
+ AptosEventHandlerConfig,
6
+ ContractConfig,
7
+ Data_AptCall,
8
+ Data_AptEvent,
9
+ Data_AptResource,
10
+ DataBinding,
11
+ HandlerType,
12
+ ProcessConfigResponse,
13
+ ProcessResult,
14
+ } from '@sentio/protos'
15
+
16
+ import { ServerError, Status } from 'nice-grpc'
17
+
18
+ import { AptosAccountProcessorState, AptosProcessorState } from './aptos-processor.js'
19
+
20
+ export class AptosPlugin extends Plugin {
21
+ name: string = 'AptosPlugin'
22
+
23
+ private aptosEventHandlers: ((event: Data_AptEvent) => Promise<ProcessResult>)[] = []
24
+ private aptosCallHandlers: ((func: Data_AptCall) => Promise<ProcessResult>)[] = []
25
+ private aptosResourceHandlers: ((resourceWithVersion: Data_AptResource) => Promise<ProcessResult>)[] = []
26
+
27
+ async configure(config: ProcessConfigResponse) {
28
+ for (const aptosProcessor of AptosProcessorState.INSTANCE.getValues()) {
29
+ const contractConfig: ContractConfig = {
30
+ transactionConfig: [],
31
+ processorType: USER_PROCESSOR,
32
+ contract: {
33
+ name: aptosProcessor.moduleName,
34
+ chainId: aptosProcessor.getChainId(),
35
+ address: aptosProcessor.config.address,
36
+ abi: '',
37
+ },
38
+ intervalConfigs: [],
39
+ logConfigs: [],
40
+ traceConfigs: [],
41
+ startBlock: aptosProcessor.config.startVersion,
42
+ endBlock: 0n,
43
+ instructionConfig: undefined,
44
+ aptosEventConfigs: [],
45
+ aptosCallConfigs: [],
46
+ }
47
+ // 1. Prepare event handlers
48
+ for (const handler of aptosProcessor.eventHandlers) {
49
+ const handlerId = this.aptosEventHandlers.push(handler.handler) - 1
50
+ const eventHandlerConfig: AptosEventHandlerConfig = {
51
+ filters: handler.filters.map((f) => {
52
+ return {
53
+ type: f.type,
54
+ account: f.account || '',
55
+ }
56
+ }),
57
+ fetchConfig: handler.fetchConfig,
58
+ handlerId,
59
+ }
60
+ contractConfig.aptosEventConfigs.push(eventHandlerConfig)
61
+ }
62
+
63
+ // 2. Prepare function handlers
64
+ for (const handler of aptosProcessor.callHandlers) {
65
+ const handlerId = this.aptosCallHandlers.push(handler.handler) - 1
66
+ const functionHandlerConfig: AptosCallHandlerConfig = {
67
+ filters: handler.filters.map((filter) => {
68
+ return {
69
+ function: filter.function,
70
+ typeArguments: filter.typeArguments || [],
71
+ withTypeArguments: !!filter.typeArguments,
72
+ includeFailed: filter.includeFailed || false,
73
+ }
74
+ }),
75
+ fetchConfig: handler.fetchConfig,
76
+ handlerId,
77
+ }
78
+ contractConfig.aptosCallConfigs.push(functionHandlerConfig)
79
+ }
80
+ config.contractConfigs.push(contractConfig)
81
+ }
82
+
83
+ for (const aptosProcessor of AptosAccountProcessorState.INSTANCE.getValues()) {
84
+ const accountConfig: AccountConfig = {
85
+ address: aptosProcessor.config.address,
86
+ chainId: aptosProcessor.getChainId(),
87
+ startBlock: aptosProcessor.config.startVersion,
88
+ aptosIntervalConfigs: [],
89
+ intervalConfigs: [],
90
+ logConfigs: [],
91
+ }
92
+ for (const handler of aptosProcessor.resourcesHandlers) {
93
+ const handlerId = this.aptosResourceHandlers.push(handler.handler) - 1
94
+ accountConfig.aptosIntervalConfigs.push({
95
+ intervalConfig: {
96
+ handlerId: handlerId,
97
+ minutes: 0,
98
+ minutesInterval: handler.timeIntervalInMinutes,
99
+ slot: 0,
100
+ slotInterval: handler.versionInterval,
101
+ },
102
+ type: handler.type || '',
103
+ })
104
+ }
105
+ config.accountConfigs.push(accountConfig)
106
+ }
107
+ }
108
+
109
+ supportedHandlers = [HandlerType.APT_CALL, HandlerType.APT_RESOURCE, HandlerType.APT_EVENT]
110
+
111
+ async processBinding(request: DataBinding): Promise<ProcessResult> {
112
+ switch (request.handlerType) {
113
+ case HandlerType.APT_CALL:
114
+ return this.processAptosFunctionCall(request)
115
+ case HandlerType.APT_EVENT:
116
+ return this.processAptosEvent(request)
117
+ case HandlerType.APT_RESOURCE:
118
+ return this.processAptosResource(request)
119
+ default:
120
+ throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
121
+ }
122
+ }
123
+
124
+ async processAptosEvent(binding: DataBinding): Promise<ProcessResult> {
125
+ if (!binding.data?.aptEvent) {
126
+ throw new ServerError(Status.INVALID_ARGUMENT, "Event can't be empty")
127
+ }
128
+ const promises: Promise<ProcessResult>[] = []
129
+ const event = binding.data.aptEvent
130
+
131
+ for (const handlerId of binding.handlerIds) {
132
+ // only support aptos event for now
133
+ promises.push(
134
+ this.aptosEventHandlers[handlerId](event).catch((e) => {
135
+ throw new ServerError(
136
+ Status.INTERNAL,
137
+ 'error processing event: ' + JSON.stringify(event) + '\n' + errorString(e)
138
+ )
139
+ })
140
+ )
141
+ }
142
+ return mergeProcessResults(await Promise.all(promises))
143
+ }
144
+
145
+ async processAptosResource(binding: DataBinding): Promise<ProcessResult> {
146
+ if (!binding.data?.aptResource) {
147
+ throw new ServerError(Status.INVALID_ARGUMENT, "Resource can't be empty")
148
+ }
149
+ const resource = binding.data.aptResource
150
+
151
+ const promises: Promise<ProcessResult>[] = []
152
+ for (const handlerId of binding.handlerIds) {
153
+ promises.push(
154
+ this.aptosResourceHandlers[handlerId](resource).catch((e) => {
155
+ throw new ServerError(
156
+ Status.INTERNAL,
157
+ 'error processing resource: ' + JSON.stringify(resource) + '\n' + errorString(e)
158
+ )
159
+ })
160
+ )
161
+ }
162
+ return mergeProcessResults(await Promise.all(promises))
163
+ }
164
+
165
+ async processAptosFunctionCall(binding: DataBinding): Promise<ProcessResult> {
166
+ if (!binding.data?.aptCall) {
167
+ throw new ServerError(Status.INVALID_ARGUMENT, "Call can't be empty")
168
+ }
169
+ const call = binding.data.aptCall
170
+
171
+ const promises: Promise<ProcessResult>[] = []
172
+ for (const handlerId of binding.handlerIds) {
173
+ // only support aptos call for now
174
+ const promise = this.aptosCallHandlers[handlerId](call).catch((e) => {
175
+ throw new ServerError(Status.INTERNAL, 'error processing call: ' + JSON.stringify(call) + '\n' + errorString(e))
176
+ })
177
+ promises.push(promise)
178
+ }
179
+ return mergeProcessResults(await Promise.all(promises))
180
+ }
181
+ }
182
+
183
+ PluginManager.INSTANCE.register(new AptosPlugin())
@@ -0,0 +1,331 @@
1
+ import { MoveResource, Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from './move-types.js'
2
+
3
+ import { MOVE_CODER, MoveCoder } from './move-coder.js'
4
+ import { AptosBindOptions, AptosNetwork, getChainId } from './network.js'
5
+ import { AptosContext, AptosResourceContext } from './context.js'
6
+ import { EventInstance } from './models.js'
7
+ import { ListStateStorage } from '@sentio/runtime'
8
+ import {
9
+ AptosFetchConfig,
10
+ Data_AptCall,
11
+ Data_AptEvent,
12
+ Data_AptResource,
13
+ HandleInterval,
14
+ ProcessResult,
15
+ } from '@sentio/protos'
16
+ import { ServerError, Status } from 'nice-grpc'
17
+
18
+ type IndexConfigure = {
19
+ address: string
20
+ network: AptosNetwork
21
+ startVersion: bigint
22
+ // endSeqNumber?: Long
23
+ }
24
+
25
+ // TODO extends ArgumentsFilter
26
+ export interface EventFilter {
27
+ type: string
28
+ account?: string
29
+ }
30
+
31
+ export interface FunctionNameAndCallFilter extends CallFilter {
32
+ function: string
33
+ }
34
+
35
+ // TODO extends ArgumentsFilter
36
+ export interface CallFilter {
37
+ includeFailed?: boolean
38
+ typeArguments?: string[]
39
+ }
40
+
41
+ export interface ArgumentsFilter {
42
+ arguments?: string[]
43
+ }
44
+
45
+ class EventHandler {
46
+ filters: EventFilter[]
47
+ handler: (event: Data_AptEvent) => Promise<ProcessResult>
48
+ fetchConfig: AptosFetchConfig
49
+ }
50
+
51
+ class CallHandler {
52
+ filters: FunctionNameAndCallFilter[]
53
+ handler: (call: Data_AptCall) => Promise<ProcessResult>
54
+ fetchConfig: AptosFetchConfig
55
+ }
56
+
57
+ class ResourceHandlder {
58
+ type?: string
59
+ versionInterval?: HandleInterval
60
+ timeIntervalInMinutes?: HandleInterval
61
+ handler: (resource: Data_AptResource) => Promise<ProcessResult>
62
+ }
63
+
64
+ export class AptosProcessorState extends ListStateStorage<AptosBaseProcessor> {
65
+ static INSTANCE = new AptosProcessorState()
66
+ }
67
+
68
+ export class AptosBaseProcessor {
69
+ readonly moduleName: string
70
+ config: IndexConfigure
71
+ eventHandlers: EventHandler[] = []
72
+ callHandlers: CallHandler[] = []
73
+
74
+ constructor(moduleName: string, options: AptosBindOptions) {
75
+ this.moduleName = moduleName
76
+ this.config = configure(options)
77
+ AptosProcessorState.INSTANCE.addValue(this)
78
+ this.loadTypes(MOVE_CODER)
79
+ }
80
+
81
+ // getABI(): MoveModule | undefined {
82
+ // return undefined
83
+ // }
84
+
85
+ public onTransaction(
86
+ handler: (transaction: Transaction_UserTransaction, ctx: AptosContext) => void,
87
+ includedFailed = false,
88
+ fetchConfig?: AptosFetchConfig
89
+ ): AptosBaseProcessor {
90
+ const _fetchConfig = fetchConfig || AptosFetchConfig.fromPartial({})
91
+
92
+ // const address = this.config.address
93
+ // const moduleName = this.moduleName
94
+ const processor = this
95
+ this.callHandlers.push({
96
+ handler: async function (data) {
97
+ if (!data.transaction) {
98
+ throw new ServerError(Status.INVALID_ARGUMENT, 'call is null')
99
+ }
100
+ const call = data.transaction as Transaction_UserTransaction
101
+ const ctx = new AptosContext(
102
+ processor.moduleName,
103
+ processor.config.network,
104
+ processor.config.address,
105
+ BigInt(data.transaction.version),
106
+ call
107
+ )
108
+ await handler(call, ctx)
109
+ return ctx.getProcessResult()
110
+ },
111
+ filters: [{ function: '', includeFailed: includedFailed }],
112
+ fetchConfig: _fetchConfig,
113
+ })
114
+ return this
115
+ }
116
+
117
+ public onEvent(
118
+ handler: (event: EventInstance, ctx: AptosContext) => void,
119
+ filter: EventFilter | EventFilter[],
120
+ fetchConfig?: AptosFetchConfig
121
+ ): AptosBaseProcessor {
122
+ let _filters: EventFilter[] = []
123
+ const _fetchConfig = fetchConfig || AptosFetchConfig.fromPartial({})
124
+
125
+ if (Array.isArray(filter)) {
126
+ _filters = filter
127
+ } else {
128
+ _filters.push(filter)
129
+ }
130
+
131
+ // const address = this.config.address
132
+ // const moduleName = this.moduleName
133
+
134
+ const processor = this
135
+
136
+ this.eventHandlers.push({
137
+ handler: async function (data) {
138
+ if (!data.transaction) {
139
+ throw new ServerError(Status.INVALID_ARGUMENT, 'event is null')
140
+ }
141
+ const txn = data.transaction as Transaction_UserTransaction
142
+ if (!txn.events.length) {
143
+ throw new ServerError(Status.INVALID_ARGUMENT, 'no event in the transactions')
144
+ }
145
+
146
+ const ctx = new AptosContext(
147
+ processor.moduleName,
148
+ processor.config.network,
149
+ processor.config.address,
150
+ BigInt(txn.version),
151
+ txn
152
+ )
153
+
154
+ const events = txn.events
155
+ txn.events = []
156
+ for (const evt of events) {
157
+ const eventInstance = evt as EventInstance
158
+ const decoded = MOVE_CODER.decodeEvent<any>(eventInstance)
159
+ await handler(decoded || eventInstance, ctx)
160
+ }
161
+
162
+ return ctx.getProcessResult()
163
+ },
164
+ filters: _filters,
165
+ fetchConfig: _fetchConfig,
166
+ })
167
+ return this
168
+ }
169
+
170
+ public onEntryFunctionCall(
171
+ handler: (call: TransactionPayload_EntryFunctionPayload, ctx: AptosContext) => void,
172
+ filter: FunctionNameAndCallFilter | FunctionNameAndCallFilter[],
173
+ fetchConfig?: AptosFetchConfig
174
+ ): AptosBaseProcessor {
175
+ let _filters: FunctionNameAndCallFilter[] = []
176
+ const _fetchConfig = fetchConfig || AptosFetchConfig.fromPartial({})
177
+
178
+ if (Array.isArray(filter)) {
179
+ _filters = filter
180
+ } else {
181
+ _filters.push(filter)
182
+ }
183
+
184
+ // const address = this.config.address
185
+ // const moduleName = this.moduleName
186
+ const processor = this
187
+
188
+ this.callHandlers.push({
189
+ handler: async function (data) {
190
+ if (!data.transaction) {
191
+ throw new ServerError(Status.INVALID_ARGUMENT, 'call is null')
192
+ }
193
+ const tx = data.transaction as Transaction_UserTransaction
194
+
195
+ const ctx = new AptosContext(
196
+ processor.moduleName,
197
+ processor.config.network,
198
+ processor.config.address,
199
+ BigInt(tx.version),
200
+ tx
201
+ )
202
+ if (tx) {
203
+ const payload = tx.payload as TransactionPayload_EntryFunctionPayload
204
+ const decoded = MOVE_CODER.decodeFunctionPayload(payload)
205
+ await handler(decoded, ctx)
206
+ }
207
+ return ctx.getProcessResult()
208
+ },
209
+ filters: _filters,
210
+ fetchConfig: _fetchConfig,
211
+ })
212
+ return this
213
+ }
214
+
215
+ getChainId(): string {
216
+ return getChainId(this.config.network)
217
+ }
218
+
219
+ loadTypes(registry: MoveCoder) {
220
+ if (registry.contains(this.config.address, this.moduleName)) {
221
+ return
222
+ }
223
+ this.loadTypesInternal(registry)
224
+ }
225
+
226
+ protected loadTypesInternal(registry: MoveCoder) {
227
+ // should be override by subclass
228
+ console.log('')
229
+ }
230
+ }
231
+
232
+ export class AptosAccountProcessorState extends ListStateStorage<AptosAccountProcessor> {
233
+ static INSTANCE = new AptosAccountProcessorState()
234
+ }
235
+
236
+ export class AptosAccountProcessor {
237
+ config: IndexConfigure
238
+
239
+ resourcesHandlers: ResourceHandlder[] = []
240
+
241
+ static bind(options: AptosBindOptions): AptosAccountProcessor {
242
+ return new AptosAccountProcessor(options)
243
+ }
244
+
245
+ protected constructor(options: AptosBindOptions) {
246
+ this.config = configure(options)
247
+ AptosAccountProcessorState.INSTANCE.addValue(this)
248
+ }
249
+
250
+ getChainId(): string {
251
+ return getChainId(this.config.network)
252
+ }
253
+
254
+ private onInterval(
255
+ handler: (resources: MoveResource[], ctx: AptosResourceContext) => void,
256
+ timeInterval: HandleInterval | undefined,
257
+ versionInterval: HandleInterval | undefined,
258
+ type: string | undefined
259
+ ) {
260
+ const processor = this
261
+ this.resourcesHandlers.push({
262
+ handler: async function (data) {
263
+ if (data.timestampMicros > Number.MAX_SAFE_INTEGER) {
264
+ throw new ServerError(Status.INVALID_ARGUMENT, 'timestamp is too large')
265
+ }
266
+ const timestamp = Number(data.timestampMicros)
267
+
268
+ const ctx = new AptosResourceContext(
269
+ processor.config.network,
270
+ processor.config.address,
271
+ data.version,
272
+ timestamp
273
+ )
274
+ await handler(data.resources as MoveResource[], ctx)
275
+ return ctx.getProcessResult()
276
+ },
277
+ timeIntervalInMinutes: timeInterval,
278
+ versionInterval: versionInterval,
279
+ type: type,
280
+ })
281
+ return this
282
+ }
283
+
284
+ public onTimeInterval(
285
+ handler: (resources: MoveResource[], ctx: AptosResourceContext) => void,
286
+ timeIntervalInMinutes = 60,
287
+ backfillTimeIntervalInMinutes = 240,
288
+ type?: string
289
+ ) {
290
+ return this.onInterval(
291
+ handler,
292
+ {
293
+ recentInterval: timeIntervalInMinutes,
294
+ backfillInterval: backfillTimeIntervalInMinutes,
295
+ },
296
+ undefined,
297
+ type
298
+ )
299
+ }
300
+
301
+ public onVersionInterval(
302
+ handler: (resources: MoveResource[], ctx: AptosResourceContext) => void,
303
+ versionInterval = 100000,
304
+ backfillVersionInterval = 400000,
305
+ typePrefix?: string
306
+ ) {
307
+ return this.onInterval(
308
+ handler,
309
+ undefined,
310
+ { recentInterval: versionInterval, backfillInterval: backfillVersionInterval },
311
+ typePrefix
312
+ )
313
+ }
314
+ }
315
+
316
+ function configure(options: AptosBindOptions): IndexConfigure {
317
+ let startVersion = 0n
318
+ if (options.startVersion !== undefined) {
319
+ if (typeof options.startVersion === 'number') {
320
+ startVersion = BigInt(options.startVersion)
321
+ } else {
322
+ startVersion = options.startVersion
323
+ }
324
+ }
325
+
326
+ return {
327
+ startVersion: startVersion,
328
+ address: options.address,
329
+ network: options.network || AptosNetwork.MAIN_NET,
330
+ }
331
+ }