@sentio/sdk 2.13.0-rc.2 → 2.13.0-rc.4

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 (81) hide show
  1. package/lib/aptos/aptos-chain-adapter.d.ts +11 -0
  2. package/lib/aptos/aptos-chain-adapter.js +56 -0
  3. package/lib/aptos/aptos-chain-adapter.js.map +1 -0
  4. package/lib/aptos/aptos-processor.d.ts +1 -2
  5. package/lib/aptos/aptos-processor.js +6 -14
  6. package/lib/aptos/aptos-processor.js.map +1 -1
  7. package/lib/aptos/builtin/0x1.js +2 -1
  8. package/lib/aptos/builtin/0x1.js.map +1 -1
  9. package/lib/aptos/builtin/0x3.js +2 -1
  10. package/lib/aptos/builtin/0x3.js.map +1 -1
  11. package/lib/aptos/codegen/codegen.js +4 -45
  12. package/lib/aptos/codegen/codegen.js.map +1 -1
  13. package/lib/aptos/context.d.ts +3 -0
  14. package/lib/aptos/context.js +5 -0
  15. package/lib/aptos/context.js.map +1 -1
  16. package/lib/aptos/ext/aptos-dex.js +1 -2
  17. package/lib/aptos/ext/aptos-dex.js.map +1 -1
  18. package/lib/aptos/move-coder.d.ts +4 -5
  19. package/lib/aptos/move-coder.js +14 -8
  20. package/lib/aptos/move-coder.js.map +1 -1
  21. package/lib/move/abstract-codegen.d.ts +4 -4
  22. package/lib/move/abstract-codegen.js +22 -10
  23. package/lib/move/abstract-codegen.js.map +1 -1
  24. package/lib/move/abstract-move-coder.d.ts +5 -2
  25. package/lib/move/abstract-move-coder.js +7 -1
  26. package/lib/move/abstract-move-coder.js.map +1 -1
  27. package/lib/move/chain-adapter.d.ts +9 -0
  28. package/lib/move/chain-adapter.js +3 -0
  29. package/lib/move/chain-adapter.js.map +1 -0
  30. package/lib/move/index.d.ts +1 -0
  31. package/lib/move/index.js +1 -0
  32. package/lib/move/index.js.map +1 -1
  33. package/lib/sui/builtin/0x1.js +2 -1
  34. package/lib/sui/builtin/0x1.js.map +1 -1
  35. package/lib/sui/builtin/0x2.js +2 -1
  36. package/lib/sui/builtin/0x2.js.map +1 -1
  37. package/lib/sui/builtin/0x3.js +2 -1
  38. package/lib/sui/builtin/0x3.js.map +1 -1
  39. package/lib/sui/codegen/codegen.js +4 -34
  40. package/lib/sui/codegen/codegen.js.map +1 -1
  41. package/lib/sui/context.d.ts +2 -0
  42. package/lib/sui/context.js +3 -0
  43. package/lib/sui/context.js.map +1 -1
  44. package/lib/sui/move-coder.d.ts +5 -5
  45. package/lib/sui/move-coder.js +14 -8
  46. package/lib/sui/move-coder.js.map +1 -1
  47. package/lib/sui/sui-chain-adapter.d.ts +11 -0
  48. package/lib/sui/sui-chain-adapter.js +43 -0
  49. package/lib/sui/sui-chain-adapter.js.map +1 -0
  50. package/lib/sui/sui-processor.d.ts +2 -0
  51. package/lib/sui/sui-processor.js +4 -2
  52. package/lib/sui/sui-processor.js.map +1 -1
  53. package/lib/sui/utils.d.ts +0 -2
  54. package/lib/sui/utils.js +0 -6
  55. package/lib/sui/utils.js.map +1 -1
  56. package/package.json +4 -4
  57. package/src/aptos/aptos-chain-adapter.ts +65 -0
  58. package/src/aptos/aptos-processor.ts +17 -15
  59. package/src/aptos/builtin/0x1.ts +2 -1
  60. package/src/aptos/builtin/0x3.ts +2 -1
  61. package/src/aptos/codegen/codegen.ts +5 -54
  62. package/src/aptos/context.ts +5 -0
  63. package/src/aptos/ext/aptos-dex.ts +2 -11
  64. package/src/aptos/move-coder.ts +17 -12
  65. package/src/move/abstract-codegen.ts +28 -17
  66. package/src/move/abstract-move-coder.ts +10 -3
  67. package/src/move/chain-adapter.ts +14 -0
  68. package/src/move/index.ts +1 -0
  69. package/src/sui/builtin/0x1.ts +2 -1
  70. package/src/sui/builtin/0x2.ts +2 -1
  71. package/src/sui/builtin/0x3.ts +2 -1
  72. package/src/sui/codegen/codegen.ts +5 -40
  73. package/src/sui/context.ts +3 -0
  74. package/src/sui/move-coder.ts +18 -11
  75. package/src/sui/sui-chain-adapter.ts +50 -0
  76. package/src/sui/sui-processor.ts +5 -3
  77. package/src/sui/utils.ts +0 -8
  78. package/lib/aptos/utils.d.ts +0 -2
  79. package/lib/aptos/utils.js +0 -10
  80. package/lib/aptos/utils.js.map +0 -1
  81. package/src/aptos/utils.ts +0 -11
