@typemove/aptos 2.0.0 → 2.0.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 (73) hide show
  1. package/dist/esm/account-resource-client.d.ts +28 -0
  2. package/dist/esm/account-resource-client.d.ts.map +1 -0
  3. package/dist/esm/account-resource-client.js +47 -0
  4. package/dist/esm/account-resource-client.js.map +1 -0
  5. package/dist/esm/aptos-chain-adapter.d.ts +16 -0
  6. package/dist/esm/aptos-chain-adapter.d.ts.map +1 -0
  7. package/dist/esm/aptos-chain-adapter.js +104 -0
  8. package/dist/esm/aptos-chain-adapter.js.map +1 -0
  9. package/dist/esm/builtin/0x1.d.ts +7522 -0
  10. package/dist/esm/builtin/0x1.d.ts.map +1 -0
  11. package/dist/esm/builtin/0x1.js +11632 -0
  12. package/dist/esm/builtin/0x1.js.map +1 -0
  13. package/dist/esm/builtin/0x3.d.ts +1087 -0
  14. package/dist/esm/builtin/0x3.d.ts.map +1 -0
  15. package/dist/esm/builtin/0x3.js +926 -0
  16. package/dist/esm/builtin/0x3.js.map +1 -0
  17. package/dist/esm/builtin/0x4.d.ts +535 -0
  18. package/dist/esm/builtin/0x4.d.ts.map +1 -0
  19. package/dist/esm/builtin/0x4.js +884 -0
  20. package/dist/esm/builtin/0x4.js.map +1 -0
  21. package/dist/esm/builtin/index.d.ts +4 -0
  22. package/dist/esm/builtin/index.d.ts.map +1 -0
  23. package/dist/esm/builtin/index.js +7 -0
  24. package/dist/esm/builtin/index.js.map +1 -0
  25. package/dist/esm/codegen/codegen.d.ts +18 -0
  26. package/dist/esm/codegen/codegen.d.ts.map +1 -0
  27. package/dist/esm/codegen/codegen.js +161 -0
  28. package/dist/esm/codegen/codegen.js.map +1 -0
  29. package/dist/esm/codegen/index.d.ts +2 -0
  30. package/dist/esm/codegen/index.d.ts.map +1 -0
  31. package/dist/esm/codegen/index.js +2 -0
  32. package/dist/esm/codegen/index.js.map +1 -0
  33. package/dist/esm/codegen/run.d.ts +3 -0
  34. package/dist/esm/codegen/run.d.ts.map +1 -0
  35. package/dist/esm/codegen/run.js.map +1 -0
  36. package/dist/esm/coder-helpers.d.ts +6 -0
  37. package/dist/esm/coder-helpers.d.ts.map +1 -0
  38. package/dist/esm/coder-helpers.js +8 -0
  39. package/dist/esm/coder-helpers.js.map +1 -0
  40. package/dist/esm/index.d.ts +5 -0
  41. package/dist/esm/index.d.ts.map +1 -0
  42. package/dist/esm/index.js +5 -0
  43. package/dist/esm/index.js.map +1 -0
  44. package/dist/esm/models.d.ts +12 -0
  45. package/dist/esm/models.d.ts.map +1 -0
  46. package/dist/esm/models.js +2 -0
  47. package/dist/esm/models.js.map +1 -0
  48. package/dist/esm/move-coder.d.ts +17 -0
  49. package/dist/esm/move-coder.d.ts.map +1 -0
  50. package/dist/esm/move-coder.js +97 -0
  51. package/dist/esm/move-coder.js.map +1 -0
  52. package/dist/esm/to-internal.d.ts +7 -0
  53. package/dist/esm/to-internal.d.ts.map +1 -0
  54. package/dist/esm/to-internal.js +55 -0
  55. package/dist/esm/to-internal.js.map +1 -0
  56. package/package.json +6 -6
  57. package/src/abis/0x1.json +43414 -0
  58. package/src/abis/0x3.json +3976 -0
  59. package/src/abis/0x4.json +2844 -0
  60. package/src/account-resource-client.ts +69 -0
  61. package/src/aptos-chain-adapter.ts +132 -0
  62. package/src/builtin/0x1.ts +19976 -0
  63. package/src/builtin/0x3.ts +2083 -0
  64. package/src/builtin/0x4.ts +1503 -0
  65. package/src/builtin/index.ts +6 -0
  66. package/src/codegen/codegen.ts +190 -0
  67. package/src/codegen/index.ts +1 -0
  68. package/src/codegen/run.ts +67 -0
  69. package/src/coder-helpers.ts +15 -0
  70. package/src/index.ts +4 -0
  71. package/src/models.ts +23 -0
  72. package/src/move-coder.ts +124 -0
  73. package/src/to-internal.ts +71 -0
