wasm-ast-types 0.19.0 → 0.21.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/main/client/client.js +16 -7
- package/main/client/test/ts-client.issue-101.spec.js +27 -0
- package/main/client/test/ts-client.issue-103.test.js +48 -0
- package/main/message-composer/message-composer.js +13 -4
- package/main/message-composer/message-composer.spec.js +14 -4
- package/main/msg-builder/msg-builder.js +21 -7
- package/main/msg-builder/msg-builder.spec.js +7 -1
- package/main/react-query/react-query.spec.js +11 -0
- package/main/utils/types.js +10 -2
- package/module/client/client.js +18 -9
- package/module/client/test/ts-client.issue-101.spec.js +20 -0
- package/module/client/test/ts-client.issue-103.test.js +41 -0
- package/module/message-composer/message-composer.js +13 -4
- package/module/message-composer/message-composer.spec.js +16 -7
- package/module/msg-builder/msg-builder.js +26 -12
- package/module/msg-builder/msg-builder.spec.js +6 -1
- package/module/react-query/react-query.spec.js +10 -0
- package/module/utils/types.js +10 -3
- package/package.json +2 -2
- package/src/client/client.ts +29 -24
- package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +47 -0
- package/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap +79 -0
- package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +8 -8
- package/src/client/test/ts-client.issue-101.spec.ts +37 -0
- package/src/client/test/ts-client.issue-103.test.ts +106 -0
- package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +60 -0
- package/src/message-composer/message-composer.spec.ts +41 -20
- package/src/message-composer/message-composer.ts +13 -5
- package/src/msg-builder/__snapshots__/msg-builder.spec.ts.snap +21 -0
- package/src/msg-builder/msg-builder.spec.ts +9 -1
- package/src/msg-builder/msg-builder.ts +36 -24
- package/src/react-query/__snapshots__/react-query.spec.ts.snap +45 -0
- package/src/react-query/react-query.spec.ts +17 -1
- package/src/utils/types.ts +11 -4
package/main/client/client.js
CHANGED
@@ -34,10 +34,12 @@ var createWasmQueryMethod = function createWasmQueryMethod(context, jsonschema)
|
|
34
34
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
35
35
|
var methodName = (0, _case.camel)(underscoreName);
|
36
36
|
var responseType = (0, _types2.getResponseType)(context, underscoreName);
|
37
|
-
var
|
37
|
+
var param = (0, _types2.createTypedObjectParams)(context, jsonschema.properties[underscoreName]);
|
38
38
|
var args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
39
|
-
var
|
40
|
-
|
39
|
+
var msgAction = t.identifier(underscoreName); // If the param is an identifier, we can just use it as is
|
40
|
+
|
41
|
+
var msgActionValue = (param === null || param === void 0 ? void 0 : param.type) === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args);
|
42
|
+
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(param ? [param] : [], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('queryContractSmart')), [t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(msgAction, msgActionValue)])]))]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier(responseType))]))), true));
|
41
43
|
};
|
42
44
|
|
43
45
|
exports.createWasmQueryMethod = createWasmQueryMethod;
|
@@ -68,12 +70,16 @@ var getWasmMethodArgs = function getWasmMethodArgs(context, jsonschema) {
|
|
68
70
|
var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {}); // only 1 degree $ref-lookup
|
69
71
|
|
70
72
|
if (!keys.length && jsonschema.$ref) {
|
71
|
-
var obj = context.refLookup(jsonschema.$ref);
|
73
|
+
var obj = context.refLookup(jsonschema.$ref); // properties
|
72
74
|
|
73
75
|
if (obj) {
|
74
76
|
var _obj$properties;
|
75
77
|
|
76
78
|
keys = Object.keys((_obj$properties = obj.properties) !== null && _obj$properties !== void 0 ? _obj$properties : {});
|
79
|
+
} // tuple struct or otherwise, use the name of the reference
|
80
|
+
|
81
|
+
|
82
|
+
if (!keys.length && obj !== null && obj !== void 0 && obj.oneOf) {// TODO????? ADAIR
|
77
83
|
}
|
78
84
|
}
|
79
85
|
|
@@ -91,10 +97,13 @@ var createWasmExecMethod = function createWasmExecMethod(context, jsonschema) {
|
|
91
97
|
context.addUtil('Coin');
|
92
98
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
93
99
|
var methodName = (0, _case.camel)(underscoreName);
|
94
|
-
var
|
100
|
+
var param = (0, _types2.createTypedObjectParams)(context, jsonschema.properties[underscoreName]);
|
95
101
|
var args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
96
|
-
|
97
|
-
|
102
|
+
var msgAction = t.identifier(underscoreName); // If the param is an identifier, we can just use it as is
|
103
|
+
|
104
|
+
var msgActionValue = (param === null || param === void 0 ? void 0 : param.type) === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args);
|
105
|
+
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(param ? [// props
|
106
|
+
param].concat(CONSTANT_EXEC_PARAMS) : CONSTANT_EXEC_PARAMS, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(msgAction, msgActionValue)]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
|
98
107
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
99
108
|
};
|
100
109
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _client = require("../client");
|
6
|
+
|
7
|
+
var _testUtils = require("../../../test-utils");
|
8
|
+
|
9
|
+
var _ownership = _interopRequireDefault(require("../../../../../__fixtures__/basic/ownership.json"));
|
10
|
+
|
11
|
+
// it('query classes', () => {
|
12
|
+
// const ctx = makeContext(cosmos_msg_for__empty);
|
13
|
+
// expectCode(createQueryClass(
|
14
|
+
// ctx,
|
15
|
+
// 'SG721QueryClient',
|
16
|
+
// 'SG721ReadOnlyInstance',
|
17
|
+
// cosmos_msg_for__empty
|
18
|
+
// ))
|
19
|
+
// });
|
20
|
+
it('execute interfaces no extends', function () {
|
21
|
+
var ctx = (0, _testUtils.makeContext)(_ownership["default"]);
|
22
|
+
(0, _testUtils.expectCode)((0, _client.createExecuteInterface)(ctx, 'OwnershipInstance', null, _ownership["default"]));
|
23
|
+
});
|
24
|
+
it('ownership client with tuple', function () {
|
25
|
+
var ctx = (0, _testUtils.makeContext)(_ownership["default"]);
|
26
|
+
(0, _testUtils.expectCode)((0, _client.createExecuteClass)(ctx, 'OwnershipClient', 'OwnershipInstance', null, _ownership["default"]));
|
27
|
+
});
|
@@ -0,0 +1,48 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _schema = _interopRequireDefault(require("../../../../../__fixtures__/issues/103/schema.json"));
|
6
|
+
|
7
|
+
var _client = require("../client");
|
8
|
+
|
9
|
+
var _testUtils = require("../../../test-utils");
|
10
|
+
|
11
|
+
var queryMessage = _schema["default"].query;
|
12
|
+
var executeMessage = _schema["default"].execute;
|
13
|
+
var queryCtx = (0, _testUtils.makeContext)(queryMessage);
|
14
|
+
var executeCtx = (0, _testUtils.makeContext)(executeMessage);
|
15
|
+
describe('query', function () {
|
16
|
+
it('execute_msg_for__empty', function () {
|
17
|
+
(0, _testUtils.expectCode)((0, _client.createTypeInterface)(queryCtx, queryMessage));
|
18
|
+
});
|
19
|
+
it('query classes', function () {
|
20
|
+
(0, _testUtils.expectCode)((0, _client.createQueryClass)(queryCtx, 'QueryClient', 'ReadOnlyInstance', queryMessage));
|
21
|
+
});
|
22
|
+
it('query classes response', function () {
|
23
|
+
(0, _testUtils.expectCode)((0, _client.createTypeInterface)(queryCtx, _schema["default"].query));
|
24
|
+
});
|
25
|
+
it('execute classes array types', function () {
|
26
|
+
(0, _testUtils.expectCode)((0, _client.createExecuteClass)(queryCtx, 'Client', 'Instance', null, queryMessage));
|
27
|
+
});
|
28
|
+
it('execute interfaces no extends', function () {
|
29
|
+
(0, _testUtils.expectCode)((0, _client.createExecuteInterface)(queryCtx, 'SG721Instance', null, queryMessage));
|
30
|
+
});
|
31
|
+
});
|
32
|
+
describe('execute', function () {
|
33
|
+
it('execute_msg_for__empty', function () {
|
34
|
+
(0, _testUtils.expectCode)((0, _client.createTypeInterface)(executeCtx, executeMessage));
|
35
|
+
});
|
36
|
+
it('query classes', function () {
|
37
|
+
(0, _testUtils.expectCode)((0, _client.createQueryClass)(executeCtx, 'QueryClient', 'ReadOnlyInstance', executeMessage));
|
38
|
+
});
|
39
|
+
it('query classes response', function () {
|
40
|
+
(0, _testUtils.expectCode)((0, _client.createTypeInterface)(executeCtx, _schema["default"].query));
|
41
|
+
});
|
42
|
+
it('execute classes array types', function () {
|
43
|
+
(0, _testUtils.expectCode)((0, _client.createExecuteClass)(executeCtx, 'Client', 'Instance', null, executeMessage));
|
44
|
+
});
|
45
|
+
it('execute interfaces no extends', function () {
|
46
|
+
(0, _testUtils.expectCode)((0, _client.createExecuteInterface)(executeCtx, 'SG721Instance', null, executeMessage));
|
47
|
+
});
|
48
|
+
});
|
@@ -34,11 +34,20 @@ var createWasmExecMethodMessageComposer = function createWasmExecMethodMessageCo
|
|
34
34
|
context.addUtil('toUtf8');
|
35
35
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
36
36
|
var methodName = (0, _case.camel)(underscoreName);
|
37
|
-
var
|
38
|
-
var args = (0, _client.getWasmMethodArgs)(context, jsonschema.properties[underscoreName]);
|
37
|
+
var param = (0, _types2.createTypedObjectParams)(context, jsonschema.properties[underscoreName]);
|
38
|
+
var args = (0, _client.getWasmMethodArgs)(context, jsonschema.properties[underscoreName]); // what the underscore named property in the message is assigned to
|
39
|
+
|
40
|
+
var actionValue;
|
41
|
+
|
42
|
+
if ((param === null || param === void 0 ? void 0 : param.type) === 'Identifier') {
|
43
|
+
actionValue = t.identifier(param.name);
|
44
|
+
} else {
|
45
|
+
actionValue = t.objectExpression(args);
|
46
|
+
}
|
47
|
+
|
39
48
|
var constantParams = [(0, _babel.identifier)('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
|
40
|
-
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(
|
41
|
-
|
49
|
+
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(param ? [// props
|
50
|
+
param].concat(constantParams) : constantParams, t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier('typeUrl'), t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')), t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier('MsgExecuteContract'), t.identifier('fromPartial')), [t.objectExpression([t.objectProperty(t.identifier('sender'), t.memberExpression(t.thisExpression(), t.identifier('sender'))), t.objectProperty(t.identifier('contract'), t.memberExpression(t.thisExpression(), t.identifier('contractAddress'))), t.objectProperty(t.identifier('msg'), t.callExpression(t.identifier('toUtf8'), [t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.objectExpression([t.objectProperty(t.identifier(underscoreName), actionValue)])])])), t.objectProperty(t.identifier('funds'), t.identifier('funds'), false, true)])]))]))]), // return type
|
42
51
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
|
43
52
|
};
|
44
53
|
|
@@ -4,15 +4,25 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
4
4
|
|
5
5
|
var _execute_msg_for__empty = _interopRequireDefault(require("../../../../__fixtures__/basic/execute_msg_for__empty.json"));
|
6
6
|
|
7
|
+
var _ownership = _interopRequireDefault(require("../../../../__fixtures__/basic/ownership.json"));
|
8
|
+
|
7
9
|
var _messageComposer = require("./message-composer");
|
8
10
|
|
9
11
|
var _testUtils = require("../../test-utils");
|
10
12
|
|
11
|
-
it(
|
13
|
+
it("execute classes", function () {
|
12
14
|
var ctx = (0, _testUtils.makeContext)(_execute_msg_for__empty["default"]);
|
13
|
-
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerClass)(ctx,
|
15
|
+
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerClass)(ctx, "SG721MessageComposer", "SG721Message", _execute_msg_for__empty["default"]));
|
14
16
|
});
|
15
|
-
it(
|
17
|
+
it("createMessageComposerInterface", function () {
|
16
18
|
var ctx = (0, _testUtils.makeContext)(_execute_msg_for__empty["default"]);
|
17
|
-
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerInterface)(ctx,
|
19
|
+
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerInterface)(ctx, "SG721Message", _execute_msg_for__empty["default"]));
|
20
|
+
});
|
21
|
+
it("ownershipClass", function () {
|
22
|
+
var ctx = (0, _testUtils.makeContext)(_ownership["default"]);
|
23
|
+
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerClass)(ctx, "OwnershipMessageComposer", "OwnershipMessage", _ownership["default"]));
|
24
|
+
});
|
25
|
+
it("ownershipInterface", function () {
|
26
|
+
var ownershipCtx = (0, _testUtils.makeContext)(_ownership["default"]);
|
27
|
+
(0, _testUtils.expectCode)((0, _messageComposer.createMessageComposerInterface)(ownershipCtx, "OwnershipMessage", _ownership["default"]));
|
18
28
|
});
|
@@ -36,19 +36,33 @@ var createMsgBuilderClass = function createMsgBuilderClass(context, className, m
|
|
36
36
|
exports.createMsgBuilderClass = createMsgBuilderClass;
|
37
37
|
|
38
38
|
function createExtractTypeAnnotation(underscoreName, msgTitle) {
|
39
|
-
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier(
|
39
|
+
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CamelCasedProperties'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier('Extract'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(msgTitle)), t.tsTypeLiteral([t.tsPropertySignature(t.identifier(underscoreName), t.tsTypeAnnotation(t.tsUnknownKeyword()))])])), t.tsLiteralType(t.stringLiteral(underscoreName)))])));
|
40
40
|
}
|
41
41
|
|
42
42
|
var createStaticExecMethodMsgBuilder = function createStaticExecMethodMsgBuilder(context, jsonschema, msgTitle) {
|
43
43
|
var underscoreName = Object.keys(jsonschema.properties)[0];
|
44
44
|
var methodName = (0, _case.camel)(underscoreName);
|
45
|
-
var
|
46
|
-
var args = (0, _client.getWasmMethodArgs)(context, jsonschema.properties[underscoreName]);
|
47
|
-
|
45
|
+
var param = (0, _types2.createTypedObjectParams)(context, jsonschema.properties[underscoreName]);
|
46
|
+
var args = (0, _client.getWasmMethodArgs)(context, jsonschema.properties[underscoreName]); // what the underscore named property in the message is assigned to
|
47
|
+
|
48
|
+
var actionValue;
|
49
|
+
|
50
|
+
if ((param === null || param === void 0 ? void 0 : param.type) === 'Identifier') {
|
51
|
+
actionValue = t.identifier(param.name);
|
52
|
+
} else {
|
53
|
+
actionValue = t.tsAsExpression(t.objectExpression(args), t.tsTypeReference(t.identifier('const')));
|
54
|
+
} // TODO: this is a hack to get the type annotation to work
|
55
|
+
// all type annotations in the future should be the extracted and camelized type
|
56
|
+
|
57
|
+
|
58
|
+
if (param && param.typeAnnotation.type === 'TSTypeAnnotation' && param.typeAnnotation.typeAnnotation.type === 'TSTypeLiteral') {
|
59
|
+
param.typeAnnotation = createExtractTypeAnnotation(underscoreName, msgTitle);
|
60
|
+
}
|
61
|
+
|
48
62
|
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)( // params
|
49
|
-
|
50
|
-
|
51
|
-
t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier(underscoreName),
|
63
|
+
param ? [// props
|
64
|
+
param] : [], // body
|
65
|
+
t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier(underscoreName), actionValue)]))]), // return type
|
52
66
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier(msgTitle))), false), null, null, false, // static
|
53
67
|
true);
|
54
68
|
};
|
@@ -6,6 +6,8 @@ var _execute_msg_for__empty = _interopRequireDefault(require("../../../../__fixt
|
|
6
6
|
|
7
7
|
var _query_msg = _interopRequireDefault(require("../../../../__fixtures__/basic/query_msg.json"));
|
8
8
|
|
9
|
+
var _ownership = _interopRequireDefault(require("../../../../__fixtures__/basic/ownership.json"));
|
10
|
+
|
9
11
|
var _msgBuilder = require("./msg-builder");
|
10
12
|
|
11
13
|
var _testUtils = require("../../test-utils");
|
@@ -15,6 +17,10 @@ it('execute class', function () {
|
|
15
17
|
(0, _testUtils.expectCode)((0, _msgBuilder.createMsgBuilderClass)(ctx, 'SG721MsgBuilder', _execute_msg_for__empty["default"]));
|
16
18
|
});
|
17
19
|
it('query class', function () {
|
18
|
-
var ctx = (0, _testUtils.makeContext)(
|
20
|
+
var ctx = (0, _testUtils.makeContext)(_query_msg["default"]);
|
19
21
|
(0, _testUtils.expectCode)((0, _msgBuilder.createMsgBuilderClass)(ctx, 'SG721MsgBuilder', _query_msg["default"]));
|
22
|
+
});
|
23
|
+
it('ownership', function () {
|
24
|
+
var ctx = (0, _testUtils.makeContext)(_ownership["default"]);
|
25
|
+
(0, _testUtils.expectCode)((0, _msgBuilder.createMsgBuilderClass)(ctx, 'Ownership', _ownership["default"]));
|
20
26
|
});
|
@@ -10,6 +10,8 @@ var _query_msg = _interopRequireDefault(require("../../../../__fixtures__/basic/
|
|
10
10
|
|
11
11
|
var _execute_msg_for__empty = _interopRequireDefault(require("../../../../__fixtures__/basic/execute_msg_for__empty.json"));
|
12
12
|
|
13
|
+
var _ownership = _interopRequireDefault(require("../../../../__fixtures__/basic/ownership.json"));
|
14
|
+
|
13
15
|
var _reactQuery = require("./react-query");
|
14
16
|
|
15
17
|
var _testUtils = require("../../test-utils");
|
@@ -77,4 +79,13 @@ it('createReactQueryHooks', function () {
|
|
77
79
|
contractName: 'Sg721',
|
78
80
|
ExecuteClient: 'Sg721Client'
|
79
81
|
})));
|
82
|
+
});
|
83
|
+
it('ownership', function () {
|
84
|
+
var ownershipCtx = (0, _testUtils.makeContext)(_ownership["default"]);
|
85
|
+
(0, _testUtils.expectCode)(t.program((0, _reactQuery.createReactQueryMutationHooks)({
|
86
|
+
context: ownershipCtx,
|
87
|
+
execMsg: _ownership["default"],
|
88
|
+
contractName: 'Ownership',
|
89
|
+
ExecuteClient: 'OwnershipClient'
|
90
|
+
})));
|
80
91
|
});
|
package/main/utils/types.js
CHANGED
@@ -400,9 +400,17 @@ var createTypedObjectParams = function createTypedObjectParams(context, jsonsche
|
|
400
400
|
if (!keys.length) {
|
401
401
|
// is there a ref?
|
402
402
|
if (jsonschema.$ref) {
|
403
|
-
var _obj = context.refLookup(jsonschema.$ref);
|
403
|
+
var _obj = context.refLookup(jsonschema.$ref); // If there is a oneOf, then we need to create a type for it
|
404
404
|
|
405
|
-
|
405
|
+
|
406
|
+
if (_obj !== null && _obj !== void 0 && _obj.oneOf) {
|
407
|
+
// the actual type of the ref
|
408
|
+
var refType = jsonschema.$ref.split('/').pop();
|
409
|
+
var refName = (0, _case.camel)(refType);
|
410
|
+
var id = t.identifier(refName);
|
411
|
+
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
|
412
|
+
return id;
|
413
|
+
} else if (_obj) {
|
406
414
|
return createTypedObjectParams(context, _obj, camelize);
|
407
415
|
}
|
408
416
|
} // no results...
|
package/module/client/client.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { camel } from 'case';
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import { arrowFunctionExpression, bindMethod, classDeclaration, classProperty, getMessageProperties, promiseTypeAnnotation, typedIdentifier } from '../utils';
|
4
|
+
import { createTypedObjectParams, getPropertyType, getResponseType, getType } from '../utils/types';
|
5
5
|
import { identifier, propertySignature } from '../utils/babel';
|
6
6
|
export const CONSTANT_EXEC_PARAMS = [t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
|
7
7
|
export const FIXED_EXECUTE_PARAMS = [identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
|
@@ -9,10 +9,12 @@ export const createWasmQueryMethod = (context, jsonschema) => {
|
|
9
9
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
10
10
|
const methodName = camel(underscoreName);
|
11
11
|
const responseType = getResponseType(context, underscoreName);
|
12
|
-
const
|
12
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
13
13
|
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
14
|
-
const
|
15
|
-
|
14
|
+
const msgAction = t.identifier(underscoreName); // If the param is an identifier, we can just use it as is
|
15
|
+
|
16
|
+
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args);
|
17
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param ? [param] : [], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('queryContractSmart')), [t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(msgAction, msgActionValue)])]))]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier(responseType))]))), true));
|
16
18
|
};
|
17
19
|
export const createQueryClass = (context, className, implementsClassName, queryMsg) => {
|
18
20
|
context.addUtil('CosmWasmClient');
|
@@ -31,10 +33,14 @@ export const getWasmMethodArgs = (context, jsonschema) => {
|
|
31
33
|
let keys = Object.keys(jsonschema.properties ?? {}); // only 1 degree $ref-lookup
|
32
34
|
|
33
35
|
if (!keys.length && jsonschema.$ref) {
|
34
|
-
const obj = context.refLookup(jsonschema.$ref);
|
36
|
+
const obj = context.refLookup(jsonschema.$ref); // properties
|
35
37
|
|
36
38
|
if (obj) {
|
37
39
|
keys = Object.keys(obj.properties ?? {});
|
40
|
+
} // tuple struct or otherwise, use the name of the reference
|
41
|
+
|
42
|
+
|
43
|
+
if (!keys.length && obj?.oneOf) {// TODO????? ADAIR
|
38
44
|
}
|
39
45
|
}
|
40
46
|
|
@@ -49,10 +55,13 @@ export const createWasmExecMethod = (context, jsonschema) => {
|
|
49
55
|
context.addUtil('Coin');
|
50
56
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
51
57
|
const methodName = camel(underscoreName);
|
52
|
-
const
|
58
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
53
59
|
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
54
|
-
|
55
|
-
|
60
|
+
const msgAction = t.identifier(underscoreName); // If the param is an identifier, we can just use it as is
|
61
|
+
|
62
|
+
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args);
|
63
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param ? [// props
|
64
|
+
param, ...CONSTANT_EXEC_PARAMS] : CONSTANT_EXEC_PARAMS, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(msgAction, msgActionValue)]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
|
56
65
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
57
66
|
};
|
58
67
|
export const createExecuteClass = (context, className, implementsClassName, extendsClassName, execMsg) => {
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { createExecuteClass, createExecuteInterface } from '../client';
|
2
|
+
import { expectCode, makeContext } from '../../../test-utils';
|
3
|
+
import ownership from '../../../../../__fixtures__/basic/ownership.json'; // it('query classes', () => {
|
4
|
+
// const ctx = makeContext(cosmos_msg_for__empty);
|
5
|
+
// expectCode(createQueryClass(
|
6
|
+
// ctx,
|
7
|
+
// 'SG721QueryClient',
|
8
|
+
// 'SG721ReadOnlyInstance',
|
9
|
+
// cosmos_msg_for__empty
|
10
|
+
// ))
|
11
|
+
// });
|
12
|
+
|
13
|
+
it('execute interfaces no extends', () => {
|
14
|
+
const ctx = makeContext(ownership);
|
15
|
+
expectCode(createExecuteInterface(ctx, 'OwnershipInstance', null, ownership));
|
16
|
+
});
|
17
|
+
it('ownership client with tuple', () => {
|
18
|
+
const ctx = makeContext(ownership);
|
19
|
+
expectCode(createExecuteClass(ctx, 'OwnershipClient', 'OwnershipInstance', null, ownership));
|
20
|
+
});
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import contract from '../../../../../__fixtures__/issues/103/schema.json';
|
2
|
+
import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
|
3
|
+
import { expectCode, makeContext } from '../../../test-utils';
|
4
|
+
const queryMessage = contract.query;
|
5
|
+
const executeMessage = contract.execute;
|
6
|
+
const queryCtx = makeContext(queryMessage);
|
7
|
+
const executeCtx = makeContext(executeMessage);
|
8
|
+
describe('query', () => {
|
9
|
+
it('execute_msg_for__empty', () => {
|
10
|
+
expectCode(createTypeInterface(queryCtx, queryMessage));
|
11
|
+
});
|
12
|
+
it('query classes', () => {
|
13
|
+
expectCode(createQueryClass(queryCtx, 'QueryClient', 'ReadOnlyInstance', queryMessage));
|
14
|
+
});
|
15
|
+
it('query classes response', () => {
|
16
|
+
expectCode(createTypeInterface(queryCtx, contract.query));
|
17
|
+
});
|
18
|
+
it('execute classes array types', () => {
|
19
|
+
expectCode(createExecuteClass(queryCtx, 'Client', 'Instance', null, queryMessage));
|
20
|
+
});
|
21
|
+
it('execute interfaces no extends', () => {
|
22
|
+
expectCode(createExecuteInterface(queryCtx, 'SG721Instance', null, queryMessage));
|
23
|
+
});
|
24
|
+
});
|
25
|
+
describe('execute', () => {
|
26
|
+
it('execute_msg_for__empty', () => {
|
27
|
+
expectCode(createTypeInterface(executeCtx, executeMessage));
|
28
|
+
});
|
29
|
+
it('query classes', () => {
|
30
|
+
expectCode(createQueryClass(executeCtx, 'QueryClient', 'ReadOnlyInstance', executeMessage));
|
31
|
+
});
|
32
|
+
it('query classes response', () => {
|
33
|
+
expectCode(createTypeInterface(executeCtx, contract.query));
|
34
|
+
});
|
35
|
+
it('execute classes array types', () => {
|
36
|
+
expectCode(createExecuteClass(executeCtx, 'Client', 'Instance', null, executeMessage));
|
37
|
+
});
|
38
|
+
it('execute interfaces no extends', () => {
|
39
|
+
expectCode(createExecuteInterface(executeCtx, 'SG721Instance', null, executeMessage));
|
40
|
+
});
|
41
|
+
});
|
@@ -12,11 +12,20 @@ const createWasmExecMethodMessageComposer = (context, jsonschema) => {
|
|
12
12
|
context.addUtil('toUtf8');
|
13
13
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
14
14
|
const methodName = camel(underscoreName);
|
15
|
-
const
|
16
|
-
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
15
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
16
|
+
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]); // what the underscore named property in the message is assigned to
|
17
|
+
|
18
|
+
let actionValue;
|
19
|
+
|
20
|
+
if (param?.type === 'Identifier') {
|
21
|
+
actionValue = t.identifier(param.name);
|
22
|
+
} else {
|
23
|
+
actionValue = t.objectExpression(args);
|
24
|
+
}
|
25
|
+
|
17
26
|
const constantParams = [identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
|
18
|
-
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(
|
19
|
-
|
27
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param ? [// props
|
28
|
+
param, ...constantParams] : constantParams, t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier('typeUrl'), t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')), t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier('MsgExecuteContract'), t.identifier('fromPartial')), [t.objectExpression([t.objectProperty(t.identifier('sender'), t.memberExpression(t.thisExpression(), t.identifier('sender'))), t.objectProperty(t.identifier('contract'), t.memberExpression(t.thisExpression(), t.identifier('contractAddress'))), t.objectProperty(t.identifier('msg'), t.callExpression(t.identifier('toUtf8'), [t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.objectExpression([t.objectProperty(t.identifier(underscoreName), actionValue)])])])), t.objectProperty(t.identifier('funds'), t.identifier('funds'), false, true)])]))]))]), // return type
|
20
29
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
|
21
30
|
};
|
22
31
|
|
@@ -1,11 +1,20 @@
|
|
1
|
-
import execute_msg from
|
2
|
-
import
|
3
|
-
import {
|
4
|
-
|
1
|
+
import execute_msg from "../../../../__fixtures__/basic/execute_msg_for__empty.json";
|
2
|
+
import ownership from "../../../../__fixtures__/basic/ownership.json";
|
3
|
+
import { createMessageComposerClass, createMessageComposerInterface } from "./message-composer";
|
4
|
+
import { expectCode, makeContext } from "../../test-utils";
|
5
|
+
it("execute classes", () => {
|
5
6
|
const ctx = makeContext(execute_msg);
|
6
|
-
expectCode(createMessageComposerClass(ctx,
|
7
|
+
expectCode(createMessageComposerClass(ctx, "SG721MessageComposer", "SG721Message", execute_msg));
|
7
8
|
});
|
8
|
-
it(
|
9
|
+
it("createMessageComposerInterface", () => {
|
9
10
|
const ctx = makeContext(execute_msg);
|
10
|
-
expectCode(createMessageComposerInterface(ctx,
|
11
|
+
expectCode(createMessageComposerInterface(ctx, "SG721Message", execute_msg));
|
12
|
+
});
|
13
|
+
it("ownershipClass", () => {
|
14
|
+
const ctx = makeContext(ownership);
|
15
|
+
expectCode(createMessageComposerClass(ctx, "OwnershipMessageComposer", "OwnershipMessage", ownership));
|
16
|
+
});
|
17
|
+
it("ownershipInterface", () => {
|
18
|
+
const ownershipCtx = makeContext(ownership);
|
19
|
+
expectCode(createMessageComposerInterface(ownershipCtx, "OwnershipMessage", ownership));
|
11
20
|
});
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import * as t from
|
2
|
-
import { camel } from
|
3
|
-
import { abstractClassDeclaration, arrowFunctionExpression, getMessageProperties } from
|
4
|
-
import { createTypedObjectParams } from
|
5
|
-
import { getWasmMethodArgs } from
|
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
6
|
export const createMsgBuilderClass = (context, className, msg) => {
|
7
7
|
const staticMethods = getMessageProperties(msg).map(schema => {
|
8
8
|
return createStaticExecMethodMsgBuilder(context, schema, msg.title);
|
@@ -15,19 +15,33 @@ export const createMsgBuilderClass = (context, className, msg) => {
|
|
15
15
|
*/
|
16
16
|
|
17
17
|
function createExtractTypeAnnotation(underscoreName, msgTitle) {
|
18
|
-
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier(
|
18
|
+
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CamelCasedProperties'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier('Extract'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(msgTitle)), t.tsTypeLiteral([t.tsPropertySignature(t.identifier(underscoreName), t.tsTypeAnnotation(t.tsUnknownKeyword()))])])), t.tsLiteralType(t.stringLiteral(underscoreName)))])));
|
19
19
|
}
|
20
20
|
|
21
21
|
const createStaticExecMethodMsgBuilder = (context, jsonschema, msgTitle) => {
|
22
22
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
23
23
|
const methodName = camel(underscoreName);
|
24
|
-
const
|
25
|
-
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
26
|
-
|
24
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
25
|
+
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]); // what the underscore named property in the message is assigned to
|
26
|
+
|
27
|
+
let actionValue;
|
28
|
+
|
29
|
+
if (param?.type === 'Identifier') {
|
30
|
+
actionValue = t.identifier(param.name);
|
31
|
+
} else {
|
32
|
+
actionValue = t.tsAsExpression(t.objectExpression(args), t.tsTypeReference(t.identifier('const')));
|
33
|
+
} // TODO: this is a hack to get the type annotation to work
|
34
|
+
// all type annotations in the future should be the extracted and camelized type
|
35
|
+
|
36
|
+
|
37
|
+
if (param && param.typeAnnotation.type === 'TSTypeAnnotation' && param.typeAnnotation.typeAnnotation.type === 'TSTypeLiteral') {
|
38
|
+
param.typeAnnotation = createExtractTypeAnnotation(underscoreName, msgTitle);
|
39
|
+
}
|
40
|
+
|
27
41
|
return t.classProperty(t.identifier(methodName), arrowFunctionExpression( // params
|
28
|
-
|
29
|
-
|
30
|
-
t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier(underscoreName),
|
42
|
+
param ? [// props
|
43
|
+
param] : [], // body
|
44
|
+
t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier(underscoreName), actionValue)]))]), // return type
|
31
45
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier(msgTitle))), false), null, null, false, // static
|
32
46
|
true);
|
33
47
|
};
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
|
2
2
|
import query_msg from '../../../../__fixtures__/basic/query_msg.json';
|
3
|
+
import ownership from '../../../../__fixtures__/basic/ownership.json';
|
3
4
|
import { createMsgBuilderClass } from './msg-builder';
|
4
5
|
import { expectCode, makeContext } from '../../test-utils';
|
5
6
|
it('execute class', () => {
|
@@ -7,6 +8,10 @@ it('execute class', () => {
|
|
7
8
|
expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', execute_msg));
|
8
9
|
});
|
9
10
|
it('query class', () => {
|
10
|
-
const ctx = makeContext(
|
11
|
+
const ctx = makeContext(query_msg);
|
11
12
|
expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', query_msg));
|
13
|
+
});
|
14
|
+
it('ownership', () => {
|
15
|
+
const ctx = makeContext(ownership);
|
16
|
+
expectCode(createMsgBuilderClass(ctx, 'Ownership', ownership));
|
12
17
|
});
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import query_msg from '../../../../__fixtures__/basic/query_msg.json';
|
3
3
|
import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
|
4
|
+
import ownership from '../../../../__fixtures__/basic/ownership.json';
|
4
5
|
import { createReactQueryHooks, createReactQueryMutationHooks } from './react-query';
|
5
6
|
import { expectCode, makeContext } from '../../test-utils';
|
6
7
|
const execCtx = makeContext(execute_msg);
|
@@ -62,4 +63,13 @@ it('createReactQueryHooks', () => {
|
|
62
63
|
contractName: 'Sg721',
|
63
64
|
ExecuteClient: 'Sg721Client'
|
64
65
|
})));
|
66
|
+
});
|
67
|
+
it('ownership', () => {
|
68
|
+
const ownershipCtx = makeContext(ownership);
|
69
|
+
expectCode(t.program(createReactQueryMutationHooks({
|
70
|
+
context: ownershipCtx,
|
71
|
+
execMsg: ownership,
|
72
|
+
contractName: 'Ownership',
|
73
|
+
ExecuteClient: 'OwnershipClient'
|
74
|
+
})));
|
65
75
|
});
|
package/module/utils/types.js
CHANGED
@@ -322,9 +322,16 @@ export const createTypedObjectParams = (context, jsonschema, camelize = true) =>
|
|
322
322
|
if (!keys.length) {
|
323
323
|
// is there a ref?
|
324
324
|
if (jsonschema.$ref) {
|
325
|
-
const obj = context.refLookup(jsonschema.$ref);
|
326
|
-
|
327
|
-
if (obj) {
|
325
|
+
const obj = context.refLookup(jsonschema.$ref); // If there is a oneOf, then we need to create a type for it
|
326
|
+
|
327
|
+
if (obj?.oneOf) {
|
328
|
+
// the actual type of the ref
|
329
|
+
const refType = jsonschema.$ref.split('/').pop();
|
330
|
+
const refName = camel(refType);
|
331
|
+
const id = t.identifier(refName);
|
332
|
+
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
|
333
|
+
return id;
|
334
|
+
} else if (obj) {
|
328
335
|
return createTypedObjectParams(context, obj, camelize);
|
329
336
|
}
|
330
337
|
} // no results...
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "wasm-ast-types",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.21.0",
|
4
4
|
"description": "CosmWasm TypeScript AST generation",
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
6
6
|
"homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
|
@@ -88,5 +88,5 @@
|
|
88
88
|
"case": "1.6.3",
|
89
89
|
"deepmerge": "4.2.2"
|
90
90
|
},
|
91
|
-
"gitHead": "
|
91
|
+
"gitHead": "90fb688d19f477d246f9021ac91d824c96b8f3fc"
|
92
92
|
}
|