@sentio/sdk 1.26.3 → 1.26.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,8 @@ import fs from 'fs'
2
2
  import path from 'path'
3
3
  import prettier from 'prettier'
4
4
  import { MoveFunction, MoveModule, MoveModuleBytecode, MoveStruct } from 'aptos-sdk/src/generated'
5
- import { AccountModulesImportInfo, AccountRegister, generateType } from './typegen'
6
- import { isFrameworkAccount } from '../aptos/utils'
5
+ import { AccountModulesImportInfo, AccountRegister, generateType, parseMoveType } from './typegen'
6
+ import { isFrameworkAccount, moduleQname, SPLITTER } from '../aptos/utils'
7
7
  import chalk from 'chalk'
8
8
  import { AptosNetwork, getChainName, getRpcClient } from '../aptos/network'
9
9
 
@@ -176,12 +176,12 @@ export class AccountCodegen {
176
176
 
177
177
  ${this.modules.map((m) => generateModule(m, this.config.network)).join('\n')}
178
178
 
179
- function loadAllTypes(registry: aptos.TypeRegistry) {
180
- ${dependedAccounts.map((m) => `${m}.loadTypes(registry)`).join('\n')}
179
+ function loadAllTypes(_r: aptos.TypeRegistry) {
180
+ ${dependedAccounts.map((m) => `${m}.loadTypes(_r)`).join('\n')}
181
181
 
182
182
  ${this.modules
183
183
  .map((m) => {
184
- return `registry.load(${m.abi?.name}.ABI)`
184
+ return `_r.load(${m.abi?.name}.ABI)`
185
185
  })
186
186
  .join('\n')}
187
187
  }
@@ -211,8 +211,13 @@ function generateModule(moduleByteCode: MoveModuleBytecode, network: AptosNetwor
211
211
  const module = moduleByteCode.abi
212
212
 
213
213
  const functions = module.exposed_functions.map((f) => generateOnEntryFunctions(module, f)).filter((s) => s !== '')
214
- const events = module.structs.map((e) => generateOnEvents(module, e)).filter((s) => s !== '')
215
- const structs = module.structs.map((s) => generateStructs(module, s))
214
+
215
+ const eventStructs = getEventStructs(module)
216
+ const eventTypes = new Set(eventStructs.keys())
217
+ const events = Array.from(eventStructs.values())
218
+ .map((e) => generateOnEvents(module, e))
219
+ .filter((s) => s !== '')
220
+ const structs = module.structs.map((s) => generateStructs(module, s, eventTypes))
216
221
  const callArgs = module.exposed_functions.map((f) => generateCallArgsStructs(module, f))
217
222
 
218
223
  let processor = ''
@@ -250,15 +255,15 @@ function generateModule(moduleByteCode: MoveModuleBytecode, network: AptosNetwor
250
255
 
251
256
  ${callArgs.join('\n')}
252
257
 
253
- export function loadTypes(registry: aptos.TypeRegistry) {
254
- loadAllTypes(registry)
258
+ export function loadTypes(_r: aptos.TypeRegistry) {
259
+ loadAllTypes(_r)
255
260
  }
256
261
  export const ABI: MoveModule = JSON.parse('${JSON.stringify(module)}')
257
262
  }
258
263
  `
259
264
  }
260
265
 
261
- function generateStructs(module: MoveModule, struct: MoveStruct) {
266
+ function generateStructs(module: MoveModule, struct: MoveStruct, events: Set<string>) {
262
267
  const genericString = generateStructTypeParameters(struct)
263
268
  const genericStringAny = generateStructTypeParameters(struct, true)
264
269
 
@@ -267,7 +272,7 @@ function generateStructs(module: MoveModule, struct: MoveStruct) {
267
272
  })
268
273
 
269
274
  let eventPayload = ''
270
- if (isEvent(struct)) {
275
+ if (events.has(moduleQname(module) + SPLITTER + struct.name)) {
271
276
  eventPayload = `
272
277
  export interface ${struct.name}Instance extends
273
278
  aptos.TypedEventInstance<${struct.name}${genericStringAny}> {
@@ -357,15 +362,57 @@ function generateOnEntryFunctions(module: MoveModule, func: MoveFunction) {
357
362
  return source
358
363
  }
359
364
 
360
- function isEvent(struct: MoveStruct) {
361
- return struct.abilities.includes('drop') && struct.abilities.includes('store') && struct.name.endsWith('Event')
365
+ function getEventStructs(module: MoveModule) {
366
+ const qname = moduleQname(module)
367
+ const structMap = new Map<string, MoveStruct>()
368
+ const eventMap = new Map<string, MoveStruct>()
369
+
370
+ for (const struct of module.structs) {
371
+ structMap.set(qname + SPLITTER + struct.name, struct)
372
+ }
373
+
374
+ for (const struct of module.structs) {
375
+ for (const field of struct.fields) {
376
+ const t = parseMoveType(field.type)
377
+ if (t.qname === '0x1::event::EventHandle') {
378
+ const event = t.typeArgs[0].qname
379
+ const eventStruct = structMap.get(event)
380
+ if (eventStruct) {
381
+ eventMap.set(event, eventStruct)
382
+ }
383
+ }
384
+ }
385
+ }
386
+
387
+ return eventMap
388
+ }
389
+
390
+ function isEvent(struct: MoveStruct, module: MoveModule) {
391
+ const hasAbility = struct.abilities.includes('drop') && struct.abilities.includes('store')
392
+
393
+ if (!hasAbility) {
394
+ return false
395
+ }
396
+ if (struct.name.endsWith('Event')) {
397
+ return true
398
+ }
399
+
400
+ // for (const struct of module.structs) {
401
+ // for (const field of struct.fields) {
402
+ // if (field.type.startsWith('0x1::event::EventHandle')
403
+ // }
404
+ // }
405
+
406
+ return false
407
+
408
+ //&&
362
409
  }
363
410
 
364
411
  function generateOnEvents(module: MoveModule, struct: MoveStruct): string {
365
412
  // for struct that has drop + store
366
- if (!isEvent(struct)) {
367
- return ''
368
- }
413
+ // if (!isEvent(struct, module)) {
414
+ // return ''
415
+ // }
369
416
 
370
417
  // const genericString = generateStructTypeParameters(struct)
371
418