@sentio/sdk 2.4.0 → 2.5.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 (167) hide show
  1. package/lib/aptos/aptos-processor.d.ts +1 -1
  2. package/lib/aptos/aptos-processor.js +1 -1
  3. package/lib/aptos/aptos-processor.js.map +1 -1
  4. package/lib/aptos/builtin/0x1.d.ts +16 -177
  5. package/lib/aptos/builtin/0x1.js +56 -538
  6. package/lib/aptos/builtin/0x1.js.map +1 -1
  7. package/lib/aptos/builtin/0x3.d.ts +2 -13
  8. package/lib/aptos/builtin/0x3.js +20 -48
  9. package/lib/aptos/builtin/0x3.js.map +1 -1
  10. package/lib/aptos/codegen/codegen.d.ts +0 -20
  11. package/lib/aptos/codegen/codegen.js +37 -357
  12. package/lib/aptos/codegen/codegen.js.map +1 -1
  13. package/lib/aptos/{types.test.d.ts → codegen/types.test.d.ts} +0 -0
  14. package/lib/aptos/codegen/types.test.js.map +1 -0
  15. package/lib/aptos/models.d.ts +0 -8
  16. package/lib/aptos/models.js.map +1 -1
  17. package/lib/aptos/move-coder.d.ts +4 -13
  18. package/lib/aptos/move-coder.js +13 -124
  19. package/lib/aptos/move-coder.js.map +1 -1
  20. package/lib/aptos/move-types.d.ts +6 -0
  21. package/lib/aptos/move-types.js +52 -1
  22. package/lib/aptos/move-types.js.map +1 -1
  23. package/lib/aptos/tests/souffl3.js +1 -1
  24. package/lib/aptos/tests/souffl3.js.map +1 -1
  25. package/lib/aptos/tests/types/reserved.d.ts +2 -8
  26. package/lib/aptos/tests/types/reserved.js +11 -24
  27. package/lib/aptos/tests/types/reserved.js.map +1 -1
  28. package/lib/aptos/tests/types/soffl3.d.ts +2 -14
  29. package/lib/aptos/tests/types/soffl3.js +15 -48
  30. package/lib/aptos/tests/types/soffl3.js.map +1 -1
  31. package/lib/aptos/tests/types/souffle.d.ts +2 -8
  32. package/lib/aptos/tests/types/souffle.js +11 -24
  33. package/lib/aptos/tests/types/souffle.js.map +1 -1
  34. package/lib/aptos/utils.d.ts +2 -7
  35. package/lib/aptos/utils.js +2 -15
  36. package/lib/aptos/utils.js.map +1 -1
  37. package/lib/eth/tests/codegen.test.js.map +1 -1
  38. package/lib/eth/tests/erc20.js +1 -1
  39. package/lib/eth/tests/erc20.js.map +1 -1
  40. package/lib/eth/tests/logger.test.js.map +1 -1
  41. package/lib/move/abstract-codegen.d.ts +44 -0
  42. package/lib/move/abstract-codegen.js +341 -0
  43. package/lib/move/abstract-codegen.js.map +1 -0
  44. package/lib/move/abstract-move-coder.d.ts +24 -0
  45. package/lib/move/abstract-move-coder.js +141 -0
  46. package/lib/move/abstract-move-coder.js.map +1 -0
  47. package/lib/move/account.d.ts +18 -0
  48. package/lib/{aptos/codegen/typegen.js → move/account.js} +20 -82
  49. package/lib/move/account.js.map +1 -0
  50. package/lib/move/filter.d.ts +25 -0
  51. package/lib/move/filter.js +11 -0
  52. package/lib/move/filter.js.map +1 -0
  53. package/lib/move/index.d.ts +5 -25
  54. package/lib/move/index.js +5 -10
  55. package/lib/move/index.js.map +1 -1
  56. package/lib/move/neutral-models.d.ts +34 -0
  57. package/lib/move/neutral-models.js +7 -0
  58. package/lib/move/neutral-models.js.map +1 -0
  59. package/lib/move/ts-type.d.ts +2 -0
  60. package/lib/move/ts-type.js +77 -0
  61. package/lib/move/ts-type.js.map +1 -0
  62. package/lib/{aptos/codegen/typegen.test.d.ts → move/ts-type.test.d.ts} +0 -0
  63. package/lib/move/ts-type.test.js.map +1 -0
  64. package/lib/{aptos → move}/types.d.ts +1 -0
  65. package/lib/{aptos → move}/types.js +26 -3
  66. package/lib/move/types.js.map +1 -0
  67. package/lib/move/utils.d.ts +9 -0
  68. package/lib/move/utils.js +20 -0
  69. package/lib/move/utils.js.map +1 -0
  70. package/lib/sui/builtin/0x1.d.ts +114 -0
  71. package/lib/sui/builtin/0x1.js +174 -0
  72. package/lib/sui/builtin/0x1.js.map +1 -0
  73. package/lib/sui/builtin/0x2.d.ts +1047 -0
  74. package/lib/sui/builtin/0x2.js +1295 -0
  75. package/lib/sui/builtin/0x2.js.map +1 -0
  76. package/lib/sui/builtin/index.d.ts +2 -0
  77. package/lib/sui/builtin/index.js +6 -0
  78. package/lib/sui/builtin/index.js.map +1 -0
  79. package/lib/sui/codegen/codegen.d.ts +1 -0
  80. package/lib/sui/codegen/codegen.js +61 -0
  81. package/lib/sui/codegen/codegen.js.map +1 -0
  82. package/lib/sui/codegen/index.d.ts +1 -0
  83. package/lib/sui/codegen/index.js +2 -0
  84. package/lib/sui/codegen/index.js.map +1 -0
  85. package/lib/sui/codegen/run.d.ts +1 -0
  86. package/lib/sui/codegen/run.js +12 -0
  87. package/lib/sui/codegen/run.js.map +1 -0
  88. package/lib/sui/index.d.ts +2 -0
  89. package/lib/sui/index.js +1 -0
  90. package/lib/sui/index.js.map +1 -1
  91. package/lib/sui/models.d.ts +7 -1
  92. package/lib/sui/models.js +0 -23
  93. package/lib/sui/models.js.map +1 -1
  94. package/lib/sui/move-coder.d.ts +11 -0
  95. package/lib/sui/move-coder.js +53 -0
  96. package/lib/sui/move-coder.js.map +1 -0
  97. package/lib/sui/move-types.d.ts +9 -0
  98. package/lib/sui/move-types.js +81 -0
  99. package/lib/sui/move-types.js.map +1 -0
  100. package/lib/sui/sui-processor.js +3 -3
  101. package/lib/sui/sui-processor.js.map +1 -1
  102. package/lib/sui/tests/sui.test.js.map +1 -1
  103. package/lib/sui/tests/types/index.d.ts +1 -0
  104. package/lib/sui/tests/types/index.js +5 -0
  105. package/lib/sui/tests/types/index.js.map +1 -0
  106. package/lib/sui/tests/types/testnet/index.d.ts +1 -0
  107. package/lib/sui/tests/types/testnet/index.js +5 -0
  108. package/lib/sui/tests/types/testnet/index.js.map +1 -0
  109. package/lib/sui/tests/types/testnet/swap.d.ts +249 -0
  110. package/lib/sui/tests/types/testnet/swap.js +310 -0
  111. package/lib/sui/tests/types/testnet/swap.js.map +1 -0
  112. package/lib/testing/aptos-facet.js +1 -1
  113. package/lib/testing/aptos-facet.js.map +1 -1
  114. package/lib/testing/sui-facet.js +3 -2
  115. package/lib/testing/sui-facet.js.map +1 -1
  116. package/package.json +8 -5
  117. package/src/aptos/aptos-processor.ts +1 -1
  118. package/src/aptos/builtin/0x1.ts +70 -652
  119. package/src/aptos/builtin/0x3.ts +24 -59
  120. package/src/aptos/codegen/codegen.ts +35 -421
  121. package/src/aptos/models.ts +0 -10
  122. package/src/aptos/move-coder.ts +16 -150
  123. package/src/aptos/move-types.ts +62 -0
  124. package/src/aptos/tests/souffl3.ts +1 -1
  125. package/src/aptos/tests/types/reserved.ts +15 -30
  126. package/src/aptos/tests/types/soffl3.ts +19 -58
  127. package/src/aptos/tests/types/souffle.ts +15 -30
  128. package/src/aptos/utils.ts +3 -21
  129. package/src/eth/tests/erc20.ts +1 -1
  130. package/src/move/abstract-codegen.ts +426 -0
  131. package/src/move/abstract-move-coder.ts +170 -0
  132. package/src/move/account.ts +96 -0
  133. package/src/move/filter.ts +33 -0
  134. package/src/move/index.ts +5 -33
  135. package/src/move/neutral-models.ts +45 -0
  136. package/src/move/ts-type.ts +86 -0
  137. package/src/{aptos → move}/types.ts +28 -3
  138. package/src/move/utils.ts +25 -0
  139. package/src/sui/abis/0x1.json +1729 -1733
  140. package/src/sui/abis/0x2.json +13279 -13283
  141. package/src/sui/builtin/0x1.ts +307 -0
  142. package/src/sui/builtin/0x2.ts +2578 -0
  143. package/src/sui/builtin/index.ts +5 -0
  144. package/src/sui/codegen/codegen.ts +71 -0
  145. package/src/sui/codegen/index.ts +1 -0
  146. package/src/sui/codegen/run.ts +13 -0
  147. package/src/sui/index.ts +4 -0
  148. package/src/sui/models.ts +7 -28
  149. package/src/sui/move-coder.ts +63 -0
  150. package/src/sui/move-types.ts +109 -0
  151. package/src/sui/sui-processor.ts +3 -3
  152. package/src/sui/tests/abis/testnet/swap.json +1752 -0
  153. package/src/sui/tests/types/index.ts +3 -0
  154. package/src/sui/tests/types/testnet/index.ts +4 -0
  155. package/src/sui/tests/types/testnet/swap.ts +698 -0
  156. package/src/testing/aptos-facet.ts +1 -1
  157. package/src/testing/sui-facet.ts +3 -2
  158. package/lib/aptos/codegen/typegen.d.ts +0 -18
  159. package/lib/aptos/codegen/typegen.js.map +0 -1
  160. package/lib/aptos/codegen/typegen.test.js.map +0 -1
  161. package/lib/aptos/types.js.map +0 -1
  162. package/lib/aptos/types.test.js.map +0 -1
  163. package/lib/jest.config.d.ts +0 -9
  164. package/lib/jest.config.js +0 -9
  165. package/lib/jest.config.js.map +0 -1
  166. package/src/aptos/codegen/typegen.ts +0 -165
  167. package/src/jest.config.ts +0 -8
