@cosmwasm/ts-codegen 0.30.0 → 0.31.0
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/README.md +5 -2
- package/main/builder/builder.js +41 -4
- package/main/generators/create-helpers.js +38 -0
- package/main/helpers/contractContextBase.js +8 -0
- package/main/helpers/contractsContextTSX.js +8 -0
- package/main/helpers/index.js +31 -0
- package/main/plugins/client.js +11 -6
- package/main/plugins/message-composer.js +10 -6
- package/main/plugins/msg-builder.js +1 -1
- package/main/plugins/plugin-base.js +9 -2
- package/main/plugins/provider-bundle.js +146 -0
- package/main/plugins/provider.js +170 -0
- package/main/plugins/react-query.js +1 -1
- package/main/plugins/recoil.js +1 -1
- package/main/plugins/types.js +1 -1
- package/main/utils/files.js +77 -0
- package/main/utils/schemas.js +1 -2
- package/main/utils/unused.js +68 -0
- package/module/builder/builder.js +28 -2
- package/module/generators/create-helpers.js +25 -0
- package/module/helpers/contractContextBase.js +92 -0
- package/module/helpers/contractsContextTSX.js +73 -0
- package/module/helpers/index.js +2 -0
- package/module/plugins/client.js +13 -10
- package/module/plugins/message-composer.js +12 -10
- package/module/plugins/msg-builder.js +1 -1
- package/module/plugins/plugin-base.js +15 -8
- package/module/plugins/provider-bundle.js +65 -0
- package/module/plugins/provider.js +81 -0
- package/module/plugins/react-query.js +1 -1
- package/module/plugins/recoil.js +1 -1
- package/module/plugins/types.js +1 -1
- package/module/utils/files.js +44 -0
- package/module/utils/schemas.js +1 -2
- package/module/utils/unused.js +45 -0
- package/package.json +3 -3
- package/src/builder/builder.ts +36 -1
- package/src/generators/create-helpers.ts +28 -0
- package/src/helpers/contractContextBase.ts +92 -0
- package/src/helpers/contractsContextTSX.ts +73 -0
- package/src/helpers/index.ts +2 -0
- package/src/plugins/client.ts +30 -14
- package/src/plugins/message-composer.ts +23 -14
- package/src/plugins/msg-builder.ts +1 -1
- package/src/plugins/plugin-base.ts +30 -20
- package/src/plugins/provider-bundle.ts +97 -0
- package/src/plugins/provider.ts +114 -0
- package/src/plugins/react-query.ts +1 -1
- package/src/plugins/recoil.ts +1 -1
- package/src/plugins/types.ts +1 -1
- package/src/utils/files.ts +73 -0
- package/src/utils/schemas.ts +1 -3
- package/src/utils/unused.ts +52 -0
- package/types/src/builder/builder.d.ts +7 -1
- package/types/src/generators/create-helpers.d.ts +3 -0
- package/types/src/helpers/contractContextBase.d.ts +1 -0
- package/types/src/helpers/contractsContextTSX.d.ts +1 -0
- package/types/src/helpers/index.d.ts +2 -0
- package/types/src/plugins/client.d.ts +4 -3
- package/types/src/plugins/message-composer.d.ts +4 -3
- package/types/src/plugins/plugin-base.d.ts +7 -3
- package/types/src/plugins/provider-bundle.d.ts +13 -0
- package/types/src/plugins/provider.d.ts +15 -0
- package/types/src/plugins/use-contracts.d.ts +12 -0
- package/types/src/utils/files.d.ts +3 -0
- package/types/src/utils/unused.d.ts +5 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
export const contractsContextTSX = `
|
2
|
+
import React, { useEffect, useMemo, useRef, useState, useContext } from 'react';
|
3
|
+
import {
|
4
|
+
CosmWasmClient,
|
5
|
+
SigningCosmWasmClient,
|
6
|
+
} from '@cosmjs/cosmwasm-stargate';
|
7
|
+
|
8
|
+
import { IContractsContext, getProviders } from './contractContextProviders';
|
9
|
+
|
10
|
+
interface ContractsConfig {
|
11
|
+
address: string | undefined;
|
12
|
+
getCosmWasmClient: () => Promise<CosmWasmClient>;
|
13
|
+
getSigningCosmWasmClient: () => Promise<SigningCosmWasmClient>;
|
14
|
+
}
|
15
|
+
|
16
|
+
const ContractsContext = React.createContext<IContractsContext | null>(null);
|
17
|
+
|
18
|
+
export const ContractsProvider = ({
|
19
|
+
children,
|
20
|
+
contractsConfig,
|
21
|
+
}: {
|
22
|
+
children: React.ReactNode;
|
23
|
+
contractsConfig: ContractsConfig;
|
24
|
+
}) => {
|
25
|
+
const [cosmWasmClient, setCosmWasmClient] = useState<CosmWasmClient>();
|
26
|
+
const [signingCosmWasmClient, setSigningCosmWasmClient] =
|
27
|
+
useState<SigningCosmWasmClient>();
|
28
|
+
|
29
|
+
const { address, getCosmWasmClient, getSigningCosmWasmClient } =
|
30
|
+
contractsConfig;
|
31
|
+
|
32
|
+
const prevAddressRef = useRef<string | undefined>(address);
|
33
|
+
|
34
|
+
const contracts: IContractsContext = useMemo(() => {
|
35
|
+
return getProviders(address, cosmWasmClient, signingCosmWasmClient);
|
36
|
+
}, [address, cosmWasmClient, signingCosmWasmClient]);
|
37
|
+
|
38
|
+
useEffect(() => {
|
39
|
+
const connectSigningCwClient = async () => {
|
40
|
+
if (address && prevAddressRef.current !== address) {
|
41
|
+
const signingCosmWasmClient = await getSigningCosmWasmClient();
|
42
|
+
setSigningCosmWasmClient(signingCosmWasmClient);
|
43
|
+
} else if (!address) {
|
44
|
+
setSigningCosmWasmClient(undefined);
|
45
|
+
}
|
46
|
+
prevAddressRef.current = address;
|
47
|
+
};
|
48
|
+
connectSigningCwClient();
|
49
|
+
}, [address, getSigningCosmWasmClient]);
|
50
|
+
|
51
|
+
useEffect(() => {
|
52
|
+
const connectCosmWasmClient = async () => {
|
53
|
+
const cosmWasmClient = await getCosmWasmClient();
|
54
|
+
setCosmWasmClient(cosmWasmClient);
|
55
|
+
};
|
56
|
+
connectCosmWasmClient();
|
57
|
+
}, [getCosmWasmClient]);
|
58
|
+
|
59
|
+
return (
|
60
|
+
<ContractsContext.Provider value={contracts}>
|
61
|
+
{children}
|
62
|
+
</ContractsContext.Provider>
|
63
|
+
);
|
64
|
+
};
|
65
|
+
|
66
|
+
export const useContracts = () => {
|
67
|
+
const contracts = useContext(ContractsContext);
|
68
|
+
if (contracts === null) {
|
69
|
+
throw new Error('useContracts must be used within a ContractsProvider');
|
70
|
+
}
|
71
|
+
return contracts;
|
72
|
+
};
|
73
|
+
`;
|
package/src/plugins/client.ts
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
import { pascal } from
|
2
|
-
import * as w from
|
3
|
-
import { findExecuteMsg, findAndParseTypes, findQueryMsg } from
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { findExecuteMsg, findAndParseTypes, findQueryMsg } from "../utils";
|
4
4
|
import {
|
5
5
|
RenderContext,
|
6
6
|
ContractInfo,
|
7
7
|
RenderContextBase,
|
8
8
|
getMessageProperties,
|
9
|
-
RenderOptions
|
10
|
-
} from
|
11
|
-
import { BuilderFileType } from
|
12
|
-
import { BuilderPluginBase } from
|
9
|
+
RenderOptions,
|
10
|
+
} from "wasm-ast-types";
|
11
|
+
import { BuilderFileType } from "../builder";
|
12
|
+
import { BuilderPluginBase } from "./plugin-base";
|
13
|
+
|
14
|
+
export const TYPE = "client";
|
13
15
|
|
14
16
|
export class ClientPlugin extends BuilderPluginBase<RenderOptions> {
|
15
17
|
initContext(
|
16
18
|
contract: ContractInfo,
|
17
19
|
options?: RenderOptions
|
18
20
|
): RenderContextBase<RenderOptions> {
|
19
|
-
return new RenderContext(contract, options);
|
21
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
20
22
|
}
|
21
23
|
|
22
24
|
async doRender(
|
@@ -38,8 +40,8 @@ export class ClientPlugin extends BuilderPluginBase<RenderOptions> {
|
|
38
40
|
|
39
41
|
const { schemas } = context.contract;
|
40
42
|
|
41
|
-
const localname = pascal(name) +
|
42
|
-
const TypesFile = pascal(name) +
|
43
|
+
const localname = pascal(name) + ".client.ts";
|
44
|
+
const TypesFile = pascal(name) + ".types";
|
43
45
|
const QueryMsg = findQueryMsg(schemas);
|
44
46
|
const ExecuteMsg = findExecuteMsg(schemas);
|
45
47
|
const typeHash = await findAndParseTypes(schemas);
|
@@ -62,6 +64,13 @@ export class ClientPlugin extends BuilderPluginBase<RenderOptions> {
|
|
62
64
|
body.push(
|
63
65
|
w.createQueryClass(context, QueryClient, ReadOnlyInstance, QueryMsg)
|
64
66
|
);
|
67
|
+
|
68
|
+
context.addProviderInfo(
|
69
|
+
name,
|
70
|
+
w.PROVIDER_TYPES.QUERY_CLIENT_TYPE,
|
71
|
+
QueryClient,
|
72
|
+
localname
|
73
|
+
);
|
65
74
|
}
|
66
75
|
|
67
76
|
// execute messages
|
@@ -89,20 +98,27 @@ export class ClientPlugin extends BuilderPluginBase<RenderOptions> {
|
|
89
98
|
ExecuteMsg
|
90
99
|
)
|
91
100
|
);
|
101
|
+
|
102
|
+
context.addProviderInfo(
|
103
|
+
name,
|
104
|
+
w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE,
|
105
|
+
Client,
|
106
|
+
localname
|
107
|
+
);
|
92
108
|
}
|
93
109
|
}
|
94
110
|
|
95
|
-
if (typeHash.hasOwnProperty(
|
111
|
+
if (typeHash.hasOwnProperty("Coin")) {
|
96
112
|
// @ts-ignore
|
97
113
|
delete context.utils.Coin;
|
98
114
|
}
|
99
115
|
|
100
116
|
return [
|
101
117
|
{
|
102
|
-
type:
|
118
|
+
type: TYPE,
|
103
119
|
localname,
|
104
|
-
body
|
105
|
-
}
|
120
|
+
body,
|
121
|
+
},
|
106
122
|
];
|
107
123
|
}
|
108
124
|
}
|
@@ -1,23 +1,25 @@
|
|
1
|
-
import { pascal } from
|
2
|
-
import * as w from
|
3
|
-
import { findAndParseTypes, findExecuteMsg } from
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { findAndParseTypes, findExecuteMsg } from "../utils";
|
4
4
|
import {
|
5
5
|
MessageComposerOptions,
|
6
6
|
getMessageProperties,
|
7
7
|
ContractInfo,
|
8
8
|
RenderContextBase,
|
9
9
|
RenderContext,
|
10
|
-
RenderOptions
|
11
|
-
} from
|
12
|
-
import { BuilderFileType } from
|
13
|
-
import { BuilderPluginBase } from
|
10
|
+
RenderOptions,
|
11
|
+
} from "wasm-ast-types";
|
12
|
+
import { BuilderFileType } from "../builder";
|
13
|
+
import { BuilderPluginBase } from "./plugin-base";
|
14
|
+
|
15
|
+
export const TYPE = "message-composer";
|
14
16
|
|
15
17
|
export class MessageComposerPlugin extends BuilderPluginBase<RenderOptions> {
|
16
18
|
initContext(
|
17
19
|
contract: ContractInfo,
|
18
20
|
options?: RenderOptions
|
19
21
|
): RenderContextBase<RenderOptions> {
|
20
|
-
return new RenderContext(contract, options);
|
22
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
21
23
|
}
|
22
24
|
|
23
25
|
async doRender(
|
@@ -39,8 +41,8 @@ export class MessageComposerPlugin extends BuilderPluginBase<RenderOptions> {
|
|
39
41
|
|
40
42
|
const { schemas } = context.contract;
|
41
43
|
|
42
|
-
const localname = pascal(name) +
|
43
|
-
const TypesFile = pascal(name) +
|
44
|
+
const localname = pascal(name) + ".message-composer.ts";
|
45
|
+
const TypesFile = pascal(name) + ".types";
|
44
46
|
const ExecuteMsg = findExecuteMsg(schemas);
|
45
47
|
const typeHash = await findAndParseTypes(schemas);
|
46
48
|
|
@@ -61,20 +63,27 @@ export class MessageComposerPlugin extends BuilderPluginBase<RenderOptions> {
|
|
61
63
|
body.push(
|
62
64
|
w.createMessageComposerClass(context, TheClass, Interface, ExecuteMsg)
|
63
65
|
);
|
66
|
+
|
67
|
+
context.addProviderInfo(
|
68
|
+
name,
|
69
|
+
w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE,
|
70
|
+
TheClass,
|
71
|
+
localname
|
72
|
+
);
|
64
73
|
}
|
65
74
|
}
|
66
75
|
|
67
|
-
if (typeHash.hasOwnProperty(
|
76
|
+
if (typeHash.hasOwnProperty("Coin")) {
|
68
77
|
// @ts-ignore
|
69
78
|
delete context.utils.Coin;
|
70
79
|
}
|
71
80
|
|
72
81
|
return [
|
73
82
|
{
|
74
|
-
type:
|
83
|
+
type: TYPE,
|
75
84
|
localname,
|
76
|
-
body
|
77
|
-
}
|
85
|
+
body,
|
86
|
+
},
|
78
87
|
];
|
79
88
|
}
|
80
89
|
}
|
@@ -16,7 +16,7 @@ export class MsgBuilderPlugin extends BuilderPluginBase<RenderOptions> {
|
|
16
16
|
contract: ContractInfo,
|
17
17
|
options?: RenderOptions
|
18
18
|
): RenderContextBase<RenderOptions> {
|
19
|
-
return new RenderContext(contract, options);
|
19
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
20
20
|
}
|
21
21
|
|
22
22
|
async doRender(
|
@@ -1,15 +1,16 @@
|
|
1
|
-
import { sync as mkdirp } from
|
2
|
-
import { join } from
|
3
|
-
import { writeFileSync } from
|
4
|
-
import { header } from
|
1
|
+
import { sync as mkdirp } from "mkdirp";
|
2
|
+
import { join } from "path";
|
3
|
+
import { writeFileSync } from "fs";
|
4
|
+
import { header } from "../utils/header";
|
5
|
+
import { ContractInfo, UtilMapping, IContext } from "wasm-ast-types";
|
6
|
+
import generate from "@babel/generator";
|
7
|
+
import * as t from "@babel/types";
|
5
8
|
import {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
import * as t from '@babel/types';
|
12
|
-
import { BuilderFile, BuilderFileType, TSBuilderOptions } from '../builder';
|
9
|
+
BuilderFile,
|
10
|
+
BuilderFileType,
|
11
|
+
TSBuilder,
|
12
|
+
TSBuilderOptions,
|
13
|
+
} from "../builder";
|
13
14
|
|
14
15
|
/**
|
15
16
|
* IBuilderPlugin is a common plugin that render generated code.
|
@@ -20,6 +21,10 @@ export interface IBuilderPlugin {
|
|
20
21
|
*/
|
21
22
|
utils: UtilMapping;
|
22
23
|
|
24
|
+
builder?: TSBuilder;
|
25
|
+
|
26
|
+
setBuilder(builder: TSBuilder);
|
27
|
+
|
23
28
|
/**
|
24
29
|
* render generated cdoe.
|
25
30
|
* @param name the name of contract
|
@@ -30,19 +35,27 @@ export interface IBuilderPlugin {
|
|
30
35
|
render(
|
31
36
|
name: string,
|
32
37
|
contractInfo: ContractInfo,
|
33
|
-
outPath: string
|
38
|
+
outPath: string
|
34
39
|
): Promise<BuilderFile[]>;
|
35
40
|
}
|
36
41
|
|
37
42
|
/**
|
38
43
|
* BuilderPluginBase enable ts-codegen users implement their own plugins by only implement a few functions.
|
39
44
|
*/
|
40
|
-
export abstract class BuilderPluginBase<TOpt extends { enabled?: boolean }>
|
45
|
+
export abstract class BuilderPluginBase<TOpt extends { enabled?: boolean }>
|
46
|
+
implements IBuilderPlugin
|
47
|
+
{
|
48
|
+
builder?: TSBuilder;
|
41
49
|
option: TOpt;
|
42
50
|
utils: UtilMapping;
|
43
51
|
|
44
|
-
constructor(opt: TOpt) {
|
52
|
+
constructor(opt: TOpt, builder?: TSBuilder) {
|
45
53
|
this.option = opt;
|
54
|
+
this.builder = builder;
|
55
|
+
}
|
56
|
+
|
57
|
+
setBuilder(builder: TSBuilder) {
|
58
|
+
this.builder = builder;
|
46
59
|
}
|
47
60
|
|
48
61
|
async render(
|
@@ -65,7 +78,7 @@ export abstract class BuilderPluginBase<TOpt extends { enabled?: boolean }> impl
|
|
65
78
|
}
|
66
79
|
|
67
80
|
return results.map((result) => {
|
68
|
-
const imports = context.getImports(this.utils);
|
81
|
+
const imports = context.getImports(this.utils, result.localname);
|
69
82
|
const code =
|
70
83
|
header + generate(t.program([...imports, ...result.body])).code;
|
71
84
|
|
@@ -78,7 +91,7 @@ export abstract class BuilderPluginBase<TOpt extends { enabled?: boolean }> impl
|
|
78
91
|
pluginType: result.pluginType,
|
79
92
|
contract: name,
|
80
93
|
localname: result.localname,
|
81
|
-
filename
|
94
|
+
filename,
|
82
95
|
};
|
83
96
|
});
|
84
97
|
}
|
@@ -88,10 +101,7 @@ export abstract class BuilderPluginBase<TOpt extends { enabled?: boolean }> impl
|
|
88
101
|
* @param contract
|
89
102
|
* @param options
|
90
103
|
*/
|
91
|
-
abstract initContext(
|
92
|
-
contract: ContractInfo,
|
93
|
-
options?: TOpt
|
94
|
-
): IContext;
|
104
|
+
abstract initContext(contract: ContractInfo, options?: TOpt): IContext;
|
95
105
|
|
96
106
|
/**
|
97
107
|
* render generated code here.
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { ContractInfo, RenderContextBase, RenderContext } from "wasm-ast-types";
|
4
|
+
import { BuilderFileType, TSBuilderOptions } from "../builder";
|
5
|
+
import { BuilderPluginBase } from "./plugin-base";
|
6
|
+
import { GetLocalBaseNameByContractName } from "./provider";
|
7
|
+
|
8
|
+
export class ContractsProviderBundlePlugin extends BuilderPluginBase<TSBuilderOptions> {
|
9
|
+
constructor(opt: TSBuilderOptions) {
|
10
|
+
super(opt);
|
11
|
+
|
12
|
+
this.utils = {
|
13
|
+
CosmWasmClient: "@cosmjs/cosmwasm-stargate",
|
14
|
+
SigningCosmWasmClient: "@cosmjs/cosmwasm-stargate",
|
15
|
+
IQueryClientProvider: "__contractContextBase__",
|
16
|
+
ISigningClientProvider: "__contractContextBase__",
|
17
|
+
IMessageComposerProvider: "__contractContextBase__",
|
18
|
+
};
|
19
|
+
}
|
20
|
+
|
21
|
+
initContext(
|
22
|
+
contract: ContractInfo,
|
23
|
+
options?: TSBuilderOptions
|
24
|
+
): RenderContextBase<TSBuilderOptions> {
|
25
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
26
|
+
}
|
27
|
+
|
28
|
+
async doRender(
|
29
|
+
name: string,
|
30
|
+
context: RenderContext
|
31
|
+
): Promise<
|
32
|
+
{
|
33
|
+
type: BuilderFileType;
|
34
|
+
pluginType?: string;
|
35
|
+
localname: string;
|
36
|
+
body: any[];
|
37
|
+
}[]
|
38
|
+
> {
|
39
|
+
if (!this.option?.useContracts?.enabled) {
|
40
|
+
return;
|
41
|
+
}
|
42
|
+
|
43
|
+
const providerInfos = context.getProviderInfos();
|
44
|
+
|
45
|
+
if (!Object.keys(providerInfos)?.length) {
|
46
|
+
return;
|
47
|
+
}
|
48
|
+
|
49
|
+
const localname = "contractContextProviders.ts";
|
50
|
+
|
51
|
+
const body = [];
|
52
|
+
|
53
|
+
context.addUtil("CosmWasmClient");
|
54
|
+
context.addUtil("SigningCosmWasmClient");
|
55
|
+
|
56
|
+
context.addUtil("IQueryClientProvider");
|
57
|
+
context.addUtil("ISigningClientProvider");
|
58
|
+
context.addUtil("IMessageComposerProvider");
|
59
|
+
|
60
|
+
for (const name in providerInfos) {
|
61
|
+
if (Object.prototype.hasOwnProperty.call(providerInfos, name)) {
|
62
|
+
const providerInfo = providerInfos[name];
|
63
|
+
|
64
|
+
for (const key in providerInfo) {
|
65
|
+
if (Object.prototype.hasOwnProperty.call(providerInfo, key)) {
|
66
|
+
const info = providerInfo[key];
|
67
|
+
|
68
|
+
body.push(
|
69
|
+
w.importStmt(
|
70
|
+
[info.classname],
|
71
|
+
`./${info.basename}`
|
72
|
+
)
|
73
|
+
);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
body.push(
|
78
|
+
w.importStmt(
|
79
|
+
[pascal(name)],
|
80
|
+
`./${GetLocalBaseNameByContractName(name)}`
|
81
|
+
)
|
82
|
+
);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
body.push(w.createIContractsContext(providerInfos));
|
87
|
+
body.push(w.createGettingProviders(providerInfos));
|
88
|
+
|
89
|
+
return [
|
90
|
+
{
|
91
|
+
type: "plugin",
|
92
|
+
localname,
|
93
|
+
body,
|
94
|
+
},
|
95
|
+
];
|
96
|
+
}
|
97
|
+
}
|
@@ -0,0 +1,114 @@
|
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { ContractInfo, RenderContextBase, RenderContext } from "wasm-ast-types";
|
4
|
+
import { BuilderFileType, TSBuilderOptions } from "../builder";
|
5
|
+
import { BuilderPluginBase } from "./plugin-base";
|
6
|
+
|
7
|
+
export const GetLocalNameByContractName = (name) =>
|
8
|
+
`${pascal(name)}.provider.ts`;
|
9
|
+
|
10
|
+
export const GetLocalBaseNameByContractName = (name) =>
|
11
|
+
`${pascal(name)}.provider`;
|
12
|
+
|
13
|
+
export class ContractsContextProviderPlugin extends BuilderPluginBase<TSBuilderOptions> {
|
14
|
+
constructor(opt: TSBuilderOptions) {
|
15
|
+
super(opt);
|
16
|
+
|
17
|
+
this.utils = {
|
18
|
+
ContractBase: "__contractContextBase__",
|
19
|
+
IContractConstructor: "__contractContextBase__",
|
20
|
+
IEmptyClient: "__contractContextBase__",
|
21
|
+
};
|
22
|
+
}
|
23
|
+
|
24
|
+
initContext(
|
25
|
+
contract: ContractInfo,
|
26
|
+
options?: TSBuilderOptions
|
27
|
+
): RenderContextBase<TSBuilderOptions> {
|
28
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
29
|
+
}
|
30
|
+
|
31
|
+
async doRender(
|
32
|
+
name: string,
|
33
|
+
context: RenderContext
|
34
|
+
): Promise<
|
35
|
+
{
|
36
|
+
type: BuilderFileType;
|
37
|
+
pluginType?: string;
|
38
|
+
localname: string;
|
39
|
+
body: any[];
|
40
|
+
}[]
|
41
|
+
> {
|
42
|
+
if (!this.option?.useContracts?.enabled) {
|
43
|
+
return;
|
44
|
+
}
|
45
|
+
|
46
|
+
const providerInfo = context.getProviderInfos()[name];
|
47
|
+
|
48
|
+
if (!Object.keys(providerInfo)?.length) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
|
52
|
+
context.addUtil("ContractBase");
|
53
|
+
context.addUtil("IContractConstructor");
|
54
|
+
|
55
|
+
const localname = GetLocalNameByContractName(name);
|
56
|
+
let needEmptyClientType = false;
|
57
|
+
let clientFile = null;
|
58
|
+
let clientClasses = [];
|
59
|
+
|
60
|
+
const body = [];
|
61
|
+
|
62
|
+
const signClientProviderInfo =
|
63
|
+
providerInfo[w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE];
|
64
|
+
|
65
|
+
if (signClientProviderInfo) {
|
66
|
+
clientFile = `./${signClientProviderInfo.basename}`;
|
67
|
+
clientClasses.push(signClientProviderInfo.classname);
|
68
|
+
} else {
|
69
|
+
needEmptyClientType = true;
|
70
|
+
}
|
71
|
+
|
72
|
+
const queryClientProviderInfo =
|
73
|
+
providerInfo[w.PROVIDER_TYPES.QUERY_CLIENT_TYPE];
|
74
|
+
|
75
|
+
if (queryClientProviderInfo) {
|
76
|
+
clientFile = `./${queryClientProviderInfo.basename}`;
|
77
|
+
clientClasses.push(queryClientProviderInfo.classname);
|
78
|
+
} else {
|
79
|
+
needEmptyClientType = true;
|
80
|
+
}
|
81
|
+
|
82
|
+
if (clientFile) {
|
83
|
+
body.push(w.importStmt(clientClasses, clientFile));
|
84
|
+
}
|
85
|
+
|
86
|
+
const messageComposerProviderInfo =
|
87
|
+
providerInfo[w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE];
|
88
|
+
|
89
|
+
if (messageComposerProviderInfo) {
|
90
|
+
body.push(
|
91
|
+
w.importStmt(
|
92
|
+
[messageComposerProviderInfo.classname],
|
93
|
+
`./${messageComposerProviderInfo.basename}`
|
94
|
+
)
|
95
|
+
);
|
96
|
+
} else {
|
97
|
+
needEmptyClientType = true;
|
98
|
+
}
|
99
|
+
|
100
|
+
if (needEmptyClientType) {
|
101
|
+
context.addUtil("IEmptyClient");
|
102
|
+
}
|
103
|
+
|
104
|
+
body.push(w.createProvider(name, providerInfo));
|
105
|
+
|
106
|
+
return [
|
107
|
+
{
|
108
|
+
type: "plugin",
|
109
|
+
localname,
|
110
|
+
body,
|
111
|
+
},
|
112
|
+
];
|
113
|
+
}
|
114
|
+
}
|
@@ -16,7 +16,7 @@ export class ReactQueryPlugin extends BuilderPluginBase<RenderOptions> {
|
|
16
16
|
contract: ContractInfo,
|
17
17
|
options?: RenderOptions
|
18
18
|
): RenderContextBase<RenderOptions> {
|
19
|
-
return new RenderContext(contract, options);
|
19
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
20
20
|
}
|
21
21
|
|
22
22
|
async doRender(
|
package/src/plugins/recoil.ts
CHANGED
@@ -19,7 +19,7 @@ export class RecoilPlugin extends BuilderPluginBase<RenderOptions> {
|
|
19
19
|
contract: ContractInfo,
|
20
20
|
options?: RenderOptions
|
21
21
|
): RenderContextBase<RenderOptions> {
|
22
|
-
return new RenderContext(contract, options);
|
22
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
23
23
|
}
|
24
24
|
|
25
25
|
async doRender(
|
package/src/plugins/types.ts
CHANGED
@@ -16,7 +16,7 @@ export class TypesPlugin extends BuilderPluginBase<RenderOptions> {
|
|
16
16
|
contract: ContractInfo,
|
17
17
|
options?: RenderOptions
|
18
18
|
): RenderContextBase<RenderOptions> {
|
19
|
-
return new RenderContext(contract, options);
|
19
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
20
20
|
}
|
21
21
|
|
22
22
|
async doRender(
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import * as t from "@babel/types";
|
2
|
+
import { parse, ParserPlugin } from "@babel/parser";
|
3
|
+
import { sync as mkdirp } from "mkdirp";
|
4
|
+
import { writeFileSync } from "fs";
|
5
|
+
import { dirname } from "path";
|
6
|
+
import generate from "@babel/generator";
|
7
|
+
import { unused } from "./unused";
|
8
|
+
import traverse from "@babel/traverse";
|
9
|
+
|
10
|
+
export const writeAstToFile = (
|
11
|
+
outPath: string,
|
12
|
+
program: t.Statement[],
|
13
|
+
filename: string,
|
14
|
+
removeUnusedImports = false,
|
15
|
+
isTsDisable = false,
|
16
|
+
isEslintDisable = false
|
17
|
+
) => {
|
18
|
+
const ast = t.program(program);
|
19
|
+
const content = generate(ast).code;
|
20
|
+
|
21
|
+
if (removeUnusedImports) {
|
22
|
+
const plugins: ParserPlugin[] = ["typescript"];
|
23
|
+
const newAst = parse(content, {
|
24
|
+
sourceType: "module",
|
25
|
+
plugins,
|
26
|
+
});
|
27
|
+
traverse(newAst, unused);
|
28
|
+
const content2 = generate(newAst).code;
|
29
|
+
writeContentToFile(
|
30
|
+
outPath,
|
31
|
+
content2,
|
32
|
+
filename,
|
33
|
+
isTsDisable,
|
34
|
+
isEslintDisable
|
35
|
+
);
|
36
|
+
} else {
|
37
|
+
writeContentToFile(
|
38
|
+
outPath,
|
39
|
+
content,
|
40
|
+
filename,
|
41
|
+
isTsDisable,
|
42
|
+
isEslintDisable
|
43
|
+
);
|
44
|
+
}
|
45
|
+
};
|
46
|
+
|
47
|
+
export const writeContentToFile = (
|
48
|
+
outPath: string,
|
49
|
+
content: string,
|
50
|
+
filename: string,
|
51
|
+
isTsDisable = false,
|
52
|
+
isEslintDisable = false
|
53
|
+
) => {
|
54
|
+
let esLintPrefix = "";
|
55
|
+
let tsLintPrefix = "";
|
56
|
+
|
57
|
+
let nameWithoutPath = filename.replace(outPath, "");
|
58
|
+
// strip off leading slash
|
59
|
+
if (nameWithoutPath.startsWith("/"))
|
60
|
+
nameWithoutPath = nameWithoutPath.replace(/^\//, "");
|
61
|
+
|
62
|
+
if (isTsDisable) {
|
63
|
+
tsLintPrefix = `//@ts-nocheck\n`;
|
64
|
+
}
|
65
|
+
|
66
|
+
if (isEslintDisable) {
|
67
|
+
esLintPrefix = `/* eslint-disable */\n`;
|
68
|
+
}
|
69
|
+
|
70
|
+
const text = tsLintPrefix + esLintPrefix + content;
|
71
|
+
mkdirp(dirname(filename));
|
72
|
+
writeFileSync(filename, text);
|
73
|
+
};
|
package/src/utils/schemas.ts
CHANGED
@@ -79,9 +79,7 @@ export const findQueryMsg = (schemas) => {
|
|
79
79
|
|
80
80
|
export const findExecuteMsg = (schemas) => {
|
81
81
|
const ExecuteMsg = schemas.find(schema =>
|
82
|
-
|
83
|
-
schema.title === 'ExecuteMsg_for_Empty' || // if cleanse is used, this is never
|
84
|
-
schema.title === 'ExecuteMsgForEmpty'
|
82
|
+
schema.title.startsWith('ExecuteMsg')
|
85
83
|
);
|
86
84
|
return ExecuteMsg;
|
87
85
|
};
|