@typemove/aptos 1.0.0-rc.13

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 (67) hide show
  1. package/LICENSE +201 -0
  2. package/dist/aptos-chain-adapter.d.ts +12 -0
  3. package/dist/aptos-chain-adapter.d.ts.map +1 -0
  4. package/dist/aptos-chain-adapter.js +64 -0
  5. package/dist/aptos-chain-adapter.js.map +1 -0
  6. package/dist/builtin/0x1.d.ts +2274 -0
  7. package/dist/builtin/0x1.d.ts.map +1 -0
  8. package/dist/builtin/0x1.js +2126 -0
  9. package/dist/builtin/0x1.js.map +1 -0
  10. package/dist/builtin/0x3.d.ts +514 -0
  11. package/dist/builtin/0x3.d.ts.map +1 -0
  12. package/dist/builtin/0x3.js +400 -0
  13. package/dist/builtin/0x3.js.map +1 -0
  14. package/dist/builtin/index.d.ts +3 -0
  15. package/dist/builtin/index.d.ts.map +1 -0
  16. package/dist/builtin/index.js +6 -0
  17. package/dist/builtin/index.js.map +1 -0
  18. package/dist/codegen/codegen.d.ts +2 -0
  19. package/dist/codegen/codegen.d.ts.map +1 -0
  20. package/dist/codegen/codegen.js +23 -0
  21. package/dist/codegen/codegen.js.map +1 -0
  22. package/dist/codegen/index.d.ts +2 -0
  23. package/dist/codegen/index.d.ts.map +1 -0
  24. package/dist/codegen/index.js +2 -0
  25. package/dist/codegen/index.js.map +1 -0
  26. package/dist/codegen/run.d.ts +2 -0
  27. package/dist/codegen/run.d.ts.map +1 -0
  28. package/dist/codegen/run.js +19 -0
  29. package/dist/codegen/run.js.map +1 -0
  30. package/dist/codegen/types.test.d.ts +2 -0
  31. package/dist/codegen/types.test.d.ts.map +1 -0
  32. package/dist/codegen/types.test.js.map +1 -0
  33. package/dist/index.d.ts +4 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +4 -0
  36. package/dist/index.js.map +1 -0
  37. package/dist/models.d.ts +11 -0
  38. package/dist/models.d.ts.map +1 -0
  39. package/dist/models.js +2 -0
  40. package/dist/models.js.map +1 -0
  41. package/dist/move-coder.d.ts +14 -0
  42. package/dist/move-coder.d.ts.map +1 -0
  43. package/dist/move-coder.js +70 -0
  44. package/dist/move-coder.js.map +1 -0
  45. package/dist/move-types.d.ts +12 -0
  46. package/dist/move-types.d.ts.map +1 -0
  47. package/dist/move-types.js +2 -0
  48. package/dist/move-types.js.map +1 -0
  49. package/dist/to-internal.d.ts +7 -0
  50. package/dist/to-internal.d.ts.map +1 -0
  51. package/dist/to-internal.js +52 -0
  52. package/dist/to-internal.js.map +1 -0
  53. package/package.json +42 -0
  54. package/src/abis/0x1.json +12790 -0
  55. package/src/abis/0x3.json +2275 -0
  56. package/src/aptos-chain-adapter.ts +91 -0
  57. package/src/builtin/0x1.ts +4258 -0
  58. package/src/builtin/0x3.ts +922 -0
  59. package/src/builtin/index.ts +5 -0
  60. package/src/codegen/codegen.ts +42 -0
  61. package/src/codegen/index.ts +1 -0
  62. package/src/codegen/run.ts +20 -0
  63. package/src/index.ts +3 -0
  64. package/src/models.ts +18 -0
  65. package/src/move-coder.ts +84 -0
  66. package/src/move-types.ts +13 -0
  67. package/src/to-internal.ts +72 -0