@@ -1,25 +1,7 @@
1
- import { MoveFunction, MoveModule } from './move-types.js'
1
+ import { TypeDescriptor } from '../move/index.js'
2
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
- }
19
-
20
- export function getMeaningfulFunctionParams(func: MoveFunction): string[] {
21
- let params = func.params
22
- if (func.params[0] === '&signer') {
3
+ export function getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
4
+ if (params[0].qname === 'signer' && params[0].reference) {
23
5
  params = params.slice(1)
24
6
  }
25
7
  return params
@@ -33,7 +33,7 @@ ERC20Processor.bind({
33
33
  ERC20Processor.bind({ address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', network: 56, name: 'usdc' })
34
34
  .onEventTransfer(
35
35
  async function (event, ctx) {
36
- // ctx.meter.Counter('c2').add(2)
36
+ ctx.meter.Counter('c2').add(2)
37
37
  // const t: Promise<string> =
38
38
  // ctx.eventLogger.emit('transfer', { distinctId: event.args.from, xx: 33, y: Promise.resolve("s")})
39
39
  // ctx.eventLogger.emit('transfer', { distinctId: event.args.from, x: { y: Promise.resolve("s")} })
@@ -0,0 +1,426 @@
1
+ import { NeutralMoveFunction, NeutralMoveModule, NeutralMoveStruct } from './neutral-models.js'
2
+ import path from 'path'
3
+ import fs from 'fs'
4
+ import { AccountModulesImportInfo, AccountRegister } from './account.js'
5
+ import chalk from 'chalk'
6
+ import { format } from 'prettier'
7
+ import { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER } from './utils.js'
8
+ import { generateTypeForDescriptor } from './ts-type.js'
9
+ import { camelCase } from 'lodash-es'
10
+ import { TypeDescriptor } from './types.js'
11
+
12
+ interface OutputFile {
13
+ fileName: string
14
+ fileContent: string
15
+ }
16
+
17
+ interface Config<NetworkType> {
18
+ fileName: string
19
+ outputDir: string
20
+ network: NetworkType
21
+ }
22
+
23
+ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
24
+ TEST_NET: NetworkType
25
+ MAIN_NET: NetworkType
26
+ ADDRESS_TYPE: string
27
+ PREFIX: string
28
+ STRUCT_FIELD_NAME: string = 'data'
29
+
30
+ abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
31
+ abstract toNeutral(modules: ModuleTypes): NeutralMoveModule[]
32
+ abstract getEventStructs(module: NeutralMoveModule): Map<string, NeutralMoveStruct>
33
+
34
+ abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
35
+
36
+ readModulesFile(fullPath: string) {
37
+ return JSON.parse(fs.readFileSync(fullPath, 'utf-8'))
38
+ }
39
+
40
+ async generate(srcDir: string, outputDir: string) {
41
+ await this.generateForNetwork(srcDir, outputDir, this.MAIN_NET)
42
+ await this.generateForNetwork(path.join(srcDir, 'testnet'), path.join(outputDir, 'testnet'), this.TEST_NET)
43
+ }
44
+
45
+ async generateForNetwork(srcDir: string, outputDir: string, network: NetworkType) {
46
+ if (!fs.existsSync(srcDir)) {
47
+ return
48
+ }
49
+ if (network === this.TEST_NET) {
50
+ console.log('Found testnet directory, generate code for testnet modules')
51
+ }
52
+ const files = fs.readdirSync(srcDir)
53
+ outputDir = path.resolve(outputDir)
54
+ const outputs: OutputFile[] = []
55
+
56
+ fs.mkdirSync(outputDir, { recursive: true })
57
+
58
+ const loader = new AccountRegister()
59
+
60
+ // when generating user code, don't need to generate framework account
61
+ loader.accountImports.set('0x1', new AccountModulesImportInfo('0x1', '0x1'))
62
+ loader.accountImports.set('0x2', new AccountModulesImportInfo('0x2', '0x2'))
63
+ loader.accountImports.set('0x3', new AccountModulesImportInfo('0x3', '0x3'))
64
+ // const client = getRpcClient(network)
65
+
66
+ for (const file of files) {
67
+ if (!file.endsWith('.json')) {
68
+ continue
69
+ }
70
+ const fullPath = path.resolve(srcDir, file)
71
+ const abi = this.readModulesFile(fullPath)
72
+ const modules = this.toNeutral(abi)
73
+
74
+ for (const module of modules) {
75
+ loader.register(module, path.basename(file, '.json'))
76
+ }
77
+ const codeGen = new AccountCodegen(this, loader, abi, modules, {
78
+ fileName: path.basename(file, '.json'),
79
+ outputDir: outputDir,
80
+ network,
81
+ })
82
+
83
+ outputs.push(...codeGen.generate())
84
+ }
85
+
86
+ while (loader.pendingAccounts.size > 0) {
87
+ for (const account of loader.pendingAccounts) {
88
+ console.log(`download dependent module for account ${account} at ${network}`)
89
+
90
+ try {
91
+ const rawModules = await this.fetchModules(account, network)
92
+ const modules = this.toNeutral(rawModules)
93
+
94
+ fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\t'))
95
+
96
+ for (const module of modules) {
97
+ loader.register(module, account)
98
+ }
99
+ const codeGen = new AccountCodegen(this, loader, rawModules, modules, {
100
+ fileName: account,
101
+ outputDir: outputDir,
102
+ network,
103
+ })
104
+
105
+ outputs.push(...codeGen.generate())
106
+ } catch (e) {
107
+ console.error(
108
+ chalk.red(
109
+ 'Error downloading account module, check if you choose the right network,or download account modules manually into your director'
110
+ )
111
+ )
112
+ console.error(e)
113
+ process.exit(1)
114
+ }
115
+ }
116
+ }
117
+
118
+ for (const output of outputs) {
119
+ const content = format(output.fileContent, { parser: 'typescript' })
120
+ fs.writeFileSync(path.join(outputDir, output.fileName), content)
121
+ }
122
+
123
+ const rootFile = path.join(outputDir, 'index.ts')
124
+ fs.writeFileSync(
125
+ rootFile,
126
+ `/* Autogenerated file. Do not edit manually. */
127
+ /* tslint:disable */
128
+ /* eslint-disable */
129
+ `
130
+ )
131
+ for (const output of outputs) {
132
+ const parsed = path.parse(output.fileName)
133
+ const content = `export * as _${parsed.name} from './${parsed.name}.js'\n`
134
+ fs.appendFileSync(rootFile, content)
135
+ }
136
+ }
137
+
138
+ generateNetworkOption(network: NetworkType) {
139
+ switch (network) {
140
+ case this.TEST_NET:
141
+ return 'TEST_NET'
142
+ }
143
+ return 'MAIN_NET'
144
+ }
145
+
146
+ generateModule(module: NeutralMoveModule, network: NetworkType) {
147
+ const functions = module.exposed_functions
148
+ .map((f) => this.generateOnEntryFunctions(module, f))
149
+ .filter((s) => s !== '')
150
+
151
+ const eventStructs = this.getEventStructs(module)
152
+ const eventTypes = new Set(eventStructs.keys())
153
+ const events = Array.from(eventStructs.values())
154
+ .map((e) => this.generateOnEvents(module, e))
155
+ .filter((s) => s !== '')
156
+ const structs = module.structs.map((s) => this.generateStructs(module, s, eventTypes))
157
+ const callArgs = module.exposed_functions.map((f) => this.generateCallArgsStructs(module, f))
158
+
159
+ const moduleName = normalizeToJSName(module.name)
160
+ let processor = ''
161
+ if (functions.length > 0 || events.length > 0) {
162
+ processor = `export class ${moduleName} extends ${this.PREFIX}BaseProcessor {
163
+
164
+ constructor(options: ${this.PREFIX}BindOptions) {
165
+ super("${module.name}", options)
166
+ }
167
+ static DEFAULT_OPTIONS: ${this.PREFIX}BindOptions = {
168
+ address: "${module.address}",
169
+ network: ${this.PREFIX}Network.${this.generateNetworkOption(network)}
170
+ }
171
+
172
+ static bind(options: Partial<${this.PREFIX}BindOptions> = {}): ${moduleName} {
173
+ return new ${moduleName}({ ...${moduleName}.DEFAULT_OPTIONS, ...options })
174
+ }
175
+
176
+ ${functions.join('\n')}
177
+
178
+ ${events.join('\n')}
179
+ }
180
+ `
181
+ }
182
+
183
+ return `
184
+ ${processor}
185
+
186
+ export namespace ${moduleName} {
187
+ ${structs.join('\n')}
188
+
189
+ ${callArgs.join('\n')}
190
+ }
191
+ `
192
+ }
193
+
194
+ generateStructs(module: NeutralMoveModule, struct: NeutralMoveStruct, events: Set<string>) {
195
+ const genericString = this.generateStructTypeParameters(struct)
196
+ const genericStringAny = this.generateStructTypeParameters(struct, true)
197
+
198
+ const structName = normalizeToJSName(struct.name)
199
+
200
+ const fields = struct.fields.map((field) => {
201
+ const type = generateTypeForDescriptor(field.type, module.address, this.ADDRESS_TYPE)
202
+ return `${field.name}: ${type}`
203
+ })
204
+
205
+ let eventPayload = ''
206
+ if (events.has(moduleQname(module) + SPLITTER + struct.name)) {
207
+ eventPayload = `
208
+ export interface ${structName}Instance extends
209
+ TypedEventInstance<${structName}${genericStringAny}> {
210
+ ${this.STRUCT_FIELD_NAME}_decoded: ${structName}${genericStringAny}
211
+ type_arguments: [${struct.generic_type_params.map((_) => 'string').join(', ')}]
212
+ }
213
+ `
214
+ }
215
+
216
+ return `
217
+ export class ${structName}${genericString} {
218
+ static TYPE_QNAME = '${module.address}::${module.name}::${struct.name}'
219
+ ${fields.join('\n')}
220
+ }
221
+
222
+ ${eventPayload}
223
+ `
224
+ }
225
+
226
+ generateFunctionTypeParameters(func: NeutralMoveFunction) {
227
+ let genericString = ''
228
+ if (func.generic_type_params && func.generic_type_params.length > 0) {
229
+ const params = func.generic_type_params
230
+ .map((v, idx) => {
231
+ return `T${idx}=any`
232
+ })
233
+ .join(',')
234
+ genericString = `<${params}>`
235
+ }
236
+ return genericString
237
+ }
238
+
239
+ generateStructTypeParameters(struct: NeutralMoveStruct, useAny = false) {
240
+ let genericString = ''
241
+
242
+ if (struct.generic_type_params && struct.generic_type_params.length > 0) {
243
+ const params = struct.generic_type_params
244
+ .map((v, idx) => {
245
+ return useAny ? 'any' : 'T' + idx
246
+ })
247
+ .join(',')
248
+ genericString = `<${params}>`
249
+ }
250
+ return genericString
251
+ }
252
+
253
+ generateCallArgsStructs(module: NeutralMoveModule, func: NeutralMoveFunction) {
254
+ if (!func.is_entry) {
255
+ return
256
+ }
257
+
258
+ const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
259
+ return generateTypeForDescriptor(param, module.address, this.ADDRESS_TYPE)
260
+ })
261
+
262
+ const camelFuncName = capitalizeFirstChar(camelCase(func.name))
263
+
264
+ const genericString = this.generateFunctionTypeParameters(func)
265
+ return `
266
+ export interface ${camelFuncName}Payload${genericString}
267
+ extends TypedEntryFunctionPayload<[${fields.join(',')}]> {
268
+ arguments_decoded: [${fields.join(',')}],
269
+ type_arguments: [${func.generic_type_params.map((_) => 'string').join(', ')}]
270
+ }
271
+ `
272
+ }
273
+
274
+ generateOnEntryFunctions(module: NeutralMoveModule, func: NeutralMoveFunction) {
275
+ if (!func.is_entry) {
276
+ return ''
277
+ }
278
+
279
+ // const genericString = generateFunctionTypeParameters(func)
280
+ const moduleName = normalizeToJSName(module.name)
281
+
282
+ const camelFuncName = capitalizeFirstChar(camelCase(func.name))
283
+ const source = `
284
+ onEntry${camelFuncName}(func: (call: ${moduleName}.${camelFuncName}Payload, ctx: ${this.PREFIX}Context) => void, filter?: CallFilter, fetchConfig?: MoveFetchConfig): ${moduleName} {
285
+ this.onEntryFunctionCall(func, {
286
+ ...filter,
287
+ function: '${module.name}::${func.name}'
288
+ },
289
+ fetchConfig)
290
+ return this
291
+ }`
292
+
293
+ return source
294
+ }
295
+
296
+ generateOnEvents(module: NeutralMoveModule, struct: NeutralMoveStruct): string {
297
+ // for struct that has drop + store
298
+ // if (!isEvent(struct, module)) {
299
+ // return ''
300
+ // }
301
+
302
+ // const genericString = generateStructTypeParameters(struct)
303
+
304
+ const moduleName = normalizeToJSName(module.name)
305
+ const source = `
306
+ onEvent${struct.name}(func: (event: ${moduleName}.${normalizeToJSName(struct.name)}Instance, ctx: ${
307
+ this.PREFIX
308
+ }Context) => void, fetchConfig?: MoveFetchConfig): ${moduleName} {
309
+ this.onMoveEvent(func, {
310
+ type: '${module.name}::${struct.name}'
311
+ },
312
+ fetchConfig)
313
+ return this
314
+ }
315
+ `
316
+ return source
317
+ }
318
+ }
319
+
320
+ export class AccountCodegen<ModuleType, NetworkType> {
321
+ modules: NeutralMoveModule[]
322
+ config: Config<NetworkType>
323
+ abi: ModuleType
324
+ loader: AccountRegister
325
+ moduleGen: AbstractCodegen<ModuleType, NetworkType>
326
+
327
+ constructor(
328
+ moduleGen: AbstractCodegen<ModuleType, NetworkType>,
329
+ loader: AccountRegister,
330
+ abi: ModuleType,
331
+ modules: NeutralMoveModule[],
332
+ config: Config<NetworkType>
333
+ ) {
334
+ // const json = fs.readFileSync(config.srcFile, 'utf-8')
335
+ this.moduleGen = moduleGen
336
+ this.abi = abi
337
+ this.modules = modules
338
+ this.config = config
339
+ this.loader = loader
340
+ }
341
+
342
+ generate(): OutputFile[] {
343
+ if (!this.modules) {
344
+ return []
345
+ }
346
+ // const baseName = path.basename(this.config.fileName, '.json')
347
+
348
+ let address: string | undefined
349
+ for (const module of this.modules) {
350
+ address = module.address
351
+ }
352
+ if (!address) {
353
+ return []
354
+ }
355
+
356
+ const imports = `
357
+ import { CallFilter } from "@sentio/sdk/move"
358
+ import {
359
+ MoveCoder, defaultMoveCoder, ${this.moduleGen.PREFIX}BindOptions, ${this.moduleGen.PREFIX}BaseProcessor,
360
+ TypedEventInstance, ${this.moduleGen.PREFIX}Network, TypedEntryFunctionPayload,
361
+ ${this.moduleGen.PREFIX}Context } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
362
+ import { MoveFetchConfig } from "@sentio/protos"
363
+ import { ${this.moduleGen.ADDRESS_TYPE} } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
364
+ `
365
+
366
+ const dependedAccounts: string[] = []
367
+
368
+ const moduleImports: string[] = []
369
+
370
+ const info = this.loader.accountImports.get(address)
371
+
372
+ if (info) {
373
+ for (const [account] of info.imports.entries()) {
374
+ // Remap to user's filename if possible, TODO codepath not well tested
375
+ const tsAccountModule = './' + (this.loader.accountImports.get(account)?.moduleName || account)
376
+ if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
377
+ // Decide where to find runtime library
378
+ moduleImports.push(`import { _${account} } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}/builtin"`)
379
+ } else {
380
+ moduleImports.push(`import * as _${account} from "${tsAccountModule}.js"`)
381
+ }
382
+
383
+ dependedAccounts.push(account)
384
+ }
385
+ }
386
+
387
+ const source = `
388
+ /* Autogenerated file. Do not edit manually. */
389
+ /* tslint:disable */
390
+ /* eslint-disable */
391
+
392
+ /* Generated modules for account ${address} */
393
+
394
+ ${imports}
395
+
396
+ ${moduleImports.join('\n')}
397
+
398
+ ${this.modules.map((m) => this.moduleGen.generateModule(m, this.config.network)).join('\n')}
399
+
400
+ const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
401
+
402
+ export function loadAllTypes(coder: MoveCoder) {
403
+ ${dependedAccounts.map((a) => `_${a}.loadAllTypes(coder)`).join('\n')}
404
+ for (const m of Object.values(MODULES)) {
405
+ coder.load(m as any)
406
+ }
407
+ }
408
+
409
+ loadAllTypes(defaultMoveCoder())
410
+ ` // source
411
+
412
+ return [
413
+ {
414
+ fileName: this.config.fileName + '.ts',
415
+ fileContent: source,
416
+ },
417
+ ]
418
+ }
419
+ }
420
+
421
+ function capitalizeFirstChar(input: string): string {
422
+ if (!input) {
423
+ return input
424
+ }
425
+ return input[0].toUpperCase() + (input.length > 1 ? input.substring(1) : '')
426
+ }
@@ -0,0 +1,170 @@
1
+ import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'
2
+ import { parseMoveType, TypeDescriptor } from './types.js'
3
+ import { NeutralMoveFunction, NeutralMoveModule, NeutralMoveStruct } from './neutral-models.js'
4
+ import { bytesToBigInt } from '../utils/index.js'
5
+
6
+ type StructWithTag<Base> = Base & {
7
+ type: string
8
+ data: any
9
+ }
10
+
11
+ type DecodedStructWithTag<B, T> = StructWithTag<B> & {
12
+ data_decoded: T
13
+ type_arguments: string[]
14
+ }
15
+
16
+ export abstract class AbstractMoveCoder<StructType> {
17
+ private moduleMapping = new Map<string, NeutralMoveModule>()
18
+ private typeMapping = new Map<string, NeutralMoveStruct>()
19
+ private funcMapping = new Map<string, NeutralMoveFunction>()
20
+
21
+ contains(account: string, name: string) {
22
+ return this.moduleMapping.has(account + '::' + name)
23
+ }
24
+
25
+ loadNeutral(module: NeutralMoveModule) {
26
+ if (this.contains(module.address, module.name)) {
27
+ return
28
+ }
29
+ this.moduleMapping.set(moduleQname(module), module)
30
+
31
+ for (const struct of module.structs) {
32
+ // TODO move to util
33
+ const key = [module.address, module.name, struct.name].join(SPLITTER)
34
+ this.typeMapping.set(key, struct)
35
+ }
36
+
37
+ for (const func of module.exposed_functions) {
38
+ if (!func.is_entry) {
39
+ continue
40
+ }
41
+ const key = [module.address, module.name, func.name].join(SPLITTER)
42
+ this.funcMapping.set(key, func)
43
+ }
44
+ }
45
+
46
+ protected dataToBigInt(data: any): bigint {
47
+ if (Array.isArray(data)) {
48
+ // Only sui function need this, strange
49
+ const bytes = data as number[]
50
+ return bytesToBigInt(new Uint8Array(bytes.slice().reverse()))
51
+ } else {
52
+ return BigInt(data)
53
+ }
54
+ }
55
+
56
+ protected getMoveStruct(type: string): NeutralMoveStruct {
57
+ const struct = this.typeMapping.get(type)
58
+ if (!struct) {
59
+ throw new Error('Failed to load type' + type + ' type are not imported anywhere')
60
+ }
61
+ return struct
62
+ }
63
+
64
+ protected getMoveFunction(type: string): NeutralMoveFunction {
65
+ const func = this.funcMapping.get(type)
66
+ if (!func) {
67
+ throw new Error('Failed to load function' + type + ' type are not imported anywhere')
68
+ }
69
+ return func
70
+ }
71
+
72
+ protected decode(data: any, type: TypeDescriptor): any {
73
+ // process simple type
74
+ if (type.reference) {
75
+ return data
76
+ }
77
+ switch (type.qname) {
78
+ case 'signer': // TODO check this, aptos only
79
+ case 'address':
80
+ case 'Address':
81
+ case '0x2::object::ID':
82
+ case '0x2::coin::Coin':
83
+ case '0x1::string::String':
84
+ case 'bool':
85
+ case 'Bool':
86
+ case 'u8':
87
+ case 'U8':
88
+ case 'u16':
89
+ case 'U16':
90
+ case 'u32':
91
+ case 'U32':
92
+ return data
93
+ case 'u64':
94
+ case 'U64':
95
+ case 'u128':
96
+ case 'U128':
97
+ return this.dataToBigInt(data)
98
+ }
99
+
100
+ // process vector
101
+ if (type.qname === VECTOR_STR) {
102
+ // vector<u8> as hex string
103
+ if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {
104
+ return data
105
+ }
106
+
107
+ const res = []
108
+ for (const entry of data) {
109
+ res.push(this.decode(entry, type.typeArgs[0]))
110
+ }
111
+ return res
112
+ }
113
+
114
+ // Process complex type
115
+ const struct = this.getMoveStruct(type.qname)
116
+
117
+ const typeCtx = new Map<string, TypeDescriptor>()
118
+ for (const [idx, typeArg] of type.typeArgs.entries()) {
119
+ typeCtx.set('T' + idx, typeArg)
120
+ }
121
+
122
+ const typedData: any = {}
123
+
124
+ for (const field of struct.fields) {
125
+ let filedType = field.type
126
+ filedType = filedType.applyTypeArgs(typeCtx)
127
+ const value = this.decode(data[field.name], filedType)
128
+ typedData[field.name] = value
129
+ }
130
+ return typedData
131
+ }
132
+
133
+ protected filterAndDecodeInternal<T>(
134
+ typeQname: string,
135
+ structs: StructWithTag<StructType>[]
136
+ ): DecodedStructWithTag<StructType, T>[] {
137
+ if (!structs) {
138
+ return []
139
+ }
140
+ const results: DecodedStructWithTag<StructType, T>[] = []
141
+ for (const resource of structs) {
142
+ if (resource.type.split('<')[0] !== typeQname) {
143
+ continue
144
+ }
145
+ const result = this.decodedInternal(resource)
146
+ if (result) {
147
+ results.push(result as DecodedStructWithTag<StructType, T>)
148
+ }
149
+ }
150
+ return results
151
+ }
152
+
153
+ protected decodedInternal<T>(typeStruct: StructWithTag<StructType>): DecodedStructWithTag<StructType, T> | undefined {
154
+ const typeDescriptor = parseMoveType(typeStruct.type)
155
+ const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature())
156
+
157
+ let dataTyped = undefined
158
+ try {
159
+ dataTyped = this.decode(typeStruct.data, typeDescriptor)
160
+ } catch (e) {
161
+ console.error('Decoding error for ', JSON.stringify(typeStruct), e)
162
+ return undefined
163
+ }
164
+ return {
165
+ ...typeStruct,
166
+ data_decoded: dataTyped,
167
+ type_arguments: typeArguments,
168
+ } as DecodedStructWithTag<StructType, T>
169
+ }
170
+ }