@@ -1,6 +1,6 @@
1
1
  import { MoveResource, Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from './move-types.js'
2
2
 
3
- import { MOVE_CODER, MoveCoder } from './move-coder.js'
3
+ import { defaultMoveCoder, MoveCoder } from './move-coder.js'
4
4
  import { AptosBindOptions, AptosNetwork, getChainId } from './network.js'
5
5
  import { AptosContext, AptosResourcesContext } from './context.js'
6
6
  import { EventInstance } from './models.js'
@@ -39,12 +39,14 @@ export class AptosBaseProcessor {
39
39
  config: IndexConfigure
40
40
  eventHandlers: EventHandler<Data_AptEvent>[] = []
41
41
  callHandlers: CallHandler<Data_AptCall>[] = []
42
+ coder: MoveCoder
42
43
 
43
44
  constructor(moduleName: string, options: AptosBindOptions) {
44
45
  this.moduleName = moduleName
45
46
  this.config = configure(options)
46
47
  AptosProcessorState.INSTANCE.addValue(this)
47
- this.loadTypes(MOVE_CODER)
48
+ this.coder = defaultMoveCoder(this.config.network)
49
+ // this.loadTypes(this.coder)
48
50
  }
49
51
 
50
52
  // getABI(): MoveModule | undefined {
@@ -129,7 +131,7 @@ export class AptosBaseProcessor {
129
131
  idx
130
132
  )
131
133
  const eventInstance = evt as EventInstance
132
- const decoded = MOVE_CODER.decodeEvent<any>(eventInstance)
134
+ const decoded = processor.coder.decodeEvent<any>(eventInstance)
133
135
  await handler(decoded || eventInstance, ctx)
134
136
  processResults.push(ctx.getProcessResult())
135
137
  }
@@ -177,7 +179,7 @@ export class AptosBaseProcessor {
177
179
  )
178
180
  if (tx) {
179
181
  const payload = tx.payload as TransactionPayload_EntryFunctionPayload
180
- const decoded = MOVE_CODER.decodeFunctionPayload(payload)
182
+ const decoded = processor.coder.decodeFunctionPayload(payload)
181
183
  await handler(decoded, ctx)
182
184
  }
183
185
  return ctx.getProcessResult()
@@ -192,17 +194,17 @@ export class AptosBaseProcessor {
192
194
  return getChainId(this.config.network)
193
195
  }
194
196
 
195
- loadTypes(registry: MoveCoder) {
196
- if (registry.contains(this.config.address, this.moduleName)) {
197
- return
198
- }
199
- this.loadTypesInternal(registry)
200
- }
201
-
202
- protected loadTypesInternal(registry: MoveCoder) {
203
- // should be override by subclass
204
- console.log('')
205
- }
197
+ // loadTypes(registry: MoveCoder) {
198
+ // if (registry.contains(this.config.address, this.moduleName)) {
199
+ // return
200
+ // }
201
+ // this.loadTypesInternal(registry)
202
+ // }
203
+ //
204
+ // protected loadTypesInternal(registry: MoveCoder) {
205
+ // // should be override by subclass
206
+ // console.log('')
207
+ // }
206
208
  }
207
209
 
208
210
  export class AptosAccountProcessorState extends ListStateStorage<AptosResourcesProcessor> {
@@ -5788,4 +5788,5 @@ export function loadAllTypes(coder: MoveCoder) {
5788
5788
  }
5789
5789
  }
5790
5790
 
5791
- loadAllTypes(defaultMoveCoder());
5791
+ loadAllTypes(defaultMoveCoder(AptosNetwork.MAIN_NET));
5792
+ loadAllTypes(defaultMoveCoder(AptosNetwork.TEST_NET));
@@ -1310,4 +1310,5 @@ export function loadAllTypes(coder: MoveCoder) {
1310
1310
  }
