wasm-ast-types 0.26.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE-Apache +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +1 -1
- package/{types/client → client}/client.d.ts +2 -2
- package/client/client.js +275 -0
- package/client/index.js +17 -0
- package/{types/context → context}/context.d.ts +9 -22
- package/context/context.js +114 -0
- package/{types/context → context}/imports.d.ts +9 -8
- package/context/imports.js +166 -0
- package/context/index.js +18 -0
- package/esm/client/client.js +238 -0
- package/esm/client/index.js +1 -0
- package/esm/context/context.js +104 -0
- package/esm/context/imports.js +136 -0
- package/{module → esm}/context/index.js +1 -1
- package/{src/index.ts → esm/index.js} +0 -1
- package/esm/message-builder/message-builder.js +64 -0
- package/esm/message-composer/message-composer.js +105 -0
- package/esm/provider/provider.js +97 -0
- package/esm/react-query/react-query.js +458 -0
- package/esm/recoil/recoil.js +110 -0
- package/esm/utils/babel.js +231 -0
- package/esm/utils/constants.js +20 -0
- package/esm/utils/ref.js +4 -0
- package/esm/utils/types.js +343 -0
- package/{types/index.d.ts → index.d.ts} +0 -1
- package/index.js +24 -0
- package/message-builder/index.js +17 -0
- package/{types/message-builder → message-builder}/message-builder.d.ts +1 -1
- package/message-builder/message-builder.js +91 -0
- package/message-composer/index.d.ts +1 -0
- package/message-composer/index.js +17 -0
- package/{types/message-composer → message-composer}/message-composer.d.ts +1 -1
- package/message-composer/message-composer.js +133 -0
- package/package.json +19 -68
- package/provider/index.d.ts +1 -0
- package/provider/index.js +17 -0
- package/provider/provider.js +127 -0
- package/react-query/index.d.ts +1 -0
- package/react-query/index.js +17 -0
- package/{types/react-query → react-query}/react-query.d.ts +2 -3
- package/react-query/react-query.js +490 -0
- package/recoil/index.d.ts +1 -0
- package/recoil/index.js +17 -0
- package/{types/recoil → recoil}/recoil.d.ts +1 -1
- package/recoil/recoil.js +140 -0
- package/{types/utils → utils}/babel.d.ts +9 -8
- package/utils/babel.js +285 -0
- package/utils/constants.js +46 -0
- package/utils/index.js +26 -0
- package/{types/utils → utils}/ref.d.ts +1 -1
- package/utils/ref.js +8 -0
- package/{types/utils → utils}/types.d.ts +8 -8
- package/utils/types.js +379 -0
- package/main/client/client.js +0 -196
- package/main/client/index.js +0 -16
- package/main/client/test/ts-client.account-nfts.spec.js +0 -28
- package/main/client/test/ts-client.arrays-ref.spec.js +0 -19
- package/main/client/test/ts-client.arrays.spec.js +0 -26
- package/main/client/test/ts-client.cw-named-groups.test.js +0 -19
- package/main/client/test/ts-client.cw-proposal-single.test.js +0 -23
- package/main/client/test/ts-client.empty-enums.spec.js +0 -13
- package/main/client/test/ts-client.issue-101.spec.js +0 -23
- package/main/client/test/ts-client.issue-103.test.js +0 -44
- package/main/client/test/ts-client.issue-71.test.js +0 -81
- package/main/client/test/ts-client.issue-98.test.js +0 -28
- package/main/client/test/ts-client.issues.test.js +0 -81
- package/main/client/test/ts-client.overrides.spec.js +0 -33
- package/main/client/test/ts-client.sg721.spec.js +0 -19
- package/main/client/test/ts-client.spec.js +0 -87
- package/main/client/test/ts-client.vectis.spec.js +0 -46
- package/main/client/test/ts-client.wager.spec.js +0 -39
- package/main/context/context.js +0 -162
- package/main/context/imports.js +0 -159
- package/main/context/index.js +0 -27
- package/main/index.js +0 -104
- package/main/message-builder/index.js +0 -16
- package/main/message-builder/message-builder.js +0 -62
- package/main/message-builder/message-builder.spec.js +0 -20
- package/main/message-composer/index.js +0 -16
- package/main/message-composer/message-composer.js +0 -86
- package/main/message-composer/message-composer.spec.js +0 -23
- package/main/provider/index.js +0 -16
- package/main/provider/provider.js +0 -71
- package/main/provider/provider.spec.js +0 -60
- package/main/react-query/index.js +0 -16
- package/main/react-query/react-query.js +0 -413
- package/main/react-query/react-query.spec.js +0 -91
- package/main/recoil/index.js +0 -16
- package/main/recoil/recoil.js +0 -61
- package/main/recoil/recoil.spec.js +0 -20
- package/main/types.js +0 -4
- package/main/utils/babel.js +0 -282
- package/main/utils/babel.spec.js +0 -109
- package/main/utils/constants.js +0 -26
- package/main/utils/index.js +0 -65
- package/main/utils/ref.js +0 -12
- package/main/utils/types.js +0 -343
- package/module/client/client.js +0 -166
- package/module/client/index.js +0 -1
- package/module/client/test/ts-client.account-nfts.spec.js +0 -25
- package/module/client/test/ts-client.arrays-ref.spec.js +0 -16
- package/module/client/test/ts-client.arrays.spec.js +0 -23
- package/module/client/test/ts-client.cw-named-groups.test.js +0 -16
- package/module/client/test/ts-client.cw-proposal-single.test.js +0 -20
- package/module/client/test/ts-client.empty-enums.spec.js +0 -10
- package/module/client/test/ts-client.issue-101.spec.js +0 -22
- package/module/client/test/ts-client.issue-103.test.js +0 -41
- package/module/client/test/ts-client.issue-71.test.js +0 -21
- package/module/client/test/ts-client.issue-98.test.js +0 -25
- package/module/client/test/ts-client.issues.test.js +0 -21
- package/module/client/test/ts-client.overrides.spec.js +0 -30
- package/module/client/test/ts-client.sg721.spec.js +0 -16
- package/module/client/test/ts-client.spec.js +0 -84
- package/module/client/test/ts-client.vectis.spec.js +0 -43
- package/module/client/test/ts-client.wager.spec.js +0 -36
- package/module/context/context.js +0 -116
- package/module/context/imports.js +0 -127
- package/module/index.js +0 -9
- package/module/message-builder/index.js +0 -1
- package/module/message-builder/message-builder.js +0 -52
- package/module/message-builder/message-builder.spec.js +0 -17
- package/module/message-composer/index.js +0 -1
- package/module/message-composer/message-composer.js +0 -70
- package/module/message-composer/message-composer.spec.js +0 -20
- package/module/provider/index.js +0 -1
- package/module/provider/provider.js +0 -45
- package/module/provider/provider.spec.js +0 -58
- package/module/react-query/index.js +0 -1
- package/module/react-query/react-query.js +0 -403
- package/module/react-query/react-query.spec.js +0 -85
- package/module/recoil/index.js +0 -1
- package/module/recoil/recoil.js +0 -46
- package/module/recoil/recoil.spec.js +0 -14
- package/module/types.js +0 -3
- package/module/utils/babel.js +0 -203
- package/module/utils/babel.spec.js +0 -70
- package/module/utils/constants.js +0 -12
- package/module/utils/index.js +0 -6
- package/module/utils/ref.js +0 -4
- package/module/utils/types.js +0 -301
- package/src/client/client.ts +0 -584
- package/src/client/index.ts +0 -1
- package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +0 -497
- package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +0 -452
- package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +0 -101
- package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +0 -141
- package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +0 -341
- package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +0 -20
- package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +0 -47
- package/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap +0 -79
- package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +0 -432
- package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +0 -117
- package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +0 -984
- package/src/client/test/__snapshots__/ts-client.overrides.spec.ts.snap +0 -709
- package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +0 -350
- package/src/client/test/__snapshots__/ts-client.spec.ts.snap +0 -723
- package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +0 -337
- package/src/client/test/__snapshots__/ts-client.wager.spec.ts.snap +0 -132
- package/src/client/test/ts-client.account-nfts.spec.ts +0 -55
- package/src/client/test/ts-client.arrays-ref.spec.ts +0 -48
- package/src/client/test/ts-client.arrays.spec.ts +0 -58
- package/src/client/test/ts-client.cw-named-groups.test.ts +0 -48
- package/src/client/test/ts-client.cw-proposal-single.test.ts +0 -50
- package/src/client/test/ts-client.empty-enums.spec.ts +0 -28
- package/src/client/test/ts-client.issue-101.spec.ts +0 -37
- package/src/client/test/ts-client.issue-103.test.ts +0 -106
- package/src/client/test/ts-client.issue-71.test.ts +0 -51
- package/src/client/test/ts-client.issue-98.test.ts +0 -55
- package/src/client/test/ts-client.issues.test.ts +0 -52
- package/src/client/test/ts-client.overrides.spec.ts +0 -74
- package/src/client/test/ts-client.sg721.spec.ts +0 -46
- package/src/client/test/ts-client.spec.ts +0 -166
- package/src/client/test/ts-client.vectis.spec.ts +0 -97
- package/src/client/test/ts-client.wager.spec.ts +0 -63
- package/src/context/context.ts +0 -229
- package/src/context/imports.ts +0 -195
- package/src/context/index.ts +0 -2
- package/src/message-builder/__snapshots__/message-builder.spec.ts.snap +0 -270
- package/src/message-builder/message-builder.spec.ts +0 -24
- package/src/message-builder/message-builder.ts +0 -119
- package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +0 -331
- package/src/message-composer/index.ts +0 -1
- package/src/message-composer/message-composer.spec.ts +0 -46
- package/src/message-composer/message-composer.ts +0 -262
- package/src/provider/__snapshots__/provider.spec.ts.snap +0 -49
- package/src/provider/index.ts +0 -1
- package/src/provider/provider.spec.ts +0 -81
- package/src/provider/provider.ts +0 -237
- package/src/react-query/__snapshots__/react-query.spec.ts.snap +0 -1359
- package/src/react-query/index.ts +0 -1
- package/src/react-query/react-query.spec.ts +0 -116
- package/src/react-query/react-query.ts +0 -1072
- package/src/recoil/__snapshots__/recoil.spec.ts.snap +0 -203
- package/src/recoil/index.ts +0 -1
- package/src/recoil/recoil.spec.ts +0 -38
- package/src/recoil/recoil.ts +0 -307
- package/src/types.ts +0 -44
- package/src/utils/__snapshots__/babel.spec.ts.snap +0 -75
- package/src/utils/babel.spec.ts +0 -511
- package/src/utils/babel.ts +0 -352
- package/src/utils/constants.ts +0 -37
- package/src/utils/ref.ts +0 -6
- package/src/utils/types.ts +0 -478
- package/types/types.d.ts +0 -41
- /package/{types/client → client}/index.d.ts +0 -0
- /package/{types/context → context}/index.d.ts +0 -0
- /package/{src/message-builder/index.ts → esm/message-builder/index.js} +0 -0
- /package/{types/message-composer/index.d.ts → esm/message-composer/index.js} +0 -0
- /package/{types/provider/index.d.ts → esm/provider/index.js} +0 -0
- /package/{types/react-query/index.d.ts → esm/react-query/index.js} +0 -0
- /package/{types/recoil/index.d.ts → esm/recoil/index.js} +0 -0
- /package/{src/utils/index.ts → esm/utils/index.js} +0 -0
- /package/{types/message-builder → message-builder}/index.d.ts +0 -0
- /package/{types/provider → provider}/provider.d.ts +0 -0
- /package/{types/utils → utils}/constants.d.ts +0 -0
- /package/{types/utils → utils}/index.d.ts +0 -0
@@ -0,0 +1,104 @@
|
|
1
|
+
import { refLookup } from "../utils";
|
2
|
+
import { convertUtilsToImportList, getImportStatements } from "./imports";
|
3
|
+
import deepmerge from "deepmerge";
|
4
|
+
import { basename, extname } from 'path';
|
5
|
+
;
|
6
|
+
export const defaultOptions = {
|
7
|
+
enabled: true,
|
8
|
+
types: {
|
9
|
+
enabled: true,
|
10
|
+
itemsUseTuples: false,
|
11
|
+
aliasExecuteMsg: false
|
12
|
+
},
|
13
|
+
client: {
|
14
|
+
enabled: true,
|
15
|
+
execExtendsQuery: true,
|
16
|
+
noImplicitOverride: false,
|
17
|
+
},
|
18
|
+
recoil: {
|
19
|
+
enabled: false
|
20
|
+
},
|
21
|
+
messageComposer: {
|
22
|
+
enabled: false
|
23
|
+
},
|
24
|
+
messageBuilder: {
|
25
|
+
enabled: false,
|
26
|
+
},
|
27
|
+
reactQuery: {
|
28
|
+
enabled: false,
|
29
|
+
optionalClient: false,
|
30
|
+
version: 'v4',
|
31
|
+
mutations: false,
|
32
|
+
camelize: true,
|
33
|
+
queryKeys: false
|
34
|
+
}
|
35
|
+
};
|
36
|
+
export const getDefinitionSchema = (schemas) => {
|
37
|
+
const aggregateSchema = {
|
38
|
+
definitions: {
|
39
|
+
//
|
40
|
+
}
|
41
|
+
};
|
42
|
+
schemas.forEach(schema => {
|
43
|
+
schema.definitions = schema.definitions || {};
|
44
|
+
aggregateSchema.definitions = {
|
45
|
+
...aggregateSchema.definitions,
|
46
|
+
...schema.definitions
|
47
|
+
};
|
48
|
+
});
|
49
|
+
return aggregateSchema;
|
50
|
+
};
|
51
|
+
export class BuilderContext {
|
52
|
+
providers = {};
|
53
|
+
addProviderInfo(contractName, type, classname, filename) {
|
54
|
+
if (!this.providers[contractName]) {
|
55
|
+
this.providers[contractName] = {};
|
56
|
+
}
|
57
|
+
this.providers[contractName][type] = {
|
58
|
+
classname,
|
59
|
+
filename,
|
60
|
+
basename: basename(filename, extname(filename))
|
61
|
+
};
|
62
|
+
}
|
63
|
+
getProviderInfos() {
|
64
|
+
return this.providers;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
/**
|
68
|
+
* context object for generating code.
|
69
|
+
* only mergeDefaultOpt needs to implementing for combine options and default options.
|
70
|
+
* @param TOpt option type
|
71
|
+
*/
|
72
|
+
export class RenderContextBase {
|
73
|
+
builderContext;
|
74
|
+
contract;
|
75
|
+
utils = {};
|
76
|
+
schema;
|
77
|
+
options;
|
78
|
+
constructor(contract, options, builderContext) {
|
79
|
+
this.contract = contract;
|
80
|
+
this.schema = getDefinitionSchema(contract.schemas);
|
81
|
+
this.options = this.mergeDefaultOpt(options);
|
82
|
+
this.builderContext = builderContext;
|
83
|
+
}
|
84
|
+
refLookup($ref) {
|
85
|
+
return refLookup($ref, this.schema);
|
86
|
+
}
|
87
|
+
addUtil(util) {
|
88
|
+
this.utils[util] = true;
|
89
|
+
}
|
90
|
+
addProviderInfo(contractName, type, classname, filename) {
|
91
|
+
this.builderContext.addProviderInfo(contractName, type, classname, filename);
|
92
|
+
}
|
93
|
+
getProviderInfos() {
|
94
|
+
return this.builderContext.providers;
|
95
|
+
}
|
96
|
+
getImports(registeredUtils, filepath) {
|
97
|
+
return getImportStatements(convertUtilsToImportList(this, Object.keys(this.utils), registeredUtils), filepath);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
export class RenderContext extends RenderContextBase {
|
101
|
+
mergeDefaultOpt(options) {
|
102
|
+
return deepmerge(defaultOptions, options ?? {});
|
103
|
+
}
|
104
|
+
}
|
@@ -0,0 +1,136 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { importAs, importStmt } from "../utils";
|
3
|
+
import { relative, dirname, extname } from 'path';
|
4
|
+
const makeReactQuerySwitch = (varName) => {
|
5
|
+
return (context) => {
|
6
|
+
switch (context.options.reactQuery.version) {
|
7
|
+
case 'v4':
|
8
|
+
return {
|
9
|
+
type: 'import',
|
10
|
+
path: '@tanstack/react-query',
|
11
|
+
name: varName
|
12
|
+
};
|
13
|
+
case 'v3':
|
14
|
+
default:
|
15
|
+
return {
|
16
|
+
type: 'import',
|
17
|
+
path: 'react-query',
|
18
|
+
name: varName
|
19
|
+
};
|
20
|
+
}
|
21
|
+
};
|
22
|
+
};
|
23
|
+
export const UTILS = {
|
24
|
+
selectorFamily: 'recoil',
|
25
|
+
MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
|
26
|
+
MsgExecuteContractEncodeObject: '@cosmjs/cosmwasm-stargate',
|
27
|
+
Coin: '@cosmjs/amino',
|
28
|
+
toUtf8: '@cosmjs/encoding',
|
29
|
+
StdFee: '@cosmjs/amino',
|
30
|
+
CosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
31
|
+
ExecuteResult: '@cosmjs/cosmwasm-stargate',
|
32
|
+
SigningCosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
33
|
+
// react-query
|
34
|
+
useQuery: makeReactQuerySwitch('useQuery'),
|
35
|
+
UseQueryOptions: makeReactQuerySwitch('UseQueryOptions'),
|
36
|
+
useMutation: makeReactQuerySwitch('useMutation'),
|
37
|
+
UseMutationOptions: makeReactQuerySwitch('UseMutationOptions')
|
38
|
+
};
|
39
|
+
export const UTIL_HELPERS = [
|
40
|
+
'__contractContextBase__',
|
41
|
+
];
|
42
|
+
export const convertUtilsToImportList = (context, utils, registeredUtils) => {
|
43
|
+
return utils.map((util) => {
|
44
|
+
let result = null;
|
45
|
+
if (registeredUtils) {
|
46
|
+
result = convertUtil(context, util, registeredUtils);
|
47
|
+
if (result) {
|
48
|
+
return result;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
result = convertUtil(context, util, UTILS);
|
52
|
+
if (result) {
|
53
|
+
return result;
|
54
|
+
}
|
55
|
+
throw new Error(`missing Util! ::[${util}]`);
|
56
|
+
});
|
57
|
+
};
|
58
|
+
export const convertUtil = (context, util, registeredUtils) => {
|
59
|
+
if (!registeredUtils.hasOwnProperty(util))
|
60
|
+
return null;
|
61
|
+
if (typeof registeredUtils[util] === 'string') {
|
62
|
+
return {
|
63
|
+
type: 'import',
|
64
|
+
path: registeredUtils[util],
|
65
|
+
name: util
|
66
|
+
};
|
67
|
+
}
|
68
|
+
else if (typeof registeredUtils[util] === 'function') {
|
69
|
+
return registeredUtils[util](context);
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
return registeredUtils[util];
|
73
|
+
}
|
74
|
+
};
|
75
|
+
// __helpers__
|
76
|
+
export const getImportStatements = (list, filepath) => {
|
77
|
+
// swap helpers with helpers file...
|
78
|
+
const modifiedImports = list.map(imp => {
|
79
|
+
if (filepath && UTIL_HELPERS.includes(imp.path)) {
|
80
|
+
const name = imp.path.replace(/__/g, '');
|
81
|
+
return {
|
82
|
+
...imp,
|
83
|
+
path: getRelativePath(filepath, `./${name}`)
|
84
|
+
};
|
85
|
+
}
|
86
|
+
return imp;
|
87
|
+
});
|
88
|
+
const imports = modifiedImports.reduce((m, obj) => {
|
89
|
+
m[obj.path] = m[obj.path] || [];
|
90
|
+
const exists = m[obj.path].find((el) => el.type === obj.type && el.path === obj.path && el.name === obj.name);
|
91
|
+
// MARKED AS NOT DRY [google.protobuf names]
|
92
|
+
// TODO some have google.protobuf.Any shows up... figure out the better way to handle this
|
93
|
+
if (/\./.test(obj.name)) {
|
94
|
+
obj.name = obj.name.split('.')[obj.name.split('.').length - 1];
|
95
|
+
}
|
96
|
+
if (!exists) {
|
97
|
+
m[obj.path].push(obj);
|
98
|
+
}
|
99
|
+
return m;
|
100
|
+
}, {});
|
101
|
+
return Object.entries(imports)
|
102
|
+
.reduce((m, [importPath, imports]) => {
|
103
|
+
const defaultImports = imports.filter(a => a.type === 'default');
|
104
|
+
if (defaultImports.length) {
|
105
|
+
if (defaultImports.length > 1)
|
106
|
+
throw new Error('more than one default name NOT allowed.');
|
107
|
+
m.push(t.importDeclaration([
|
108
|
+
t.importDefaultSpecifier(t.identifier(defaultImports[0].name))
|
109
|
+
], t.stringLiteral(defaultImports[0].path)));
|
110
|
+
}
|
111
|
+
const namedImports = imports.filter(a => a.type === 'import' && (!a.importAs || (a.name === a.importAs)));
|
112
|
+
if (namedImports.length) {
|
113
|
+
m.push(importStmt(namedImports.map(i => i.name), namedImports[0].path));
|
114
|
+
}
|
115
|
+
const aliasNamedImports = imports.filter(a => a.type === 'import' && (a.importAs && (a.name !== a.importAs)));
|
116
|
+
aliasNamedImports.forEach(imp => {
|
117
|
+
m.push(importAs(imp.name, imp.importAs, imp.path));
|
118
|
+
});
|
119
|
+
const namespaced = imports.filter(a => a.type === 'namespace');
|
120
|
+
if (namespaced.length) {
|
121
|
+
if (namespaced.length > 1)
|
122
|
+
throw new Error('more than one namespaced name NOT allowed.');
|
123
|
+
m.push(t.importDeclaration([
|
124
|
+
t.importNamespaceSpecifier(t.identifier(namespaced[0].name))
|
125
|
+
], t.stringLiteral(namespaced[0].path)));
|
126
|
+
}
|
127
|
+
return m;
|
128
|
+
}, []);
|
129
|
+
};
|
130
|
+
export const getRelativePath = (f1, f2) => {
|
131
|
+
const rel = relative(dirname(f1), f2);
|
132
|
+
let importPath = rel.replace(extname(rel), '');
|
133
|
+
if (!/^\./.test(importPath))
|
134
|
+
importPath = `./${importPath}`;
|
135
|
+
return importPath;
|
136
|
+
};
|
@@ -1,2 +1,2 @@
|
|
1
1
|
export * from './context';
|
2
|
-
export * from './imports';
|
2
|
+
export * from './imports';
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel } from 'case';
|
3
|
+
import { abstractClassDeclaration, arrowFunctionExpression, getMessageProperties } from '../utils';
|
4
|
+
import { createTypedObjectParams } from '../utils/types';
|
5
|
+
import { getWasmMethodArgs } from '../client/client';
|
6
|
+
export const createMessageBuilderClass = (context, className, msg) => {
|
7
|
+
const staticMethods = getMessageProperties(msg).map((schema) => {
|
8
|
+
return createStaticExecMethodMessageBuilder(context, schema, msg.title);
|
9
|
+
});
|
10
|
+
// const blockStmt = bindings;
|
11
|
+
return t.exportNamedDeclaration(abstractClassDeclaration(className, staticMethods, [], null));
|
12
|
+
};
|
13
|
+
/**
|
14
|
+
* CamelCasedProperties<Extract<ExecuteMsg, { exec_on_module: unknown }>['exec_on_module']>
|
15
|
+
*/
|
16
|
+
function createExtractTypeAnnotation(underscoreName, msgTitle) {
|
17
|
+
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CamelCasedProperties'), t.tsTypeParameterInstantiation([
|
18
|
+
t.tsIndexedAccessType(t.tsTypeReference(t.identifier('Extract'), t.tsTypeParameterInstantiation([
|
19
|
+
t.tsTypeReference(t.identifier(msgTitle)),
|
20
|
+
t.tsTypeLiteral([
|
21
|
+
t.tsPropertySignature(t.identifier(underscoreName), t.tsTypeAnnotation(t.tsUnknownKeyword()))
|
22
|
+
])
|
23
|
+
])), t.tsLiteralType(t.stringLiteral(underscoreName)))
|
24
|
+
])));
|
25
|
+
}
|
26
|
+
const createStaticExecMethodMessageBuilder = (context, jsonschema, msgTitle) => {
|
27
|
+
const underscoreName = Object.keys(jsonschema.properties)[0];
|
28
|
+
const methodName = camel(underscoreName);
|
29
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
30
|
+
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
31
|
+
// what the underscore named property in the message is assigned to
|
32
|
+
let actionValue;
|
33
|
+
if (param?.type === 'Identifier') {
|
34
|
+
actionValue = t.identifier(param.name);
|
35
|
+
}
|
36
|
+
else {
|
37
|
+
actionValue = t.tsAsExpression(t.objectExpression(args), t.tsTypeReference(t.identifier('const')));
|
38
|
+
}
|
39
|
+
// TODO: this is a hack to get the type annotation to work
|
40
|
+
// all type annotations in the future should be the extracted and camelized type
|
41
|
+
if (param &&
|
42
|
+
param.typeAnnotation.type === 'TSTypeAnnotation' &&
|
43
|
+
param.typeAnnotation.typeAnnotation.type === 'TSTypeLiteral') {
|
44
|
+
param.typeAnnotation = createExtractTypeAnnotation(underscoreName, msgTitle);
|
45
|
+
}
|
46
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(
|
47
|
+
// params
|
48
|
+
param
|
49
|
+
? [
|
50
|
+
// props
|
51
|
+
param
|
52
|
+
]
|
53
|
+
: [],
|
54
|
+
// body
|
55
|
+
t.blockStatement([
|
56
|
+
t.returnStatement(t.objectExpression([
|
57
|
+
t.objectProperty(t.identifier(underscoreName), actionValue)
|
58
|
+
]))
|
59
|
+
]),
|
60
|
+
// return type
|
61
|
+
t.tsTypeAnnotation(t.tsTypeReference(t.identifier(msgTitle))), false), null, null, false,
|
62
|
+
// static
|
63
|
+
true);
|
64
|
+
};
|
@@ -0,0 +1,105 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel } from 'case';
|
3
|
+
import { arrowFunctionExpression, bindMethod, classDeclaration, classProperty, getMessageProperties, OPTIONAL_FUNDS_PARAM, typedIdentifier } from '../utils';
|
4
|
+
import { createTypedObjectParams } from '../utils/types';
|
5
|
+
import { getWasmMethodArgs } from '../client/client';
|
6
|
+
const createWasmExecMethodMessageComposer = (context, jsonschema) => {
|
7
|
+
context.addUtil('Coin');
|
8
|
+
context.addUtil('MsgExecuteContractEncodeObject');
|
9
|
+
context.addUtil('MsgExecuteContract');
|
10
|
+
context.addUtil('toUtf8');
|
11
|
+
const underscoreName = Object.keys(jsonschema.properties)[0];
|
12
|
+
const methodName = camel(underscoreName);
|
13
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
14
|
+
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
15
|
+
// what the underscore named property in the message is assigned to
|
16
|
+
let actionValue;
|
17
|
+
if (param?.type === 'Identifier') {
|
18
|
+
actionValue = t.identifier(param.name);
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
actionValue = t.objectExpression(args);
|
22
|
+
}
|
23
|
+
const constantParams = [OPTIONAL_FUNDS_PARAM];
|
24
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param
|
25
|
+
? [
|
26
|
+
// props
|
27
|
+
param,
|
28
|
+
...constantParams
|
29
|
+
]
|
30
|
+
: constantParams, t.blockStatement([
|
31
|
+
t.returnStatement(t.objectExpression([
|
32
|
+
t.objectProperty(t.identifier('typeUrl'), t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')),
|
33
|
+
t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier('MsgExecuteContract'), t.identifier('fromPartial')), [
|
34
|
+
t.objectExpression([
|
35
|
+
t.objectProperty(t.identifier('sender'), t.memberExpression(t.thisExpression(), t.identifier('sender'))),
|
36
|
+
t.objectProperty(t.identifier('contract'), t.memberExpression(t.thisExpression(), t.identifier('contractAddress'))),
|
37
|
+
t.objectProperty(t.identifier('msg'), t.callExpression(t.identifier('toUtf8'), [
|
38
|
+
t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [
|
39
|
+
t.objectExpression([
|
40
|
+
t.objectProperty(t.identifier(underscoreName), actionValue)
|
41
|
+
])
|
42
|
+
])
|
43
|
+
])),
|
44
|
+
t.objectProperty(t.identifier('funds'), t.identifier('_funds'))
|
45
|
+
])
|
46
|
+
]))
|
47
|
+
]))
|
48
|
+
]),
|
49
|
+
// return type
|
50
|
+
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
|
51
|
+
};
|
52
|
+
export const createMessageComposerClass = (context, className, implementsClassName, execMsg) => {
|
53
|
+
const propertyNames = getMessageProperties(execMsg)
|
54
|
+
.map((method) => Object.keys(method.properties)?.[0])
|
55
|
+
.filter(Boolean);
|
56
|
+
const bindings = propertyNames.map(camel).map(bindMethod);
|
57
|
+
const methods = getMessageProperties(execMsg).map((schema) => {
|
58
|
+
return createWasmExecMethodMessageComposer(context, schema);
|
59
|
+
});
|
60
|
+
const blockStmt = [];
|
61
|
+
blockStmt.push(...[
|
62
|
+
t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('sender')), t.identifier('sender'))),
|
63
|
+
t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))),
|
64
|
+
...bindings
|
65
|
+
]);
|
66
|
+
return t.exportNamedDeclaration(classDeclaration(className, [
|
67
|
+
// sender
|
68
|
+
classProperty('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
|
69
|
+
// contractAddress
|
70
|
+
classProperty('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword())),
|
71
|
+
// constructor
|
72
|
+
t.classMethod('constructor', t.identifier('constructor'), [
|
73
|
+
typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())),
|
74
|
+
typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))
|
75
|
+
], t.blockStatement(blockStmt)),
|
76
|
+
...methods
|
77
|
+
], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))], null));
|
78
|
+
};
|
79
|
+
export const createMessageComposerInterface = (context, className, execMsg) => {
|
80
|
+
const methods = getMessageProperties(execMsg).map((jsonschema) => {
|
81
|
+
const underscoreName = Object.keys(jsonschema.properties)[0];
|
82
|
+
const methodName = camel(underscoreName);
|
83
|
+
return createPropertyFunctionWithObjectParamsForMessageComposer(context, methodName, 'MsgExecuteContractEncodeObject', jsonschema.properties[underscoreName]);
|
84
|
+
});
|
85
|
+
const extendsAst = [];
|
86
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([
|
87
|
+
// contract address
|
88
|
+
t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())),
|
89
|
+
// contract address
|
90
|
+
t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())),
|
91
|
+
...methods
|
92
|
+
])));
|
93
|
+
};
|
94
|
+
const createPropertyFunctionWithObjectParamsForMessageComposer = (context, methodName, responseType, jsonschema) => {
|
95
|
+
const obj = createTypedObjectParams(context, jsonschema);
|
96
|
+
const fixedParams = [OPTIONAL_FUNDS_PARAM];
|
97
|
+
const func = {
|
98
|
+
type: 'TSFunctionType',
|
99
|
+
typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier(responseType))),
|
100
|
+
parameters: obj ? [obj, ...fixedParams] : fixedParams
|
101
|
+
};
|
102
|
+
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(
|
103
|
+
// @ts-ignore:next-line
|
104
|
+
func));
|
105
|
+
};
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import * as t from "@babel/types";
|
2
|
+
import { camel, pascal } from "case";
|
3
|
+
import { PROVIDER_TYPES } from "../utils/constants";
|
4
|
+
import { identifier, tsObjectPattern } from "../utils";
|
5
|
+
export const createProvider = (name, providerInfos) => {
|
6
|
+
const classDeclaration = t.classDeclaration(t.identifier(name), t.identifier("ContractBase"), t.classBody([
|
7
|
+
t.classMethod("constructor", t.identifier("constructor"), [
|
8
|
+
tsObjectPattern([
|
9
|
+
t.objectProperty(t.identifier("address"), t.identifier("address"), false, true),
|
10
|
+
t.objectProperty(t.identifier("cosmWasmClient"), t.identifier("cosmWasmClient"), false, true),
|
11
|
+
t.objectProperty(t.identifier("signingCosmWasmClient"), t.identifier("signingCosmWasmClient"), false, true),
|
12
|
+
], t.tsTypeAnnotation(t.tsTypeReference(t.identifier("IContractConstructor")))),
|
13
|
+
], t.blockStatement([
|
14
|
+
t.expressionStatement(t.callExpression(t.super(), [
|
15
|
+
t.identifier("address"),
|
16
|
+
t.identifier("cosmWasmClient"),
|
17
|
+
t.identifier("signingCosmWasmClient"),
|
18
|
+
t.identifier(providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE]
|
19
|
+
? providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE].classname
|
20
|
+
: "undefined"),
|
21
|
+
t.identifier(providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE]
|
22
|
+
? providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE].classname
|
23
|
+
: "undefined"),
|
24
|
+
t.identifier(providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE]
|
25
|
+
? providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE]
|
26
|
+
.classname
|
27
|
+
: "undefined"),
|
28
|
+
])),
|
29
|
+
])),
|
30
|
+
]));
|
31
|
+
classDeclaration.superTypeParameters = t.tsTypeParameterInstantiation([
|
32
|
+
t.tsTypeReference(t.identifier(providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE]
|
33
|
+
? providerInfos[PROVIDER_TYPES.SIGNING_CLIENT_TYPE].classname
|
34
|
+
: "IEmptyClient")),
|
35
|
+
t.tsTypeReference(t.identifier(providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE]
|
36
|
+
? providerInfos[PROVIDER_TYPES.QUERY_CLIENT_TYPE].classname
|
37
|
+
: "IEmptyClient")),
|
38
|
+
t.tsTypeReference(t.identifier(providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE]
|
39
|
+
? providerInfos[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE].classname
|
40
|
+
: "IEmptyClient")),
|
41
|
+
]);
|
42
|
+
return t.exportNamedDeclaration(classDeclaration);
|
43
|
+
};
|
44
|
+
export const createIContractsContext = (providerInfos) => {
|
45
|
+
const properties = [];
|
46
|
+
for (const key in providerInfos) {
|
47
|
+
if (Object.prototype.hasOwnProperty.call(providerInfos, key)) {
|
48
|
+
const contractProviderInfo = providerInfos[key];
|
49
|
+
properties.push(createProperty(key, contractProviderInfo));
|
50
|
+
}
|
51
|
+
}
|
52
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier("IContractsContext"), null, null, t.tsInterfaceBody(properties)));
|
53
|
+
};
|
54
|
+
let PROVIDER_MAPPING = {};
|
55
|
+
PROVIDER_MAPPING[PROVIDER_TYPES.SIGNING_CLIENT_TYPE] = "ISigningClientProvider";
|
56
|
+
PROVIDER_MAPPING[PROVIDER_TYPES.QUERY_CLIENT_TYPE] = "IQueryClientProvider";
|
57
|
+
PROVIDER_MAPPING[PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE] =
|
58
|
+
"IMessageComposerProvider";
|
59
|
+
export const createProperty = (name, providerInfos) => {
|
60
|
+
let typeAnnotation = null;
|
61
|
+
const keys = Object.keys(providerInfos);
|
62
|
+
if (keys?.length == 1) {
|
63
|
+
const key = keys[0];
|
64
|
+
typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(PROVIDER_MAPPING[key]), t.tsTypeParameterInstantiation([
|
65
|
+
t.tsTypeReference(t.identifier(providerInfos[key].classname)),
|
66
|
+
])));
|
67
|
+
}
|
68
|
+
else {
|
69
|
+
const typeRefs = [];
|
70
|
+
for (const key of keys) {
|
71
|
+
typeRefs.push(t.tsTypeReference(t.identifier(PROVIDER_MAPPING[key]), t.tsTypeParameterInstantiation([
|
72
|
+
t.tsTypeReference(t.identifier(providerInfos[key].classname)),
|
73
|
+
])));
|
74
|
+
}
|
75
|
+
typeAnnotation = t.tsTypeAnnotation(t.tsIntersectionType(typeRefs));
|
76
|
+
}
|
77
|
+
return t.tsPropertySignature(t.identifier(camel(name)), typeAnnotation);
|
78
|
+
};
|
79
|
+
export const createGettingProviders = (providerInfos) => {
|
80
|
+
const properties = [];
|
81
|
+
for (const key of Object.keys(providerInfos)) {
|
82
|
+
properties.push(t.objectProperty(t.identifier(camel(key)), t.newExpression(t.identifier(pascal(key)), [
|
83
|
+
t.objectExpression([
|
84
|
+
t.objectProperty(t.identifier("address"), t.identifier("address"), false, true),
|
85
|
+
t.objectProperty(t.identifier("cosmWasmClient"), t.identifier("cosmWasmClient"), false, true),
|
86
|
+
t.objectProperty(t.identifier("signingCosmWasmClient"), t.identifier("signingCosmWasmClient"), false, true),
|
87
|
+
]),
|
88
|
+
])));
|
89
|
+
}
|
90
|
+
return t.exportNamedDeclaration(t.variableDeclaration("const", [
|
91
|
+
t.variableDeclarator(t.identifier("getProviders"), t.arrowFunctionExpression([
|
92
|
+
identifier("address?", t.tsTypeAnnotation(t.tsTypeReference(t.identifier("string")))),
|
93
|
+
identifier("cosmWasmClient?", t.tsTypeAnnotation(t.tsTypeReference(t.identifier("CosmWasmClient")))),
|
94
|
+
identifier("signingCosmWasmClient?", t.tsTypeAnnotation(t.tsTypeReference(t.identifier("SigningCosmWasmClient")))),
|
95
|
+
], t.objectExpression(properties))),
|
96
|
+
]));
|
97
|
+
};
|