@@ -0,0 +1,69 @@
1
+ import {
2
+ AccountAddressInput,
3
+ Aptos,
4
+ MoveStructId,
5
+ LedgerVersionArg,
6
+ PaginationArgs,
7
+ MoveResource
8
+ } from '@aptos-labs/ts-sdk'
9
+ import { TypeDescriptor } from '@typemove/move'
10
+ import { defaultMoveCoder } from './move-coder.js'
11
+ import { TypedMoveResource } from './models.js'
12
+
13
+ // type ResourceQuery = {
14
+ // ledgerVersion?: bigint | number
15
+ // }
16
+
17
+ export class AccountResourceClient {
18
+ client: Aptos
19
+ constructor(client: Aptos) {
20
+ this.client = client
21
+ }
22
+
23
+ /**
24
+ * Get all resources of an account, same as `getAccountResources` in aptos client
25
+ * @param accountAddress
26
+ * @param query
27
+ */
28
+ async getAll(accountAddress: AccountAddressInput, options?: PaginationArgs & LedgerVersionArg) {
29
+ return this.client.getAccountResources({ accountAddress, options })
30
+ }
31
+
32
+ /**
33
+ * Match a single resource with exact type, resource type should not contain any type
34
+ * @param accountAddress
35
+ * @param resourceType
36
+ * @param options
37
+ */
38
+ async matchExact<T>(
39
+ accountAddress: AccountAddressInput,
40
+ resourceType: TypeDescriptor<T>,
41
+ options?: LedgerVersionArg
42
+ ): Promise<TypedMoveResource<T> | undefined> {
43
+ if (resourceType.existAnyType()) {
44
+ throw new Error('resource type for match call should not contain any type')
45
+ }
46
+ const typeStr = resourceType.getSignature() as MoveStructId
47
+ const result = await this.client.getAccountResource({ accountAddress, resourceType: typeStr, options })
48
+ const resource: MoveResource = {
49
+ type: typeStr,
50
+ data: result
51
+ }
52
+ return defaultMoveCoder(this.client.config).decodeResource<T>(resource)
53
+ }
54
+
55
+ /**
56
+ * Match all resources with type pattern, it could be a partial type like `amm.Pool<aptos_coin.AptosCoin.type(), ANY_TYPE>`
57
+ * @param accountAddress
58
+ * @param resourceType
59
+ * @param options
60
+ */
61
+ async matchAll<T>(
62
+ accountAddress: AccountAddressInput,
63
+ resourceType: TypeDescriptor<T>,
64
+ options?: PaginationArgs & LedgerVersionArg
65
+ ): Promise<TypedMoveResource<T>[]> {
66
+ const result = await this.client.getAccountResources({ accountAddress, options })
67
+ return defaultMoveCoder(this.client.config).filterAndDecodeResources<T>(resourceType, result)
68
+ }
69
+ }
@@ -0,0 +1,132 @@
1
+ import {
2
+ ChainAdapter,
3
+ moduleQname,
4
+ SPLITTER,
5
+ TypeDescriptor,
6
+ InternalMoveModule,
7
+ InternalMoveStruct
8
+ } from '@typemove/move'
9
+
10
+ import { Aptos, Event, MoveModuleBytecode, MoveResource } from '@aptos-labs/ts-sdk'
11
+ import { toInternalModule } from './to-internal.js'
12
+
13
+ // Drop bytecode — typemove's codegen only reads `m.abi`, and bytecode is
14
+ // ~50% of each Aptos module entry's bytes. Cast is needed because
15
+ // MoveModuleBytecode declares `bytecode: string` as required.
16
+ function stripBytecode({ abi }: MoveModuleBytecode): MoveModuleBytecode {
17
+ return { abi } as MoveModuleBytecode
18
+ }
19
+
20
+ export class AptosChainAdapter extends ChainAdapter<MoveModuleBytecode, Event | MoveResource> {
21
+ // static INSTANCE = new AptosChainAdapter()
22
+ client: Aptos
23
+ private optimisticEventDetection: boolean = true
24
+
25
+ constructor(client: Aptos, optimisticEventDetection = true) {
26
+ super()
27
+ this.client = client
28
+ this.optimisticEventDetection = optimisticEventDetection
29
+ }
30
+
31
+ async fetchModules(account: string): Promise<MoveModuleBytecode[]> {
32
+ const modules = await this.client.getAccountModules({
33
+ accountAddress: account
34
+ })
35
+ return modules.map(stripBytecode)
36
+ }
37
+
38
+ async fetchModule(account: string, module: string): Promise<MoveModuleBytecode> {
39
+ const m = await this.client.getAccountModule({
40
+ accountAddress: account,
41
+ moduleName: module
42
+ })
43
+ return stripBytecode(m)
44
+ }
45
+
46
+ toInternalModules(modules: MoveModuleBytecode[]): InternalMoveModule[] {
47
+ return modules.flatMap((m) => (m.abi ? [toInternalModule(m)] : []))
48
+ }
49
+
50
+ getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
51
+ if (params.length === 0) {
52
+ return params
53
+ }
54
+ if (params[0].qname === 'signer' && params[0].reference) {
55
+ params = params.slice(1)
56
+ }
57
+ return params
58
+ }
59
+
60
+ getAllEventStructs(modules: InternalMoveModule[]) {
61
+ const eventMap = new Map<string, InternalMoveStruct>()
62
+ const structMap = new Map<string, InternalMoveStruct>()
63
+ for (const module of modules) {
64
+ const qname = moduleQname(module)
65
+ for (const struct of module.structs) {
66
+ const typeName = qname + SPLITTER + struct.name
67
+
68
+ // Skip deprecated v2 events
69
+ if (typeName == '0x1::coin::Deposit' || typeName == '0x1::coin::Withdraw') {
70
+ continue
71
+ }
72
+
73
+ structMap.set(typeName, struct)
74
+
75
+ // Check if struct is explicitly marked as event
76
+ if (struct.isEvent) {
77
+ eventMap.set(typeName, struct)
78
+ continue
79
+ }
80
+
81
+ if (struct.name.endsWith('Event')) {
82
+ // this is a hack to support some old events
83
+ eventMap.set(typeName, struct)
84
+ continue
85
+ }
86
+
87
+ // Check for native events: structs with EXACTLY drop and store abilities
88
+ if (this.optimisticEventDetection) {
89
+ const abilities = new Set(struct.abilities)
90
+ if (abilities.size === 2 && abilities.has('drop') && abilities.has('store')) {
91
+ eventMap.set(typeName, struct)
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ // Also check for legacy events (in EventHandle fields)
98
+ for (const module of modules) {
99
+ for (const struct of module.structs) {
100
+ for (const field of struct.fields) {
101
+ const t = field.type
102
+ if (t.qname === '0x1::event::EventHandle') {
103
+ const event = t.typeArgs[0].qname
104
+ const eventStruct = structMap.get(event)
105
+ if (eventStruct) {
106
+ eventMap.set(event, eventStruct)
107
+ }
108
+ }
109
+ }
110
+ }
111
+ }
112
+ return eventMap
113
+ }
114
+
115
+ getType(data: Event | MoveResource): string {
116
+ return data.type
117
+ }
118
+
119
+ getData(data: Event | MoveResource) {
120
+ if ('data' in data && 'type' in data) {
121
+ return data.data
122
+ }
123
+ return data
124
+ }
125
+
126
+ async getChainId(): Promise<string> {
127
+ return (await this.client.getChainId()).toString()
128
+ }
129
+ // validateAndNormalizeAddress(address: string): string {
130
+ // return validateAndNormalizeAddress(address)
131
+ // }
132
+ }