1311
1311
  }
1312
1312
 
1313
- loadAllTypes(defaultMoveCoder());
1313
+ loadAllTypes(defaultMoveCoder(AptosNetwork.MAIN_NET));
1314
+ loadAllTypes(defaultMoveCoder(AptosNetwork.TEST_NET));
@@ -1,13 +1,10 @@
1
1
  import * as fs from 'fs'
2
- import { MoveModuleBytecode, toInternalModule } from '../move-types.js'
3
- import { moduleQname, SPLITTER, TypeDescriptor } from '../../move/index.js'
2
+ import { MoveModuleBytecode } from '../move-types.js'
4
3
  import chalk from 'chalk'
5
4
  import { AptosNetwork } from '../network.js'
6
- import { AptosClient } from 'aptos-sdk'
7
- import { getMeaningfulFunctionParams } from '../utils.js'
8
- import { InternalMoveModule, InternalMoveStruct } from '../../move/internal-models.js'
5
+ import { join } from 'path'
6
+ import { AptosChainAdapter } from '../aptos-chain-adapter.js'
9
7
  import { AbstractCodegen } from '../../move/abstract-codegen.js'
10
- import {join} from "path";
11
8
 
12
9
  export async function codegen(abisDir: string, outDir = join('src', 'types', 'aptos'), genExample = false) {
13
10
  if (!fs.existsSync(abisDir)) {
@@ -18,59 +15,13 @@ export async function codegen(abisDir: string, outDir = join('src', 'types', 'ap
18
15
  console.log(chalk.green(`Generated ${numFiles} for Aptos`))
19
16
  }
20
17
 
21
- function getRpcEndpoint(network: AptosNetwork): string {
22
- switch (network) {
23
- case AptosNetwork.TEST_NET:
24
- return 'https://testnet.aptoslabs.com/'
25
- }
26
- return 'https://mainnet.aptoslabs.com/'
27
- }
28
-
29
- function getRpcClient(network: AptosNetwork): AptosClient {
30
- return new AptosClient(getRpcEndpoint(network))
31
- }
32
-
33
18
  class AptosCodegen extends AbstractCodegen<MoveModuleBytecode[], AptosNetwork> {
34
19
  ADDRESS_TYPE = 'Address'
35
20
  MAIN_NET = AptosNetwork.MAIN_NET
36
21
  TEST_NET = AptosNetwork.TEST_NET
37
22
  PREFIX = 'Aptos'
38
23
 
39
- async fetchModules(account: string, network: AptosNetwork): Promise<MoveModuleBytecode[]> {
40
- const client = getRpcClient(network)
41
- return await client.getAccountModules(account)
42
- }
43
-
44
- toInternalModules(modules: MoveModuleBytecode[]): InternalMoveModule[] {
45
- return modules.flatMap((m) => (m.abi ? [toInternalModule(m)] : []))
46
- }
47
-
48
- getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
49
- return getMeaningfulFunctionParams(params)
50
- }
51
-
52
- getEventStructs(module: InternalMoveModule) {
53
- const qname = moduleQname(module)
54
- const structMap = new Map<string, InternalMoveStruct>()
55
- const eventMap = new Map<string, InternalMoveStruct>()
56
-
57
- for (const struct of module.structs) {
58
- structMap.set(qname + SPLITTER + struct.name, struct)
59
- }
60
-
61
- for (const struct of module.structs) {
62
- for (const field of struct.fields) {
63
- const t = field.type
64
- if (t.qname === '0x1::event::EventHandle') {
65
- const event = t.typeArgs[0].qname
66
- const eventStruct = structMap.get(event)
67
- if (eventStruct) {
68
- eventMap.set(event, eventStruct)
69
- }
70
- }
71
- }
72
- }
73
-
74
- return eventMap
24
+ constructor() {
25
+ super(AptosChainAdapter.INSTANCE)
75
26
  }
76
27
  }
@@ -2,6 +2,7 @@ import { RecordMetaData } from '@sentio/protos'
2
2
  import { type Labels, BaseContext, normalizeLabels } from '@sentio/sdk'
3
3
  import { Transaction_UserTransaction } from './move-types.js'
4
4
  import { AptosNetwork, getChainId } from './network.js'
5
+ import { defaultMoveCoder, MoveCoder } from './move-coder.js'
5
6
 
6
7
  export class AptosContext extends BaseContext {
7
8
  address: string
@@ -10,6 +11,7 @@ export class AptosContext extends BaseContext {
10
11
  version: bigint
11
12
  transaction: Transaction_UserTransaction
12
13
  eventIndex: number
14
+ coder: MoveCoder
13
15
 
14
16
  constructor(
15
17
  moduleName: string,
@@ -25,6 +27,7 @@ export class AptosContext extends BaseContext {
25
27
  this.moduleName = moduleName
26
28
  this.version = version
27
29
  this.eventIndex = eventIndex
30
+ this.coder = defaultMoveCoder(network)
28
31
  if (transaction) {
29
32
  this.transaction = transaction
30
33
  }
@@ -54,6 +57,7 @@ export class AptosResourcesContext extends BaseContext {
54
57
  network: AptosNetwork
55
58
  version: bigint
56
59
  timestampInMicros: number
60
+ coder: MoveCoder
57
61
 
58
62
  constructor(network: AptosNetwork, address: string, version: bigint, timestampInMicros: number) {
59
63
  super()
@@ -61,6 +65,7 @@ export class AptosResourcesContext extends BaseContext {
61
65
  this.network = network
62
66
  this.version = version
63
67
  this.timestampInMicros = timestampInMicros
68
+ this.coder = defaultMoveCoder(network)
64
69
  }
65
70
 
66
71
  getChainId(): string {
@@ -1,13 +1,7 @@
1
1
  import { Gauge } from '@sentio/sdk'
2
2
  import { BigDecimal } from '@sentio/bigdecimal'
3
3
  import { calculateValueInUsd, getCoinInfo, whitelistCoins, whiteListed } from './coin.js'
4
- import {
5
- AptosResourcesContext,
6
- TypedMoveResource,
7
- MoveResource,
8
- defaultMoveCoder,
9
- AptosContext,
10
- } from '@sentio/sdk/aptos'
4
+ import { AptosResourcesContext, TypedMoveResource, MoveResource, AptosContext } from '@sentio/sdk/aptos'
11
5
 
12
6
  export interface PoolAdaptor<T> {
13
7
  getXReserve(pool: T): bigint
@@ -104,10 +98,7 @@ export class AptosDex<T> {
104
98
  ctx: AptosResourcesContext,
105
99
  poolsHandler?: (pools: TypedMoveResource<T>[]) => Promise<void> | void
106
100
  ) {
107
- const pools: TypedMoveResource<T>[] = defaultMoveCoder().filterAndDecodeResources(
108
- this.poolAdaptor.poolTypeName,
109
- resources
110
- )
101
+ const pools: TypedMoveResource<T>[] = ctx.coder.filterAndDecodeResources(this.poolAdaptor.poolTypeName, resources)
111
102
 
112
103
  const volumeByCoin = new Map<string, BigDecimal>()
113
104
  const timestamp = ctx.timestampInMicros
@@ -6,12 +6,17 @@ import {
6
6
  TransactionPayload_EntryFunctionPayload,
7
7
  } from './move-types.js'
8
8
 
9
- import { TypedEventInstance, TypedMoveResource, TypedFunctionPayload } from './models.js'
10
- import { getMeaningfulFunctionParams } from './utils.js'
9
+ import { TypedEventInstance, TypedFunctionPayload, TypedMoveResource } from './models.js'
11
10
  import { AbstractMoveCoder } from '../move/abstract-move-coder.js'
12
- import { TypeDescriptor } from '../move/index.js'
11
+ import { AptosNetwork } from './network.js'
12
+ import { AptosChainAdapter } from './aptos-chain-adapter.js'
13
+
14
+ export class MoveCoder extends AbstractMoveCoder<AptosNetwork, MoveModuleBytecode[], Event | MoveResource> {
15
+ constructor(network: AptosNetwork) {
16
+ super(network)
17
+ this.adapter = new AptosChainAdapter()
18
+ }
13
19
 
14
- export class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {
15
20
  load(module: MoveModuleBytecode) {
16
21
  if (!module.abi) {
17
22
  throw Error('Module without abi')
@@ -35,13 +40,9 @@ export class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {
35
40
  return this.filterAndDecodeStruct(typeQname, resources) as any
36
41
  }
37
42
 
38
- getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
39
- return getMeaningfulFunctionParams(params)
40
- }
41
-
42
43
  decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {
43
44
  const func = this.getMoveFunction(payload.function)
44
- const params = getMeaningfulFunctionParams(func.params)
45
+ const params = this.adapter.getMeaningfulFunctionParams(func.params)
45
46
  const argumentsDecoded = this.decodeArray(payload.arguments, params)
46
47
 
47
48
  return {
@@ -51,8 +52,12 @@ export class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {
51
52
  }
52
53
  }
53
54
 
54
- export const MOVE_CODER = new MoveCoder()
55
+ const MOVE_CODER = new MoveCoder(AptosNetwork.MAIN_NET)
56
+ const TESTNET_MOVE_CODER = new MoveCoder(AptosNetwork.TEST_NET)
55
57
 
56
- export function defaultMoveCoder(): MoveCoder {
57
- return MOVE_CODER
58
+ export function defaultMoveCoder(network: AptosNetwork = AptosNetwork.MAIN_NET): MoveCoder {
59
+ if (network == AptosNetwork.MAIN_NET) {
60
+ return MOVE_CODER
61
+ }
62
+ return TESTNET_MOVE_CODER
58
63
  }
@@ -12,6 +12,7 @@ import { format } from 'prettier'
12
12
  import { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER, VECTOR_STR } from './utils.js'
13
13
  import { camelCase, upperFirst } from 'lodash-es'
14
14
  import { TypeDescriptor } from './types.js'
15
+ import { ChainAdapter } from './chain-adapter.js'
15
16
 
16
17
  interface OutputFile {
17
18
  fileName: string
@@ -33,14 +34,13 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
33
34
  GENERATE_CLIENT = false
34
35
  GENERATE_ON_ENTRY = true
35
36
  PAYLOAD_OPTIONAL = false
37
+ SYSTEM_MODULES = new Set(['0x1', '0x2', '0x3'])
36
38
 
37
- abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
38
- abstract toInternalModules(modules: ModuleTypes): InternalMoveModule[]
39
- // Get the structs that represent Events
40
- abstract getEventStructs(module: InternalMoveModule): Map<string, InternalMoveStruct>
41
- // Get the parameters that actually have arguments in runtime
42
- // Aptos first signer and Sui's last TxContext are no use
43
- abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
39
+ chainAdapter: ChainAdapter<NetworkType, ModuleTypes>
40
+
41
+ protected constructor(chainAdapter: ChainAdapter<NetworkType, ModuleTypes>) {
42
+ this.chainAdapter = chainAdapter
43
+ }
44
44
 
45
45
  readModulesFile(fullPath: string) {
46
46
  return JSON.parse(fs.readFileSync(fullPath, 'utf-8'))
@@ -70,9 +70,9 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
70
70
  const loader = new AccountRegister()
71
71
 
72
72
  // when generating user code, don't need to generate framework account
73
- loader.accountImports.set('0x1', new AccountModulesImportInfo('0x1', '0x1'))
74
- loader.accountImports.set('0x2', new AccountModulesImportInfo('0x2', '0x2'))
75
- loader.accountImports.set('0x3', new AccountModulesImportInfo('0x3', '0x3'))
73
+ for (const sysModule of this.SYSTEM_MODULES) {
74
+ loader.accountImports.set(sysModule, new AccountModulesImportInfo(sysModule, sysModule))
75
+ }
76
76
  // const client = getRpcClient(network)
77
77
 
78
78
  for (const file of files) {
@@ -81,7 +81,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
81
81
  }
82
82
  const fullPath = path.resolve(srcDir, file)
83
83
  const abi = this.readModulesFile(fullPath)
84
- const modules = this.toInternalModules(abi)
84
+ const modules = this.chainAdapter.toInternalModules(abi)
85
85
 
86
86
  for (const module of modules) {
87
87
  loader.register(module, path.basename(file, '.json'))
@@ -100,8 +100,8 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
100
100
  console.log(`download dependent module for account ${account} at ${network}`)
101
101
 
102
102
  try {
103
- const rawModules = await this.fetchModules(account, network)
104
- const modules = this.toInternalModules(rawModules)
103
+ const rawModules = await this.chainAdapter.fetchModules(account, network)
104
+ const modules = this.chainAdapter.toInternalModules(rawModules)
105
105
 
106
106
  fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\t'))
107
107
  for (const module of modules) {
@@ -163,7 +163,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
163
163
  const clientFunctions = this.GENERATE_CLIENT
164
164
  ? module.exposedFunctions.map((f) => this.generateClientFunctions(module, f)).filter((s) => s !== '')
165
165
  : []
166
- const eventStructs = this.getEventStructs(module)
166
+ const eventStructs = this.chainAdapter.getEventStructs(module)
167
167
  const eventTypes = new Set(eventStructs.keys())
168
168
  const events = Array.from(eventStructs.values())
169
169
  .map((e) => this.generateOnEvents(module, e))
@@ -282,7 +282,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
282
282
  return
283
283
  }
284
284
 
285
- const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
285
+ const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
286
286
  return this.generateTypeForDescriptor(param, module.address) + (this.PAYLOAD_OPTIONAL ? ' | undefined' : '')
287
287
  })
288
288
 
@@ -307,7 +307,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
307
307
  }
308
308
  // const moduleName = normalizeToJSName(module.name)
309
309
  const funcName = camelCase(func.name)
310
- const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
310
+ const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
311
311
  return this.generateTypeForDescriptor(param, module.address)
312
312
  })
313
313
  const genericString = this.generateFunctionTypeParameters(func)
@@ -514,6 +514,17 @@ export class AccountCodegen<ModuleType, NetworkType> {
514
514
  }
515
515
  }
516
516
 
517
+ let loadAllTypes = `loadAllTypes(defaultMoveCoder(${
518
+ this.moduleGen.PREFIX
519
+ }Network.${this.moduleGen.generateNetworkOption(this.config.network)}))`
520
+
521
+ if (this.moduleGen.SYSTEM_MODULES.has(address)) {
522
+ loadAllTypes = `
523
+ loadAllTypes(defaultMoveCoder(${this.moduleGen.PREFIX}Network.MAIN_NET))
524
+ loadAllTypes(defaultMoveCoder(${this.moduleGen.PREFIX}Network.TEST_NET))
525
+ `
526
+ }
527
+
517
528
  const source = `
518
529
  /* Autogenerated file. Do not edit manually. */
519
530
  /* tslint:disable */
@@ -536,7 +547,7 @@ export class AccountCodegen<ModuleType, NetworkType> {
536
547
  }
537
548
  }
538
549
 
539
- loadAllTypes(defaultMoveCoder())
550
+ ${loadAllTypes}
540
551
  ` // source
541
552
 
542
553
  return [
@@ -2,6 +2,7 @@ import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'
2
2
  import { parseMoveType, TypeDescriptor } from './types.js'
3
3
  import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
4
4
  import { bytesToBigInt } from '../utils/index.js'
5
+ import { ChainAdapter } from './chain-adapter.js'
5
6
 
6
7
  export type StructWithTag<Base> = {
7
8
  type: string
@@ -13,16 +14,22 @@ export type DecodedStructWithTag<B, T> = StructWithTag<B> & {
13
14
  type_arguments: string[]
14
15
  }
15
16
 
16
- export abstract class AbstractMoveCoder<StructType> {
17
+ export abstract class AbstractMoveCoder<Network, ModuleTypes, StructType> {
17
18
  private moduleMapping = new Map<string, InternalMoveModule>()
18
19
  private typeMapping = new Map<string, InternalMoveStruct>()
19
20
  private funcMapping = new Map<string, InternalMoveFunction>()
21
+ network: Network
22
+ adapter: ChainAdapter<Network, ModuleTypes>
23
+
24
+ protected constructor(network: Network) {
25
+ this.network = network
26
+ }
20
27
 
21
28
  contains(account: string, name: string) {
22
29
  return this.moduleMapping.has(account + '::' + name)
23
30
  }
24
31
 
25
- protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
32
+ // protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
26
33
 
27
34
  protected toStructWithTag(val: StructType): StructWithTag<StructType> {
28
35
  return val as any
@@ -232,7 +239,7 @@ export abstract class AbstractMoveCoder<StructType> {
232
239
 
233
240
  encodeCallArgs(args: any[], func: string): any[] {
234
241
  const f = this.getMoveFunction(func)
235
- return this.encodeArray(args, this.getMeaningfulFunctionParams(f.params))
242
+ return this.encodeArray(args, this.adapter.getMeaningfulFunctionParams(f.params))
236
243
  }
237
244
 
238
245
  decodeCallResult(res: any[], func: string): any[] {
@@ -0,0 +1,14 @@
1
+ import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'
2
+ import { TypeDescriptor } from './types.js'
3
+
4
+ export abstract class ChainAdapter<NetworkType, ModuleTypes> {
5
+ abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
6
+
7
+ abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
8
+ abstract toInternalModules(modules: ModuleTypes): InternalMoveModule[]
9
+ // Get the structs that represent Events
10
+ abstract getEventStructs(module: InternalMoveModule): Map<string, InternalMoveStruct>
11
+ // Get the parameters that actually have arguments in runtime
12
+ // Aptos first signer and Sui's last TxContext are no use
13
+ abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
14
+ }
package/src/move/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from './filter.js'
2
2
  export * from './types.js'
3
3
  export * from './utils.js'
4
4
  export * from './account.js'
5
+ export * from './chain-adapter.js'
@@ -268,4 +268,5 @@ export function loadAllTypes(coder: MoveCoder) {
268
268
  }
269
269
  }
270
270
 
271
- loadAllTypes(defaultMoveCoder());
271
+ loadAllTypes(defaultMoveCoder(SuiNetwork.MAIN_NET));
272
+ loadAllTypes(defaultMoveCoder(SuiNetwork.TEST_NET));
@@ -1553,4 +1553,5 @@ export function loadAllTypes(coder: MoveCoder) {
1553
1553
  }
1554
1554
  }
1555
1555
 
1556
- loadAllTypes(defaultMoveCoder());
1556
+ loadAllTypes(defaultMoveCoder(SuiNetwork.MAIN_NET));
1557
+ loadAllTypes(defaultMoveCoder(SuiNetwork.TEST_NET));
@@ -1577,4 +1577,5 @@ export function loadAllTypes(coder: MoveCoder) {
1577
1577
  }
1578
1578
  }
1579
1579
 
1580
- loadAllTypes(defaultMoveCoder());
1580
+ loadAllTypes(defaultMoveCoder(SuiNetwork.MAIN_NET));
1581
+ loadAllTypes(defaultMoveCoder(SuiNetwork.TEST_NET));
@@ -1,14 +1,13 @@
1
- import { Connection, JsonRpcProvider, SuiMoveNormalizedModules } from '@mysten/sui.js'
1
+ import { SuiMoveNormalizedModules } from '@mysten/sui.js'
2
2
 
3
3
  import { SuiNetwork } from '../network.js'
4
4
  import * as fs from 'fs'
5
5
  import chalk from 'chalk'
6
6
  import { InternalMoveModule, InternalMoveStruct } from '../../move/internal-models.js'
7
7
  import { AbstractCodegen } from '../../move/abstract-codegen.js'
8
- import { toInternalModule } from '../move-types.js'
9
- import { moduleQname, SPLITTER, structQname, TypeDescriptor } from '../../move/index.js'
10
- import { getMeaningfulFunctionParams } from '../utils.js'
8
+ import { structQname } from '../../move/index.js'
11
9
  import { join } from 'path'
10
+ import { SuiChainAdapter } from '../sui-chain-adapter.js'
12
11
 
13
12
  export async function codegen(abisDir: string, outDir = join('src', 'types', 'sui'), genExample = false) {
14
13
  if (!fs.existsSync(abisDir)) {
@@ -19,18 +18,6 @@ export async function codegen(abisDir: string, outDir = join('src', 'types', 'su
19
18
  console.log(chalk.green(`Generated ${numFiles} for Sui`))
20
19
  }
21
20
 
22
- function getRpcEndpoint(network: SuiNetwork): string {
23
- switch (network) {
24
- case SuiNetwork.TEST_NET:
25
- return 'https://fullnode.testnet.sui.io/'
26
- }
27
- return 'https://fullnode.mainnet.sui.io/'
28
- }
29
-
30
- function getRpcClient(network: SuiNetwork): JsonRpcProvider {
31
- return new JsonRpcProvider(new Connection({ fullnode: getRpcEndpoint(network) }))
32
- }
33
-
34
21
  class SuiCodegen extends AbstractCodegen<SuiMoveNormalizedModules, SuiNetwork> {
35
22
  ADDRESS_TYPE = 'SuiAddress'
36
23
  MAIN_NET = SuiNetwork.MAIN_NET
@@ -40,30 +27,8 @@ class SuiCodegen extends AbstractCodegen<SuiMoveNormalizedModules, SuiNetwork> {
40
27
  // GENERATE_ON_ENTRY = true
41
28
  PAYLOAD_OPTIONAL = true
42
29
 
43
- async fetchModules(account: string, network: SuiNetwork): Promise<SuiMoveNormalizedModules> {
44
- const client = getRpcClient(network)
45
- return await client.getNormalizedMoveModulesByPackage({ package: account })
46
- }
47
-
48
- getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
49
- return getMeaningfulFunctionParams(params)
50
- }
51
-
52
- toInternalModules(modules: SuiMoveNormalizedModules): InternalMoveModule[] {
53
- return Object.values(modules).map(toInternalModule)
54
- }
55
-
56
- getEventStructs(module: InternalMoveModule) {
57
- const qname = moduleQname(module)
58
- const eventMap = new Map<string, InternalMoveStruct>()
59
-
60
- for (const struct of module.structs) {
61
- const abilities = new Set(struct.abilities)
62
- if (abilities.has('Drop') && abilities.has('Copy')) {
63
- eventMap.set(qname + SPLITTER + struct.name, struct)
64
- }
65
- }
66
- return eventMap
30
+ constructor() {
31
+ super(new SuiChainAdapter())
67
32
  }
68
33
 
69
34
  readModulesFile(fullPath: string) {
@@ -2,6 +2,7 @@ import { RecordMetaData } from '@sentio/protos'
2
2
  import { type Labels, BaseContext, normalizeLabels } from '../index.js'
3
3
  import { SuiNetwork, getChainId } from './network.js'
4
4
  import { SuiTransactionBlockResponse } from '@mysten/sui.js'
5
+ import { MoveCoder, defaultMoveCoder } from './move-coder.js'
5
6
 
6
7
  export class SuiContext extends BaseContext {
7
8
  address: string
@@ -11,6 +12,7 @@ export class SuiContext extends BaseContext {
11
12
  slot: bigint
12
13
  transaction: SuiTransactionBlockResponse
13
14
  eventIndex: number
15
+ coder: MoveCoder
14
16
 
15
17
  constructor(
16
18
  moduleName: string,
@@ -28,6 +30,7 @@ export class SuiContext extends BaseContext {
28
30
  this.timestamp = timestamp
29
31
  this.slot = slot
30
32
  this.eventIndex = eventIndex
33
+ this.coder = defaultMoveCoder(network)
31
34
  if (transaction) {
32
35
  this.transaction = transaction
33
36
  }
@@ -2,18 +2,25 @@ import { TypedEventInstance, TypedFunctionPayload, TypedSuiMoveObject } from './
2
2
  import { AbstractMoveCoder, StructWithTag } from '../move/abstract-move-coder.js'
3
3
  import {
4
4
  MoveCallSuiTransaction,
5
+ SuiCallArg,
5
6
  SuiEvent,
6
7
  SuiMoveNormalizedModule,
7
- SuiCallArg,
8
+ SuiMoveNormalizedModules,
8
9
  SuiMoveObject,
9
10
  SuiRawData,
10
11
  } from '@mysten/sui.js'
11
12
  import { toInternalModule } from './move-types.js'
12
13
  import { SPLITTER, TypeDescriptor } from '../move/index.js'
13
- import { getMeaningfulFunctionParams } from './utils.js'
14
14
  import { dynamic_field } from './builtin/0x2.js'
15
+ import { SuiNetwork } from './network.js'
16
+ import { SuiChainAdapter } from './sui-chain-adapter.js'
17
+
18
+ export class MoveCoder extends AbstractMoveCoder<SuiNetwork, SuiMoveNormalizedModules, SuiEvent | SuiMoveObject> {
19
+ constructor(network: SuiNetwork) {
20
+ super(network)
21
+ this.adapter = new SuiChainAdapter()
22
+ }
15
23
 
16
- export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
17
24
  load(module: SuiMoveNormalizedModule) {
18
25
  if (this.contains(module.address, module.name)) {
19
26
  return
@@ -65,14 +72,10 @@ export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
65
72
  return this.filterAndDecodeStruct(typeQname, objects)
66
73
  }
67
74
 
68
- getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
69
- return getMeaningfulFunctionParams(params)
70
- }
71
-
72
75
  decodeFunctionPayload(payload: MoveCallSuiTransaction, inputs: SuiCallArg[]): MoveCallSuiTransaction {
73
76
  const functionType = [payload.package, payload.module, payload.function].join(SPLITTER)
74
77
  const func = this.getMoveFunction(functionType)
75
- const params = getMeaningfulFunctionParams(func.params)
78
+ const params = this.adapter.getMeaningfulFunctionParams(func.params)
76
79
  const args = []
77
80
  for (const value of payload.arguments || []) {
78
81
  const argValue = value as any
@@ -101,8 +104,12 @@ export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
101
104
  }
102
105
  }
103
106
 
104
- export const MOVE_CODER = new MoveCoder()
107
+ const MOVE_CODER = new MoveCoder(SuiNetwork.MAIN_NET)
108
+ const TESTNET_MOVE_CODER = new MoveCoder(SuiNetwork.TEST_NET)
105
109
 
106
- export function defaultMoveCoder(): MoveCoder {
107
- return MOVE_CODER
110
+ export function defaultMoveCoder(network: SuiNetwork = SuiNetwork.MAIN_NET): MoveCoder {
111
+ if (network == SuiNetwork.MAIN_NET) {
112
+ return MOVE_CODER
113
+ }
114
+ return TESTNET_MOVE_CODER
108
115
  }