@dedot/codegen 0.18.7 → 1.0.0-next.a0f8d41e.25
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/chaintypes/generator/ConstsGen.js +1 -1
- package/chaintypes/generator/ErrorsGen.js +3 -3
- package/chaintypes/generator/EventsGen.js +3 -3
- package/chaintypes/generator/IndexGen.d.ts +1 -0
- package/chaintypes/generator/IndexGen.js +34 -2
- package/chaintypes/generator/JsonRpcGen.js +1 -1
- package/chaintypes/generator/QueryGen.js +4 -4
- package/chaintypes/generator/RuntimeApisGen.js +5 -5
- package/chaintypes/generator/TxGen.js +6 -10
- package/chaintypes/generator/ViewFunctionsGen.js +3 -3
- package/chaintypes/index.d.ts +22 -3
- package/chaintypes/index.js +63 -13
- package/chaintypes/templates/consts.hbs +1 -1
- package/chaintypes/templates/errors.hbs +1 -1
- package/chaintypes/templates/events.hbs +1 -1
- package/chaintypes/templates/index.hbs +10 -14
- package/chaintypes/templates/json-rpc.hbs +3 -3
- package/chaintypes/templates/query.hbs +1 -1
- package/chaintypes/templates/runtime.hbs +1 -1
- package/chaintypes/templates/tx.hbs +1 -1
- package/chaintypes/templates/view-functions.hbs +1 -1
- package/cjs/chaintypes/generator/ConstsGen.js +1 -1
- package/cjs/chaintypes/generator/ErrorsGen.js +3 -3
- package/cjs/chaintypes/generator/EventsGen.js +3 -3
- package/cjs/chaintypes/generator/IndexGen.js +34 -2
- package/cjs/chaintypes/generator/JsonRpcGen.js +1 -1
- package/cjs/chaintypes/generator/QueryGen.js +4 -4
- package/cjs/chaintypes/generator/RuntimeApisGen.js +5 -5
- package/cjs/chaintypes/generator/TxGen.js +6 -10
- package/cjs/chaintypes/generator/ViewFunctionsGen.js +3 -3
- package/cjs/chaintypes/index.js +61 -11
- package/cjs/chaintypes/templates/consts.hbs +1 -1
- package/cjs/chaintypes/templates/errors.hbs +1 -1
- package/cjs/chaintypes/templates/events.hbs +1 -1
- package/cjs/chaintypes/templates/index.hbs +10 -14
- package/cjs/chaintypes/templates/json-rpc.hbs +3 -3
- package/cjs/chaintypes/templates/query.hbs +1 -1
- package/cjs/chaintypes/templates/runtime.hbs +1 -1
- package/cjs/chaintypes/templates/tx.hbs +1 -1
- package/cjs/chaintypes/templates/view-functions.hbs +1 -1
- package/cjs/index.js +3 -0
- package/cjs/sol/generator/ConstructorQueryGen.js +1 -1
- package/cjs/sol/generator/IndexGen.js +1 -1
- package/cjs/sol/generator/QueryGen.js +1 -1
- package/cjs/sol/templates/index.hbs +7 -7
- package/cjs/typink/generator/ConstructorQueryGen.js +1 -1
- package/cjs/typink/generator/IndexGen.js +1 -1
- package/cjs/typink/generator/QueryGen.js +1 -1
- package/cjs/typink/templates/index.hbs +7 -7
- package/cjs/utils.js +27 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +11 -11
- package/sol/generator/ConstructorQueryGen.js +1 -1
- package/sol/generator/IndexGen.js +1 -1
- package/sol/generator/QueryGen.js +1 -1
- package/sol/templates/index.hbs +7 -7
- package/typink/generator/ConstructorQueryGen.js +1 -1
- package/typink/generator/IndexGen.js +1 -1
- package/typink/generator/QueryGen.js +1 -1
- package/typink/templates/index.hbs +7 -7
- package/utils.d.ts +9 -0
- package/utils.js +25 -0
|
@@ -9,7 +9,7 @@ class QueryGen extends ApiGen_js_1.ApiGen {
|
|
|
9
9
|
generate(useSubPaths = false) {
|
|
10
10
|
const { pallets } = this.metadata;
|
|
11
11
|
this.typesGen.clearCache();
|
|
12
|
-
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback'
|
|
12
|
+
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
|
|
13
13
|
let defTypeOut = '';
|
|
14
14
|
for (let pallet of pallets) {
|
|
15
15
|
const storage = pallet.storage;
|
|
@@ -18,17 +18,17 @@ class QueryGen extends ApiGen_js_1.ApiGen {
|
|
|
18
18
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
19
19
|
const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
|
|
20
20
|
if (keyTypeOut) {
|
|
21
|
-
return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<
|
|
21
|
+
return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}, ${keyTypeOut}>`;
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<
|
|
24
|
+
return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`;
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
28
28
|
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
29
29
|
${queryDefs.join(',\n')}
|
|
30
30
|
|
|
31
|
-
${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery
|
|
31
|
+
${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
|
32
32
|
},`;
|
|
33
33
|
}
|
|
34
34
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
@@ -17,7 +17,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
17
17
|
}
|
|
18
18
|
generate(useSubPaths = false) {
|
|
19
19
|
this.typesGen.clearCache();
|
|
20
|
-
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod'
|
|
20
|
+
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
|
|
21
21
|
let runtimeCallsOut = '';
|
|
22
22
|
if (this.metadata.apis.length > 0) {
|
|
23
23
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
@@ -26,7 +26,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
26
26
|
runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
|
|
27
27
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
28
28
|
|
|
29
|
-
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
29
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
30
30
|
}`;
|
|
31
31
|
});
|
|
32
32
|
}
|
|
@@ -43,7 +43,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
43
43
|
}))
|
|
44
44
|
.join('\n')}
|
|
45
45
|
|
|
46
|
-
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
46
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
47
47
|
}`;
|
|
48
48
|
});
|
|
49
49
|
}
|
|
@@ -72,7 +72,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
72
72
|
.map(({ name, isOptional, plainType }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
73
73
|
.join(', ');
|
|
74
74
|
const typeOut = this.#getGeneratedTypeName(type, false);
|
|
75
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<
|
|
75
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
76
76
|
}
|
|
77
77
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
78
78
|
const { name: methodName, inputs, output, docs, deprecationInfo } = methodDef;
|
|
@@ -97,7 +97,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
97
97
|
const paramsOut = typedInputs
|
|
98
98
|
.map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
99
99
|
.join(', ');
|
|
100
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`), deprecationComments)}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<
|
|
100
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`), deprecationComments)}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
101
101
|
}
|
|
102
102
|
#targetRuntimeApiSpecs() {
|
|
103
103
|
const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
|
|
@@ -8,7 +8,7 @@ class TxGen extends index_js_1.ApiGen {
|
|
|
8
8
|
generate(useSubPaths = false) {
|
|
9
9
|
const { pallets, types } = this.metadata;
|
|
10
10
|
this.typesGen.clearCache();
|
|
11
|
-
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', '
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'GenericChainKnownTypes', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall');
|
|
12
12
|
const { callTypeId, addressTypeId, signatureTypeId } = this.metadata.extrinsic;
|
|
13
13
|
const callTypeIn = this.typesGen.generateType(callTypeId, 1);
|
|
14
14
|
const addressTypeIn = this.typesGen.generateType(addressTypeId, 1);
|
|
@@ -50,23 +50,19 @@ class TxGen extends index_js_1.ApiGen {
|
|
|
50
50
|
if (deprecationComments.length > 0) {
|
|
51
51
|
deprecationComments.unshift('\n');
|
|
52
52
|
}
|
|
53
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`), deprecationComments)}${functionName}: GenericTxCall<
|
|
53
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`), deprecationComments)}${functionName}: GenericTxCall<(${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<${callInput}, ChainKnownTypes>>`;
|
|
54
54
|
})
|
|
55
55
|
.join(',\n')}
|
|
56
56
|
|
|
57
|
-
${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<
|
|
57
|
+
${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<TxCall<ChainKnownTypes>>,
|
|
58
58
|
},`;
|
|
59
59
|
}
|
|
60
60
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
61
|
-
// TODO make explicit separate type for Extra
|
|
62
61
|
const defTypes = `
|
|
63
|
-
export type ChainSubmittableExtrinsic<
|
|
64
|
-
Extrinsic<${addressTypeIn}, T, ${signatureTypeIn},
|
|
65
|
-
(Rv extends RpcV2
|
|
66
|
-
? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord>>
|
|
67
|
-
: ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord>>)
|
|
62
|
+
export type ChainSubmittableExtrinsic<T extends IRuntimeTxCall = ${callTypeIn}, ChainKnownTypes extends GenericChainKnownTypes = GenericChainKnownTypes> =
|
|
63
|
+
Extrinsic<${addressTypeIn}, T, ${signatureTypeIn}, ChainKnownTypes['Extra']> & ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord>, ChainKnownTypes['AssetId']>
|
|
68
64
|
|
|
69
|
-
export type TxCall<
|
|
65
|
+
export type TxCall<ChainKnownTypes extends GenericChainKnownTypes = GenericChainKnownTypes> = (...args: any[]) => ChainSubmittableExtrinsic<${callTypeIn}, ChainKnownTypes>;
|
|
70
66
|
`;
|
|
71
67
|
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/tx.hbs');
|
|
72
68
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypes, txDefsOut }));
|
|
@@ -10,7 +10,7 @@ class ViewFunctionsGen extends ApiGen_js_1.ApiGen {
|
|
|
10
10
|
const { pallets } = this.metadata;
|
|
11
11
|
this.typesGen.clearCache();
|
|
12
12
|
this.typesGen.typeImports.addKnownType('GenericChainViewFunctions', // --
|
|
13
|
-
'GenericViewFunction'
|
|
13
|
+
'GenericViewFunction');
|
|
14
14
|
let defTypeOut = '';
|
|
15
15
|
for (let pallet of pallets) {
|
|
16
16
|
if (!pallet.viewFunctions || pallet.viewFunctions.length === 0)
|
|
@@ -27,13 +27,13 @@ class ViewFunctionsGen extends ApiGen_js_1.ApiGen {
|
|
|
27
27
|
}
|
|
28
28
|
const params = vf.inputs.map((input) => `${(0, utils_1.stringCamelCase)(input.name)}: ${this.typesGen.generateType(input.typeId, 1)}`);
|
|
29
29
|
const outputType = this.typesGen.generateType(vf.output, 1, true);
|
|
30
|
-
return `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringCamelCase)(vf.name)}: GenericViewFunction<
|
|
30
|
+
return `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringCamelCase)(vf.name)}: GenericViewFunction<(${params.join(', ')}) => Promise<${outputType}>>`;
|
|
31
31
|
});
|
|
32
32
|
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s view functions`);
|
|
33
33
|
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
34
34
|
${viewFns.join(',\n')}
|
|
35
35
|
|
|
36
|
-
${(0, utils_js_1.commentBlock)('Generic pallet view function')}[name: string]: GenericViewFunction
|
|
36
|
+
${(0, utils_js_1.commentBlock)('Generic pallet view function')}[name: string]: GenericViewFunction;
|
|
37
37
|
},`;
|
|
38
38
|
}
|
|
39
39
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
package/cjs/chaintypes/index.js
CHANGED
|
@@ -29,19 +29,67 @@ const providers_1 = require("@dedot/providers");
|
|
|
29
29
|
const utils_1 = require("@dedot/utils");
|
|
30
30
|
const fs = __importStar(require("fs"));
|
|
31
31
|
const path = __importStar(require("path"));
|
|
32
|
+
const utils_js_1 = require("../utils.js");
|
|
32
33
|
const index_js_1 = require("./generator/index.js");
|
|
33
|
-
async function generateTypesFromEndpoint(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
async function generateTypesFromEndpoint(options) {
|
|
35
|
+
const { chain, endpoint, client: providedClient, outDir, extension = 'd.ts', useSubPaths = false, at } = options;
|
|
36
|
+
// Validate that either endpoint or client is provided
|
|
37
|
+
if (!endpoint && !providedClient) {
|
|
38
|
+
throw new Error('Either "endpoint" or "client" must be provided');
|
|
39
|
+
}
|
|
40
|
+
if (endpoint && providedClient) {
|
|
41
|
+
throw new Error('Cannot provide both "endpoint" and "client"');
|
|
42
|
+
}
|
|
43
|
+
// Create client if not provided
|
|
44
|
+
const client = providedClient || (await api_1.DedotClient.legacy(new providers_1.WsProvider({ endpoint: endpoint, retryDelayMs: 0, timeout: 0 })));
|
|
45
|
+
const shouldDisconnect = !providedClient;
|
|
46
|
+
try {
|
|
47
|
+
const { methods } = await client.rpc.rpc_methods();
|
|
48
|
+
// Resolve block hash if `at` is provided
|
|
49
|
+
let blockHash;
|
|
50
|
+
if (at) {
|
|
51
|
+
blockHash = await (0, utils_js_1.resolveBlockHash)(client, at);
|
|
52
|
+
}
|
|
53
|
+
// Get runtime version and metadata at the specified block
|
|
54
|
+
let runtimeVersion;
|
|
55
|
+
let metadata;
|
|
56
|
+
if (blockHash) {
|
|
57
|
+
// Get the header of the target block
|
|
58
|
+
const header = await client.block.header(blockHash);
|
|
59
|
+
(0, utils_1.assert)(`Header not found for block hash: ${blockHash}`);
|
|
60
|
+
const childBlockHash = await client.rpc.chain_getBlockHash(header.number + 1);
|
|
61
|
+
const clientAt = await client.at(childBlockHash);
|
|
62
|
+
runtimeVersion = clientAt.runtimeVersion;
|
|
63
|
+
metadata = clientAt.metadata;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
runtimeVersion = client.runtimeVersion;
|
|
67
|
+
metadata = client.metadata;
|
|
68
|
+
}
|
|
69
|
+
const resolvedChain = chain || runtimeVersion.specName || 'local';
|
|
70
|
+
return await generateTypes({
|
|
71
|
+
chain: resolvedChain,
|
|
72
|
+
metadata: metadata.latest,
|
|
73
|
+
rpcMethods: methods,
|
|
74
|
+
runtimeVersion,
|
|
75
|
+
outDir,
|
|
76
|
+
extension,
|
|
77
|
+
useSubPaths,
|
|
78
|
+
appendSpecVersion: !!blockHash,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (shouldDisconnect) {
|
|
83
|
+
await client.disconnect();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
41
86
|
}
|
|
42
87
|
exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
|
|
43
|
-
async function generateTypes(
|
|
44
|
-
const
|
|
88
|
+
async function generateTypes(options) {
|
|
89
|
+
const { chain, metadata, rpcMethods, runtimeVersion, outDir = '.', extension = 'd.ts', useSubPaths = false, appendSpecVersion = false, } = options;
|
|
90
|
+
// Build folder suffix with spec version if requested
|
|
91
|
+
const folderSuffix = appendSpecVersion ? `-${runtimeVersion.specVersion}` : '';
|
|
92
|
+
const dirPath = path.resolve(outDir, (0, utils_1.stringDashCase)(chain) + folderSuffix);
|
|
45
93
|
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
46
94
|
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
47
95
|
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
@@ -55,7 +103,9 @@ async function generateTypes(chain, metadata, rpcMethods, runtimeVersion, outDir
|
|
|
55
103
|
if (!fs.existsSync(dirPath)) {
|
|
56
104
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
57
105
|
}
|
|
58
|
-
|
|
106
|
+
// Build interface suffix with spec version if requested
|
|
107
|
+
const interfaceSuffix = appendSpecVersion ? runtimeVersion.specVersion : '';
|
|
108
|
+
const interfaceName = `${(0, utils_1.stringPascalCase)(chain)}Api${interfaceSuffix}`;
|
|
59
109
|
const typesGen = new index_js_1.TypesGen(metadata);
|
|
60
110
|
const constsGen = new index_js_1.ConstsGen(typesGen);
|
|
61
111
|
const queryGen = new index_js_1.QueryGen(typesGen);
|
|
@@ -17,22 +17,18 @@ interface ChainKnownTypes extends GenericChainKnownTypes {
|
|
|
17
17
|
Signature: {{{signatureType}}};
|
|
18
18
|
RuntimeCall: {{{runtimeCallType}}};
|
|
19
19
|
Extra: {{{extraType}}};
|
|
20
|
+
AssetId: {{{assetIdType}}};
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export interface
|
|
23
|
-
rpc: ChainJsonRpcApis
|
|
24
|
-
consts: ChainConsts
|
|
25
|
-
query: ChainStorage
|
|
26
|
-
errors: ChainErrors
|
|
27
|
-
events: ChainEvents
|
|
28
|
-
call: RuntimeApis
|
|
29
|
-
view: ChainViewFunctions
|
|
30
|
-
tx: ChainTx<
|
|
23
|
+
{{interfaceDocs}}export interface {{{interfaceName}}} extends GenericSubstrateApi {
|
|
24
|
+
rpc: ChainJsonRpcApis;
|
|
25
|
+
consts: ChainConsts;
|
|
26
|
+
query: ChainStorage;
|
|
27
|
+
errors: ChainErrors;
|
|
28
|
+
events: ChainEvents;
|
|
29
|
+
call: RuntimeApis;
|
|
30
|
+
view: ChainViewFunctions;
|
|
31
|
+
tx: ChainTx<ChainKnownTypes>;
|
|
31
32
|
|
|
32
33
|
types: ChainKnownTypes;
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
-
{{interfaceDocs}}export interface {{{interfaceName}}} {
|
|
36
|
-
legacy: Versioned{{{interfaceName}}}<RpcLegacy>;
|
|
37
|
-
v2: Versioned{{{interfaceName}}}<RpcV2>;
|
|
38
|
-
}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export type ChainJsonRpcApis
|
|
5
|
+
export type ChainJsonRpcApis =
|
|
6
6
|
{{#if jsonRpcMethods.length}}
|
|
7
|
-
Pick<JsonRpcApis, {{{jsonRpcMethods}}}> & GenericJsonRpcApis
|
|
7
|
+
Pick<JsonRpcApis, {{{jsonRpcMethods}}}> & GenericJsonRpcApis;
|
|
8
8
|
{{else}}
|
|
9
|
-
JsonRpcApis & GenericJsonRpcApis
|
|
9
|
+
JsonRpcApis & GenericJsonRpcApis;
|
|
10
10
|
{{/if}}
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
{{{ defTypes }}}
|
|
6
6
|
|
|
7
|
-
export interface ChainTx<
|
|
7
|
+
export interface ChainTx<ChainKnownTypes extends GenericChainKnownTypes = GenericChainKnownTypes> extends GenericChainTx<TxCall<ChainKnownTypes>> {
|
|
8
8
|
{{{ txDefsOut }}}
|
|
9
9
|
}
|
package/cjs/index.js
CHANGED
|
@@ -14,7 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.resolveBlockHash = void 0;
|
|
17
18
|
__exportStar(require("./types.js"), exports);
|
|
18
19
|
__exportStar(require("./chaintypes/index.js"), exports);
|
|
19
20
|
__exportStar(require("./typink/index.js"), exports);
|
|
20
21
|
__exportStar(require("./sol/index.js"), exports);
|
|
22
|
+
var utils_js_1 = require("./utils.js");
|
|
23
|
+
Object.defineProperty(exports, "resolveBlockHash", { enumerable: true, get: function () { return utils_js_1.resolveBlockHash; } });
|
|
@@ -32,7 +32,7 @@ class ConstructorQueryGen {
|
|
|
32
32
|
}
|
|
33
33
|
generateMethodDef(abiItem, optionsParamName = 'options') {
|
|
34
34
|
const paramsOut = this.generateParamsOut(abiItem);
|
|
35
|
-
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<[], ContractInstantiateResult
|
|
35
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<[], ContractInstantiateResult>>, Type>`;
|
|
36
36
|
}
|
|
37
37
|
generateParamsOut(abiItem) {
|
|
38
38
|
return abiItem.inputs
|
|
@@ -13,7 +13,7 @@ class IndexGen {
|
|
|
13
13
|
generate(useSubPaths = false) {
|
|
14
14
|
const interfaceName = this.interfaceName;
|
|
15
15
|
const typeImports = new index_js_1.TypeImports();
|
|
16
|
-
typeImports.addKnownType('
|
|
16
|
+
typeImports.addKnownType('GenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
17
17
|
typeImports.addContractType('SolGenericContractApi', 'SolRegistry');
|
|
18
18
|
typeImports.addChainType('SubstrateApi');
|
|
19
19
|
const importTypes = typeImports.toImports({ useSubPaths });
|
|
@@ -39,7 +39,7 @@ class QueryGen {
|
|
|
39
39
|
const typeOutInner = `${outputs.map((o) => this.typesGen.generateType(o, abiItem, 1, true)).join(',')}`;
|
|
40
40
|
// If there is only one output, we don't need to wrap it in a tuple, for user-friendly
|
|
41
41
|
const typeOut = outputs.length === 1 ? typeOutInner : `[${typeOutInner}]`;
|
|
42
|
-
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult
|
|
42
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult>>, Type>`;
|
|
43
43
|
}
|
|
44
44
|
generateParamsOut(abiItem) {
|
|
45
45
|
return abiItem.inputs
|
|
@@ -9,15 +9,15 @@ import { ContractEvents } from './events.js';
|
|
|
9
9
|
|
|
10
10
|
export * from './types.js';
|
|
11
11
|
|
|
12
|
-
{{{interfaceDocs}}}export interface {{{interfaceName}}}<
|
|
12
|
+
{{{interfaceDocs}}}export interface {{{interfaceName}}}< ChainApi extends GenericSubstrateApi = SubstrateApi> extends SolGenericContractApi<ChainApi> {
|
|
13
13
|
metadataType: 'sol';
|
|
14
|
-
query: ContractQuery<ChainApi
|
|
15
|
-
tx: ContractTx<ChainApi
|
|
16
|
-
constructorQuery: ConstructorQuery<ChainApi
|
|
17
|
-
events: ContractEvents<ChainApi
|
|
18
|
-
constructorTx: ConstructorTx<ChainApi
|
|
14
|
+
query: ContractQuery<ChainApi, 'sol'>;
|
|
15
|
+
tx: ContractTx<ChainApi, 'sol'>;
|
|
16
|
+
constructorQuery: ConstructorQuery<ChainApi, 'sol'>;
|
|
17
|
+
events: ContractEvents<ChainApi, 'sol'>;
|
|
18
|
+
constructorTx: ConstructorTx<ChainApi, {{{interfaceName}}}, 'sol'>;
|
|
19
19
|
|
|
20
20
|
types: {
|
|
21
|
-
ChainApi: ChainApi
|
|
21
|
+
ChainApi: ChainApi;
|
|
22
22
|
};
|
|
23
23
|
}
|
|
@@ -20,7 +20,7 @@ class ConstructorQueryGen extends QueryGen_js_1.QueryGen {
|
|
|
20
20
|
const typeOutRaw = this.typesGen.generateType(returnType.type, 0, true);
|
|
21
21
|
// Unwrap langError result
|
|
22
22
|
const typeOut = typeOutRaw.match(/^(\w+)<(.*), (.*)>$/).at(2);
|
|
23
|
-
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<${typeOut}, ContractInstantiateResult
|
|
23
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<${typeOut}, ContractInstantiateResult>>, Type>`;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.ConstructorQueryGen = ConstructorQueryGen;
|
|
@@ -18,7 +18,7 @@ class IndexGen {
|
|
|
18
18
|
const langErrorId = this.contractMetadata.spec.lang_error.type;
|
|
19
19
|
const langErrorName = this.typesGen.cleanPath(this.contractMetadata.types[langErrorId].type.path);
|
|
20
20
|
const typeImports = new index_js_1.TypeImports();
|
|
21
|
-
typeImports.addKnownType('
|
|
21
|
+
typeImports.addKnownType('GenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
22
22
|
typeImports.addContractType('InkGenericContractApi', 'WithLazyStorage');
|
|
23
23
|
typeImports.addChainType('SubstrateApi');
|
|
24
24
|
typeImports.addPortableType(langErrorName);
|
|
@@ -40,7 +40,7 @@ class QueryGen {
|
|
|
40
40
|
const typeOutRaw = this.typesGen.generateType(returnType.type, 0, true);
|
|
41
41
|
// Unwrap langError result
|
|
42
42
|
const typeOut = typeOutRaw.match(/^(\w+)<(.*), (.*)>$/).at(2);
|
|
43
|
-
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult
|
|
43
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult>>, Type>`;
|
|
44
44
|
}
|
|
45
45
|
generateParamsOut(args) {
|
|
46
46
|
return args
|
|
@@ -9,20 +9,20 @@ import { ContractEvents } from './events.js';
|
|
|
9
9
|
|
|
10
10
|
export * from './types.js';
|
|
11
11
|
|
|
12
|
-
{{{interfaceDocs}}}export interface {{{interfaceName}}}<
|
|
12
|
+
{{{interfaceDocs}}}export interface {{{interfaceName}}}< ChainApi extends GenericSubstrateApi = SubstrateApi> extends InkGenericContractApi<ChainApi> {
|
|
13
13
|
metadataType: 'ink';
|
|
14
|
-
query: ContractQuery<ChainApi
|
|
15
|
-
tx: ContractTx<ChainApi
|
|
16
|
-
constructorQuery: ConstructorQuery<ChainApi
|
|
17
|
-
constructorTx: ConstructorTx<ChainApi
|
|
18
|
-
events: ContractEvents<ChainApi
|
|
14
|
+
query: ContractQuery<ChainApi, 'ink'>;
|
|
15
|
+
tx: ContractTx<ChainApi, 'ink'>;
|
|
16
|
+
constructorQuery: ConstructorQuery<ChainApi, 'ink'>;
|
|
17
|
+
constructorTx: ConstructorTx<ChainApi, {{{interfaceName}}}, 'ink'>;
|
|
18
|
+
events: ContractEvents<ChainApi, 'ink'>;
|
|
19
19
|
storage: {
|
|
20
20
|
root(): Promise<{{{rootStorageName}}}>;
|
|
21
21
|
lazy(): {{{lazyStorageName}}};
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
types: {
|
|
25
|
-
ChainApi: ChainApi
|
|
25
|
+
ChainApi: ChainApi;
|
|
26
26
|
RootStorage: {{{rootStorageName}}};
|
|
27
27
|
LazyStorage: {{{lazyStorageName}}};
|
|
28
28
|
LangError: {{{langErrorName}}};
|
package/cjs/utils.js
CHANGED
|
@@ -26,7 +26,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.getVariantDeprecationComment = exports.getDeprecationComment = exports.isNativeType = exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
|
|
29
|
+
exports.resolveBlockHash = exports.getVariantDeprecationComment = exports.getDeprecationComment = exports.isNativeType = exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
|
|
30
|
+
const utils_1 = require("@dedot/utils");
|
|
30
31
|
const fs = __importStar(require("fs"));
|
|
31
32
|
const handlebars_1 = __importDefault(require("handlebars"));
|
|
32
33
|
const path = __importStar(require("path"));
|
|
@@ -51,6 +52,7 @@ exports.commentBlock = commentBlock;
|
|
|
51
52
|
const beautifySourceCode = async (source) => {
|
|
52
53
|
const prettierOptions = await prettier.resolveConfig(path.resolve((0, dirname_js_1.currentDirname)(), '../../../.prettierrc.cjs'));
|
|
53
54
|
return prettier.format(source, { parser: 'babel-ts', ...prettierOptions });
|
|
55
|
+
// return source;
|
|
54
56
|
};
|
|
55
57
|
exports.beautifySourceCode = beautifySourceCode;
|
|
56
58
|
const compileTemplate = (templateFile) => {
|
|
@@ -141,3 +143,27 @@ const getVariantDeprecationComment = (deprecationInfo, variantIndex) => {
|
|
|
141
143
|
}
|
|
142
144
|
};
|
|
143
145
|
exports.getVariantDeprecationComment = getVariantDeprecationComment;
|
|
146
|
+
/**
|
|
147
|
+
* Resolve block hash from either a block hash or block number
|
|
148
|
+
* @param client - The API client
|
|
149
|
+
* @param at - Block hash (0x...) or block number (number)
|
|
150
|
+
* @returns The resolved block hash
|
|
151
|
+
*/
|
|
152
|
+
async function resolveBlockHash(client, at) {
|
|
153
|
+
// Check if it's a hex string (block hash)
|
|
154
|
+
if ((0, utils_1.isHex)(at)) {
|
|
155
|
+
return at;
|
|
156
|
+
}
|
|
157
|
+
// Try to parse as a number (block height)
|
|
158
|
+
const blockNumber = parseInt(at, 10);
|
|
159
|
+
if (isNaN(blockNumber) || blockNumber < 0) {
|
|
160
|
+
throw new utils_1.DedotError(`Invalid block specifier: ${at}. Must be a block hash (0x...) or block number.`);
|
|
161
|
+
}
|
|
162
|
+
// Resolve block number to block hash
|
|
163
|
+
const blockHash = await client.rpc.chain_getBlockHash(blockNumber);
|
|
164
|
+
if (!blockHash) {
|
|
165
|
+
throw new utils_1.DedotError(`Block not found at height: ${blockNumber}`);
|
|
166
|
+
}
|
|
167
|
+
return blockHash;
|
|
168
|
+
}
|
|
169
|
+
exports.resolveBlockHash = resolveBlockHash;
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dedot/codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
4
4
|
"description": "Generate types",
|
|
5
5
|
"author": "Thang X. Vu <thang@dedot.dev>",
|
|
6
6
|
"homepage": "https://dedot.dev",
|
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
"copy": "cp -R ./src/chaintypes/templates ./dist/chaintypes && cp -R ./src/chaintypes/templates ./dist/cjs/chaintypes && cp -R ./src/typink/templates ./dist/typink && cp -R ./src/typink/templates ./dist/cjs/typink && cp -R ./src/sol/templates ./dist/sol && cp -R ./src/sol/templates ./dist/cjs/sol"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@dedot/api": "0.
|
|
23
|
-
"@dedot/codecs": "0.
|
|
24
|
-
"@dedot/contracts": "0.
|
|
25
|
-
"@dedot/providers": "0.
|
|
26
|
-
"@dedot/runtime-specs": "0.
|
|
27
|
-
"@dedot/shape": "0.
|
|
28
|
-
"@dedot/types": "0.
|
|
29
|
-
"@dedot/utils": "0.
|
|
22
|
+
"@dedot/api": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
23
|
+
"@dedot/codecs": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
24
|
+
"@dedot/contracts": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
25
|
+
"@dedot/providers": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
26
|
+
"@dedot/runtime-specs": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
27
|
+
"@dedot/shape": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
28
|
+
"@dedot/types": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
29
|
+
"@dedot/utils": "1.0.0-next.a0f8d41e.25+a0f8d41e",
|
|
30
30
|
"handlebars": "^4.7.8",
|
|
31
|
-
"prettier": "^3.
|
|
31
|
+
"prettier": "^3.7.3"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"node": ">=18"
|
|
39
39
|
},
|
|
40
40
|
"license": "Apache-2.0",
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "a0f8d41e632a44c03ccc31ee77bcb7d19df936e9",
|
|
42
42
|
"module": "./index.js",
|
|
43
43
|
"types": "./index.d.ts",
|
|
44
44
|
"exports": {
|
|
@@ -29,7 +29,7 @@ export class ConstructorQueryGen {
|
|
|
29
29
|
}
|
|
30
30
|
generateMethodDef(abiItem, optionsParamName = 'options') {
|
|
31
31
|
const paramsOut = this.generateParamsOut(abiItem);
|
|
32
|
-
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<[], ContractInstantiateResult
|
|
32
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<[], ContractInstantiateResult>>, Type>`;
|
|
33
33
|
}
|
|
34
34
|
generateParamsOut(abiItem) {
|
|
35
35
|
return abiItem.inputs
|
|
@@ -10,7 +10,7 @@ export class IndexGen {
|
|
|
10
10
|
generate(useSubPaths = false) {
|
|
11
11
|
const interfaceName = this.interfaceName;
|
|
12
12
|
const typeImports = new TypeImports();
|
|
13
|
-
typeImports.addKnownType('
|
|
13
|
+
typeImports.addKnownType('GenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
14
14
|
typeImports.addContractType('SolGenericContractApi', 'SolRegistry');
|
|
15
15
|
typeImports.addChainType('SubstrateApi');
|
|
16
16
|
const importTypes = typeImports.toImports({ useSubPaths });
|
|
@@ -36,7 +36,7 @@ export class QueryGen {
|
|
|
36
36
|
const typeOutInner = `${outputs.map((o) => this.typesGen.generateType(o, abiItem, 1, true)).join(',')}`;
|
|
37
37
|
// If there is only one output, we don't need to wrap it in a tuple, for user-friendly
|
|
38
38
|
const typeOut = outputs.length === 1 ? typeOutInner : `[${typeOutInner}]`;
|
|
39
|
-
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult
|
|
39
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult>>, Type>`;
|
|
40
40
|
}
|
|
41
41
|
generateParamsOut(abiItem) {
|
|
42
42
|
return abiItem.inputs
|