@sentio/sdk 2.5.4-rc.1 → 2.5.5-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 (74) hide show
  1. package/lib/aptos/builtin/0x1.d.ts +70 -76
  2. package/lib/aptos/builtin/0x1.js +4 -4
  3. package/lib/aptos/builtin/0x1.js.map +1 -1
  4. package/lib/aptos/builtin/0x3.d.ts +14 -32
  5. package/lib/aptos/builtin/0x3.js.map +1 -1
  6. package/lib/aptos/codegen/codegen.js +1 -0
  7. package/lib/aptos/codegen/codegen.js.map +1 -1
  8. package/lib/aptos/codegen/types.test.js.map +1 -1
  9. package/lib/aptos/index.d.ts +2 -1
  10. package/lib/aptos/index.js +1 -0
  11. package/lib/aptos/index.js.map +1 -1
  12. package/lib/aptos/models.d.ts +1 -1
  13. package/lib/aptos/models.js.map +1 -1
  14. package/lib/aptos/module-client.d.ts +6 -0
  15. package/lib/aptos/module-client.js +25 -0
  16. package/lib/aptos/module-client.js.map +1 -0
  17. package/lib/aptos/module-client.test.d.ts +1 -0
  18. package/lib/aptos/module-client.test.js.map +1 -0
  19. package/lib/aptos/move-coder.d.ts +2 -0
  20. package/lib/aptos/move-coder.js +7 -15
  21. package/lib/aptos/move-coder.js.map +1 -1
  22. package/lib/aptos/network.d.ts +2 -0
  23. package/lib/aptos/network.js +1 -0
  24. package/lib/aptos/network.js.map +1 -1
  25. package/lib/move/abstract-codegen.d.ts +2 -0
  26. package/lib/move/abstract-codegen.js +44 -7
  27. package/lib/move/abstract-codegen.js.map +1 -1
  28. package/lib/move/abstract-move-coder.d.ts +12 -5
  29. package/lib/move/abstract-move-coder.js +100 -6
  30. package/lib/move/abstract-move-coder.js.map +1 -1
  31. package/lib/move/ts-type.js +1 -1
  32. package/lib/move/ts-type.js.map +1 -1
  33. package/lib/move/utils.js +1 -1
  34. package/lib/move/utils.js.map +1 -1
  35. package/lib/sui/builtin/0x1.js.map +1 -1
  36. package/lib/sui/builtin/0x2.d.ts +49 -49
  37. package/lib/sui/builtin/0x2.js.map +1 -1
  38. package/lib/sui/codegen/codegen.js +2 -4
  39. package/lib/sui/codegen/codegen.js.map +1 -1
  40. package/lib/sui/index.d.ts +1 -0
  41. package/lib/sui/index.js +1 -0
  42. package/lib/sui/index.js.map +1 -1
  43. package/lib/sui/models.d.ts +1 -1
  44. package/lib/sui/models.js.map +1 -1
  45. package/lib/sui/module-client.d.ts +3 -0
  46. package/lib/sui/module-client.js +7 -0
  47. package/lib/sui/module-client.js.map +1 -0
  48. package/lib/sui/move-coder.d.ts +2 -0
  49. package/lib/sui/move-coder.js +8 -17
  50. package/lib/sui/move-coder.js.map +1 -1
  51. package/lib/sui/utils.d.ts +2 -0
  52. package/lib/sui/utils.js +6 -0
  53. package/lib/sui/utils.js.map +1 -1
  54. package/package.json +4 -4
  55. package/src/aptos/builtin/0x1.ts +71 -81
  56. package/src/aptos/builtin/0x3.ts +15 -21
  57. package/src/aptos/codegen/codegen.ts +1 -0
  58. package/src/aptos/index.ts +2 -1
  59. package/src/aptos/models.ts +1 -1
  60. package/src/aptos/module-client.ts +30 -0
  61. package/src/aptos/move-coder.ts +11 -17
  62. package/src/aptos/network.ts +2 -0
  63. package/src/move/abstract-codegen.ts +55 -9
  64. package/src/move/abstract-move-coder.ts +116 -10
  65. package/src/move/ts-type.ts +1 -1
  66. package/src/move/utils.ts +1 -1
  67. package/src/sui/builtin/0x1.ts +2 -2
  68. package/src/sui/builtin/0x2.ts +50 -51
  69. package/src/sui/codegen/codegen.ts +2 -4
  70. package/src/sui/index.ts +2 -0
  71. package/src/sui/models.ts +1 -1
  72. package/src/sui/module-client.ts +7 -0
  73. package/src/sui/move-coder.ts +11 -21
  74. package/src/sui/utils.ts +8 -0
