@sentio/sdk 1.20.2 → 1.21.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 (189) hide show
  1. package/lib/aptos/aptos-processor.d.ts +16 -15
  2. package/lib/aptos/aptos-processor.js +65 -13
  3. package/lib/aptos/aptos-processor.js.map +1 -1
  4. package/lib/aptos/bind-options.d.ts +0 -6
  5. package/lib/aptos/bind-options.js +1 -8
  6. package/lib/aptos/bind-options.js.map +1 -1
  7. package/lib/aptos/context.d.ts +5 -4
  8. package/lib/aptos/context.js +9 -6
  9. package/lib/aptos/context.js.map +1 -1
  10. package/lib/aptos/index.d.ts +3 -2
  11. package/lib/aptos/index.js +1 -2
  12. package/lib/aptos/index.js.map +1 -1
  13. package/lib/aptos/runtime.d.ts +2 -0
  14. package/lib/aptos/runtime.js +16 -0
  15. package/lib/aptos/runtime.js.map +1 -0
  16. package/lib/aptos/types.d.ts +28 -0
  17. package/lib/aptos/types.js +159 -0
  18. package/lib/aptos/types.js.map +1 -0
  19. package/lib/aptos/utils.d.ts +6 -0
  20. package/lib/aptos/utils.js +19 -0
  21. package/lib/aptos/utils.js.map +1 -0
  22. package/lib/aptos-codegen/codegen.d.ts +5 -2
  23. package/lib/aptos-codegen/codegen.js +195 -17
  24. package/lib/aptos-codegen/codegen.js.map +1 -1
  25. package/lib/aptos-codegen/typegen.d.ts +18 -0
  26. package/lib/aptos-codegen/typegen.js +196 -0
  27. package/lib/aptos-codegen/typegen.js.map +1 -0
  28. package/lib/binds.d.ts +2 -3
  29. package/lib/binds.js +6 -33
  30. package/lib/binds.js.map +1 -1
  31. package/lib/builtin/aptos/0x1.d.ts +1624 -0
  32. package/lib/builtin/aptos/0x1.js +2570 -0
  33. package/lib/builtin/aptos/0x1.js.map +1 -0
  34. package/lib/builtin/aptos/0x3.d.ts +414 -0
  35. package/lib/builtin/aptos/0x3.js +474 -0
  36. package/lib/builtin/aptos/0x3.js.map +1 -0
  37. package/lib/builtin/internal/eacaggregatorproxy_processor.d.ts +2 -2
  38. package/lib/builtin/internal/eacaggregatorproxy_processor.js +12 -11
  39. package/lib/builtin/internal/eacaggregatorproxy_processor.js.map +1 -1
  40. package/lib/builtin/internal/erc20_processor.d.ts +2 -2
  41. package/lib/builtin/internal/erc20_processor.js +12 -11
  42. package/lib/builtin/internal/erc20_processor.js.map +1 -1
  43. package/lib/builtin/internal/erc20bytes_processor.d.ts +2 -2
  44. package/lib/builtin/internal/erc20bytes_processor.js +12 -11
  45. package/lib/builtin/internal/erc20bytes_processor.js.map +1 -1
  46. package/lib/builtin/internal/weth9_processor.d.ts +2 -2
  47. package/lib/builtin/internal/weth9_processor.js +12 -11
  48. package/lib/builtin/internal/weth9_processor.js.map +1 -1
  49. package/lib/builtin/solana/types.d.ts +5 -5
  50. package/lib/builtin/solana/wormhole-processor.d.ts +6 -6
  51. package/lib/builtin/solana/wormhole-processor.js +15 -12
  52. package/lib/builtin/solana/wormhole-processor.js.map +1 -1
  53. package/lib/cli/build.js +17 -16
  54. package/lib/cli/build.js.map +1 -1
  55. package/lib/core/base-processor-template.d.ts +7 -7
  56. package/lib/core/base-processor-template.js.map +1 -1
  57. package/lib/core/base-processor.d.ts +5 -6
  58. package/lib/core/base-processor.js +6 -4
  59. package/lib/core/base-processor.js.map +1 -1
  60. package/lib/core/big-decimal.d.ts +1 -0
  61. package/lib/core/big-decimal.js +6 -0
  62. package/lib/core/big-decimal.js.map +1 -0
  63. package/lib/core/bind-options.d.ts +0 -2
  64. package/lib/core/bind-options.js +4 -4
  65. package/lib/core/bind-options.js.map +1 -1
  66. package/lib/core/context.d.ts +14 -11
  67. package/lib/core/context.js +28 -19
  68. package/lib/core/context.js.map +1 -1
  69. package/lib/core/generic-processor.js +5 -5
  70. package/lib/core/generic-processor.js.map +1 -1
  71. package/lib/core/index.d.ts +5 -5
  72. package/lib/core/index.js +6 -6
  73. package/lib/core/index.js.map +1 -1
  74. package/lib/core/logger.d.ts +2 -3
  75. package/lib/core/logger.js +6 -6
  76. package/lib/core/logger.js.map +1 -1
  77. package/lib/core/metadata.d.ts +7 -4
  78. package/lib/core/metadata.js +13 -10
  79. package/lib/core/metadata.js.map +1 -1
  80. package/lib/core/meter.d.ts +4 -7
  81. package/lib/core/meter.js +12 -13
  82. package/lib/core/meter.js.map +1 -1
  83. package/lib/core/solana-processor.d.ts +7 -4
  84. package/lib/core/solana-processor.js +14 -20
  85. package/lib/core/solana-processor.js.map +1 -1
  86. package/lib/core/sui-processor.d.ts +5 -4
  87. package/lib/core/sui-processor.js +10 -17
  88. package/lib/core/sui-processor.js.map +1 -1
  89. package/lib/error.d.ts +2 -2
  90. package/lib/error.js.map +1 -1
  91. package/lib/gen/processor/protos/processor.d.ts +24 -12
  92. package/lib/gen/processor/protos/processor.js +147 -58
  93. package/lib/gen/processor/protos/processor.js.map +1 -1
  94. package/lib/index.d.ts +1 -2
  95. package/lib/index.js +1 -4
  96. package/lib/index.js.map +1 -1
  97. package/lib/processor-state.d.ts +1 -1
  98. package/lib/processor-state.js.map +1 -1
  99. package/lib/service.js +15 -13
  100. package/lib/service.js.map +1 -1
  101. package/lib/target-ethers-sentio/codegen.js +14 -15
  102. package/lib/target-ethers-sentio/codegen.js.map +1 -1
  103. package/lib/test-abi-code-gen.js.map +1 -1
  104. package/lib/testing/metric-utils.js +2 -2
  105. package/lib/testing/metric-utils.js.map +1 -1
  106. package/lib/tests/aptos.test.js +95 -2
  107. package/lib/tests/aptos.test.js.map +1 -1
  108. package/lib/tests/erc20.js +1 -1
  109. package/lib/tests/erc20.js.map +1 -1
  110. package/lib/tests/erc20.test.js +3 -4
  111. package/lib/tests/erc20.test.js.map +1 -1
  112. package/lib/tests/solana.test.js +5 -1
  113. package/lib/tests/solana.test.js.map +1 -1
  114. package/lib/tests/souffl3.js +17 -4
  115. package/lib/tests/souffl3.js.map +1 -1
  116. package/lib/tests/sui.test.js +1 -4
  117. package/lib/tests/sui.test.js.map +1 -1
  118. package/lib/tests/types/aptos/souffle.d.ts +311 -22
  119. package/lib/tests/types/aptos/souffle.js +172 -20
  120. package/lib/tests/types/aptos/souffle.js.map +1 -1
  121. package/lib/tests/types/solana/basic_1_processor.d.ts +2 -2
  122. package/lib/tests/types/solana/basic_1_processor.js +4 -4
  123. package/lib/tests/types/solana/basic_1_processor.js.map +1 -1
  124. package/lib/tests/wormhole-token-bridge.js +2 -1
  125. package/lib/tests/wormhole-token-bridge.js.map +1 -1
  126. package/lib/utils/chain.d.ts +1 -0
  127. package/lib/utils/chain.js +15 -1
  128. package/lib/utils/chain.js.map +1 -1
  129. package/lib/utils/conversion.d.ts +1 -1
  130. package/lib/utils/conversion.js.map +1 -1
  131. package/package.json +3 -2
  132. package/src/abis/aptos/0x1.json +9205 -0
  133. package/src/abis/aptos/0x3.json +1515 -0
  134. package/src/aptos/aptos-processor.ts +89 -25
  135. package/src/aptos/bind-options.ts +0 -7
  136. package/src/aptos/context.ts +11 -8
  137. package/src/aptos/index.ts +3 -2
  138. package/src/aptos/runtime.ts +13 -0
  139. package/src/aptos/types.ts +203 -0
  140. package/src/aptos/utils.ts +18 -0
  141. package/src/aptos-codegen/codegen.ts +222 -18
  142. package/src/aptos-codegen/typegen.test.ts +29 -0
  143. package/src/aptos-codegen/typegen.ts +216 -0
  144. package/src/binds.ts +5 -39
  145. package/src/builtin/aptos/0x1.ts +3917 -0
  146. package/src/builtin/aptos/0x3.ts +824 -0
  147. package/src/builtin/internal/eacaggregatorproxy_processor.ts +14 -31
  148. package/src/builtin/internal/erc20_processor.ts +14 -25
  149. package/src/builtin/internal/erc20bytes_processor.ts +14 -25
  150. package/src/builtin/internal/weth9_processor.ts +14 -25
  151. package/src/builtin/solana/wormhole-processor.ts +21 -18
  152. package/src/cli/build.ts +19 -17
  153. package/src/core/base-processor-template.ts +7 -7
  154. package/src/core/base-processor.ts +30 -9
  155. package/src/core/big-decimal.ts +1 -0
  156. package/src/core/bind-options.ts +3 -2
  157. package/src/core/context.ts +40 -24
  158. package/src/core/generic-processor.ts +6 -7
  159. package/src/core/index.ts +5 -5
  160. package/src/core/logger.ts +7 -7
  161. package/src/core/metadata.ts +14 -12
  162. package/src/core/meter.ts +12 -14
  163. package/src/core/solana-processor.ts +24 -21
  164. package/src/core/sui-processor.ts +10 -21
  165. package/src/error.ts +2 -2
  166. package/src/gen/processor/protos/processor.ts +177 -69
  167. package/src/index.ts +1 -2
  168. package/src/processor-state.ts +1 -1
  169. package/src/service.ts +23 -16
  170. package/src/target-ethers-sentio/codegen.ts +14 -15
  171. package/src/test-abi-code-gen.ts +1 -0
  172. package/src/testing/metric-utils.ts +2 -2
  173. package/src/tests/aptos.test.ts +102 -3
  174. package/src/tests/erc20.test.ts +3 -4
  175. package/src/tests/erc20.ts +1 -1
  176. package/src/tests/solana.test.ts +5 -1
  177. package/src/tests/souffl3.ts +21 -6
  178. package/src/tests/sui.test.ts +1 -4
  179. package/src/tests/types/aptos/souffle.ts +470 -56
  180. package/src/tests/types/solana/basic_1_processor.ts +6 -6
  181. package/src/tests/wormhole-token-bridge.ts +2 -1
  182. package/src/types/global.d.ts +1 -1
  183. package/src/utils/chain.ts +14 -0
  184. package/src/utils/conversion.ts +1 -1
  185. package/lib/contract-namer.d.ts +0 -6
  186. package/lib/contract-namer.js +0 -20
  187. package/lib/contract-namer.js.map +0 -1
  188. package/src/aptos-codegen/codgen.test.ts +0 -11
  189. package/src/contract-namer.ts +0 -17
