@sentio/sdk 2.4.0 → 2.5.0-rc.2
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/lib/aptos/aptos-processor.d.ts +1 -1
- package/lib/aptos/aptos-processor.js +1 -1
- package/lib/aptos/aptos-processor.js.map +1 -1
- package/lib/aptos/builtin/0x1.d.ts +16 -177
- package/lib/aptos/builtin/0x1.js +56 -538
- package/lib/aptos/builtin/0x1.js.map +1 -1
- package/lib/aptos/builtin/0x3.d.ts +2 -13
- package/lib/aptos/builtin/0x3.js +20 -48
- package/lib/aptos/builtin/0x3.js.map +1 -1
- package/lib/aptos/codegen/codegen.d.ts +0 -20
- package/lib/aptos/codegen/codegen.js +37 -357
- package/lib/aptos/codegen/codegen.js.map +1 -1
- package/lib/aptos/{types.test.d.ts → codegen/types.test.d.ts} +0 -0
- package/lib/aptos/codegen/types.test.js.map +1 -0
- package/lib/aptos/models.d.ts +0 -8
- package/lib/aptos/models.js.map +1 -1
- package/lib/aptos/move-coder.d.ts +4 -13
- package/lib/aptos/move-coder.js +13 -124
- package/lib/aptos/move-coder.js.map +1 -1
- package/lib/aptos/move-types.d.ts +6 -0
- package/lib/aptos/move-types.js +52 -1
- package/lib/aptos/move-types.js.map +1 -1
- package/lib/aptos/tests/souffl3.js +1 -1
- package/lib/aptos/tests/souffl3.js.map +1 -1
- package/lib/aptos/tests/types/reserved.d.ts +2 -8
- package/lib/aptos/tests/types/reserved.js +11 -24
- package/lib/aptos/tests/types/reserved.js.map +1 -1
- package/lib/aptos/tests/types/soffl3.d.ts +2 -14
- package/lib/aptos/tests/types/soffl3.js +15 -48
- package/lib/aptos/tests/types/soffl3.js.map +1 -1
- package/lib/aptos/tests/types/souffle.d.ts +2 -8
- package/lib/aptos/tests/types/souffle.js +11 -24
- package/lib/aptos/tests/types/souffle.js.map +1 -1
- package/lib/aptos/utils.d.ts +2 -7
- package/lib/aptos/utils.js +2 -15
- package/lib/aptos/utils.js.map +1 -1
- package/lib/eth/bind-options.js +6 -0
- package/lib/eth/bind-options.js.map +1 -1
- package/lib/eth/tests/codegen.test.js.map +1 -1
- package/lib/eth/tests/erc20.js +1 -1
- package/lib/eth/tests/erc20.js.map +1 -1
- package/lib/eth/tests/logger.test.js.map +1 -1
- package/lib/move/abstract-codegen.d.ts +44 -0
- package/lib/move/abstract-codegen.js +341 -0
- package/lib/move/abstract-codegen.js.map +1 -0
- package/lib/move/abstract-move-coder.d.ts +24 -0
- package/lib/move/abstract-move-coder.js +141 -0
- package/lib/move/abstract-move-coder.js.map +1 -0
- package/lib/move/account.d.ts +18 -0
- package/lib/{aptos/codegen/typegen.js → move/account.js} +20 -82
- package/lib/move/account.js.map +1 -0
- package/lib/move/filter.d.ts +25 -0
- package/lib/move/filter.js +11 -0
- package/lib/move/filter.js.map +1 -0
- package/lib/move/index.d.ts +5 -25
- package/lib/move/index.js +5 -10
- package/lib/move/index.js.map +1 -1
- package/lib/move/neutral-models.d.ts +34 -0
- package/lib/move/neutral-models.js +7 -0
- package/lib/move/neutral-models.js.map +1 -0
- package/lib/move/ts-type.d.ts +2 -0
- package/lib/move/ts-type.js +77 -0
- package/lib/move/ts-type.js.map +1 -0
- package/lib/{aptos/codegen/typegen.test.d.ts → move/ts-type.test.d.ts} +0 -0
- package/lib/move/ts-type.test.js.map +1 -0
- package/lib/{aptos → move}/types.d.ts +1 -0
- package/lib/{aptos → move}/types.js +26 -3
- package/lib/move/types.js.map +1 -0
- package/lib/move/utils.d.ts +9 -0
- package/lib/move/utils.js +20 -0
- package/lib/move/utils.js.map +1 -0
- package/lib/sui/builtin/0x1.d.ts +114 -0
- package/lib/sui/builtin/0x1.js +174 -0
- package/lib/sui/builtin/0x1.js.map +1 -0
- package/lib/sui/builtin/0x2.d.ts +1047 -0
- package/lib/sui/builtin/0x2.js +1295 -0
- package/lib/sui/builtin/0x2.js.map +1 -0
- package/lib/sui/builtin/index.d.ts +2 -0
- package/lib/sui/builtin/index.js +6 -0
- package/lib/sui/builtin/index.js.map +1 -0
- package/lib/sui/codegen/codegen.d.ts +1 -0
- package/lib/sui/codegen/codegen.js +61 -0
- package/lib/sui/codegen/codegen.js.map +1 -0
- package/lib/sui/codegen/index.d.ts +1 -0
- package/lib/sui/codegen/index.js +2 -0
- package/lib/sui/codegen/index.js.map +1 -0
- package/lib/sui/codegen/run.d.ts +1 -0
- package/lib/sui/codegen/run.js +12 -0
- package/lib/sui/codegen/run.js.map +1 -0
- package/lib/sui/index.d.ts +2 -0
- package/lib/sui/index.js +1 -0
- package/lib/sui/index.js.map +1 -1
- package/lib/sui/models.d.ts +7 -1
- package/lib/sui/models.js +0 -23
- package/lib/sui/models.js.map +1 -1
- package/lib/sui/move-coder.d.ts +11 -0
- package/lib/sui/move-coder.js +53 -0
- package/lib/sui/move-coder.js.map +1 -0
- package/lib/sui/move-types.d.ts +9 -0
- package/lib/sui/move-types.js +81 -0
- package/lib/sui/move-types.js.map +1 -0
- package/lib/sui/sui-processor.js +3 -3
- package/lib/sui/sui-processor.js.map +1 -1
- package/lib/sui/tests/sui.test.js.map +1 -1
- package/lib/sui/tests/types/index.d.ts +1 -0
- package/lib/sui/tests/types/index.js +5 -0
- package/lib/sui/tests/types/index.js.map +1 -0
- package/lib/sui/tests/types/testnet/index.d.ts +1 -0
- package/lib/sui/tests/types/testnet/index.js +5 -0
- package/lib/sui/tests/types/testnet/index.js.map +1 -0
- package/lib/sui/tests/types/testnet/swap.d.ts +249 -0
- package/lib/sui/tests/types/testnet/swap.js +310 -0
- package/lib/sui/tests/types/testnet/swap.js.map +1 -0
- package/lib/testing/aptos-facet.js +1 -1
- package/lib/testing/aptos-facet.js.map +1 -1
- package/lib/testing/sui-facet.js +3 -2
- package/lib/testing/sui-facet.js.map +1 -1
- package/package.json +8 -5
- package/src/aptos/aptos-processor.ts +1 -1
- package/src/aptos/builtin/0x1.ts +70 -652
- package/src/aptos/builtin/0x3.ts +24 -59
- package/src/aptos/codegen/codegen.ts +35 -421
- package/src/aptos/models.ts +0 -10
- package/src/aptos/move-coder.ts +16 -150
- package/src/aptos/move-types.ts +62 -0
- package/src/aptos/tests/souffl3.ts +1 -1
- package/src/aptos/tests/types/reserved.ts +15 -30
- package/src/aptos/tests/types/soffl3.ts +19 -58
- package/src/aptos/tests/types/souffle.ts +15 -30
- package/src/aptos/utils.ts +3 -21
- package/src/eth/bind-options.ts +6 -0
- package/src/eth/tests/erc20.ts +1 -1
- package/src/move/abstract-codegen.ts +426 -0
- package/src/move/abstract-move-coder.ts +170 -0
- package/src/move/account.ts +96 -0
- package/src/move/filter.ts +33 -0
- package/src/move/index.ts +5 -33
- package/src/move/neutral-models.ts +45 -0
- package/src/move/ts-type.ts +86 -0
- package/src/{aptos → move}/types.ts +28 -3
- package/src/move/utils.ts +25 -0
- package/src/sui/abis/0x1.json +1729 -1733
- package/src/sui/abis/0x2.json +13279 -13283
- package/src/sui/builtin/0x1.ts +307 -0
- package/src/sui/builtin/0x2.ts +2578 -0
- package/src/sui/builtin/index.ts +5 -0
- package/src/sui/codegen/codegen.ts +71 -0
- package/src/sui/codegen/index.ts +1 -0
- package/src/sui/codegen/run.ts +13 -0
- package/src/sui/index.ts +4 -0
- package/src/sui/models.ts +7 -28
- package/src/sui/move-coder.ts +63 -0
- package/src/sui/move-types.ts +109 -0
- package/src/sui/sui-processor.ts +3 -3
- package/src/sui/tests/abis/testnet/swap.json +1752 -0
- package/src/sui/tests/types/index.ts +3 -0
- package/src/sui/tests/types/testnet/index.ts +4 -0
- package/src/sui/tests/types/testnet/swap.ts +698 -0
- package/src/testing/aptos-facet.ts +1 -1
- package/src/testing/sui-facet.ts +3 -2
- package/lib/aptos/codegen/typegen.d.ts +0 -18
- package/lib/aptos/codegen/typegen.js.map +0 -1
- package/lib/aptos/codegen/typegen.test.js.map +0 -1
- package/lib/aptos/types.js.map +0 -1
- package/lib/aptos/types.test.js.map +0 -1
- package/lib/jest.config.d.ts +0 -9
- package/lib/jest.config.js +0 -9
- package/lib/jest.config.js.map +0 -1
- package/src/aptos/codegen/typegen.ts +0 -165
- package/src/jest.config.ts +0 -8
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { NeutralMoveFunction, NeutralMoveModule, NeutralMoveStruct } from './neutral-models.js';
|
|
2
|
+
import { AccountRegister } from './account.js';
|
|
3
|
+
import { TypeDescriptor } from './types.js';
|
|
4
|
+
interface OutputFile {
|
|
5
|
+
fileName: string;
|
|
6
|
+
fileContent: string;
|
|
7
|
+
}
|
|
8
|
+
interface Config<NetworkType> {
|
|
9
|
+
fileName: string;
|
|
10
|
+
outputDir: string;
|
|
11
|
+
network: NetworkType;
|
|
12
|
+
}
|
|
13
|
+
export declare abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
14
|
+
TEST_NET: NetworkType;
|
|
15
|
+
MAIN_NET: NetworkType;
|
|
16
|
+
ADDRESS_TYPE: string;
|
|
17
|
+
PREFIX: string;
|
|
18
|
+
STRUCT_FIELD_NAME: string;
|
|
19
|
+
abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>;
|
|
20
|
+
abstract toNeutral(modules: ModuleTypes): NeutralMoveModule[];
|
|
21
|
+
abstract getEventStructs(module: NeutralMoveModule): Map<string, NeutralMoveStruct>;
|
|
22
|
+
abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[];
|
|
23
|
+
readModulesFile(fullPath: string): any;
|
|
24
|
+
generate(srcDir: string, outputDir: string): Promise<void>;
|
|
25
|
+
generateForNetwork(srcDir: string, outputDir: string, network: NetworkType): Promise<void>;
|
|
26
|
+
generateNetworkOption(network: NetworkType): "TEST_NET" | "MAIN_NET";
|
|
27
|
+
generateModule(module: NeutralMoveModule, network: NetworkType): string;
|
|
28
|
+
generateStructs(module: NeutralMoveModule, struct: NeutralMoveStruct, events: Set<string>): string;
|
|
29
|
+
generateFunctionTypeParameters(func: NeutralMoveFunction): string;
|
|
30
|
+
generateStructTypeParameters(struct: NeutralMoveStruct, useAny?: boolean): string;
|
|
31
|
+
generateCallArgsStructs(module: NeutralMoveModule, func: NeutralMoveFunction): string | undefined;
|
|
32
|
+
generateOnEntryFunctions(module: NeutralMoveModule, func: NeutralMoveFunction): string;
|
|
33
|
+
generateOnEvents(module: NeutralMoveModule, struct: NeutralMoveStruct): string;
|
|
34
|
+
}
|
|
35
|
+
export declare class AccountCodegen<ModuleType, NetworkType> {
|
|
36
|
+
modules: NeutralMoveModule[];
|
|
37
|
+
config: Config<NetworkType>;
|
|
38
|
+
abi: ModuleType;
|
|
39
|
+
loader: AccountRegister;
|
|
40
|
+
moduleGen: AbstractCodegen<ModuleType, NetworkType>;
|
|
41
|
+
constructor(moduleGen: AbstractCodegen<ModuleType, NetworkType>, loader: AccountRegister, abi: ModuleType, modules: NeutralMoveModule[], config: Config<NetworkType>);
|
|
42
|
+
generate(): OutputFile[];
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { AccountModulesImportInfo, AccountRegister } from './account.js';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import { format } from 'prettier';
|
|
6
|
+
import { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER } from './utils.js';
|
|
7
|
+
import { generateTypeForDescriptor } from './ts-type.js';
|
|
8
|
+
import { camelCase } from 'lodash-es';
|
|
9
|
+
export class AbstractCodegen {
|
|
10
|
+
TEST_NET;
|
|
11
|
+
MAIN_NET;
|
|
12
|
+
ADDRESS_TYPE;
|
|
13
|
+
PREFIX;
|
|
14
|
+
STRUCT_FIELD_NAME = 'data';
|
|
15
|
+
readModulesFile(fullPath) {
|
|
16
|
+
return JSON.parse(fs.readFileSync(fullPath, 'utf-8'));
|
|
17
|
+
}
|
|
18
|
+
async generate(srcDir, outputDir) {
|
|
19
|
+
await this.generateForNetwork(srcDir, outputDir, this.MAIN_NET);
|
|
20
|
+
await this.generateForNetwork(path.join(srcDir, 'testnet'), path.join(outputDir, 'testnet'), this.TEST_NET);
|
|
21
|
+
}
|
|
22
|
+
async generateForNetwork(srcDir, outputDir, network) {
|
|
23
|
+
if (!fs.existsSync(srcDir)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (network === this.TEST_NET) {
|
|
27
|
+
console.log('Found testnet directory, generate code for testnet modules');
|
|
28
|
+
}
|
|
29
|
+
const files = fs.readdirSync(srcDir);
|
|
30
|
+
outputDir = path.resolve(outputDir);
|
|
31
|
+
const outputs = [];
|
|
32
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
33
|
+
const loader = new AccountRegister();
|
|
34
|
+
// when generating user code, don't need to generate framework account
|
|
35
|
+
loader.accountImports.set('0x1', new AccountModulesImportInfo('0x1', '0x1'));
|
|
36
|
+
loader.accountImports.set('0x2', new AccountModulesImportInfo('0x2', '0x2'));
|
|
37
|
+
loader.accountImports.set('0x3', new AccountModulesImportInfo('0x3', '0x3'));
|
|
38
|
+
// const client = getRpcClient(network)
|
|
39
|
+
for (const file of files) {
|
|
40
|
+
if (!file.endsWith('.json')) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const fullPath = path.resolve(srcDir, file);
|
|
44
|
+
const abi = this.readModulesFile(fullPath);
|
|
45
|
+
const modules = this.toNeutral(abi);
|
|
46
|
+
for (const module of modules) {
|
|
47
|
+
loader.register(module, path.basename(file, '.json'));
|
|
48
|
+
}
|
|
49
|
+
const codeGen = new AccountCodegen(this, loader, abi, modules, {
|
|
50
|
+
fileName: path.basename(file, '.json'),
|
|
51
|
+
outputDir: outputDir,
|
|
52
|
+
network,
|
|
53
|
+
});
|
|
54
|
+
outputs.push(...codeGen.generate());
|
|
55
|
+
}
|
|
56
|
+
while (loader.pendingAccounts.size > 0) {
|
|
57
|
+
for (const account of loader.pendingAccounts) {
|
|
58
|
+
console.log(`download dependent module for account ${account} at ${network}`);
|
|
59
|
+
try {
|
|
60
|
+
const rawModules = await this.fetchModules(account, network);
|
|
61
|
+
const modules = this.toNeutral(rawModules);
|
|
62
|
+
fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\t'));
|
|
63
|
+
for (const module of modules) {
|
|
64
|
+
loader.register(module, account);
|
|
65
|
+
}
|
|
66
|
+
const codeGen = new AccountCodegen(this, loader, rawModules, modules, {
|
|
67
|
+
fileName: account,
|
|
68
|
+
outputDir: outputDir,
|
|
69
|
+
network,
|
|
70
|
+
});
|
|
71
|
+
outputs.push(...codeGen.generate());
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
console.error(chalk.red('Error downloading account module, check if you choose the right network,or download account modules manually into your director'));
|
|
75
|
+
console.error(e);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const output of outputs) {
|
|
81
|
+
const content = format(output.fileContent, { parser: 'typescript' });
|
|
82
|
+
fs.writeFileSync(path.join(outputDir, output.fileName), content);
|
|
83
|
+
}
|
|
84
|
+
const rootFile = path.join(outputDir, 'index.ts');
|
|
85
|
+
fs.writeFileSync(rootFile, `/* Autogenerated file. Do not edit manually. */
|
|
86
|
+
/* tslint:disable */
|
|
87
|
+
/* eslint-disable */
|
|
88
|
+
`);
|
|
89
|
+
for (const output of outputs) {
|
|
90
|
+
const parsed = path.parse(output.fileName);
|
|
91
|
+
const content = `export * as _${parsed.name} from './${parsed.name}.js'\n`;
|
|
92
|
+
fs.appendFileSync(rootFile, content);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
generateNetworkOption(network) {
|
|
96
|
+
switch (network) {
|
|
97
|
+
case this.TEST_NET:
|
|
98
|
+
return 'TEST_NET';
|
|
99
|
+
}
|
|
100
|
+
return 'MAIN_NET';
|
|
101
|
+
}
|
|
102
|
+
generateModule(module, network) {
|
|
103
|
+
const functions = module.exposed_functions
|
|
104
|
+
.map((f) => this.generateOnEntryFunctions(module, f))
|
|
105
|
+
.filter((s) => s !== '');
|
|
106
|
+
const eventStructs = this.getEventStructs(module);
|
|
107
|
+
const eventTypes = new Set(eventStructs.keys());
|
|
108
|
+
const events = Array.from(eventStructs.values())
|
|
109
|
+
.map((e) => this.generateOnEvents(module, e))
|
|
110
|
+
.filter((s) => s !== '');
|
|
111
|
+
const structs = module.structs.map((s) => this.generateStructs(module, s, eventTypes));
|
|
112
|
+
const callArgs = module.exposed_functions.map((f) => this.generateCallArgsStructs(module, f));
|
|
113
|
+
const moduleName = normalizeToJSName(module.name);
|
|
114
|
+
let processor = '';
|
|
115
|
+
if (functions.length > 0 || events.length > 0) {
|
|
116
|
+
processor = `export class ${moduleName} extends ${this.PREFIX}BaseProcessor {
|
|
117
|
+
|
|
118
|
+
constructor(options: ${this.PREFIX}BindOptions) {
|
|
119
|
+
super("${module.name}", options)
|
|
120
|
+
}
|
|
121
|
+
static DEFAULT_OPTIONS: ${this.PREFIX}BindOptions = {
|
|
122
|
+
address: "${module.address}",
|
|
123
|
+
network: ${this.PREFIX}Network.${this.generateNetworkOption(network)}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static bind(options: Partial<${this.PREFIX}BindOptions> = {}): ${moduleName} {
|
|
127
|
+
return new ${moduleName}({ ...${moduleName}.DEFAULT_OPTIONS, ...options })
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
${functions.join('\n')}
|
|
131
|
+
|
|
132
|
+
${events.join('\n')}
|
|
133
|
+
}
|
|
134
|
+
`;
|
|
135
|
+
}
|
|
136
|
+
return `
|
|
137
|
+
${processor}
|
|
138
|
+
|
|
139
|
+
export namespace ${moduleName} {
|
|
140
|
+
${structs.join('\n')}
|
|
141
|
+
|
|
142
|
+
${callArgs.join('\n')}
|
|
143
|
+
}
|
|
144
|
+
`;
|
|
145
|
+
}
|
|
146
|
+
generateStructs(module, struct, events) {
|
|
147
|
+
const genericString = this.generateStructTypeParameters(struct);
|
|
148
|
+
const genericStringAny = this.generateStructTypeParameters(struct, true);
|
|
149
|
+
const structName = normalizeToJSName(struct.name);
|
|
150
|
+
const fields = struct.fields.map((field) => {
|
|
151
|
+
const type = generateTypeForDescriptor(field.type, module.address, this.ADDRESS_TYPE);
|
|
152
|
+
return `${field.name}: ${type}`;
|
|
153
|
+
});
|
|
154
|
+
let eventPayload = '';
|
|
155
|
+
if (events.has(moduleQname(module) + SPLITTER + struct.name)) {
|
|
156
|
+
eventPayload = `
|
|
157
|
+
export interface ${structName}Instance extends
|
|
158
|
+
TypedEventInstance<${structName}${genericStringAny}> {
|
|
159
|
+
${this.STRUCT_FIELD_NAME}_decoded: ${structName}${genericStringAny}
|
|
160
|
+
type_arguments: [${struct.generic_type_params.map((_) => 'string').join(', ')}]
|
|
161
|
+
}
|
|
162
|
+
`;
|
|
163
|
+
}
|
|
164
|
+
return `
|
|
165
|
+
export class ${structName}${genericString} {
|
|
166
|
+
static TYPE_QNAME = '${module.address}::${module.name}::${struct.name}'
|
|
167
|
+
${fields.join('\n')}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
${eventPayload}
|
|
171
|
+
`;
|
|
172
|
+
}
|
|
173
|
+
generateFunctionTypeParameters(func) {
|
|
174
|
+
let genericString = '';
|
|
175
|
+
if (func.generic_type_params && func.generic_type_params.length > 0) {
|
|
176
|
+
const params = func.generic_type_params
|
|
177
|
+
.map((v, idx) => {
|
|
178
|
+
return `T${idx}=any`;
|
|
179
|
+
})
|
|
180
|
+
.join(',');
|
|
181
|
+
genericString = `<${params}>`;
|
|
182
|
+
}
|
|
183
|
+
return genericString;
|
|
184
|
+
}
|
|
185
|
+
generateStructTypeParameters(struct, useAny = false) {
|
|
186
|
+
let genericString = '';
|
|
187
|
+
if (struct.generic_type_params && struct.generic_type_params.length > 0) {
|
|
188
|
+
const params = struct.generic_type_params
|
|
189
|
+
.map((v, idx) => {
|
|
190
|
+
return useAny ? 'any' : 'T' + idx;
|
|
191
|
+
})
|
|
192
|
+
.join(',');
|
|
193
|
+
genericString = `<${params}>`;
|
|
194
|
+
}
|
|
195
|
+
return genericString;
|
|
196
|
+
}
|
|
197
|
+
generateCallArgsStructs(module, func) {
|
|
198
|
+
if (!func.is_entry) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
|
|
202
|
+
return generateTypeForDescriptor(param, module.address, this.ADDRESS_TYPE);
|
|
203
|
+
});
|
|
204
|
+
const camelFuncName = capitalizeFirstChar(camelCase(func.name));
|
|
205
|
+
const genericString = this.generateFunctionTypeParameters(func);
|
|
206
|
+
return `
|
|
207
|
+
export interface ${camelFuncName}Payload${genericString}
|
|
208
|
+
extends TypedEntryFunctionPayload<[${fields.join(',')}]> {
|
|
209
|
+
arguments_decoded: [${fields.join(',')}],
|
|
210
|
+
type_arguments: [${func.generic_type_params.map((_) => 'string').join(', ')}]
|
|
211
|
+
}
|
|
212
|
+
`;
|
|
213
|
+
}
|
|
214
|
+
generateOnEntryFunctions(module, func) {
|
|
215
|
+
if (!func.is_entry) {
|
|
216
|
+
return '';
|
|
217
|
+
}
|
|
218
|
+
// const genericString = generateFunctionTypeParameters(func)
|
|
219
|
+
const moduleName = normalizeToJSName(module.name);
|
|
220
|
+
const camelFuncName = capitalizeFirstChar(camelCase(func.name));
|
|
221
|
+
const source = `
|
|
222
|
+
onEntry${camelFuncName}(func: (call: ${moduleName}.${camelFuncName}Payload, ctx: ${this.PREFIX}Context) => void, filter?: CallFilter, fetchConfig?: MoveFetchConfig): ${moduleName} {
|
|
223
|
+
this.onEntryFunctionCall(func, {
|
|
224
|
+
...filter,
|
|
225
|
+
function: '${module.name}::${func.name}'
|
|
226
|
+
},
|
|
227
|
+
fetchConfig)
|
|
228
|
+
return this
|
|
229
|
+
}`;
|
|
230
|
+
return source;
|
|
231
|
+
}
|
|
232
|
+
generateOnEvents(module, struct) {
|
|
233
|
+
// for struct that has drop + store
|
|
234
|
+
// if (!isEvent(struct, module)) {
|
|
235
|
+
// return ''
|
|
236
|
+
// }
|
|
237
|
+
// const genericString = generateStructTypeParameters(struct)
|
|
238
|
+
const moduleName = normalizeToJSName(module.name);
|
|
239
|
+
const source = `
|
|
240
|
+
onEvent${struct.name}(func: (event: ${moduleName}.${normalizeToJSName(struct.name)}Instance, ctx: ${this.PREFIX}Context) => void, fetchConfig?: MoveFetchConfig): ${moduleName} {
|
|
241
|
+
this.onMoveEvent(func, {
|
|
242
|
+
type: '${module.name}::${struct.name}'
|
|
243
|
+
},
|
|
244
|
+
fetchConfig)
|
|
245
|
+
return this
|
|
246
|
+
}
|
|
247
|
+
`;
|
|
248
|
+
return source;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
export class AccountCodegen {
|
|
252
|
+
modules;
|
|
253
|
+
config;
|
|
254
|
+
abi;
|
|
255
|
+
loader;
|
|
256
|
+
moduleGen;
|
|
257
|
+
constructor(moduleGen, loader, abi, modules, config) {
|
|
258
|
+
// const json = fs.readFileSync(config.srcFile, 'utf-8')
|
|
259
|
+
this.moduleGen = moduleGen;
|
|
260
|
+
this.abi = abi;
|
|
261
|
+
this.modules = modules;
|
|
262
|
+
this.config = config;
|
|
263
|
+
this.loader = loader;
|
|
264
|
+
}
|
|
265
|
+
generate() {
|
|
266
|
+
if (!this.modules) {
|
|
267
|
+
return [];
|
|
268
|
+
}
|
|
269
|
+
// const baseName = path.basename(this.config.fileName, '.json')
|
|
270
|
+
let address;
|
|
271
|
+
for (const module of this.modules) {
|
|
272
|
+
address = module.address;
|
|
273
|
+
}
|
|
274
|
+
if (!address) {
|
|
275
|
+
return [];
|
|
276
|
+
}
|
|
277
|
+
const imports = `
|
|
278
|
+
import { CallFilter } from "@sentio/sdk/move"
|
|
279
|
+
import {
|
|
280
|
+
MoveCoder, defaultMoveCoder, ${this.moduleGen.PREFIX}BindOptions, ${this.moduleGen.PREFIX}BaseProcessor,
|
|
281
|
+
TypedEventInstance, ${this.moduleGen.PREFIX}Network, TypedEntryFunctionPayload,
|
|
282
|
+
${this.moduleGen.PREFIX}Context } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
|
|
283
|
+
import { MoveFetchConfig } from "@sentio/protos"
|
|
284
|
+
import { ${this.moduleGen.ADDRESS_TYPE} } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}"
|
|
285
|
+
`;
|
|
286
|
+
const dependedAccounts = [];
|
|
287
|
+
const moduleImports = [];
|
|
288
|
+
const info = this.loader.accountImports.get(address);
|
|
289
|
+
if (info) {
|
|
290
|
+
for (const [account] of info.imports.entries()) {
|
|
291
|
+
// Remap to user's filename if possible, TODO codepath not well tested
|
|
292
|
+
const tsAccountModule = './' + (this.loader.accountImports.get(account)?.moduleName || account);
|
|
293
|
+
if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {
|
|
294
|
+
// Decide where to find runtime library
|
|
295
|
+
moduleImports.push(`import { _${account} } from "@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}/builtin"`);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
moduleImports.push(`import * as _${account} from "${tsAccountModule}.js"`);
|
|
299
|
+
}
|
|
300
|
+
dependedAccounts.push(account);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const source = `
|
|
304
|
+
/* Autogenerated file. Do not edit manually. */
|
|
305
|
+
/* tslint:disable */
|
|
306
|
+
/* eslint-disable */
|
|
307
|
+
|
|
308
|
+
/* Generated modules for account ${address} */
|
|
309
|
+
|
|
310
|
+
${imports}
|
|
311
|
+
|
|
312
|
+
${moduleImports.join('\n')}
|
|
313
|
+
|
|
314
|
+
${this.modules.map((m) => this.moduleGen.generateModule(m, this.config.network)).join('\n')}
|
|
315
|
+
|
|
316
|
+
const MODULES = JSON.parse('${JSON.stringify(this.abi)}')
|
|
317
|
+
|
|
318
|
+
export function loadAllTypes(coder: MoveCoder) {
|
|
319
|
+
${dependedAccounts.map((a) => `_${a}.loadAllTypes(coder)`).join('\n')}
|
|
320
|
+
for (const m of Object.values(MODULES)) {
|
|
321
|
+
coder.load(m as any)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
loadAllTypes(defaultMoveCoder())
|
|
326
|
+
`; // source
|
|
327
|
+
return [
|
|
328
|
+
{
|
|
329
|
+
fileName: this.config.fileName + '.ts',
|
|
330
|
+
fileContent: source,
|
|
331
|
+
},
|
|
332
|
+
];
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function capitalizeFirstChar(input) {
|
|
336
|
+
if (!input) {
|
|
337
|
+
return input;
|
|
338
|
+
}
|
|
339
|
+
return input[0].toUpperCase() + (input.length > 1 ? input.substring(1) : '');
|
|
340
|
+
}
|
|
341
|
+
//# sourceMappingURL=abstract-codegen.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract-codegen.js","sourceRoot":"","sources":["../../src/move/abstract-codegen.ts"],"names":[],"mappings":"AACA,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,wBAAwB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AACxE,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACzF,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAA;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAcrC,MAAM,OAAgB,eAAe;IACnC,QAAQ,CAAa;IACrB,QAAQ,CAAa;IACrB,YAAY,CAAQ;IACpB,MAAM,CAAQ;IACd,iBAAiB,GAAW,MAAM,CAAA;IAQlC,eAAe,CAAC,QAAgB;QAC9B,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAA;IACvD,CAAC;IAED,KAAK,CAAC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-models.js'\nimport path from 'path'\nimport fs from 'fs'\nimport { AccountModulesImportInfo, AccountRegister } from './account.js'\nimport chalk from 'chalk'\nimport { format } from 'prettier'\nimport { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER } from './utils.js'\nimport { generateTypeForDescriptor } from './ts-type.js'\nimport { camelCase } from 'lodash-es'\nimport { TypeDescriptor } from './types.js'\n\ninterface OutputFile {\n fileName: string\n fileContent: string\n}\n\ninterface Config<NetworkType> {\n fileName: string\n outputDir: string\n network: NetworkType\n}\n\nexport abstract class AbstractCodegen<ModuleTypes, NetworkType> {\n TEST_NET: NetworkType\n MAIN_NET: NetworkType\n ADDRESS_TYPE: string\n PREFIX: string\n STRUCT_FIELD_NAME: string = 'data'\n\n abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>\n abstract toNeutral(modules: ModuleTypes): NeutralMoveModule[]\n abstract getEventStructs(module: NeutralMoveModule): Map<string, NeutralMoveStruct>\n\n abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]\n\n readModulesFile(fullPath: string) {\n return JSON.parse(fs.readFileSync(fullPath, 'utf-8'))\n }\n\n async generate(srcDir: string, outputDir: string) {\n await this.generateForNetwork(srcDir, outputDir, this.MAIN_NET)\n await this.generateForNetwork(path.join(srcDir, 'testnet'), path.join(outputDir, 'testnet'), this.TEST_NET)\n }\n\n async generateForNetwork(srcDir: string, outputDir: string, network: NetworkType) {\n if (!fs.existsSync(srcDir)) {\n return\n }\n if (network === this.TEST_NET) {\n console.log('Found testnet directory, generate code for testnet modules')\n }\n const files = fs.readdirSync(srcDir)\n outputDir = path.resolve(outputDir)\n const outputs: OutputFile[] = []\n\n fs.mkdirSync(outputDir, { recursive: true })\n\n const loader = new AccountRegister()\n\n // when generating user code, don't need to generate framework account\n loader.accountImports.set('0x1', new AccountModulesImportInfo('0x1', '0x1'))\n loader.accountImports.set('0x2', new AccountModulesImportInfo('0x2', '0x2'))\n loader.accountImports.set('0x3', new AccountModulesImportInfo('0x3', '0x3'))\n // const client = getRpcClient(network)\n\n for (const file of files) {\n if (!file.endsWith('.json')) {\n continue\n }\n const fullPath = path.resolve(srcDir, file)\n const abi = this.readModulesFile(fullPath)\n const modules = this.toNeutral(abi)\n\n for (const module of modules) {\n loader.register(module, path.basename(file, '.json'))\n }\n const codeGen = new AccountCodegen(this, loader, abi, modules, {\n fileName: path.basename(file, '.json'),\n outputDir: outputDir,\n network,\n })\n\n outputs.push(...codeGen.generate())\n }\n\n while (loader.pendingAccounts.size > 0) {\n for (const account of loader.pendingAccounts) {\n console.log(`download dependent module for account ${account} at ${network}`)\n\n try {\n const rawModules = await this.fetchModules(account, network)\n const modules = this.toNeutral(rawModules)\n\n fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\\t'))\n\n for (const module of modules) {\n loader.register(module, account)\n }\n const codeGen = new AccountCodegen(this, loader, rawModules, modules, {\n fileName: account,\n outputDir: outputDir,\n network,\n })\n\n outputs.push(...codeGen.generate())\n } catch (e) {\n console.error(\n chalk.red(\n 'Error downloading account module, check if you choose the right network,or download account modules manually into your director'\n )\n )\n console.error(e)\n process.exit(1)\n }\n }\n }\n\n for (const output of outputs) {\n const content = format(output.fileContent, { parser: 'typescript' })\n fs.writeFileSync(path.join(outputDir, output.fileName), content)\n }\n\n const rootFile = path.join(outputDir, 'index.ts')\n fs.writeFileSync(\n rootFile,\n `/* Autogenerated file. Do not edit manually. */\n/* tslint:disable */\n/* eslint-disable */\n`\n )\n for (const output of outputs) {\n const parsed = path.parse(output.fileName)\n const content = `export * as _${parsed.name} from './${parsed.name}.js'\\n`\n fs.appendFileSync(rootFile, content)\n }\n }\n\n generateNetworkOption(network: NetworkType) {\n switch (network) {\n case this.TEST_NET:\n return 'TEST_NET'\n }\n return 'MAIN_NET'\n }\n\n generateModule(module: NeutralMoveModule, network: NetworkType) {\n const functions = module.exposed_functions\n .map((f) => this.generateOnEntryFunctions(module, f))\n .filter((s) => s !== '')\n\n const eventStructs = this.getEventStructs(module)\n const eventTypes = new Set(eventStructs.keys())\n const events = Array.from(eventStructs.values())\n .map((e) => this.generateOnEvents(module, e))\n .filter((s) => s !== '')\n const structs = module.structs.map((s) => this.generateStructs(module, s, eventTypes))\n const callArgs = module.exposed_functions.map((f) => this.generateCallArgsStructs(module, f))\n\n const moduleName = normalizeToJSName(module.name)\n let processor = ''\n if (functions.length > 0 || events.length > 0) {\n processor = `export class ${moduleName} extends ${this.PREFIX}BaseProcessor {\n\n constructor(options: ${this.PREFIX}BindOptions) {\n super(\"${module.name}\", options)\n }\n static DEFAULT_OPTIONS: ${this.PREFIX}BindOptions = {\n address: \"${module.address}\",\n network: ${this.PREFIX}Network.${this.generateNetworkOption(network)} \n }\n\n static bind(options: Partial<${this.PREFIX}BindOptions> = {}): ${moduleName} {\n return new ${moduleName}({ ...${moduleName}.DEFAULT_OPTIONS, ...options })\n }\n \n ${functions.join('\\n')}\n \n ${events.join('\\n')}\n }\n `\n }\n\n return `\n ${processor}\n\n export namespace ${moduleName} {\n ${structs.join('\\n')}\n \n ${callArgs.join('\\n')}\n }\n `\n }\n\n generateStructs(module: NeutralMoveModule, struct: NeutralMoveStruct, events: Set<string>) {\n const genericString = this.generateStructTypeParameters(struct)\n const genericStringAny = this.generateStructTypeParameters(struct, true)\n\n const structName = normalizeToJSName(struct.name)\n\n const fields = struct.fields.map((field) => {\n const type = generateTypeForDescriptor(field.type, module.address, this.ADDRESS_TYPE)\n return `${field.name}: ${type}`\n })\n\n let eventPayload = ''\n if (events.has(moduleQname(module) + SPLITTER + struct.name)) {\n eventPayload = `\n export interface ${structName}Instance extends \n TypedEventInstance<${structName}${genericStringAny}> {\n ${this.STRUCT_FIELD_NAME}_decoded: ${structName}${genericStringAny}\n type_arguments: [${struct.generic_type_params.map((_) => 'string').join(', ')}]\n }\n `\n }\n\n return `\n export class ${structName}${genericString} {\n static TYPE_QNAME = '${module.address}::${module.name}::${struct.name}'\n ${fields.join('\\n')} \n }\n \n ${eventPayload}\n `\n }\n\n generateFunctionTypeParameters(func: NeutralMoveFunction) {\n let genericString = ''\n if (func.generic_type_params && func.generic_type_params.length > 0) {\n const params = func.generic_type_params\n .map((v, idx) => {\n return `T${idx}=any`\n })\n .join(',')\n genericString = `<${params}>`\n }\n return genericString\n }\n\n generateStructTypeParameters(struct: NeutralMoveStruct, useAny = false) {\n let genericString = ''\n\n if (struct.generic_type_params && struct.generic_type_params.length > 0) {\n const params = struct.generic_type_params\n .map((v, idx) => {\n return useAny ? 'any' : 'T' + idx\n })\n .join(',')\n genericString = `<${params}>`\n }\n return genericString\n }\n\n generateCallArgsStructs(module: NeutralMoveModule, func: NeutralMoveFunction) {\n if (!func.is_entry) {\n return\n }\n\n const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {\n return generateTypeForDescriptor(param, module.address, this.ADDRESS_TYPE)\n })\n\n const camelFuncName = capitalizeFirstChar(camelCase(func.name))\n\n const genericString = this.generateFunctionTypeParameters(func)\n return `\n export interface ${camelFuncName}Payload${genericString}\n extends TypedEntryFunctionPayload<[${fields.join(',')}]> {\n arguments_decoded: [${fields.join(',')}],\n type_arguments: [${func.generic_type_params.map((_) => 'string').join(', ')}]\n }\n `\n }\n\n generateOnEntryFunctions(module: NeutralMoveModule, func: NeutralMoveFunction) {\n if (!func.is_entry) {\n return ''\n }\n\n // const genericString = generateFunctionTypeParameters(func)\n const moduleName = normalizeToJSName(module.name)\n\n const camelFuncName = capitalizeFirstChar(camelCase(func.name))\n const source = `\n onEntry${camelFuncName}(func: (call: ${moduleName}.${camelFuncName}Payload, ctx: ${this.PREFIX}Context) => void, filter?: CallFilter, fetchConfig?: MoveFetchConfig): ${moduleName} {\n this.onEntryFunctionCall(func, {\n ...filter,\n function: '${module.name}::${func.name}'\n },\n fetchConfig)\n return this\n }`\n\n return source\n }\n\n generateOnEvents(module: NeutralMoveModule, struct: NeutralMoveStruct): string {\n // for struct that has drop + store\n // if (!isEvent(struct, module)) {\n // return ''\n // }\n\n // const genericString = generateStructTypeParameters(struct)\n\n const moduleName = normalizeToJSName(module.name)\n const source = `\n onEvent${struct.name}(func: (event: ${moduleName}.${normalizeToJSName(struct.name)}Instance, ctx: ${\n this.PREFIX\n }Context) => void, fetchConfig?: MoveFetchConfig): ${moduleName} {\n this.onMoveEvent(func, {\n type: '${module.name}::${struct.name}'\n },\n fetchConfig)\n return this\n }\n `\n return source\n }\n}\n\nexport class AccountCodegen<ModuleType, NetworkType> {\n modules: NeutralMoveModule[]\n config: Config<NetworkType>\n abi: ModuleType\n loader: AccountRegister\n moduleGen: AbstractCodegen<ModuleType, NetworkType>\n\n constructor(\n moduleGen: AbstractCodegen<ModuleType, NetworkType>,\n loader: AccountRegister,\n abi: ModuleType,\n modules: NeutralMoveModule[],\n config: Config<NetworkType>\n ) {\n // const json = fs.readFileSync(config.srcFile, 'utf-8')\n this.moduleGen = moduleGen\n this.abi = abi\n this.modules = modules\n this.config = config\n this.loader = loader\n }\n\n generate(): OutputFile[] {\n if (!this.modules) {\n return []\n }\n // const baseName = path.basename(this.config.fileName, '.json')\n\n let address: string | undefined\n for (const module of this.modules) {\n address = module.address\n }\n if (!address) {\n return []\n }\n\n const imports = `\n import { CallFilter } from \"@sentio/sdk/move\"\n import { \n MoveCoder, defaultMoveCoder, ${this.moduleGen.PREFIX}BindOptions, ${this.moduleGen.PREFIX}BaseProcessor, \n TypedEventInstance, ${this.moduleGen.PREFIX}Network, TypedEntryFunctionPayload,\n ${this.moduleGen.PREFIX}Context } from \"@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}\"\n import { MoveFetchConfig } from \"@sentio/protos\"\n import { ${this.moduleGen.ADDRESS_TYPE} } from \"@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}\"\n `\n\n const dependedAccounts: string[] = []\n\n const moduleImports: string[] = []\n\n const info = this.loader.accountImports.get(address)\n\n if (info) {\n for (const [account] of info.imports.entries()) {\n // Remap to user's filename if possible, TODO codepath not well tested\n const tsAccountModule = './' + (this.loader.accountImports.get(account)?.moduleName || account)\n if (isFrameworkAccount(account) && !isFrameworkAccount(address)) {\n // Decide where to find runtime library\n moduleImports.push(`import { _${account} } from \"@sentio/sdk/${this.moduleGen.PREFIX.toLowerCase()}/builtin\"`)\n } else {\n moduleImports.push(`import * as _${account} from \"${tsAccountModule}.js\"`)\n }\n\n dependedAccounts.push(account)\n }\n }\n\n const source = `\n /* Autogenerated file. Do not edit manually. */\n /* tslint:disable */\n /* eslint-disable */\n \n /* Generated modules for account ${address} */\n \n ${imports}\n \n ${moduleImports.join('\\n')}\n \n ${this.modules.map((m) => this.moduleGen.generateModule(m, this.config.network)).join('\\n')}\n \n const MODULES = JSON.parse('${JSON.stringify(this.abi)}')\n\n export function loadAllTypes(coder: MoveCoder) {\n ${dependedAccounts.map((a) => `_${a}.loadAllTypes(coder)`).join('\\n')}\n for (const m of Object.values(MODULES)) {\n coder.load(m as any)\n }\n }\n \n loadAllTypes(defaultMoveCoder())\n ` // source\n\n return [\n {\n fileName: this.config.fileName + '.ts',\n fileContent: source,\n },\n ]\n }\n}\n\nfunction capitalizeFirstChar(input: string): string {\n if (!input) {\n return input\n }\n return input[0].toUpperCase() + (input.length > 1 ? input.substring(1) : '')\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { TypeDescriptor } from './types.js';
|
|
2
|
+
import { NeutralMoveFunction, NeutralMoveModule, NeutralMoveStruct } from './neutral-models.js';
|
|
3
|
+
type StructWithTag<Base> = Base & {
|
|
4
|
+
type: string;
|
|
5
|
+
data: any;
|
|
6
|
+
};
|
|
7
|
+
type DecodedStructWithTag<B, T> = StructWithTag<B> & {
|
|
8
|
+
data_decoded: T;
|
|
9
|
+
type_arguments: string[];
|
|
10
|
+
};
|
|
11
|
+
export declare abstract class AbstractMoveCoder<StructType> {
|
|
12
|
+
private moduleMapping;
|
|
13
|
+
private typeMapping;
|
|
14
|
+
private funcMapping;
|
|
15
|
+
contains(account: string, name: string): boolean;
|
|
16
|
+
loadNeutral(module: NeutralMoveModule): void;
|
|
17
|
+
protected dataToBigInt(data: any): bigint;
|
|
18
|
+
protected getMoveStruct(type: string): NeutralMoveStruct;
|
|
19
|
+
protected getMoveFunction(type: string): NeutralMoveFunction;
|
|
20
|
+
protected decode(data: any, type: TypeDescriptor): any;
|
|
21
|
+
protected filterAndDecodeInternal<T>(typeQname: string, structs: StructWithTag<StructType>[]): DecodedStructWithTag<StructType, T>[];
|
|
22
|
+
protected decodedInternal<T>(typeStruct: StructWithTag<StructType>): DecodedStructWithTag<StructType, T> | undefined;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js';
|
|
2
|
+
import { parseMoveType } from './types.js';
|
|
3
|
+
import { bytesToBigInt } from '../utils/index.js';
|
|
4
|
+
export class AbstractMoveCoder {
|
|
5
|
+
moduleMapping = new Map();
|
|
6
|
+
typeMapping = new Map();
|
|
7
|
+
funcMapping = new Map();
|
|
8
|
+
contains(account, name) {
|
|
9
|
+
return this.moduleMapping.has(account + '::' + name);
|
|
10
|
+
}
|
|
11
|
+
loadNeutral(module) {
|
|
12
|
+
if (this.contains(module.address, module.name)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
this.moduleMapping.set(moduleQname(module), module);
|
|
16
|
+
for (const struct of module.structs) {
|
|
17
|
+
// TODO move to util
|
|
18
|
+
const key = [module.address, module.name, struct.name].join(SPLITTER);
|
|
19
|
+
this.typeMapping.set(key, struct);
|
|
20
|
+
}
|
|
21
|
+
for (const func of module.exposed_functions) {
|
|
22
|
+
if (!func.is_entry) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const key = [module.address, module.name, func.name].join(SPLITTER);
|
|
26
|
+
this.funcMapping.set(key, func);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
dataToBigInt(data) {
|
|
30
|
+
if (Array.isArray(data)) {
|
|
31
|
+
// Only sui function need this, strange
|
|
32
|
+
const bytes = data;
|
|
33
|
+
return bytesToBigInt(new Uint8Array(bytes.slice().reverse()));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return BigInt(data);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
getMoveStruct(type) {
|
|
40
|
+
const struct = this.typeMapping.get(type);
|
|
41
|
+
if (!struct) {
|
|
42
|
+
throw new Error('Failed to load type' + type + ' type are not imported anywhere');
|
|
43
|
+
}
|
|
44
|
+
return struct;
|
|
45
|
+
}
|
|
46
|
+
getMoveFunction(type) {
|
|
47
|
+
const func = this.funcMapping.get(type);
|
|
48
|
+
if (!func) {
|
|
49
|
+
throw new Error('Failed to load function' + type + ' type are not imported anywhere');
|
|
50
|
+
}
|
|
51
|
+
return func;
|
|
52
|
+
}
|
|
53
|
+
decode(data, type) {
|
|
54
|
+
// process simple type
|
|
55
|
+
if (type.reference) {
|
|
56
|
+
return data;
|
|
57
|
+
}
|
|
58
|
+
switch (type.qname) {
|
|
59
|
+
case 'signer': // TODO check this, aptos only
|
|
60
|
+
case 'address':
|
|
61
|
+
case 'Address':
|
|
62
|
+
case '0x2::object::ID':
|
|
63
|
+
case '0x2::coin::Coin':
|
|
64
|
+
case '0x1::string::String':
|
|
65
|
+
case 'bool':
|
|
66
|
+
case 'Bool':
|
|
67
|
+
case 'u8':
|
|
68
|
+
case 'U8':
|
|
69
|
+
case 'u16':
|
|
70
|
+
case 'U16':
|
|
71
|
+
case 'u32':
|
|
72
|
+
case 'U32':
|
|
73
|
+
return data;
|
|
74
|
+
case 'u64':
|
|
75
|
+
case 'U64':
|
|
76
|
+
case 'u128':
|
|
77
|
+
case 'U128':
|
|
78
|
+
return this.dataToBigInt(data);
|
|
79
|
+
}
|
|
80
|
+
// process vector
|
|
81
|
+
if (type.qname === VECTOR_STR) {
|
|
82
|
+
// vector<u8> as hex string
|
|
83
|
+
if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {
|
|
84
|
+
return data;
|
|
85
|
+
}
|
|
86
|
+
const res = [];
|
|
87
|
+
for (const entry of data) {
|
|
88
|
+
res.push(this.decode(entry, type.typeArgs[0]));
|
|
89
|
+
}
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
92
|
+
// Process complex type
|
|
93
|
+
const struct = this.getMoveStruct(type.qname);
|
|
94
|
+
const typeCtx = new Map();
|
|
95
|
+
for (const [idx, typeArg] of type.typeArgs.entries()) {
|
|
96
|
+
typeCtx.set('T' + idx, typeArg);
|
|
97
|
+
}
|
|
98
|
+
const typedData = {};
|
|
99
|
+
for (const field of struct.fields) {
|
|
100
|
+
let filedType = field.type;
|
|
101
|
+
filedType = filedType.applyTypeArgs(typeCtx);
|
|
102
|
+
const value = this.decode(data[field.name], filedType);
|
|
103
|
+
typedData[field.name] = value;
|
|
104
|
+
}
|
|
105
|
+
return typedData;
|
|
106
|
+
}
|
|
107
|
+
filterAndDecodeInternal(typeQname, structs) {
|
|
108
|
+
if (!structs) {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
const results = [];
|
|
112
|
+
for (const resource of structs) {
|
|
113
|
+
if (resource.type.split('<')[0] !== typeQname) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const result = this.decodedInternal(resource);
|
|
117
|
+
if (result) {
|
|
118
|
+
results.push(result);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return results;
|
|
122
|
+
}
|
|
123
|
+
decodedInternal(typeStruct) {
|
|
124
|
+
const typeDescriptor = parseMoveType(typeStruct.type);
|
|
125
|
+
const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature());
|
|
126
|
+
let dataTyped = undefined;
|
|
127
|
+
try {
|
|
128
|
+
dataTyped = this.decode(typeStruct.data, typeDescriptor);
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
console.error('Decoding error for ', JSON.stringify(typeStruct), e);
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
...typeStruct,
|
|
136
|
+
data_decoded: dataTyped,
|
|
137
|
+
type_arguments: typeArguments,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=abstract-move-coder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract-move-coder.js","sourceRoot":"","sources":["../../src/move/abstract-move-coder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC9D,OAAO,EAAE,aAAa,EAAkB,MAAM,YAAY,CAAA;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAYjD,MAAM,OAAgB,iBAAiB;IAC7B,aAAa,GAAG,IAAI,GAAG,EAA6B,CAAA;IACpD,WAAW,GAAG,IAAI,GAAG,EAA6B,CAAA;IAClD,WAAW,GAAG,IAAI,GAAG,EAA+B,CAAA;IAE5D,QAAQ,CAAC,OAAe,EAAE,IAAY;QACpC,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,CAAA;IACtD,CAAC;IAED,WAAW,CAAC,MAAyB;QACnC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE;YAC9C,OAAM;SACP;QACD,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAA;QAEnD,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE;YACnC,oBAAoB;YACpB,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YACrE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAA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GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAA;QAE1E,IAAI,SAAS,GAAG,SAAS,CAAA;QACzB,IAAI;YACF,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;SACzD;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAA;YACnE,OAAO,SAAS,CAAA;SACjB;QACD,OAAO;YACL,GAAG,UAAU;YACb,YAAY,EAAE,SAAS;YACvB,cAAc,EAAE,aAAa;SACS,CAAA;IAC1C,CAAC;CACF","sourcesContent":["import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'\nimport { parseMoveType, TypeDescriptor } from './types.js'\nimport { NeutralMoveFunction, NeutralMoveModule, NeutralMoveStruct } from './neutral-models.js'\nimport { bytesToBigInt } from '../utils/index.js'\n\ntype StructWithTag<Base> = Base & {\n type: string\n data: any\n}\n\ntype DecodedStructWithTag<B, T> = StructWithTag<B> & {\n data_decoded: T\n type_arguments: string[]\n}\n\nexport abstract class AbstractMoveCoder<StructType> {\n private moduleMapping = new Map<string, NeutralMoveModule>()\n private typeMapping = new Map<string, NeutralMoveStruct>()\n private funcMapping = new Map<string, NeutralMoveFunction>()\n\n contains(account: string, name: string) {\n return this.moduleMapping.has(account + '::' + name)\n }\n\n loadNeutral(module: NeutralMoveModule) {\n if (this.contains(module.address, module.name)) {\n return\n }\n this.moduleMapping.set(moduleQname(module), module)\n\n for (const struct of module.structs) {\n // TODO move to util\n const key = [module.address, module.name, struct.name].join(SPLITTER)\n this.typeMapping.set(key, struct)\n }\n\n for (const func of module.exposed_functions) {\n if (!func.is_entry) {\n continue\n }\n const key = [module.address, module.name, func.name].join(SPLITTER)\n this.funcMapping.set(key, func)\n }\n }\n\n protected dataToBigInt(data: any): bigint {\n if (Array.isArray(data)) {\n // Only sui function need this, strange\n const bytes = data as number[]\n return bytesToBigInt(new Uint8Array(bytes.slice().reverse()))\n } else {\n return BigInt(data)\n }\n }\n\n protected getMoveStruct(type: string): NeutralMoveStruct {\n const struct = this.typeMapping.get(type)\n if (!struct) {\n throw new Error('Failed to load type' + type + ' type are not imported anywhere')\n }\n return struct\n }\n\n protected getMoveFunction(type: string): NeutralMoveFunction {\n const func = this.funcMapping.get(type)\n if (!func) {\n throw new Error('Failed to load function' + type + ' type are not imported anywhere')\n }\n return func\n }\n\n protected decode(data: any, type: TypeDescriptor): any {\n // process simple type\n if (type.reference) {\n return data\n }\n switch (type.qname) {\n case 'signer': // TODO check this, aptos only\n case 'address':\n case 'Address':\n case '0x2::object::ID':\n case '0x2::coin::Coin':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n return data\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n return this.dataToBigInt(data)\n }\n\n // process vector\n if (type.qname === VECTOR_STR) {\n // vector<u8> as hex string\n if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {\n return data\n }\n\n const res = []\n for (const entry of data) {\n res.push(this.decode(entry, type.typeArgs[0]))\n }\n return res\n }\n\n // Process complex type\n const struct = this.getMoveStruct(type.qname)\n\n const typeCtx = new Map<string, TypeDescriptor>()\n for (const [idx, typeArg] of type.typeArgs.entries()) {\n typeCtx.set('T' + idx, typeArg)\n }\n\n const typedData: any = {}\n\n for (const field of struct.fields) {\n let filedType = field.type\n filedType = filedType.applyTypeArgs(typeCtx)\n const value = this.decode(data[field.name], filedType)\n typedData[field.name] = value\n }\n return typedData\n }\n\n protected filterAndDecodeInternal<T>(\n typeQname: string,\n structs: StructWithTag<StructType>[]\n ): DecodedStructWithTag<StructType, T>[] {\n if (!structs) {\n return []\n }\n const results: DecodedStructWithTag<StructType, T>[] = []\n for (const resource of structs) {\n if (resource.type.split('<')[0] !== typeQname) {\n continue\n }\n const result = this.decodedInternal(resource)\n if (result) {\n results.push(result as DecodedStructWithTag<StructType, T>)\n }\n }\n return results\n }\n\n protected decodedInternal<T>(typeStruct: StructWithTag<StructType>): DecodedStructWithTag<StructType, T> | undefined {\n const typeDescriptor = parseMoveType(typeStruct.type)\n const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature())\n\n let dataTyped = undefined\n try {\n dataTyped = this.decode(typeStruct.data, typeDescriptor)\n } catch (e) {\n console.error('Decoding error for ', JSON.stringify(typeStruct), e)\n return undefined\n }\n return {\n ...typeStruct,\n data_decoded: dataTyped,\n type_arguments: typeArguments,\n } as DecodedStructWithTag<StructType, T>\n }\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TypeDescriptor } from './types.js';
|
|
2
|
+
import { NeutralMoveModule } from './neutral-models.js';
|
|
3
|
+
export declare class AccountModulesImportInfo {
|
|
4
|
+
imports: Map<string, Set<string>>;
|
|
5
|
+
account: string;
|
|
6
|
+
moduleName: string;
|
|
7
|
+
constructor(account: string, tsModuleName: string);
|
|
8
|
+
addImport(account: string, module: string): void;
|
|
9
|
+
}
|
|
10
|
+
export declare class AccountRegister {
|
|
11
|
+
accountImports: Map<string, AccountModulesImportInfo>;
|
|
12
|
+
pendingAccounts: Set<string>;
|
|
13
|
+
typeDescriptors: Map<string, TypeDescriptor>;
|
|
14
|
+
protected loadTypeDescriptor(type: string): TypeDescriptor;
|
|
15
|
+
register(module: NeutralMoveModule, tsModuleName: string): AccountModulesImportInfo;
|
|
16
|
+
registerFunctions(module: NeutralMoveModule, accountModuleImports: AccountModulesImportInfo): void;
|
|
17
|
+
registerStruct(module: NeutralMoveModule, accountModuleImports: AccountModulesImportInfo): void;
|
|
18
|
+
}
|