@@ -0,0 +1,30 @@
1
+ import { defaultMoveCoder } from './index.js'
2
+ import { AptosClient, Types } from 'aptos-sdk'
3
+
4
+ export class ModuleClient {
5
+ client: AptosClient
6
+ constructor(client: AptosClient | string) {
7
+ if (typeof client === 'string') {
8
+ this.client = new AptosClient(client)
9
+ } else {
10
+ this.client = client
11
+ }
12
+ }
13
+ public async viewDecoded(
14
+ func: string,
15
+ typeArguments: string[],
16
+ args: any[],
17
+ ledger_version?: bigint
18
+ ): Promise<any[]> {
19
+ const coder = defaultMoveCoder()
20
+ const encodedArgs = coder.encodeCallArgs(args, func)
21
+
22
+ const request: Types.ViewRequest = {
23
+ function: func,
24
+ type_arguments: typeArguments,
25
+ arguments: encodedArgs,
26
+ }
27
+ const res = await this.client.view(request, ledger_version?.toString())
28
+ return coder.decodeCallResult(res, func)
29
+ }
30
+ }
@@ -6,9 +6,10 @@ import {
6
6
  TransactionPayload_EntryFunctionPayload,
7
7
  } from './move-types.js'
8
8
 
9
- import { TypedEventInstance, TypedMoveResource, TypedEntryFunctionPayload } from './models.js'
9
+ import { TypedEventInstance, TypedMoveResource, TypedFunctionPayload } from './models.js'
10
10
  import { getMeaningfulFunctionParams } from './utils.js'
11
11
  import { AbstractMoveCoder } from '../move/abstract-move-coder.js'
12
+ import { TypeDescriptor } from '../move/index.js'
12
13
 
13
14
  export class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {
14
15
  load(module: MoveModuleBytecode) {
@@ -34,26 +35,19 @@ export class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {
34
35
  return this.filterAndDecodeInternal(typeQname, resources)
35
36
  }
36
37
 
37
- decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {
38
- const argumentsTyped: any[] = []
38
+ getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
39
+ return getMeaningfulFunctionParams(params)
40
+ }
39
41
 
40
- try {
41
- const func = this.getMoveFunction(payload.function)
42
- const params = getMeaningfulFunctionParams(func.params)
43
- for (const [idx, arg] of payload.arguments.entries()) {
44
- // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
45
- const argType = params[idx]
46
- argumentsTyped.push(this.decode(arg, argType))
47
- }
48
- } catch (e) {
49
- console.error('Decoding error for ', JSON.stringify(payload), e)
50
- return payload
51
- }
42
+ decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {
43
+ const func = this.getMoveFunction(payload.function)
44
+ const params = getMeaningfulFunctionParams(func.params)
45
+ const argumentsDecoded = this.decodeArray(payload.arguments, params)
52
46
 
53
47
  return {
54
48
  ...payload,
55
- arguments_decoded: argumentsTyped,
56
- } as TypedEntryFunctionPayload<any>
49
+ arguments_decoded: argumentsDecoded,
50
+ } as TypedFunctionPayload<any>
57
51
  }
58
52
  }
59
53
 
@@ -1,4 +1,5 @@
1
1
  import { CHAIN_IDS, getChainName } from '../core/chain.js'
2
+ import { AptosClient } from 'aptos-sdk'
2
3
 
3
4
  export enum AptosNetwork {
4
5
  MAIN_NET = 1,
@@ -25,5 +26,6 @@ export function getAptosChainName(network: AptosNetwork): string {
25
26
  export class AptosBindOptions {
26
27
  address: string
27
28
  network?: AptosNetwork = AptosNetwork.MAIN_NET
29
+ client?: AptosClient
28
30
  startVersion?: bigint | number
29
31
  }
@@ -1,4 +1,9 @@
1
- import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
1
+ import {
2
+ InternalMoveFunction,
3
+ InternalMoveFunctionVisibility,
4
+ InternalMoveModule,
5
+ InternalMoveStruct,
6
+ } from './internal-models.js'
2
7
  import path from 'path'
3
8
  import fs from 'fs'
4
9
  import { AccountModulesImportInfo, AccountRegister } from './account.js'
@@ -26,6 +31,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
26
31
  ADDRESS_TYPE: string
27
32
  PREFIX: string
28
33
  STRUCT_FIELD_NAME: string = 'data'
34
+ GENERATED_CLIENT = false
29
35
 
30
36
  abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
31
37
  abstract toInternalModules(modules: ModuleTypes): InternalMoveModule[]
@@ -48,9 +54,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
48
54
  if (!fs.existsSync(srcDir)) {
49
55
  return
50
56
  }
51
- if (network === this.TEST_NET) {
52
- console.log('Found testnet directory, generate code for testnet modules')
53
- }
57
+
54
58
  const files = fs.readdirSync(srcDir)
55
59
  outputDir = path.resolve(outputDir)
56
60
  const outputs: OutputFile[] = []
@@ -118,6 +122,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
118
122
  }
119
123
 
120
124
  for (const output of outputs) {
125
+ // const content = output.fileContent
121
126
  const content = format(output.fileContent, { parser: 'typescript' })
122
127
  fs.writeFileSync(path.join(outputDir, output.fileName), content)
123
128
  }
@@ -132,7 +137,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
132
137
  )
