@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.
- package/dist/esm/account-resource-client.d.ts +28 -0
- package/dist/esm/account-resource-client.d.ts.map +1 -0
- package/dist/esm/account-resource-client.js +47 -0
- package/dist/esm/account-resource-client.js.map +1 -0
- package/dist/esm/aptos-chain-adapter.d.ts +16 -0
- package/dist/esm/aptos-chain-adapter.d.ts.map +1 -0
- package/dist/esm/aptos-chain-adapter.js +104 -0
- package/dist/esm/aptos-chain-adapter.js.map +1 -0
- package/dist/esm/builtin/0x1.d.ts +7522 -0
- package/dist/esm/builtin/0x1.d.ts.map +1 -0
- package/dist/esm/builtin/0x1.js +11632 -0
- package/dist/esm/builtin/0x1.js.map +1 -0
- package/dist/esm/builtin/0x3.d.ts +1087 -0
- package/dist/esm/builtin/0x3.d.ts.map +1 -0
- package/dist/esm/builtin/0x3.js +926 -0
- package/dist/esm/builtin/0x3.js.map +1 -0
- package/dist/esm/builtin/0x4.d.ts +535 -0
- package/dist/esm/builtin/0x4.d.ts.map +1 -0
- package/dist/esm/builtin/0x4.js +884 -0
- package/dist/esm/builtin/0x4.js.map +1 -0
- package/dist/esm/builtin/index.d.ts +4 -0
- package/dist/esm/builtin/index.d.ts.map +1 -0
- package/dist/esm/builtin/index.js +7 -0
- package/dist/esm/builtin/index.js.map +1 -0
- package/dist/esm/codegen/codegen.d.ts +18 -0
- package/dist/esm/codegen/codegen.d.ts.map +1 -0
- package/dist/esm/codegen/codegen.js +161 -0
- package/dist/esm/codegen/codegen.js.map +1 -0
- package/dist/esm/codegen/index.d.ts +2 -0
- package/dist/esm/codegen/index.d.ts.map +1 -0
- package/dist/esm/codegen/index.js +2 -0
- package/dist/esm/codegen/index.js.map +1 -0
- package/dist/esm/codegen/run.d.ts +3 -0
- package/dist/esm/codegen/run.d.ts.map +1 -0
- package/dist/esm/codegen/run.js.map +1 -0
- package/dist/esm/coder-helpers.d.ts +6 -0
- package/dist/esm/coder-helpers.d.ts.map +1 -0
- package/dist/esm/coder-helpers.js +8 -0
- package/dist/esm/coder-helpers.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/models.d.ts +12 -0
- package/dist/esm/models.d.ts.map +1 -0
- package/dist/esm/models.js +2 -0
- package/dist/esm/models.js.map +1 -0
- package/dist/esm/move-coder.d.ts +17 -0
- package/dist/esm/move-coder.d.ts.map +1 -0
- package/dist/esm/move-coder.js +97 -0
- package/dist/esm/move-coder.js.map +1 -0
- package/dist/esm/to-internal.d.ts +7 -0
- package/dist/esm/to-internal.d.ts.map +1 -0
- package/dist/esm/to-internal.js +55 -0
- package/dist/esm/to-internal.js.map +1 -0
- package/package.json +6 -6
- package/src/abis/0x1.json +43414 -0
- package/src/abis/0x3.json +3976 -0
- package/src/abis/0x4.json +2844 -0
- package/src/account-resource-client.ts +69 -0
- package/src/aptos-chain-adapter.ts +132 -0
- package/src/builtin/0x1.ts +19976 -0
- package/src/builtin/0x3.ts +2083 -0
- package/src/builtin/0x4.ts +1503 -0
- package/src/builtin/index.ts +6 -0
- package/src/codegen/codegen.ts +190 -0
- package/src/codegen/index.ts +1 -0
- package/src/codegen/run.ts +67 -0
- package/src/coder-helpers.ts +15 -0
- package/src/index.ts +4 -0
- package/src/models.ts +23 -0
- package/src/move-coder.ts +124 -0
- package/src/to-internal.ts +71 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
import { join } from 'path'
|
|
4
|
+
import { AptosChainAdapter } from '../aptos-chain-adapter.js'
|
|
5
|
+
import {
|
|
6
|
+
camel,
|
|
7
|
+
InternalMoveFunction,
|
|
8
|
+
InternalMoveModule,
|
|
9
|
+
InternalMoveStruct,
|
|
10
|
+
normalizeToJSName,
|
|
11
|
+
structQname
|
|
12
|
+
} from '@typemove/move'
|
|
13
|
+
import { AbstractCodegen } from '@typemove/move/codegen'
|
|
14
|
+
import { Aptos, AptosConfig, Event, MoveModuleBytecode, MoveResource } from '@aptos-labs/ts-sdk'
|
|
15
|
+
|
|
16
|
+
export async function codegen(
|
|
17
|
+
abisDir: string,
|
|
18
|
+
outDir = join('src', 'types', 'aptos'),
|
|
19
|
+
config: AptosConfig,
|
|
20
|
+
genExample = false,
|
|
21
|
+
builtin = false
|
|
22
|
+
) {
|
|
23
|
+
if (!fs.existsSync(abisDir)) {
|
|
24
|
+
console.error(chalk.red(`ABIs directory ${abisDir} does not exist`))
|
|
25
|
+
return 0
|
|
26
|
+
}
|
|
27
|
+
const gen = new AptosCodegen(config)
|
|
28
|
+
try {
|
|
29
|
+
const numFiles = await gen.generate(abisDir, outDir, builtin)
|
|
30
|
+
if (numFiles > 0) {
|
|
31
|
+
console.log(chalk.green(`Generated for ${numFiles} accounts for Aptos to ${outDir}`))
|
|
32
|
+
} else {
|
|
33
|
+
console.error(chalk.red(`No account found`))
|
|
34
|
+
}
|
|
35
|
+
return numFiles
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.error(chalk.red(`Failed to generate for ${abisDir}, please check if ABI json files are valid`))
|
|
38
|
+
console.log(e)
|
|
39
|
+
return 0
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class AptosCodegen extends AbstractCodegen<MoveModuleBytecode, Event | MoveResource> {
|
|
44
|
+
ADDRESS_TYPE = 'MoveAddressType'
|
|
45
|
+
PREFIX = 'Aptos'
|
|
46
|
+
SYSTEM_PACKAGE = '@typemove/aptos'
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
config: AptosConfig,
|
|
50
|
+
readonly useViewJson = false
|
|
51
|
+
) {
|
|
52
|
+
super(new AptosChainAdapter(new Aptos(config)))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
generateImports(): string {
|
|
56
|
+
return `
|
|
57
|
+
${super.generateImports()}
|
|
58
|
+
import { Aptos, Account as AptosAccount, MoveAddressType, PendingTransactionResponse, InputGenerateTransactionOptions, MoveStructId, InputViewFunctionData, InputViewFunctionJsonData } from '@aptos-labs/ts-sdk'
|
|
59
|
+
`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected generateExtra(address: string | undefined, module: InternalMoveModule) {
|
|
63
|
+
const funcs = module.exposedFunctions.map((f) => this.generateEntryForFunction(module, f))
|
|
64
|
+
|
|
65
|
+
const viewFuncs = module.exposedFunctions.map((f) => this.generateViewFunction(module, f))
|
|
66
|
+
|
|
67
|
+
return `
|
|
68
|
+
export namespace entry {
|
|
69
|
+
${funcs.join('\n')}
|
|
70
|
+
}
|
|
71
|
+
export namespace view {
|
|
72
|
+
${viewFuncs.join('\n')}
|
|
73
|
+
}
|
|
74
|
+
`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
generateStructs(module: InternalMoveModule, struct: InternalMoveStruct, events: Set<string>): string {
|
|
78
|
+
let content = ''
|
|
79
|
+
switch (structQname(module, struct)) {
|
|
80
|
+
case '0x1::object::Object':
|
|
81
|
+
content += `export type ${struct.name}<T> = MoveAddressType`
|
|
82
|
+
break
|
|
83
|
+
}
|
|
84
|
+
return content + super.generateStructs(module, struct, events, content !== '')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
protected generateViewFunction(module: InternalMoveModule, func: InternalMoveFunction): string {
|
|
88
|
+
if (!func.isView) {
|
|
89
|
+
return ''
|
|
90
|
+
}
|
|
91
|
+
const genericString = this.generateFunctionTypeParameters(func)
|
|
92
|
+
const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
|
|
93
|
+
return this.generateTypeForDescriptor(param, module.address)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
const returns = func.return.map((param) => {
|
|
97
|
+
return this.generateTypeForDescriptor(param, module.address)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const typeParamArg = func.typeParams
|
|
101
|
+
.map((v, idx) => {
|
|
102
|
+
return `TypeDescriptor<T${idx}> | string`
|
|
103
|
+
})
|
|
104
|
+
.join(',')
|
|
105
|
+
|
|
106
|
+
// const args = this.generateArgs(module, func)
|
|
107
|
+
const allEmpty = func.typeParams.length === 0 && func.params.length === 0
|
|
108
|
+
const requestArg = allEmpty
|
|
109
|
+
? ''
|
|
110
|
+
: `request: {
|
|
111
|
+
${func.typeParams.length > 0 ? `typeArguments: [${func.typeParams.map((_) => 'MoveStructId').join(', ')}],` : ''}
|
|
112
|
+
${func.params.length > 0 ? `functionArguments: [${fields.join(',')}]` : ''}},`
|
|
113
|
+
|
|
114
|
+
if (this.useViewJson) {
|
|
115
|
+
return `export async function ${camel(normalizeToJSName(func.name))}${genericString}(
|
|
116
|
+
client: Aptos,
|
|
117
|
+
${requestArg}
|
|
118
|
+
version?: bigint): Promise<[${returns.join(',')}]> {
|
|
119
|
+
const coder = ${this.getGetDefaultCoder()}
|
|
120
|
+
const data: InputViewFunctionJsonData = {
|
|
121
|
+
function: "${module.address}::${module.name}::${func.name}",
|
|
122
|
+
functionArguments: ${func.params.length > 0 ? 'request.functionArguments.map(coder.toMoveValue)' : '[]'},
|
|
123
|
+
typeArguments: ${func.typeParams.length > 0 ? 'request.typeArguments' : '[]'},
|
|
124
|
+
}
|
|
125
|
+
const res = await client.viewJson({payload: data, options: { ledgerVersion: version } });
|
|
126
|
+
const type = await coder.getMoveFunction("${module.address}::${module.name}::${func.name}")
|
|
127
|
+
return await coder.decodeArray(res, type.return) as any
|
|
128
|
+
}`
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return `export async function ${camel(normalizeToJSName(func.name))}${genericString}(
|
|
132
|
+
client: Aptos,
|
|
133
|
+
${requestArg}
|
|
134
|
+
version?: bigint): Promise<[${returns.join(',')}]> {
|
|
135
|
+
const coder = ${this.getGetDefaultCoder()}
|
|
136
|
+
const data: InputViewFunctionData = {
|
|
137
|
+
function: "${module.address}::${module.name}::${func.name}",
|
|
138
|
+
functionArguments: ${func.params.length > 0 ? 'coder.encodeArray(request.functionArguments)' : '[]'},
|
|
139
|
+
typeArguments: ${func.typeParams.length > 0 ? 'request.typeArguments' : '[]'},
|
|
140
|
+
}
|
|
141
|
+
const res = await client.view({payload: data, options: { ledgerVersion: version } });
|
|
142
|
+
const type = await coder.getMoveFunction("${module.address}::${module.name}::${func.name}")
|
|
143
|
+
return await coder.decodeArray(res, type.return) as any
|
|
144
|
+
}`
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
protected getGetDefaultCoder() {
|
|
148
|
+
return `defaultMoveCoder(client.config)`
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected generateEntryForFunction(module: InternalMoveModule, func: InternalMoveFunction): string {
|
|
152
|
+
if (!func.isEntry) {
|
|
153
|
+
return ''
|
|
154
|
+
}
|
|
155
|
+
const genericString = this.generateFunctionTypeParameters(func)
|
|
156
|
+
const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
|
|
157
|
+
return this.generateTypeForDescriptor(param, module.address)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
// const typeParamArg = func.typeParams
|
|
161
|
+
// .map((v, idx) => {
|
|
162
|
+
// return `TypeDescriptor<T${idx}> | string`
|
|
163
|
+
// })
|
|
164
|
+
// .join(',')
|
|
165
|
+
//
|
|
166
|
+
// const args = this.generateArgs(module, func)
|
|
167
|
+
|
|
168
|
+
return `export async function ${camel(normalizeToJSName(func.name))}${genericString}(
|
|
169
|
+
client: Aptos,
|
|
170
|
+
account: AptosAccount,
|
|
171
|
+
request: {
|
|
172
|
+
typeArguments: [${func.typeParams.map((_) => 'MoveStructId').join(', ')}],
|
|
173
|
+
functionArguments: [${fields.join(',')}]
|
|
174
|
+
},
|
|
175
|
+
options?: InputGenerateTransactionOptions
|
|
176
|
+
): Promise<PendingTransactionResponse> {
|
|
177
|
+
const coder = ${this.getGetDefaultCoder()}
|
|
178
|
+
const transaction = await client.transaction.build.simple({
|
|
179
|
+
sender: account.accountAddress,
|
|
180
|
+
data: {
|
|
181
|
+
function: "${module.address}::${module.name}::${func.name}",
|
|
182
|
+
functionArguments: ${func.params.length > 0 ? 'coder.encodeArray(request.functionArguments)' : '[]'},
|
|
183
|
+
typeArguments: ${func.typeParams.length > 0 ? 'request.typeArguments' : '[]'},
|
|
184
|
+
},
|
|
185
|
+
options
|
|
186
|
+
})
|
|
187
|
+
return await client.signAndSubmitTransaction({ signer: account, transaction });
|
|
188
|
+
}`
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './codegen.js'
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { codegen } from './codegen.js'
|
|
4
|
+
import { AptosChainAdapter } from '../aptos-chain-adapter.js'
|
|
5
|
+
import { Aptos, AptosConfig, Network } from '@aptos-labs/ts-sdk'
|
|
6
|
+
import * as path from 'path'
|
|
7
|
+
import * as fs from 'fs'
|
|
8
|
+
import { Command } from 'commander'
|
|
9
|
+
import { createRequire } from 'module'
|
|
10
|
+
const require = createRequire(import.meta.url)
|
|
11
|
+
let pkg = undefined
|
|
12
|
+
try {
|
|
13
|
+
pkg = require('../../package.json')
|
|
14
|
+
} catch (e) {
|
|
15
|
+
pkg = require('../../../package.json')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const program = new Command()
|
|
19
|
+
|
|
20
|
+
program
|
|
21
|
+
.name('typemove-aptos')
|
|
22
|
+
.description('CLI to generate typescript types from Aptos ABIs')
|
|
23
|
+
.showHelpAfterError()
|
|
24
|
+
.version(pkg.version)
|
|
25
|
+
.argument('<location>', 'Directory of ABI json files or address of account to generate types for')
|
|
26
|
+
.option('-a, --abi-dir <dir>', 'Directory to store downloaded ABI. Only useful if <location> is address', './abis')
|
|
27
|
+
.option('-t, --target-dir <dir>', 'Directory to output generated files', './types')
|
|
28
|
+
.option(
|
|
29
|
+
'-n, --network <mainnet|testnet|$url>',
|
|
30
|
+
'Network to use, could be either "mainnet", "testnet" or any node URL',
|
|
31
|
+
'mainnet'
|
|
32
|
+
)
|
|
33
|
+
.action(async (location, options) => {
|
|
34
|
+
const endpoint = options.network
|
|
35
|
+
let config: AptosConfig
|
|
36
|
+
if (endpoint == 'mainnet') {
|
|
37
|
+
config = new AptosConfig({
|
|
38
|
+
network: Network.MAINNET
|
|
39
|
+
})
|
|
40
|
+
} else if (endpoint == 'testnet') {
|
|
41
|
+
config = new AptosConfig({
|
|
42
|
+
network: Network.TESTNET
|
|
43
|
+
})
|
|
44
|
+
} else {
|
|
45
|
+
config = new AptosConfig({
|
|
46
|
+
network: Network.CUSTOM,
|
|
47
|
+
fullnode: endpoint
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
const aptosClient = new Aptos(config)
|
|
51
|
+
|
|
52
|
+
let abisDir = location
|
|
53
|
+
if (location.startsWith('0x')) {
|
|
54
|
+
const abiAddress = abisDir
|
|
55
|
+
const abi = await new AptosChainAdapter(aptosClient).fetchModules(abiAddress)
|
|
56
|
+
abisDir = options.abiDir
|
|
57
|
+
if (!fs.existsSync(abisDir)) {
|
|
58
|
+
fs.mkdirSync(abisDir, { recursive: true })
|
|
59
|
+
}
|
|
60
|
+
fs.writeFileSync(path.join(abisDir, abiAddress + '.json'), JSON.stringify(abi, null, 2))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const num = await codegen(abisDir, options.targetDir, config, true)
|
|
64
|
+
process.exit(num == 0 ? 1 : 0)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
program.parse()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ResourceChange } from './models.js'
|
|
2
|
+
import { MoveCoder } from './move-coder.js'
|
|
3
|
+
|
|
4
|
+
export async function decodeResourceChange<T>(
|
|
5
|
+
resources: {
|
|
6
|
+
[key: string]: any
|
|
7
|
+
}[],
|
|
8
|
+
coder: MoveCoder
|
|
9
|
+
): Promise<ResourceChange<T>[]> {
|
|
10
|
+
const promises = resources.map(async (r) => {
|
|
11
|
+
r.data = await coder.decodeResource<T>(r.data)
|
|
12
|
+
return r as ResourceChange<T>
|
|
13
|
+
})
|
|
14
|
+
return await Promise.all(promises)
|
|
15
|
+
}
|
package/src/index.ts
ADDED
package/src/models.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DecodedStruct, NestedDecodedStruct } from '@typemove/move'
|
|
2
|
+
import {
|
|
3
|
+
EntryFunctionPayloadResponse,
|
|
4
|
+
MoveResource,
|
|
5
|
+
Event,
|
|
6
|
+
WriteSetChangeWriteResource,
|
|
7
|
+
WriteSetChangeDeleteResource
|
|
8
|
+
} from '@aptos-labs/ts-sdk'
|
|
9
|
+
|
|
10
|
+
export type TypedEventInstance<T> = DecodedStruct<Event, T>
|
|
11
|
+
export type TypedMoveResource<T> = DecodedStruct<MoveResource, T>
|
|
12
|
+
|
|
13
|
+
// Don't use intermediate type to make IDE happier
|
|
14
|
+
export type TypedFunctionPayload<T extends Array<any>> = EntryFunctionPayloadResponse & {
|
|
15
|
+
/**
|
|
16
|
+
* decoded argument data using ABI, undefined if there is decoding error, usually because the ABI/data mismatch
|
|
17
|
+
*/
|
|
18
|
+
arguments_decoded: T
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ResourceChange<T> =
|
|
22
|
+
| NestedDecodedStruct<MoveResource, WriteSetChangeWriteResource, T>
|
|
23
|
+
| WriteSetChangeDeleteResource
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { AbstractMoveCoder, InternalMoveModule, parseMoveType, TypeDescriptor } from '@typemove/move'
|
|
2
|
+
import { TypedEventInstance, TypedFunctionPayload, TypedMoveResource } from './models.js'
|
|
3
|
+
import { AptosChainAdapter } from './aptos-chain-adapter.js'
|
|
4
|
+
import { toInternalModule } from './to-internal.js'
|
|
5
|
+
import {
|
|
6
|
+
Aptos,
|
|
7
|
+
AptosConfig,
|
|
8
|
+
EntryFunctionPayloadResponse,
|
|
9
|
+
Event,
|
|
10
|
+
MoveModuleBytecode,
|
|
11
|
+
MoveResource,
|
|
12
|
+
MoveValue,
|
|
13
|
+
Network
|
|
14
|
+
} from '@aptos-labs/ts-sdk'
|
|
15
|
+
|
|
16
|
+
export class MoveCoder extends AbstractMoveCoder<MoveModuleBytecode, Event | MoveResource> {
|
|
17
|
+
constructor(
|
|
18
|
+
client: Aptos,
|
|
19
|
+
readonly ignoreObjectInnerField: boolean = false
|
|
20
|
+
) {
|
|
21
|
+
super(new AptosChainAdapter(client))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
load(module: MoveModuleBytecode, address: string): InternalMoveModule {
|
|
25
|
+
if (!module.abi) {
|
|
26
|
+
throw Error('Module without abi')
|
|
27
|
+
}
|
|
28
|
+
let m = this.moduleMapping.get(module.abi.address + '::' + module.abi.name)
|
|
29
|
+
// if (this.contains(module.abi.address, module.abi.name)) {
|
|
30
|
+
// return
|
|
31
|
+
// }
|
|
32
|
+
if (m) {
|
|
33
|
+
return m
|
|
34
|
+
}
|
|
35
|
+
this.accounts.add(module.abi.address)
|
|
36
|
+
m = toInternalModule(module)
|
|
37
|
+
this.loadInternal(m, address)
|
|
38
|
+
return m
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
decodeEvent<T>(event: Event): Promise<TypedEventInstance<T> | undefined> {
|
|
42
|
+
// TODO fix type
|
|
43
|
+
return this.decodedStruct(event)
|
|
44
|
+
}
|
|
45
|
+
filterAndDecodeEvents<T>(type: string | TypeDescriptor<T>, resources: Event[]): Promise<TypedEventInstance<T>[]> {
|
|
46
|
+
if (typeof type === 'string') {
|
|
47
|
+
type = parseMoveType(type)
|
|
48
|
+
}
|
|
49
|
+
// TODO fix type
|
|
50
|
+
return this.filterAndDecodeStruct(type, resources)
|
|
51
|
+
}
|
|
52
|
+
decodeResource<T>(res: MoveResource): Promise<TypedMoveResource<T> | undefined> {
|
|
53
|
+
return this.decodedStruct(res)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected async decode(data: any, type: TypeDescriptor): Promise<any> {
|
|
57
|
+
switch (type.qname) {
|
|
58
|
+
case '0x1::object::Object':
|
|
59
|
+
if (this.ignoreObjectInnerField && typeof data === 'string') {
|
|
60
|
+
return data
|
|
61
|
+
}
|
|
62
|
+
if (typeof data === 'object' && data?.inner !== undefined && typeof data?.inner === 'string') {
|
|
63
|
+
return data.inner
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return super.decode(data, type)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
filterAndDecodeResources<T>(
|
|
70
|
+
type: string | TypeDescriptor<T>,
|
|
71
|
+
resources: MoveResource[]
|
|
72
|
+
): Promise<TypedMoveResource<T>[]> {
|
|
73
|
+
if (typeof type === 'string') {
|
|
74
|
+
type = parseMoveType(type)
|
|
75
|
+
}
|
|
76
|
+
return this.filterAndDecodeStruct(type, resources)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async decodeFunctionPayload<T extends Array<any>>(
|
|
80
|
+
payload: EntryFunctionPayloadResponse
|
|
81
|
+
): Promise<TypedFunctionPayload<T>> {
|
|
82
|
+
const func = await this.getMoveFunction(payload.function)
|
|
83
|
+
const params = this.adapter.getMeaningfulFunctionParams(func.params)
|
|
84
|
+
const argumentsDecoded = await this.decodeArray(payload.arguments, params)
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
...payload,
|
|
88
|
+
arguments_decoded: argumentsDecoded
|
|
89
|
+
} as TypedFunctionPayload<T>
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
toMoveValue(value: any): MoveValue {
|
|
93
|
+
switch (typeof value) {
|
|
94
|
+
case 'boolean':
|
|
95
|
+
return value
|
|
96
|
+
case 'number':
|
|
97
|
+
return value
|
|
98
|
+
case 'bigint':
|
|
99
|
+
return value.toString()
|
|
100
|
+
case 'object':
|
|
101
|
+
if (Array.isArray(value)) {
|
|
102
|
+
return value.map(this.toMoveValue)
|
|
103
|
+
}
|
|
104
|
+
return value
|
|
105
|
+
default:
|
|
106
|
+
return value.toString()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const DEFAULT_CONFIG = new AptosConfig({ network: Network.MAINNET })
|
|
112
|
+
|
|
113
|
+
const CODER_MAP = new Map<string, MoveCoder>()
|
|
114
|
+
|
|
115
|
+
export function defaultMoveCoder(config = DEFAULT_CONFIG): MoveCoder {
|
|
116
|
+
const configKey = config.fullnode ? config.fullnode : config.network
|
|
117
|
+
|
|
118
|
+
let coder = CODER_MAP.get(configKey)
|
|
119
|
+
if (!coder) {
|
|
120
|
+
coder = new MoveCoder(new Aptos(config))
|
|
121
|
+
CODER_MAP.set(configKey, coder)
|
|
122
|
+
}
|
|
123
|
+
return coder
|
|
124
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
InternalMoveFunction,
|
|
3
|
+
InternalMoveFunctionVisibility,
|
|
4
|
+
InternalMoveModule,
|
|
5
|
+
InternalMoveStruct,
|
|
6
|
+
InternalMoveStructField,
|
|
7
|
+
parseMoveType
|
|
8
|
+
} from '@typemove/move'
|
|
9
|
+
import {
|
|
10
|
+
MoveFunctionVisibility,
|
|
11
|
+
MoveFunction,
|
|
12
|
+
MoveModuleBytecode,
|
|
13
|
+
MoveStruct,
|
|
14
|
+
MoveStructField
|
|
15
|
+
} from '@aptos-labs/ts-sdk'
|
|
16
|
+
|
|
17
|
+
export function toInternalModule(module: MoveModuleBytecode): InternalMoveModule {
|
|
18
|
+
if (!module.abi) {
|
|
19
|
+
throw Error('module with no ABI found')
|
|
20
|
+
}
|
|
21
|
+
const abi = module.abi
|
|
22
|
+
return {
|
|
23
|
+
address: abi.address,
|
|
24
|
+
exposedFunctions: abi.exposed_functions.map(toInternalFunction),
|
|
25
|
+
name: abi.name,
|
|
26
|
+
structs: abi.structs.map(toInternalStruct),
|
|
27
|
+
enums: []
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function toInternalFunction(func: MoveFunction): InternalMoveFunction {
|
|
32
|
+
let visibility
|
|
33
|
+
switch (func.visibility) {
|
|
34
|
+
case MoveFunctionVisibility.PRIVATE:
|
|
35
|
+
visibility = InternalMoveFunctionVisibility.PRIVATE
|
|
36
|
+
break
|
|
37
|
+
case MoveFunctionVisibility.PUBLIC:
|
|
38
|
+
visibility = InternalMoveFunctionVisibility.PUBLIC
|
|
39
|
+
break
|
|
40
|
+
case MoveFunctionVisibility.FRIEND:
|
|
41
|
+
visibility = InternalMoveFunctionVisibility.FRIEND
|
|
42
|
+
break
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
typeParams: func.generic_type_params,
|
|
46
|
+
isEntry: func.is_entry,
|
|
47
|
+
isView: func.is_view,
|
|
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
|
+
isEvent: struct.is_event,
|
|
62
|
+
name: struct.name
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function toInternalField(module: MoveStructField): InternalMoveStructField {
|
|
67
|
+
return {
|
|
68
|
+
name: module.name,
|
|
69
|
+
type: parseMoveType(module.type)
|
|
70
|
+
}
|
|
71
|
+
}
|