@@ -1,17 +1,19 @@
1
- import { ProcessResult } from '../gen'
1
+ import { ProcessResult, RecordMetaData } from '../gen'
2
2
  import {
3
3
  AptosBindOptions,
4
4
  AptosContext,
5
5
  AptosNetwork,
6
6
  Transaction_UserTransaction,
7
7
  TransactionPayload_EntryFunctionPayload,
8
+ TypedEntryFunctionPayload,
9
+ TypedEventInstance,
10
+ TypeRegistry,
8
11
  } from '.'
9
- import type { Event as OldEvent } from 'aptos/src/generated'
10
12
 
11
13
  import Long from 'long'
12
14
  import { APTOS_MAINNET_ID, APTOS_TESTNET_ID } from '../utils/chain'
13
-
14
- export type Event = OldEvent & { version: string }
15
+ import { EventInstance, GLOBAL_TYPE_REGISTRY } from './types'
16
+ import { parseMoveType } from '../aptos-codegen/typegen'
15
17
 
16
18
  type IndexConfigure = {
17
19
  address: string
@@ -20,15 +22,18 @@ type IndexConfigure = {
20
22
  // endSeqNumber?: Long
21
23
  }
22
24
 
23
- export interface EventFilter extends ArgumentsFilter {
25
+ // TODO extends ArgumentsFilter
26
+ export interface EventFilter {
24
27
  type: string
25
28
  }
26
29
 
27
- export interface CallFilter extends CallArgumentsFilter {
30
+ export interface FunctionNameAndCallFilter extends CallFilter {
28
31
  function: string
29
32
  }
30
33
 
31
- export interface CallArgumentsFilter extends ArgumentsFilter {
34
+ // TODO extends ArgumentsFilter
35
+ export interface CallFilter {
36
+ includeFailed?: boolean
32
37
  typeArguments?: string[]
33
38
  }
34
39
 
@@ -38,37 +43,38 @@ export interface ArgumentsFilter {
38
43
 
39
44
  class EventHandler {
40
45
  filters: EventFilter[]
41
- handler: (event: Event) => Promise<ProcessResult>
46
+ handler: (event: EventInstance) => Promise<ProcessResult>
42
47
  }
43
48
 
44
49
  class CallHandler {
45
- filters: CallFilter[]
50
+ filters: FunctionNameAndCallFilter[]
46
51
  handler: (call: Transaction_UserTransaction) => Promise<ProcessResult>
47
52
  }
48
53
 
49
54
  export class AptosBaseProcessor {
50
- name: string
55
+ readonly moduleName: string
51
56
  config: IndexConfigure
52
57
  eventHandlers: EventHandler[] = []
53
58
  callHandlers: CallHandler[] = []
54
59
 
55
- constructor(options: AptosBindOptions) {
56
- this.name = options.name || options.address
60
+ constructor(moduleName: string, options: AptosBindOptions) {
61
+ this.moduleName = moduleName
57
62
  this.configure(options)
58
63
  global.PROCESSOR_STATE.aptosProcessors.push(this)
59
64
  }
60
65
 
61
- static bind(options: AptosBindOptions): AptosBaseProcessor {
62
- return new AptosBaseProcessor(options)
63
- }
66
+ // getABI(): MoveModule | undefined {
67
+ // return undefined
68
+ // }
64
69
 
65
70
  public onTransaction(
66
71
  handler: (transaction: Transaction_UserTransaction, ctx: AptosContext) => void
67
72
  ): AptosBaseProcessor {
68
73
  const address = this.config.address
74
+ const moduleName = this.moduleName
69
75
  this.callHandlers.push({
70
76
  handler: async function (tx) {
71
- const ctx = new AptosContext(address, Long.fromString(tx.version), tx)
77
+ const ctx = new AptosContext(moduleName, address, Long.fromString(tx.version), tx)
72
78
  if (tx) {
73
79
  handler(tx, ctx)
74
80
  }
@@ -84,7 +90,7 @@ export class AptosBaseProcessor {
84
90
  }
85
91
 
86
92
  public onEvent(
87
- handler: (event: Event, ctx: AptosContext) => void,
93
+ handler: (event: EventInstance, ctx: AptosContext) => void,
88
94
  filter: EventFilter | EventFilter[]
89
95
  ): AptosBaseProcessor {
90
96
  let _filters: EventFilter[] = []
@@ -95,13 +101,18 @@ export class AptosBaseProcessor {
95
101
  _filters.push(filter)
96
102
  }
97
103
 
98
- const address = this.config.address
104
+ // const address = this.config.address
105
+ // const moduleName = this.moduleName
106
+
107
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
108
+ const processor = this
99
109
 
100
110
  this.eventHandlers.push({
101
111
  handler: async function (event) {
102
- const ctx = new AptosContext(address, Long.fromString(event.version))
112
+ const ctx = new AptosContext(processor.moduleName, processor.config.address, Long.fromString(event.version))
103
113
  if (event) {
104
- handler(event, ctx)
114
+ const decoded = processor.decodeEvent(event)
115
+ handler(decoded, ctx)
105
116
  }
106
117
  return {
107
118
  gauges: ctx.gauges,
@@ -116,9 +127,9 @@ export class AptosBaseProcessor {
116
127
 
117
128
  public onEntryFunctionCall(
118
129
  handler: (call: TransactionPayload_EntryFunctionPayload, ctx: AptosContext) => void,
119
- filter: CallFilter | CallFilter[]
130
+ filter: FunctionNameAndCallFilter | FunctionNameAndCallFilter[]
120
131
  ): AptosBaseProcessor {
121
- let _filters: CallFilter[] = []
132
+ let _filters: FunctionNameAndCallFilter[] = []
122
133
 
123
134
  if (Array.isArray(filter)) {
124
135
  _filters = filter
@@ -126,14 +137,18 @@ export class AptosBaseProcessor {
126
137
  _filters.push(filter)
127
138
  }
128
139
 
129
- const address = this.config.address
140
+ // const address = this.config.address
141
+ // const moduleName = this.moduleName
142
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
143
+ const processor = this
130
144
 
131
145
  this.callHandlers.push({
132
146
  handler: async function (tx) {
133
- const ctx = new AptosContext(address, Long.fromString(tx.version), tx)
147
+ const ctx = new AptosContext(processor.moduleName, processor.config.address, Long.fromString(tx.version), tx)
134
148
  if (tx) {
135
149
  const payload = tx.payload as TransactionPayload_EntryFunctionPayload
136
- handler(payload, ctx)
150
+ const decoded = processor.decodeFunctionPayload(payload)
151
+ handler(decoded, ctx)
137
152
  }
138
153
  return {
139
154
  gauges: ctx.gauges,
@@ -167,4 +182,53 @@ export class AptosBaseProcessor {
167
182
  return APTOS_MAINNET_ID
168
183
  }
169
184
  }
185
+
186
+ loadTypes(registry: TypeRegistry) {
187
+ if (registry.contains(this.config.address, this.moduleName)) {
188
+ return
189
+ }
190
+ this.loadTypesInternal(registry)
191
+ }
192
+
193
+ protected loadTypesInternal(registry: TypeRegistry) {
194
+ // should be override by subclass
195
+ }
196
+
197
+ private decodeEvent(event: EventInstance): EventInstance {
198
+ const registry = GLOBAL_TYPE_REGISTRY
199
+ this.loadTypes(registry)
200
+ // TODO check if module is not loaded
201
+
202
+ let dataTyped = undefined
203
+ try {
204
+ dataTyped = registry.decode(event.data, parseMoveType(event.type))
205
+ } catch (e) {
206
+ console.warn('Decoding error for ', event.type)
207
+ return event
208
+ }
209
+
210
+ return { ...event, data_typed: dataTyped } as TypedEventInstance<any>
211
+ }
212
+
213
+ private decodeFunctionPayload(
214
+ payload: TransactionPayload_EntryFunctionPayload
215
+ ): TransactionPayload_EntryFunctionPayload {
216
+ const registry = GLOBAL_TYPE_REGISTRY
217
+ this.loadTypes(registry)
218
+ const argumentsTyped: any[] = []
219
+
220
+ try {
221
+ const func = registry.getMoveFunction(payload.function)
222
+ for (const [idx, arg] of payload.arguments.entries()) {
223
+ // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
224
+ const argType = parseMoveType(func.params[idx + 1])
225
+ argumentsTyped.push(registry.decode(arg, argType))
226
+ }
227
+ } catch (e) {
228
+ console.warn('Decoding error for ', payload.function)
229
+ return payload
230
+ }
231
+
232
+ return { ...payload, arguments_typed: argumentsTyped } as TypedEntryFunctionPayload<any>
233
+ }
170
234
  }
@@ -9,13 +9,6 @@ export enum AptosNetwork {
9
9
  export class AptosBindOptions {
10
10
  address: string
11
11
  network?: AptosNetwork = AptosNetwork.TEST_NET
12
- name?: string
13
12
  startVersion?: Long | number
14
13
  // endBlock?: Long | number
15
14
  }
16
-
17
- export class NamedAptosBindOptions {
18
- address: string
19
- network?: AptosNetwork = AptosNetwork.TEST_NET
20
- startVersion?: Long | number
21
- }
@@ -1,5 +1,5 @@
1
1
  import Long from 'long'
2
- import { MetricDescriptor, RecordMetaData } from '../gen'
2
+ import { DataDescriptor, RecordMetaData } from '../gen'
3
3
  import { Labels } from '../core/metadata'
4
4
  import { APTOS_TESTNET_ID } from '../utils/chain'
5
5
  import { normalizeLabels } from '../core/meter'
@@ -8,27 +8,30 @@ import { Transaction_UserTransaction } from './'
8
8
 
9
9
  export class AptosContext extends BaseContext {
10
10
  address: string
11
- blockNumber: Long
11
+ moduleName: string
12
+ version: Long
12
13
  transaction: Transaction_UserTransaction
13
14
 
14
- constructor(address: string, slot: Long, transaction?: Transaction_UserTransaction) {
15
+ constructor(moduleName: string, address: string, version: Long, transaction?: Transaction_UserTransaction) {
15
16
  super()
16
17
  this.address = address
17
- this.blockNumber = slot
18
+ this.moduleName = moduleName
19
+ this.version = version
18
20
  if (transaction) {
19
21
  this.transaction = transaction
20
22
  }
21
23
  }
22
24
 
23
- getMetaData(descriptor: MetricDescriptor | undefined, labels: Labels): RecordMetaData {
25
+ getMetaData(descriptor: DataDescriptor | undefined, labels: Labels): RecordMetaData {
24
26
  return {
25
- contractAddress: this.address,
26
- blockNumber: this.blockNumber,
27
+ address: this.address,
28
+ contractName: this.moduleName,
29
+ blockNumber: this.version,
27
30
  transactionIndex: 0,
28
31
  transactionHash: this.transaction?.hash || '', // TODO
29
32
  logIndex: 0,
30
33
  chainId: APTOS_TESTNET_ID, // TODO set in context
31
- descriptor: descriptor,
34
+ dataDescriptor: descriptor,
32
35
  labels: normalizeLabels(labels),
33
36
  }
34
37
  }
@@ -1,5 +1,6 @@
1
1
  export type { Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from 'aptos/src/generated'
2
- export type { Event, CallFilter, EventFilter } from './aptos-processor'
2
+ export type { EventInstance, TypedEventInstance, TypeRegistry, TypedEntryFunctionPayload } from './types'
3
+ export type { FunctionNameAndCallFilter, EventFilter, CallFilter, ArgumentsFilter } from './aptos-processor'
3
4
  export { AptosBaseProcessor } from './aptos-processor'
4
5
  export { AptosContext } from './context'
5
- export { AptosBindOptions, NamedAptosBindOptions, AptosNetwork } from './bind-options'
6
+ export { AptosBindOptions, AptosNetwork } from './bind-options'
@@ -0,0 +1,13 @@
1
+ import { MoveStruct } from 'aptos/src/generated/models/MoveStruct'
2
+
3
+ export function decode<T>(struct: MoveStruct, args: any[]): T | undefined {
4
+ if (args.length != struct.fields.length) {
5
+ console.log('type mismatch actually data for', struct.name)
6
+ return undefined
7
+ }
8
+ const res: any = {}
9
+ for (const [i, field] of struct.fields.entries()) {
10
+ res[field.name] = args[i]
11
+ }
12
+ return res as T
13
+ }
@@ -0,0 +1,203 @@
1
+ import {
2
+ Event,
3
+ MoveFunction,
4
+ MoveModule,
5
+ MoveStruct,
6
+ TransactionPayload_EntryFunctionPayload,
7
+ } from 'aptos/src/generated'
8
+ import { moduleQname, SPLITTER, VECTOR_STR } from './utils'
9
+ import { parseMoveType } from '../aptos-codegen/typegen'
10
+
11
+ export type EventInstance = Event & {
12
+ version: string
13
+ }
14
+
15
+ export type TypedEventInstance<T> = EventInstance & {
16
+ // Typed data converted from ABI
17
+ // undefined if there is converting error, usually because the ABI/data
18
+ // mismatch
19
+ data_typed: T
20
+ }
21
+
22
+ // Don't use intermedidate type to make IDE happier
23
+ export type TypedEntryFunctionPayload<T extends Array<any>> = TransactionPayload_EntryFunctionPayload & {
24
+ arguments_typed: T
25
+ }
26
+
27
+ export class TypeDescriptor {
28
+ // type: string
29
+
30
+ // qualified name without type parameters
31
+ qname: string
32
+ // account?: string
33
+ // module?: string
34
+
35
+ typeArgs: TypeDescriptor[]
36
+
37
+ constructor(symbol: string, typeParams?: TypeDescriptor[]) {
38
+ this.qname = symbol
39
+ this.typeArgs = typeParams || []
40
+ }
41
+
42
+ // Replace T0, T1 with more concrete type
43
+ applyArgs(ctx: Map<string, TypeDescriptor>): TypeDescriptor {
44
+ const replace = ctx.get(this.qname)
45
+ if (replace) {
46
+ return replace
47
+ }
48
+ if (ctx.size === 0 || this.typeArgs.length === 0) {
49
+ return this
50
+ }
51
+
52
+ const typeArgs: TypeDescriptor[] = []
53
+ for (const arg of this.typeArgs) {
54
+ const replace = ctx.get(arg.qname)
55
+ if (replace) {
56
+ typeArgs.push(replace)
57
+ } else {
58
+ typeArgs.push(arg.applyArgs(ctx))
59
+ }
60
+ }
61
+ return new TypeDescriptor(this.qname, typeArgs)
62
+ }
63
+
64
+ // all depended types including itself, not include system type
65
+ dependedTypes(): string[] {
66
+ if (this.qname.startsWith('&')) {
67
+ return []
68
+ }
69
+ switch (this.qname) {
70
+ case 'signer':
71
+ case 'address':
72
+ case '0x1::string::String':
73
+ case 'bool':
74
+ case 'u8':
75
+ case 'u16':
76
+ case 'u32':
77
+ case 'u64':
78
+ case 'u128':
79
+ return []
80
+ }
81
+
82
+ // Type parameters are not depended
83
+ if (this.qname.indexOf(SPLITTER) == -1) {
84
+ if (this.qname.startsWith('T')) {
85
+ return []
86
+ }
87
+ }
88
+
89
+ const types = new Set<string>()
90
+ for (const param of this.typeArgs) {
91
+ param.dependedTypes().forEach((t) => types.add(t))
92
+ }
93
+
94
+ if (this.qname !== VECTOR_STR) {
95
+ types.add(this.qname)
96
+ }
97
+
98
+ return Array.from(types)
99
+ }
100
+ }
101
+
102
+ export class TypeRegistry {
103
+ moduleMapping = new Map<string, MoveModule>()
104
+ typeMapping = new Map<string, MoveStruct>()
105
+ funcMapping = new Map<string, MoveFunction>()
106
+
107
+ contains(account: string, name: string) {
108
+ return this.moduleMapping.has(account + '::' + name)
109
+ }
110
+
111
+ load(module: MoveModule) {
112
+ if (this.contains(module.address, module.name)) {
113
+ return
114
+ }
115
+ this.moduleMapping.set(moduleQname(module), module)
116
+
117
+ for (const struct of module.structs) {
118
+ // TODO move to util
119
+ const key = [module.address, module.name, struct.name].join(SPLITTER)
120
+ this.typeMapping.set(key, struct)
121
+ }
122
+
123
+ for (const func of module.exposed_functions) {
124
+ if (!func.is_entry) {
125
+ continue
126
+ }
127
+ const key = [module.address, module.name, func.name].join(SPLITTER)
128
+ this.funcMapping.set(key, func)
129
+ }
130
+ }
131
+
132
+ getMoveStruct(type: string): MoveStruct {
133
+ const struct = this.typeMapping.get(type)
134
+ if (!struct) {
135
+ throw new Error('Failed to load type' + type)
136
+ }
137
+ return struct
138
+ }
139
+
140
+ getMoveFunction(type: string): MoveFunction {
141
+ const func = this.funcMapping.get(type)
142
+ if (!func) {
143
+ throw new Error('Failed to load function' + type)
144
+ }
145
+ return func
146
+ }
147
+
148
+ decode(data: any, type: TypeDescriptor): any {
149
+ // process simple type
150
+ if (type.qname.startsWith('&')) {
151
+ return data
152
+ }
153
+ switch (type.qname) {
154
+ case 'signer': // TODO check this
155
+ case 'address':
156
+ case '0x1::string::String':
157
+ case 'bool':
158
+ case 'u8':
159
+ case 'u16':
160
+ case 'u32':
161
+ return data
162
+ case 'u64':
163
+ case 'u128':
164
+ return BigInt(data)
165
+ }
166
+
167
+ // process vector
168
+ if (type.qname === VECTOR_STR) {
169
+ // vector<u8> as hex string
170
+ if (type.typeArgs[0].qname === 'u8') {
171
+ return data
172
+ }
173
+
174
+ const res = []
175
+ for (const entry of data) {
176
+ res.push(this.decode(entry, type.typeArgs[0]))
177
+ }
178
+ return res
179
+ }
180
+
181
+ // Process complex type
182
+ const struct = this.getMoveStruct(type.qname)
183
+
184
+ const typeCtx = new Map<string, TypeDescriptor>()
185
+ for (const [idx, typeArg] of type.typeArgs.entries()) {
186
+ typeCtx.set('T' + idx, typeArg)
187
+ }
188
+
189
+ const typedData: any = {}
190
+
191
+ for (const field of struct.fields) {
192
+ let filedType = parseMoveType(field.type)
193
+ filedType = filedType.applyArgs(typeCtx)
194
+ const value = this.decode(data[field.name], filedType)
195
+ typedData[field.name] = value
196
+ }
197
+ return typedData
198
+ }
199
+
200
+ // private decodeInternal(data: any, type: TypeDescriptor, typeArgs: )
201
+ }
202
+
203
+ export const GLOBAL_TYPE_REGISTRY = new TypeRegistry()
@@ -0,0 +1,18 @@
1
+ import { MoveModule } from 'aptos/src/generated'
2
+
3
+ export const SPLITTER = '::'
4
+
5
+ export const VECTOR_STR = 'vector'
6
+
7
+ export function isFrameworkAccount(account: string) {
8
+ return account === '0x1' || account === '0x2' || account === '0x3'
9
+ }
10
+
11
+ export function moduleQname(module: MoveModule): string {
12
+ return module.address.toLowerCase() + SPLITTER + module.name
13
+ }
14
+
15
+ export function moduleQnameForType(type: string): [string, string] {
16
+ const parts = type.split(SPLITTER).slice(0, 2)
17
+ return [parts[0], parts[1]]
18
+ }