@sentio/sdk 1.20.1 → 1.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. package/lib/aptos/aptos-processor.d.ts +17 -12
  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 -18
  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 +2502 -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 +461 -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 +23 -12
  92. package/lib/gen/processor/protos/processor.js +129 -57
  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 +13 -12
  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 +142 -6
  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 +88 -20
  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 -19
  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 +3760 -0
  146. package/src/builtin/aptos/0x3.ts +798 -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 +158 -68
  167. package/src/index.ts +1 -2
  168. package/src/processor-state.ts +1 -1
  169. package/src/service.ts +22 -15
  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 +424 -42
  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
@@ -5,13 +5,15 @@ import {
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,18 +22,25 @@ type IndexConfigure = {
20
22
  // endSeqNumber?: Long
21
23
  }
22
24
 
23
- export interface EventFilter {
25
+ export interface EventFilter extends ArgumentsFilter {
24
26
  type: string
25
27
  }
26
28
 
27
- export interface CallFilter {
29
+ export interface CallFilter extends CallArgumentsFilter {
28
30
  function: string
31
+ }
32
+
33
+ export interface CallArgumentsFilter extends ArgumentsFilter {
29
34
  typeArguments?: string[]
30
35
  }
31
36
 
37
+ export interface ArgumentsFilter {
38
+ arguments?: string[]
39
+ }
40
+
32
41
  class EventHandler {
33
42
  filters: EventFilter[]
34
- handler: (event: Event) => Promise<ProcessResult>
43
+ handler: (event: EventInstance) => Promise<ProcessResult>
35
44
  }
36
45
 
37
46
  class CallHandler {
@@ -40,28 +49,29 @@ class CallHandler {
40
49
  }
41
50
 
42
51
  export class AptosBaseProcessor {
43
- name: string
52
+ readonly moduleName: string
44
53
  config: IndexConfigure
45
54
  eventHandlers: EventHandler[] = []
46
55
  callHandlers: CallHandler[] = []
47
56
 
48
- constructor(options: AptosBindOptions) {
49
- this.name = options.name || options.address
57
+ constructor(moduleName: string, options: AptosBindOptions) {
58
+ this.moduleName = moduleName
50
59
  this.configure(options)
51
60
  global.PROCESSOR_STATE.aptosProcessors.push(this)
52
61
  }
53
62
 
54
- static bind(options: AptosBindOptions): AptosBaseProcessor {
55
- return new AptosBaseProcessor(options)
56
- }
63
+ // getABI(): MoveModule | undefined {
64
+ // return undefined
65
+ // }
57
66
 
58
67
  public onTransaction(
59
68
  handler: (transaction: Transaction_UserTransaction, ctx: AptosContext) => void
60
69
  ): AptosBaseProcessor {
61
70
  const address = this.config.address
71
+ const moduleName = this.moduleName
62
72
  this.callHandlers.push({
63
73
  handler: async function (tx) {
64
- const ctx = new AptosContext(address, Long.fromString(tx.version), tx)
74
+ const ctx = new AptosContext(moduleName, address, Long.fromString(tx.version), tx)
65
75
  if (tx) {
66
76
  handler(tx, ctx)
67
77
  }
@@ -77,7 +87,7 @@ export class AptosBaseProcessor {
77
87
  }
78
88
 
79
89
  public onEvent(
80
- handler: (event: Event, ctx: AptosContext) => void,
90
+ handler: (event: EventInstance, ctx: AptosContext) => void,
81
91
  filter: EventFilter | EventFilter[]
82
92
  ): AptosBaseProcessor {
83
93
  let _filters: EventFilter[] = []
@@ -88,13 +98,18 @@ export class AptosBaseProcessor {
88
98
  _filters.push(filter)
89
99
  }
90
100
 
91
- const address = this.config.address
101
+ // const address = this.config.address
102
+ // const moduleName = this.moduleName
103
+
104
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
105
+ const processor = this
92
106
 
93
107
  this.eventHandlers.push({
94
108
  handler: async function (event) {
95
- const ctx = new AptosContext(address, Long.fromString(event.version))
109
+ const ctx = new AptosContext(processor.moduleName, processor.config.address, Long.fromString(event.version))
96
110
  if (event) {
97
- handler(event, ctx)
111
+ const decoded = processor.decodeEvent(event)
112
+ handler(decoded, ctx)
98
113
  }
99
114
  return {
100
115
  gauges: ctx.gauges,
@@ -119,14 +134,18 @@ export class AptosBaseProcessor {
119
134
  _filters.push(filter)
120
135
  }
121
136
 
122
- const address = this.config.address
137
+ // const address = this.config.address
138
+ // const moduleName = this.moduleName
139
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
140
+ const processor = this
123
141
 
124
142
  this.callHandlers.push({
125
143
  handler: async function (tx) {
126
- const ctx = new AptosContext(address, Long.fromString(tx.version), tx)
144
+ const ctx = new AptosContext(processor.moduleName, processor.config.address, Long.fromString(tx.version), tx)
127
145
  if (tx) {
128
146
  const payload = tx.payload as TransactionPayload_EntryFunctionPayload
129
- handler(payload, ctx)
147
+ const decoded = processor.decodeFunctionPayload(payload)
148
+ handler(decoded, ctx)
130
149
  }
131
150
  return {
132
151
  gauges: ctx.gauges,
@@ -160,4 +179,53 @@ export class AptosBaseProcessor {
160
179
  return APTOS_MAINNET_ID
161
180
  }
162
181
  }
182
+
183
+ loadTypes(registry: TypeRegistry) {
184
+ if (registry.contains(this.config.address, this.moduleName)) {
185
+ return
186
+ }
187
+ this.loadTypesInternal(registry)
188
+ }
189
+
190
+ protected loadTypesInternal(registry: TypeRegistry) {
191
+ // should be override by subclass
192
+ }
193
+
194
+ private decodeEvent(event: EventInstance): EventInstance {
195
+ const registry = GLOBAL_TYPE_REGISTRY
196
+ this.loadTypes(registry)
197
+ // TODO check if module is not loaded
198
+
199
+ let dataTyped = undefined
200
+ try {
201
+ dataTyped = registry.decode(event.data, parseMoveType(event.type))
202
+ } catch (e) {
203
+ console.warn('Decoding error for ', event.type)
204
+ return event
205
+ }
206
+
207
+ return { ...event, data_typed: dataTyped } as TypedEventInstance<any>
208
+ }
209
+
210
+ private decodeFunctionPayload(
211
+ payload: TransactionPayload_EntryFunctionPayload
212
+ ): TransactionPayload_EntryFunctionPayload {
213
+ const registry = GLOBAL_TYPE_REGISTRY
214
+ this.loadTypes(registry)
215
+ const argumentsTyped: any[] = []
216
+
217
+ try {
218
+ const func = registry.getMoveFunction(payload.function)
219
+ for (const [idx, arg] of payload.arguments.entries()) {
220
+ // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
221
+ const argType = parseMoveType(func.params[idx + 1])
222
+ argumentsTyped.push(registry.decode(arg, argType))
223
+ }
224
+ } catch (e) {
225
+ console.warn('Decoding error for ', payload.function)
226
+ return payload
227
+ }
228
+
229
+ return { ...payload, arguments_typed: argumentsTyped } as TypedEntryFunctionPayload<any>
230
+ }
163
231
  }
@@ -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 { CallFilter, EventFilter } 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
+ }