@dedot/codegen 0.17.0 → 0.17.1-next.db98ff20.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/cjs/index.js +1 -0
- package/cjs/sol/generator/ConstructorQueryGen.js +55 -0
- package/cjs/sol/generator/ConstructorTxGen.js +21 -0
- package/cjs/sol/generator/EventsGen.js +38 -0
- package/cjs/sol/generator/IndexGen.js +27 -0
- package/cjs/sol/generator/QueryGen.js +50 -0
- package/cjs/sol/generator/TxGen.js +21 -0
- package/cjs/sol/generator/TypesGen.js +216 -0
- package/cjs/sol/generator/index.js +23 -0
- package/cjs/sol/index.js +42 -0
- package/cjs/sol/templates/constructor-query.hbs +7 -0
- package/cjs/sol/templates/constructor-tx.hbs +11 -0
- package/cjs/sol/templates/events.hbs +7 -0
- package/cjs/sol/templates/index.hbs +23 -0
- package/cjs/sol/templates/query.hbs +7 -0
- package/cjs/sol/templates/tx.hbs +7 -0
- package/cjs/sol/templates/types.hbs +5 -0
- package/cjs/typink/generator/ConstructorQueryGen.js +2 -2
- package/cjs/typink/generator/ConstructorTxGen.js +2 -2
- package/cjs/typink/generator/EventsGen.js +2 -2
- package/cjs/typink/generator/IndexGen.js +1 -1
- package/cjs/typink/generator/QueryGen.js +2 -2
- package/cjs/typink/generator/TxGen.js +2 -2
- package/cjs/typink/templates/constructor-query.hbs +1 -1
- package/cjs/typink/templates/constructor-tx.hbs +3 -2
- package/cjs/typink/templates/events.hbs +1 -1
- package/cjs/typink/templates/index.hbs +8 -7
- package/cjs/typink/templates/query.hbs +1 -1
- package/cjs/typink/templates/tx.hbs +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +12 -11
- package/sol/generator/ConstructorQueryGen.d.ts +12 -0
- package/sol/generator/ConstructorQueryGen.js +51 -0
- package/sol/generator/ConstructorTxGen.d.ts +6 -0
- package/sol/generator/ConstructorTxGen.js +17 -0
- package/sol/generator/EventsGen.d.ts +10 -0
- package/sol/generator/EventsGen.js +34 -0
- package/sol/generator/IndexGen.d.ts +7 -0
- package/sol/generator/IndexGen.js +23 -0
- package/sol/generator/QueryGen.d.ts +11 -0
- package/sol/generator/QueryGen.js +46 -0
- package/sol/generator/TxGen.d.ts +6 -0
- package/sol/generator/TxGen.js +17 -0
- package/sol/generator/TypesGen.d.ts +15 -0
- package/sol/generator/TypesGen.js +212 -0
- package/sol/generator/index.d.ts +7 -0
- package/sol/generator/index.js +7 -0
- package/sol/index.d.ts +3 -0
- package/sol/index.js +35 -0
- package/sol/templates/constructor-query.hbs +7 -0
- package/sol/templates/constructor-tx.hbs +11 -0
- package/sol/templates/events.hbs +7 -0
- package/sol/templates/index.hbs +23 -0
- package/sol/templates/query.hbs +7 -0
- package/sol/templates/tx.hbs +7 -0
- package/sol/templates/types.hbs +5 -0
- package/typink/generator/ConstructorQueryGen.js +2 -2
- package/typink/generator/ConstructorTxGen.js +2 -2
- package/typink/generator/EventsGen.js +2 -2
- package/typink/generator/IndexGen.js +1 -1
- package/typink/generator/QueryGen.js +2 -2
- package/typink/generator/TxGen.js +2 -2
- package/typink/templates/constructor-query.hbs +1 -1
- package/typink/templates/constructor-tx.hbs +3 -2
- package/typink/templates/events.hbs +1 -1
- package/typink/templates/index.hbs +8 -7
- package/typink/templates/query.hbs +1 -1
- package/typink/templates/tx.hbs +1 -1
|
@@ -19,7 +19,7 @@ class IndexGen {
|
|
|
19
19
|
const langErrorName = this.typesGen.cleanPath(this.contractMetadata.types[langErrorId].type.path);
|
|
20
20
|
const typeImports = new index_js_1.TypeImports();
|
|
21
21
|
typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
22
|
-
typeImports.addContractType('
|
|
22
|
+
typeImports.addContractType('InkGenericContractApi', 'WithLazyStorage');
|
|
23
23
|
typeImports.addChainType('SubstrateApi');
|
|
24
24
|
typeImports.addPortableType(langErrorName);
|
|
25
25
|
const [rootStorageName, lazyStorageName] = this.#extractRootStorageNames(typeImports);
|
|
@@ -14,7 +14,7 @@ class QueryGen {
|
|
|
14
14
|
generate(useSubPaths = false) {
|
|
15
15
|
this.typesGen.clearCache();
|
|
16
16
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
17
|
-
this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult');
|
|
17
|
+
this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult', 'MetadataType');
|
|
18
18
|
const { messages } = this.contractMetadata.spec;
|
|
19
19
|
const queryCallsOut = this.doGenerate(messages, 'ContractCallOptions');
|
|
20
20
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
@@ -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<ChainApi
|
|
43
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>, Type>`;
|
|
44
44
|
}
|
|
45
45
|
generateParamsOut(args) {
|
|
46
46
|
return args
|
|
@@ -7,7 +7,7 @@ class TxGen extends QueryGen_js_1.QueryGen {
|
|
|
7
7
|
generate(useSubPaths = false) {
|
|
8
8
|
this.typesGen.clearCache();
|
|
9
9
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
10
|
-
this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic');
|
|
10
|
+
this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic', 'MetadataType');
|
|
11
11
|
const { messages } = this.contractMetadata.spec;
|
|
12
12
|
const txMessages = messages.filter((one) => one.mutates);
|
|
13
13
|
const txCallsOut = this.doGenerate(txMessages, 'ContractTxOptions');
|
|
@@ -17,7 +17,7 @@ class TxGen extends QueryGen_js_1.QueryGen {
|
|
|
17
17
|
}
|
|
18
18
|
generateMethodDef(def, optionsParamName = 'options') {
|
|
19
19
|
const paramsOut = this.generateParamsOut(def.args);
|
|
20
|
-
return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi
|
|
20
|
+
return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi>, Type>`;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
exports.TxGen = TxGen;
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export interface ConstructorQuery<ChainApi extends GenericSubstrateApi> extends GenericConstructorQuery<ChainApi> {
|
|
5
|
+
export interface ConstructorQuery<ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericConstructorQuery<ChainApi, Type> {
|
|
6
6
|
{{{ constructorsOut }}}
|
|
7
7
|
}
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
export interface ConstructorTx<
|
|
6
6
|
ChainApi extends GenericSubstrateApi,
|
|
7
|
-
ContractApi extends
|
|
8
|
-
|
|
7
|
+
ContractApi extends InkGenericContractApi,
|
|
8
|
+
Type extends MetadataType
|
|
9
|
+
> extends GenericConstructorTx<ChainApi, Type> {
|
|
9
10
|
{{{ constructorsOut }}}
|
|
10
11
|
}
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export interface ContractEvents<ChainApi extends GenericSubstrateApi> extends GenericContractEvents<ChainApi> {
|
|
5
|
+
export interface ContractEvents <ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericContractEvents <ChainApi, Type> {
|
|
6
6
|
{{{ eventsOut }}}
|
|
7
7
|
}
|
|
@@ -9,21 +9,22 @@ import { ContractEvents } from './events.js';
|
|
|
9
9
|
|
|
10
10
|
export * from './types.js';
|
|
11
11
|
|
|
12
|
-
{{{interfaceDocs}}}export interface {{{interfaceName}}}<Rv extends RpcVersion = RpcVersion, ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> extends
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
{{{interfaceDocs}}}export interface {{{interfaceName}}}<Rv extends RpcVersion = RpcVersion, ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> extends InkGenericContractApi<Rv, ChainApi> {
|
|
13
|
+
metadataType: 'ink';
|
|
14
|
+
query: ContractQuery<ChainApi[Rv], 'ink'>;
|
|
15
|
+
tx: ContractTx<ChainApi[Rv], 'ink'>;
|
|
16
|
+
constructorQuery: ConstructorQuery<ChainApi[Rv], 'ink'>;
|
|
17
|
+
constructorTx: ConstructorTx<ChainApi[Rv], {{{interfaceName}}}, 'ink'>;
|
|
18
|
+
events: ContractEvents<ChainApi[Rv], 'ink'>;
|
|
18
19
|
storage: {
|
|
19
20
|
root(): Promise<{{{rootStorageName}}}>;
|
|
20
21
|
lazy(): {{{lazyStorageName}}};
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
types: {
|
|
25
|
+
ChainApi: ChainApi[Rv];
|
|
24
26
|
RootStorage: {{{rootStorageName}}};
|
|
25
27
|
LazyStorage: {{{lazyStorageName}}};
|
|
26
28
|
LangError: {{{langErrorName}}};
|
|
27
|
-
ChainApi: ChainApi[Rv];
|
|
28
29
|
};
|
|
29
30
|
}
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export interface ContractQuery<ChainApi extends GenericSubstrateApi> extends GenericContractQuery<ChainApi> {
|
|
5
|
+
export interface ContractQuery<ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericContractQuery<ChainApi, Type> {
|
|
6
6
|
{{{ queryCallsOut }}}
|
|
7
7
|
}
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export interface ContractTx<ChainApi extends GenericSubstrateApi> extends GenericContractTx<ChainApi> {
|
|
5
|
+
export interface ContractTx<ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericContractTx<ChainApi, Type> {
|
|
6
6
|
{{{ txCallsOut }}}
|
|
7
7
|
}
|
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.17.
|
|
3
|
+
"version": "0.17.1-next.db98ff20.1+db98ff20",
|
|
4
4
|
"description": "Generate types",
|
|
5
5
|
"author": "Thang X. Vu <thang@dedot.dev>",
|
|
6
6
|
"homepage": "https://dedot.dev",
|
|
@@ -14,18 +14,19 @@
|
|
|
14
14
|
"type": "module",
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && yarn copy",
|
|
17
|
+
"test": "npx vitest --watch=false",
|
|
17
18
|
"clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo",
|
|
18
|
-
"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"
|
|
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"
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
|
-
"@dedot/api": "0.17.
|
|
22
|
-
"@dedot/codecs": "0.17.
|
|
23
|
-
"@dedot/contracts": "0.17.
|
|
24
|
-
"@dedot/providers": "0.17.
|
|
25
|
-
"@dedot/runtime-specs": "0.17.
|
|
26
|
-
"@dedot/shape": "0.17.
|
|
27
|
-
"@dedot/types": "0.17.
|
|
28
|
-
"@dedot/utils": "0.17.
|
|
22
|
+
"@dedot/api": "0.17.1-next.db98ff20.1+db98ff20",
|
|
23
|
+
"@dedot/codecs": "0.17.1-next.db98ff20.1+db98ff20",
|
|
24
|
+
"@dedot/contracts": "0.17.1-next.db98ff20.1+db98ff20",
|
|
25
|
+
"@dedot/providers": "0.17.1-next.db98ff20.1+db98ff20",
|
|
26
|
+
"@dedot/runtime-specs": "0.17.1-next.db98ff20.1+db98ff20",
|
|
27
|
+
"@dedot/shape": "0.17.1-next.db98ff20.1+db98ff20",
|
|
28
|
+
"@dedot/types": "0.17.1-next.db98ff20.1+db98ff20",
|
|
29
|
+
"@dedot/utils": "0.17.1-next.db98ff20.1+db98ff20",
|
|
29
30
|
"handlebars": "^4.7.8",
|
|
30
31
|
"prettier": "^3.6.2"
|
|
31
32
|
},
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"node": ">=18"
|
|
38
39
|
},
|
|
39
40
|
"license": "Apache-2.0",
|
|
40
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "db98ff20684d2a74b5e5402cabf43ef601d05d31",
|
|
41
42
|
"module": "./index.js",
|
|
42
43
|
"types": "./index.d.ts",
|
|
43
44
|
"exports": {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TypesGen } from '@dedot/codegen/sol/generator/TypesGen';
|
|
2
|
+
import { SolAbi, SolAbiConstructor } from '@dedot/contracts';
|
|
3
|
+
export declare class ConstructorQueryGen {
|
|
4
|
+
readonly abi: SolAbi;
|
|
5
|
+
readonly typesGen: TypesGen;
|
|
6
|
+
constructor(abi: SolAbi, typesGen: TypesGen);
|
|
7
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
8
|
+
doGenerateConstructorFragment(abiItem: SolAbiConstructor, optionsTypeName?: string): string;
|
|
9
|
+
generateMethodDef(abiItem: SolAbiConstructor, optionsParamName?: string): string;
|
|
10
|
+
generateParamsOut(abiItem: SolAbiConstructor): string;
|
|
11
|
+
protected findConstructor(): SolAbiConstructor;
|
|
12
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
3
|
+
export class ConstructorQueryGen {
|
|
4
|
+
abi;
|
|
5
|
+
typesGen;
|
|
6
|
+
constructor(abi, typesGen) {
|
|
7
|
+
this.abi = abi;
|
|
8
|
+
this.typesGen = typesGen;
|
|
9
|
+
}
|
|
10
|
+
generate(useSubPaths = false) {
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
12
|
+
this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'GenericConstructorCallResult', 'ConstructorCallOptions', 'ContractInstantiateResult', 'MetadataType');
|
|
13
|
+
let constructor = this.findConstructor();
|
|
14
|
+
const constructorsOut = this.doGenerateConstructorFragment(constructor, 'ConstructorCallOptions');
|
|
15
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
16
|
+
const template = compileTemplate('sol/templates/constructor-query.hbs');
|
|
17
|
+
return beautifySourceCode(template({ importTypes, constructorsOut }));
|
|
18
|
+
}
|
|
19
|
+
doGenerateConstructorFragment(abiItem, optionsTypeName) {
|
|
20
|
+
let callsOut = '';
|
|
21
|
+
const { inputs = [] } = abiItem;
|
|
22
|
+
// In case there is an arg has label `options`,
|
|
23
|
+
// we use the name `_options` for the last options param
|
|
24
|
+
// This is just and edge case, so this approach works for now
|
|
25
|
+
const optionsParamName = inputs.some(({ name }) => name === 'options') ? '_options' : 'options';
|
|
26
|
+
callsOut += `${commentBlock(inputs.map((input, idx) => `@param {${this.typesGen.generateType(input, abiItem, 1)}} ${stringCamelCase(input.name || `arg${idx}`)}`), optionsTypeName ? `@param {${optionsTypeName}} ${optionsParamName}` : '')}`;
|
|
27
|
+
callsOut += `new: ${this.generateMethodDef(abiItem, optionsParamName)};\n\n`;
|
|
28
|
+
return callsOut;
|
|
29
|
+
}
|
|
30
|
+
generateMethodDef(abiItem, optionsParamName = 'options') {
|
|
31
|
+
const paramsOut = this.generateParamsOut(abiItem);
|
|
32
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<[], ContractInstantiateResult<ChainApi>>>, Type>`;
|
|
33
|
+
}
|
|
34
|
+
generateParamsOut(abiItem) {
|
|
35
|
+
return abiItem.inputs
|
|
36
|
+
.map((input, idx) => `${stringCamelCase(input.name || `arg${idx}`)}: ${this.typesGen.generateType(input, abiItem, 1)}`)
|
|
37
|
+
.join(', ');
|
|
38
|
+
}
|
|
39
|
+
findConstructor() {
|
|
40
|
+
let constructor = this.abi.find((item) => item.type === 'constructor');
|
|
41
|
+
// Fallback to default constructor
|
|
42
|
+
if (!constructor) {
|
|
43
|
+
constructor = {
|
|
44
|
+
inputs: [],
|
|
45
|
+
stateMutability: 'nonpayable',
|
|
46
|
+
type: 'constructor',
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return constructor;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SolAbiConstructor } from '@dedot/contracts';
|
|
2
|
+
import { ConstructorQueryGen } from './ConstructorQueryGen.js';
|
|
3
|
+
export declare class ConstructorTxGen extends ConstructorQueryGen {
|
|
4
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
5
|
+
generateMethodDef(abiItem: SolAbiConstructor, optionsParamName?: string): string;
|
|
6
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
2
|
+
import { ConstructorQueryGen } from './ConstructorQueryGen.js';
|
|
3
|
+
export class ConstructorTxGen extends ConstructorQueryGen {
|
|
4
|
+
generate(useSubPaths = false) {
|
|
5
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
6
|
+
this.typesGen.typeImports.addContractType('GenericConstructorTx', 'SolGenericContractApi', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'GenericInstantiateSubmittableExtrinsic', 'MetadataType');
|
|
7
|
+
let constructor = this.findConstructor();
|
|
8
|
+
const constructorsOut = this.doGenerateConstructorFragment(constructor, 'ConstructorTxOptions');
|
|
9
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
10
|
+
const template = compileTemplate('sol/templates/constructor-tx.hbs');
|
|
11
|
+
return beautifySourceCode(template({ importTypes, constructorsOut }));
|
|
12
|
+
}
|
|
13
|
+
generateMethodDef(abiItem, optionsParamName = 'options') {
|
|
14
|
+
const paramsOut = this.generateParamsOut(abiItem);
|
|
15
|
+
return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi, ContractApi>, Type>`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SolAbi, SolAbiEvent } from '@dedot/contracts';
|
|
2
|
+
import { TypesGen } from './TypesGen.js';
|
|
3
|
+
export declare class EventsGen {
|
|
4
|
+
#private;
|
|
5
|
+
readonly abi: SolAbi;
|
|
6
|
+
readonly typesGen: TypesGen;
|
|
7
|
+
constructor(abi: SolAbi, typesGen: TypesGen);
|
|
8
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
9
|
+
generateParamsOut(abiItem: SolAbiEvent): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
3
|
+
export class EventsGen {
|
|
4
|
+
abi;
|
|
5
|
+
typesGen;
|
|
6
|
+
constructor(abi, typesGen) {
|
|
7
|
+
this.abi = abi;
|
|
8
|
+
this.typesGen = typesGen;
|
|
9
|
+
}
|
|
10
|
+
generate(useSubPaths = false) {
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
12
|
+
this.typesGen.typeImports.addContractType('GenericContractEvents', 'GenericContractEvent', 'MetadataType');
|
|
13
|
+
const events = this.abi.filter((o) => o.type === 'event');
|
|
14
|
+
let eventsOut = '';
|
|
15
|
+
events.forEach((event) => {
|
|
16
|
+
const { name, anonymous } = event;
|
|
17
|
+
eventsOut += `${stringPascalCase(name)}: ${this.#generateEventDef(event)};\n\n`;
|
|
18
|
+
});
|
|
19
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
20
|
+
const template = compileTemplate('sol/templates/events.hbs');
|
|
21
|
+
return beautifySourceCode(template({ importTypes, eventsOut }));
|
|
22
|
+
}
|
|
23
|
+
#generateEventDef(abiItem) {
|
|
24
|
+
const { name } = abiItem;
|
|
25
|
+
const paramsOut = this.generateParamsOut(abiItem);
|
|
26
|
+
return `GenericContractEvent<'${stringPascalCase(name)}', {${paramsOut}}, Type>`;
|
|
27
|
+
}
|
|
28
|
+
generateParamsOut(abiItem) {
|
|
29
|
+
const { inputs } = abiItem;
|
|
30
|
+
return inputs
|
|
31
|
+
.map((o, idx) => `${commentBlock(`@indexed: ${o.indexed}`)}${stringCamelCase(o.name || `arg${idx}`)}: ${this.typesGen.generateType(o, abiItem, 1)}`)
|
|
32
|
+
.join(', ');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TypeImports } from '../../shared/index.js';
|
|
2
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
3
|
+
export class IndexGen {
|
|
4
|
+
interfaceName;
|
|
5
|
+
typesGen;
|
|
6
|
+
constructor(interfaceName, typesGen) {
|
|
7
|
+
this.interfaceName = interfaceName;
|
|
8
|
+
this.typesGen = typesGen;
|
|
9
|
+
}
|
|
10
|
+
generate(useSubPaths = false) {
|
|
11
|
+
const interfaceName = this.interfaceName;
|
|
12
|
+
const typeImports = new TypeImports();
|
|
13
|
+
typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
14
|
+
typeImports.addContractType('SolGenericContractApi', 'SolRegistry');
|
|
15
|
+
typeImports.addChainType('SubstrateApi');
|
|
16
|
+
const importTypes = typeImports.toImports({ useSubPaths });
|
|
17
|
+
const template = compileTemplate('sol/templates/index.hbs');
|
|
18
|
+
return beautifySourceCode(template({
|
|
19
|
+
interfaceName,
|
|
20
|
+
importTypes,
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SolAbi, SolAbiFunction } from '@dedot/contracts';
|
|
2
|
+
import { TypesGen } from './TypesGen.js';
|
|
3
|
+
export declare class QueryGen {
|
|
4
|
+
readonly abi: SolAbi;
|
|
5
|
+
readonly typesGen: TypesGen;
|
|
6
|
+
constructor(abi: SolAbi, typesGen: TypesGen);
|
|
7
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
8
|
+
doGenerate(functions: SolAbiFunction[], optionsTypeName?: string): string;
|
|
9
|
+
generateMethodDef(abiItem: SolAbiFunction, optionsParamName?: string): string;
|
|
10
|
+
generateParamsOut(abiItem: SolAbiFunction): string;
|
|
11
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { normalizeLabel } from '@dedot/contracts';
|
|
2
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
4
|
+
export class QueryGen {
|
|
5
|
+
abi;
|
|
6
|
+
typesGen;
|
|
7
|
+
constructor(abi, typesGen) {
|
|
8
|
+
this.abi = abi;
|
|
9
|
+
this.typesGen = typesGen;
|
|
10
|
+
}
|
|
11
|
+
generate(useSubPaths = false) {
|
|
12
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
13
|
+
this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult', 'MetadataType');
|
|
14
|
+
const functions = this.abi.filter((item) => item.type === 'function');
|
|
15
|
+
const queryCallsOut = this.doGenerate(functions, 'ContractCallOptions');
|
|
16
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
17
|
+
const template = compileTemplate('sol/templates/query.hbs');
|
|
18
|
+
return beautifySourceCode(template({ importTypes, queryCallsOut }));
|
|
19
|
+
}
|
|
20
|
+
doGenerate(functions, optionsTypeName) {
|
|
21
|
+
let callsOut = '';
|
|
22
|
+
functions.forEach((def) => {
|
|
23
|
+
const { inputs, name } = def;
|
|
24
|
+
// In case there is an arg has label `options`,
|
|
25
|
+
// we use the name `_options` for the last options param
|
|
26
|
+
// This is just and edge case, so this approach works for now
|
|
27
|
+
const optionsParamName = inputs.some(({ name }) => name === 'options') ? '_options' : 'options';
|
|
28
|
+
callsOut += `${commentBlock(inputs.map((input, idx) => `@param {${this.typesGen.generateType(input, def, 1)}} ${stringCamelCase(input.name || `arg${idx}`)}`), optionsTypeName ? `@param {${optionsTypeName}} ${optionsParamName}` : '')}`;
|
|
29
|
+
callsOut += `${normalizeLabel(name)}: ${this.generateMethodDef(def, optionsParamName)};\n\n`;
|
|
30
|
+
});
|
|
31
|
+
return callsOut;
|
|
32
|
+
}
|
|
33
|
+
generateMethodDef(abiItem, optionsParamName = 'options') {
|
|
34
|
+
const { outputs } = abiItem;
|
|
35
|
+
const paramsOut = this.generateParamsOut(abiItem);
|
|
36
|
+
const typeOutInner = `${outputs.map((o) => this.typesGen.generateType(o, abiItem, 1, true)).join(',')}`;
|
|
37
|
+
// If there is only one output, we don't need to wrap it in a tuple, for user-friendly
|
|
38
|
+
const typeOut = outputs.length === 1 ? typeOutInner : `[${typeOutInner}]`;
|
|
39
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>, Type>`;
|
|
40
|
+
}
|
|
41
|
+
generateParamsOut(abiItem) {
|
|
42
|
+
return abiItem.inputs
|
|
43
|
+
.map((input, idx) => `${stringCamelCase(input.name || `arg${idx}`)}: ${this.typesGen.generateType(input, abiItem, 1)}`)
|
|
44
|
+
.join(', ');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SolAbiFunction } from '@dedot/contracts';
|
|
2
|
+
import { QueryGen } from './QueryGen.js';
|
|
3
|
+
export declare class TxGen extends QueryGen {
|
|
4
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
5
|
+
generateMethodDef(abiItem: SolAbiFunction, optionsParamName?: string): string;
|
|
6
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
2
|
+
import { QueryGen } from './QueryGen.js';
|
|
3
|
+
export class TxGen extends QueryGen {
|
|
4
|
+
generate(useSubPaths = false) {
|
|
5
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
6
|
+
this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic', 'MetadataType');
|
|
7
|
+
const txFunctions = this.abi.filter((item) => item.type === 'function' && item.stateMutability !== 'view');
|
|
8
|
+
const txCallsOut = this.doGenerate(txFunctions, 'ContractTxOptions');
|
|
9
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
10
|
+
const template = compileTemplate('sol/templates/tx.hbs');
|
|
11
|
+
return beautifySourceCode(template({ importTypes, txCallsOut }));
|
|
12
|
+
}
|
|
13
|
+
generateMethodDef(abiItem, optionsParamName = 'options') {
|
|
14
|
+
const paramsOut = this.generateParamsOut(abiItem);
|
|
15
|
+
return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi>, Type>`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SolAbi, SolAbiItem, SolAbiTypeDef } from '@dedot/contracts';
|
|
2
|
+
import { TypeImports } from '../../shared/TypeImports.js';
|
|
3
|
+
export declare const SUPPORTED_SOLIDITY_TYPES: RegExp[];
|
|
4
|
+
export declare const BASIC_KNOWN_TYPES: RegExp[];
|
|
5
|
+
export declare class TypesGen {
|
|
6
|
+
#private;
|
|
7
|
+
readonly abi: SolAbi;
|
|
8
|
+
typeImports: TypeImports;
|
|
9
|
+
constructor(abi: SolAbi);
|
|
10
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
11
|
+
generateTypeName(typeDef: SolAbiTypeDef, abiItem: SolAbiItem, typeOut?: boolean): string;
|
|
12
|
+
generateType(typeDef: SolAbiTypeDef, abiItem?: SolAbiItem, nestedLevel?: number, typeOut?: boolean): string;
|
|
13
|
+
generateObjectType(components: SolAbiTypeDef[], nestedLevel?: number, typeOut?: boolean): string;
|
|
14
|
+
addTypeImport(typeName: string | string[]): void;
|
|
15
|
+
}
|