133
138
  for (const output of outputs) {
134
139
  const parsed = path.parse(output.fileName)
135
- const content = `export * as _${parsed.name} from './${parsed.name}.js'\n`
140
+ const content = `export * as _${parsed.name.replaceAll('-', '_')} from './${parsed.name}.js'\n`
136
141
  fs.appendFileSync(rootFile, content)
137
142
  }
138
143
  }
@@ -149,7 +154,9 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
149
154
  const functions = module.exposedFunctions
150
155
  .map((f) => this.generateOnEntryFunctions(module, f))
151
156
  .filter((s) => s !== '')
152
-
157
+ const clientFunctions = module.exposedFunctions
158
+ .map((f) => this.generateClientFunctions(module, f))
159
+ .filter((s) => s !== '')
153
160
  const eventStructs = this.getEventStructs(module)
154
161
  const eventTypes = new Set(eventStructs.keys())
155
162
  const events = Array.from(eventStructs.values())
@@ -160,6 +167,16 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
160
167
 
161
168
  const moduleName = normalizeToJSName(module.name)
162
169
  let processor = ''
170
+ let client = ''
171
+
172
+ if (this.GENERATED_CLIENT && clientFunctions.length > 0) {
173
+ client = `
174
+ export class ${moduleName}_client extends ModuleClient {
175
+ ${clientFunctions.join('\n')}
176
+ }
177
+ `
178
+ }
179
+
163
180
  if (functions.length > 0 || events.length > 0) {
164
181
  processor = `export class ${moduleName} extends ${this.PREFIX}BaseProcessor {
165
182
 
@@ -183,6 +200,8 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
183
200
  }
184
201
 
185
202
  return `
203
+ ${client}
204
+
186
205
  ${processor}
187
206
 
188
207
  export namespace ${moduleName} {
@@ -266,13 +285,40 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
266
285
  const genericString = this.generateFunctionTypeParameters(func)
267
286
  return `
268
287
  export interface ${camelFuncName}Payload${genericString}
269
- extends TypedEntryFunctionPayload<[${fields.join(',')}]> {
288
+ extends TypedFunctionPayload<[${fields.join(',')}]> {
270
289
  arguments_decoded: [${fields.join(',')}],
271
290
  type_arguments: [${func.typeParams.map((_) => 'string').join(', ')}]
272
291
  }
273
292
  `
274
293
  }
275
294
 
295
+ generateClientFunctions(module: InternalMoveModule, func: InternalMoveFunction) {
296
+ if (func.visibility === InternalMoveFunctionVisibility.PRIVATE) {
297
+ return ''
298
+ }
299
+ if (func.isEntry) {
300
+ return ''
301
+ }
302
+ // const moduleName = normalizeToJSName(module.name)
303
+ const funcName = camelCase(func.name)
304
+ const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
305
+ return generateTypeForDescriptor(param, module.address, this.ADDRESS_TYPE)
306
+ })
307
+ const genericString = this.generateFunctionTypeParameters(func)
308
+
309
+ const returns = func.return.map((param) => {
310
+ return generateTypeForDescriptor(param, module.address, this.ADDRESS_TYPE)
311
+ })
312
+
313
+ const source = `
314
+ ${funcName}${genericString}(type_arguments: [${func.typeParams
315
+ .map((_) => 'string')
316
+ .join(', ')}], args: [${fields.join(',')}], version?: bigint): Promise<[${returns.join(',')}]> {
317
+ return this.viewDecoded('${module.address}::${module.name}::${func.name}', type_arguments, args, version) as any
318
+ }`
319
+ return source
320
+ }
321
+
276
322
  generateOnEntryFunctions(module: InternalMoveModule, func: InternalMoveFunction) {
277
323
  if (!func.isEntry) {
278
324
  return ''
@@ -359,10 +405,10 @@ export class AccountCodegen<ModuleType, NetworkType> {
359
405
  import { CallFilter } from "@sentio/sdk/move"
360
406
  import {
361
407
  MoveCoder, defaultMoveCoder, ${this.moduleGen.PREFIX}BindOptions, ${this.moduleGen.PREFIX}BaseProcessor,
362
- TypedEventInstance, ${this.moduleGen.PREFIX}Network, TypedEntryFunctionPayload,
408
+ TypedEventInstance, ${this.moduleGen.PREFIX}Network, TypedFunctionPayload,
363
409
  ${this.moduleGen.PREFIX}Context } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
364
410
  import { MoveFetchConfig } from "@sentio/protos"
365
- import { ${this.moduleGen.ADDRESS_TYPE} } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
411
+ import { ${this.moduleGen.ADDRESS_TYPE}, ModuleClient } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
366
412
  `
367
413
 
368
414
  const dependedAccounts: string[] = []
@@ -22,7 +22,9 @@ export abstract class AbstractMoveCoder<StructType> {
22
22
  return this.moduleMapping.has(account + '::' + name)
23
23
  }
24
24
 
25
- loadInternal(module: InternalMoveModule) {
25
+ protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
26
+
27
+ protected loadInternal(module: InternalMoveModule) {
26
28
  if (this.contains(module.address, module.name)) {
27
29
  return
28
30
  }
@@ -35,15 +37,15 @@ export abstract class AbstractMoveCoder<StructType> {
35
37
  }
36
38
 
37
39
  for (const func of module.exposedFunctions) {
38
- if (!func.isEntry) {
39
- continue
40
- }
40
+ // if (!func.isEntry) {
41
+ // continue
42
+ // }
41
43
  const key = [module.address, module.name, func.name].join(SPLITTER)
42
44
  this.funcMapping.set(key, func)
43
45
  }
44
46
  }
45
47
 
46
- protected dataToBigInt(data: any): bigint {
48
+ protected decodeBigInt(data: any): bigint {
47
49
  if (Array.isArray(data)) {
48
50
  // Only sui function need this, strange
49
51
  const bytes = data as number[]
@@ -53,7 +55,11 @@ export abstract class AbstractMoveCoder<StructType> {
53
55
  }
54
56
  }
55
57
 
56
- protected getMoveStruct(type: string): InternalMoveStruct {
58
+ protected encodeBigInt(data: bigint): any {
59
+ return '0x' + data.toString(16)
60
+ }
61
+
62
+ getMoveStruct(type: string): InternalMoveStruct {
57
63
  const struct = this.typeMapping.get(type)
58
64
  if (!struct) {
59
65
  throw new Error('Failed to load type' + type + ' type are not imported anywhere')
@@ -61,15 +67,15 @@ export abstract class AbstractMoveCoder<StructType> {
61
67
  return struct
62
68
  }
63
69
 
64
- protected getMoveFunction(type: string): InternalMoveFunction {
70
+ getMoveFunction(type: string): InternalMoveFunction {
65
71
  const func = this.funcMapping.get(type)
66
72
  if (!func) {
67
- throw new Error('Failed to load function' + type + ' type are not imported anywhere')
73
+ throw new Error('Failed to load function ' + type + ' type are not imported anywhere')
68
74
  }
69
75
  return func
70
76
  }
71
77
 
72
- protected decode(data: any, type: TypeDescriptor): any {
78
+ decode(data: any, type: TypeDescriptor): any {
73
79
  // process simple type
74
80
  if (type.reference) {
75
81
  return data
@@ -94,7 +100,7 @@ export abstract class AbstractMoveCoder<StructType> {
94
100
  case 'U64':
95
101
  case 'u128':
96
102
  case 'U128':
97
- return this.dataToBigInt(data)
103
+ return this.decodeBigInt(data)
98
104
  }
99
105
 
100
106
  // process vector
@@ -130,6 +136,106 @@ export abstract class AbstractMoveCoder<StructType> {
130
136
  return typedData
131
137
  }
132
138
 
139
+ encode(data: any, type: TypeDescriptor): any {
140
+ // process simple type
141
+ if (type.reference) {
142
+ return data
143
+ }
144
+ switch (type.qname) {
145
+ case 'signer': // TODO check this, aptos only
146
+ case 'address':
147
+ case 'Address':
148
+ case '0x2::object::ID':
149
+ case '0x2::coin::Coin':
150
+ case '0x1::string::String':
151
+ case 'bool':
152
+ case 'Bool':
153
+ case 'u8':
154
+ case 'U8':
155
+ case 'u16':
156
+ case 'U16':
157
+ case 'u32':
158
+ case 'U32':
159
+ return data
160
+ case 'u64':
161
+ case 'U64':
162
+ case 'u128':
163
+ case 'U128':
164
+ return this.encodeBigInt(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' || type.typeArgs[0].qname === 'U8') {
171
+ return data
172
+ }
173
+
174
+ const res = []
175
+ for (const entry of data) {
176
+ res.push(this.encode(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 = field.type
193
+ filedType = filedType.applyTypeArgs(typeCtx)
194
+ const value = this.encode(data[field.name], filedType)
195
+ typedData[field.name] = value
196
+ }
197
+ return typedData
198
+ }
199
+
200
+ decodeArray(entries: any[], types: TypeDescriptor[]): any[] {
201
+ const entriesDecoded: any[] = []
202
+ for (const [idx, arg] of entries.entries()) {
203
+ // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
204
+ const argType = types[idx]
205
+ try {
206
+ entriesDecoded.push(this.decode(arg, argType))
207
+ } catch (e) {
208
+ console.error(e, 'Decoding error for ', JSON.stringify(arg), 'using type', argType)
209
+ return entries
210
+ }
211
+ }
212
+ return entriesDecoded
213
+ }
214
+
215
+ encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): any[] {
216
+ const entries: any[] = []
217
+ for (const [idx, arg] of entriesDecoded.entries()) {
218
+ // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
219
+ const argType = types[idx]
220
+ try {
221
+ entries.push(this.encode(arg, argType))
222
+ } catch (e) {
223
+ throw Error('Decoding error for ' + JSON.stringify(arg) + 'using type' + argType + e.toString())
224
+ }
225
+ }
226
+ return entries
227
+ }
228
+
229
+ encodeCallArgs(args: any[], func: string): any[] {
230
+ const f = this.getMoveFunction(func)
231
+ return this.encodeArray(args, this.getMeaningfulFunctionParams(f.params))
232
+ }
233
+
234
+ decodeCallResult(res: any[], func: string): any[] {
235
+ const f = this.getMoveFunction(func)
236
+ return this.decodeArray(res, f.return)
237
+ }
238
+
133
239
  protected filterAndDecodeInternal<T>(
134
240
  typeQname: string,
135
241
  structs: StructWithTag<StructType>[]
@@ -52,7 +52,7 @@ export function generateTypeForDescriptor(type: TypeDescriptor, currentAddress:
52
52
  if (simpleName.length === 0) {
53
53
  console.error('unexpected error')
54
54
  }
55
- if (simpleName.toLowerCase().includes(VECTOR_STR)) {
55
+ if (simpleName.toLowerCase() === VECTOR_STR || simpleName.toLowerCase().startsWith(VECTOR_STR + SPLITTER)) {
56
56
  console.error('unexpected vector type error')
57
57
  }
58
58
  if (type.typeArgs.length > 0) {
package/src/move/utils.ts CHANGED
@@ -6,7 +6,7 @@ export function isFrameworkAccount(account: string) {
6
6
  return account === '0x1' || account === '0x2' || account === '0x3'
7
7
  }
8
8
 
9
- const KEYWORDS = new Set(['package', 'namespace', 'volatile', 'object', 'string', 'number', 'bigint'])
9
+ const KEYWORDS = new Set(['package', 'namespace', 'volatile', 'object', 'string', 'number', 'bigint', 'any'])
10
10
 
11
11
  export function normalizeToJSName(name: string) {
12
12
  if (KEYWORDS.has(name)) {
@@ -12,11 +12,11 @@ import {
12
12
  SuiBaseProcessor,
13
13
  TypedEventInstance,
14
14
  SuiNetwork,
15
- TypedEntryFunctionPayload,
15
+ TypedFunctionPayload,
16
16
  SuiContext,
17
17
  } from "@sentio/sdk/sui";
18
18
  import { MoveFetchConfig } from "@sentio/protos";
19
- import { SuiAddress } from "@sentio/sdk/sui";
19
+ import { SuiAddress, ModuleClient } from "@sentio/sdk/sui";
20
20
 
21
21
  export namespace address {}
22
22