@@ -0,0 +1,5 @@
1
+ /* Autogenerated file. Do not edit manually. */
2
+ /* tslint:disable */
3
+ /* eslint-disable */
4
+ export * as _0x1 from './0x1.js'
5
+ export * as _0x3 from './0x3.js'
@@ -0,0 +1,42 @@
1
+ import * as fs from 'fs'
2
+ import { Event, MoveModuleBytecode, MoveResource } from '../move-types.js'
3
+ import chalk from 'chalk'
4
+ import { join } from 'path'
5
+ import { AptosChainAdapter } from '../aptos-chain-adapter.js'
6
+ import { AbstractCodegen } from '@typemove/move'
7
+
8
+ export async function codegen(
9
+ abisDir: string,
10
+ outDir = join('src', 'types', 'aptos'),
11
+ endpoint: string,
12
+ genExample = false,
13
+ builtin = false
14
+ ) {
15
+ if (!fs.existsSync(abisDir)) {
16
+ return
17
+ }
18
+ const gen = new AptosCodegen(endpoint)
19
+ const numFiles = await gen.generate(abisDir, outDir, builtin)
20
+ console.log(chalk.green(`Generated ${numFiles} for Aptos`))
21
+ }
22
+
23
+ class AptosCodegen extends AbstractCodegen<
24
+ MoveModuleBytecode,
25
+ Event | MoveResource
26
+ > {
27
+ ADDRESS_TYPE = 'Address'
28
+ REFERENCE_TYPE = 'Address'
29
+ PREFIX = 'Aptos'
30
+ SYSTEM_PACKAGE = '@typemove/aptos'
31
+
32
+ constructor(endpoint: string) {
33
+ super(new AptosChainAdapter(endpoint))
34
+ }
35
+
36
+ // generateImports(): string {
37
+ // return `
38
+ // ${super.generateImports()}
39
+ // import { Address } from '@typemove/aptos'
40
+ // `
41
+ // }
42
+ }
@@ -0,0 +1 @@
1
+ export * from './codegen.js'
@@ -0,0 +1,20 @@
1
+ // #!/usr/bin/env node
2
+
3
+ import { codegen } from './codegen.js'
4
+
5
+ if (process.argv.length > 3) {
6
+ const abisDir = process.argv[2]
7
+ const targetDir = process.argv[3]
8
+ let endpoint: string | undefined = process.argv[4]
9
+ if (!endpoint || endpoint == 'mainnet') {
10
+ endpoint = 'https://mainnet.aptoslabs.com/'
11
+ }
12
+ if (endpoint == 'testnet') {
13
+ endpoint = 'https://testnet.aptoslabs.com/'
14
+ }
15
+
16
+ await codegen(abisDir, targetDir, endpoint, true)
17
+ } else {
18
+ console.error('Not enough argument')
19
+ process.exit(1)
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './models.js'
2
+ export * from './move-types.js'
3
+ export { MoveCoder, defaultMoveCoder } from './move-coder.js'
package/src/models.ts ADDED
@@ -0,0 +1,18 @@
1
+ import {
2
+ Event,
3
+ MoveResource,
4
+ TransactionPayload_EntryFunctionPayload,
5
+ } from './move-types.js'
6
+ import { DecodedStruct } from '@typemove/move'
7
+
8
+ export type TypedEventInstance<T> = DecodedStruct<Event, T>
9
+ export type TypedMoveResource<T> = DecodedStruct<MoveResource, T>
10
+
11
+ // Don't use intermediate type to make IDE happier
12
+ export type TypedFunctionPayload<T extends Array<any>> =
13
+ TransactionPayload_EntryFunctionPayload & {
14
+ /**
15
+ * decoded argument data using ABI, undefined if there is decoding error, usually because the ABI/data mismatch
16
+ */
17
+ arguments_decoded: T
18
+ }
@@ -0,0 +1,84 @@
1
+ import { AbstractMoveCoder, InternalMoveModule, parseMoveType, TypeDescriptor } from '@typemove/move'
2
+ import { Event, MoveModuleBytecode, MoveResource, TransactionPayload_EntryFunctionPayload } from './move-types.js'
3
+ import { TypedEventInstance, TypedFunctionPayload, TypedMoveResource } from './models.js'
4
+ import { AptosChainAdapter } from './aptos-chain-adapter.js'
5
+ import { toInternalModule } from './to-internal.js'
6
+
7
+ export class MoveCoder extends AbstractMoveCoder<MoveModuleBytecode, Event | MoveResource> {
8
+ constructor(network: string) {
9
+ super(new AptosChainAdapter(network))
10
+ }
11
+
12
+ load(module: MoveModuleBytecode): InternalMoveModule {
13
+ if (!module.abi) {
14
+ throw Error('Module without abi')
15
+ }
16
+ let m = this.moduleMapping.get(module.abi.address + '::' + module.abi.name)
17
+ // if (this.contains(module.abi.address, module.abi.name)) {
18
+ // return
19
+ // }
20
+ if (m) {
21
+ return m
22
+ }
23
+ m = toInternalModule(module)
24
+ this.loadInternal(m)
25
+ return m
26
+ }
27
+
28
+ decodeEvent<T>(event: Event): Promise<TypedEventInstance<T> | undefined> {
29
+ return this.decodedStruct(event)
30
+ }
31
+ filterAndDecodeEvents<T>(type: string | TypeDescriptor<T>, resources: Event[]): Promise<TypedEventInstance<T>[]> {
32
+ if (typeof type === 'string') {
33
+ type = parseMoveType(type)
34
+ }
35
+ return this.filterAndDecodeStruct(type, resources)
36
+ }
37
+ decodeResource<T>(res: MoveResource): Promise<TypedMoveResource<T> | undefined> {
38
+ return this.decodedStruct(res)
39
+ }
40
+ filterAndDecodeResources<T>(
41
+ type: string | TypeDescriptor<T>,
42
+ resources: MoveResource[]
43
+ ): Promise<TypedMoveResource<T>[]> {
44
+ if (typeof type === 'string') {
45
+ type = parseMoveType(type)
46
+ }
47
+ return this.filterAndDecodeStruct(type, resources)
48
+ }
49
+
50
+ async decodeFunctionPayload(
51
+ payload: TransactionPayload_EntryFunctionPayload
52
+ ): Promise<TransactionPayload_EntryFunctionPayload> {
53
+ const func = await this.getMoveFunction(payload.function)
54
+ const params = this.adapter.getMeaningfulFunctionParams(func.params)
55
+ const argumentsDecoded = await this.decodeArray(payload.arguments, params)
56
+
57
+ return {
58
+ ...payload,
59
+ arguments_decoded: argumentsDecoded,
60
+ } as TypedFunctionPayload<any>
61
+ }
62
+ }
63
+
64
+ // const MOVE_CODER = new MoveCoder(AptosNetwork.MAIN_NET)
65
+ // const TESTNET_MOVE_CODER = new MoveCoder(AptosNetwork.TEST_NET)
66
+ //
67
+ // export function defaultMoveCoder(network: AptosNetwork = AptosNetwork.MAIN_NET): MoveCoder {
68
+ // if (network == AptosNetwork.MAIN_NET) {
69
+ // return MOVE_CODER
70
+ // }
71
+ // return TESTNET_MOVE_CODER
72
+ // }
73
+
74
+ const DEFAULT_ENDPOINT = 'https://mainnet.aptoslabs.com/'
75
+ const CODER_MAP = new Map<string, MoveCoder>()
76
+
77
+ export function defaultMoveCoder(endpoint: string = DEFAULT_ENDPOINT): MoveCoder {
78
+ let coder = CODER_MAP.get(endpoint)
79
+ if (!coder) {
80
+ coder = new MoveCoder(DEFAULT_ENDPOINT)
81
+ CODER_MAP.set(endpoint, coder)
82
+ }
83
+ return coder
84
+ }
@@ -0,0 +1,13 @@
1
+ import { Types } from 'aptos'
2
+
3
+ export type Address = Types.Address
4
+ export type Event = Types.Event
5
+ export type MoveFunction = Types.MoveFunction
6
+ export type MoveModule = Types.MoveModule
7
+ export type MoveResource = Types.MoveResource
8
+ export type MoveStruct = Types.MoveStruct
9
+ export type MoveStructField = Types.MoveStructField
10
+ export type MoveModuleBytecode = Types.MoveModuleBytecode
11
+ export type TransactionPayload_EntryFunctionPayload =
12
+ Types.TransactionPayload_EntryFunctionPayload
13
+ export type Transaction_UserTransaction = Types.Transaction_UserTransaction
@@ -0,0 +1,72 @@
1
+ import {
2
+ InternalMoveFunction,
3
+ InternalMoveFunctionVisibility,
4
+ InternalMoveModule,
5
+ InternalMoveStruct,
6
+ InternalMoveStructField,
7
+ parseMoveType,
8
+ } from '@typemove/move'
9
+ import { Types } from 'aptos'
10
+ import {
11
+ MoveFunction,
12
+ MoveModuleBytecode,
13
+ MoveStruct,
14
+ MoveStructField,
15
+ } from './move-types'
16
+
17
+ export function toInternalModule(
18
+ module: MoveModuleBytecode
19
+ ): InternalMoveModule {
20
+ if (!module.abi) {
21
+ throw Error('module with no ABI found')
22
+ }
23
+ const abi = module.abi
24
+ return {
25
+ address: abi.address,
26
+ exposedFunctions: abi.exposed_functions.map(toInternalFunction),
27
+ name: abi.name,
28
+ structs: abi.structs.map(toInternalStruct),
29
+ }
30
+ }
31
+
32
+ export function toInternalFunction(func: MoveFunction): InternalMoveFunction {
33
+ let visibility
34
+ switch (func.visibility) {
35
+ case Types.MoveFunctionVisibility.PRIVATE:
36
+ visibility = InternalMoveFunctionVisibility.PRIVATE
37
+ break
38
+ case Types.MoveFunctionVisibility.PUBLIC:
39
+ visibility = InternalMoveFunctionVisibility.PUBLIC
40
+ break
41
+ case Types.MoveFunctionVisibility.FRIEND:
42
+ visibility = InternalMoveFunctionVisibility.FRIEND
43
+ break
44
+ }
45
+ return {
46
+ typeParams: func.generic_type_params,
47
+ isEntry: func.is_entry,
48
+ name: func.name,
49
+ params: func.params.map(parseMoveType),
50
+ return: func.return.map(parseMoveType),
51
+ visibility: visibility,
52
+ }
53
+ }
54
+
55
+ export function toInternalStruct(struct: MoveStruct): InternalMoveStruct {
56
+ return {
57
+ abilities: struct.abilities,
58
+ fields: struct.fields.map(toInternalField),
59
+ typeParams: struct.generic_type_params,
60
+ isNative: struct.is_native,
61
+ name: struct.name,
62
+ }
63
+ }
64
+
65
+ export function toInternalField(
66
+ module: MoveStructField
67
+ ): InternalMoveStructField {
68
+ return {
69
+ name: module.name,
70
+ type: parseMoveType(module.type),
71
+ }
72
+ }