wasm-ast-types 0.19.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- package/main/client/client.js +16 -7
- package/main/client/test/ts-client.issue-101.spec.js +27 -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/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.vectis.spec.ts.snap +8 -8
- package/src/client/test/ts-client.issue-101.spec.ts +37 -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
|
+
});
|
@@ -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
|
+
});
|
@@ -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.20.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": "d2edba320c63e2db5fe2db0bfb1f4cd5e44f2188"
|
92
92
|
}
|
package/src/client/client.ts
CHANGED
@@ -1,23 +1,19 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
|
-
import { camel
|
2
|
+
import { camel } from 'case';
|
3
3
|
import {
|
4
|
+
arrowFunctionExpression,
|
4
5
|
bindMethod,
|
5
|
-
typedIdentifier,
|
6
|
-
promiseTypeAnnotation,
|
7
6
|
classDeclaration,
|
8
7
|
classProperty,
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
getMessageProperties,
|
9
|
+
promiseTypeAnnotation,
|
10
|
+
typedIdentifier
|
11
|
+
} from '../utils';
|
12
12
|
|
13
|
-
import {
|
14
|
-
QueryMsg,
|
15
|
-
ExecuteMsg
|
16
|
-
} from '../types';
|
13
|
+
import { ExecuteMsg, JSONSchema, QueryMsg } from '../types';
|
17
14
|
|
18
|
-
import {
|
15
|
+
import { createTypedObjectParams, getPropertyType, getResponseType, getType } from '../utils/types';
|
19
16
|
import { RenderContext } from '../context';
|
20
|
-
import { JSONSchema } from '../types';
|
21
17
|
import { identifier, propertySignature } from '../utils/babel';
|
22
18
|
|
23
19
|
export const CONSTANT_EXEC_PARAMS = [
|
@@ -89,7 +85,7 @@ export const createWasmQueryMethod = (
|
|
89
85
|
const methodName = camel(underscoreName);
|
90
86
|
const responseType = getResponseType(context, underscoreName);
|
91
87
|
|
92
|
-
const
|
88
|
+
const param = createTypedObjectParams(
|
93
89
|
context,
|
94
90
|
jsonschema.properties[underscoreName]
|
95
91
|
);
|
@@ -99,13 +95,14 @@ export const createWasmQueryMethod = (
|
|
99
95
|
jsonschema.properties[underscoreName]
|
100
96
|
);
|
101
97
|
|
102
|
-
const
|
103
|
-
|
98
|
+
const msgAction = t.identifier(underscoreName);
|
99
|
+
// If the param is an identifier, we can just use it as is
|
100
|
+
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)
|
104
101
|
|
105
102
|
return t.classProperty(
|
106
103
|
t.identifier(methodName),
|
107
104
|
arrowFunctionExpression(
|
108
|
-
|
105
|
+
param ? [param] : [],
|
109
106
|
t.blockStatement(
|
110
107
|
[
|
111
108
|
t.returnStatement(
|
@@ -120,7 +117,7 @@ export const createWasmQueryMethod = (
|
|
120
117
|
[
|
121
118
|
t.memberExpression(t.thisExpression(), t.identifier('contractAddress')),
|
122
119
|
t.objectExpression([
|
123
|
-
|
120
|
+
t.objectProperty(msgAction, msgActionValue)
|
124
121
|
])
|
125
122
|
]
|
126
123
|
)
|
@@ -237,9 +234,15 @@ export const getWasmMethodArgs = (
|
|
237
234
|
// only 1 degree $ref-lookup
|
238
235
|
if (!keys.length && jsonschema.$ref) {
|
239
236
|
const obj = context.refLookup(jsonschema.$ref);
|
237
|
+
// properties
|
240
238
|
if (obj) {
|
241
239
|
keys = Object.keys(obj.properties ?? {})
|
242
240
|
}
|
241
|
+
|
242
|
+
// tuple struct or otherwise, use the name of the reference
|
243
|
+
if (!keys.length && obj?.oneOf) {
|
244
|
+
// TODO????? ADAIR
|
245
|
+
}
|
243
246
|
}
|
244
247
|
|
245
248
|
const args = keys.map(prop => {
|
@@ -265,7 +268,7 @@ export const createWasmExecMethod = (
|
|
265
268
|
|
266
269
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
267
270
|
const methodName = camel(underscoreName);
|
268
|
-
const
|
271
|
+
const param = createTypedObjectParams(
|
269
272
|
context,
|
270
273
|
jsonschema.properties[underscoreName]
|
271
274
|
);
|
@@ -274,12 +277,16 @@ export const createWasmExecMethod = (
|
|
274
277
|
jsonschema.properties[underscoreName]
|
275
278
|
);
|
276
279
|
|
280
|
+
const msgAction = t.identifier(underscoreName);
|
281
|
+
// If the param is an identifier, we can just use it as is
|
282
|
+
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)
|
283
|
+
|
277
284
|
return t.classProperty(
|
278
285
|
t.identifier(methodName),
|
279
286
|
arrowFunctionExpression(
|
280
|
-
|
287
|
+
param ? [
|
281
288
|
// props
|
282
|
-
|
289
|
+
param,
|
283
290
|
...CONSTANT_EXEC_PARAMS
|
284
291
|
] : CONSTANT_EXEC_PARAMS,
|
285
292
|
t.blockStatement(
|
@@ -306,10 +313,8 @@ export const createWasmExecMethod = (
|
|
306
313
|
t.objectExpression(
|
307
314
|
[
|
308
315
|
t.objectProperty(
|
309
|
-
|
310
|
-
|
311
|
-
...args
|
312
|
-
])
|
316
|
+
msgAction,
|
317
|
+
msgActionValue
|
313
318
|
)
|
314
319
|
|
315
320
|
]
|
@@ -0,0 +1,47 @@
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
2
|
+
|
3
|
+
exports[`execute interfaces no extends 1`] = `
|
4
|
+
"export interface OwnershipInstance {
|
5
|
+
contractAddress: string;
|
6
|
+
sender: string;
|
7
|
+
setFactory: ({
|
8
|
+
newFactory
|
9
|
+
}: {
|
10
|
+
newFactory: string;
|
11
|
+
}, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
|
12
|
+
updateOwnership: (action: Action, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
|
13
|
+
}"
|
14
|
+
`;
|
15
|
+
|
16
|
+
exports[`ownership client with tuple 1`] = `
|
17
|
+
"export class OwnershipClient implements OwnershipInstance {
|
18
|
+
client: SigningCosmWasmClient;
|
19
|
+
sender: string;
|
20
|
+
contractAddress: string;
|
21
|
+
|
22
|
+
constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) {
|
23
|
+
this.client = client;
|
24
|
+
this.sender = sender;
|
25
|
+
this.contractAddress = contractAddress;
|
26
|
+
this.setFactory = this.setFactory.bind(this);
|
27
|
+
this.updateOwnership = this.updateOwnership.bind(this);
|
28
|
+
}
|
29
|
+
|
30
|
+
setFactory = async ({
|
31
|
+
newFactory
|
32
|
+
}: {
|
33
|
+
newFactory: string;
|
34
|
+
}, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
|
35
|
+
return await this.client.execute(this.sender, this.contractAddress, {
|
36
|
+
set_factory: {
|
37
|
+
new_factory: newFactory
|
38
|
+
}
|
39
|
+
}, fee, memo, funds);
|
40
|
+
};
|
41
|
+
updateOwnership = async (action: Action, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
|
42
|
+
return await this.client.execute(this.sender, this.contractAddress, {
|
43
|
+
update_ownership: action
|
44
|
+
}, fee, memo, funds);
|
45
|
+
};
|
46
|
+
}"
|
47
|
+
`;
|
@@ -193,9 +193,9 @@ exports[`query classes 1`] = `
|
|
193
193
|
this.wasm = this.wasm.bind(this);
|
194
194
|
}
|
195
195
|
|
196
|
-
bank = async (): Promise<BankResponse> => {
|
196
|
+
bank = async (bankMsg: BankMsg): Promise<BankResponse> => {
|
197
197
|
return this.client.queryContractSmart(this.contractAddress, {
|
198
|
-
bank:
|
198
|
+
bank: bankMsg
|
199
199
|
});
|
200
200
|
};
|
201
201
|
custom = async (): Promise<CustomResponse> => {
|
@@ -203,19 +203,19 @@ exports[`query classes 1`] = `
|
|
203
203
|
custom: {}
|
204
204
|
});
|
205
205
|
};
|
206
|
-
staking = async (): Promise<StakingResponse> => {
|
206
|
+
staking = async (stakingMsg: StakingMsg): Promise<StakingResponse> => {
|
207
207
|
return this.client.queryContractSmart(this.contractAddress, {
|
208
|
-
staking:
|
208
|
+
staking: stakingMsg
|
209
209
|
});
|
210
210
|
};
|
211
|
-
distribution = async (): Promise<DistributionResponse> => {
|
211
|
+
distribution = async (distributionMsg: DistributionMsg): Promise<DistributionResponse> => {
|
212
212
|
return this.client.queryContractSmart(this.contractAddress, {
|
213
|
-
distribution:
|
213
|
+
distribution: distributionMsg
|
214
214
|
});
|
215
215
|
};
|
216
|
-
wasm = async (): Promise<WasmResponse> => {
|
216
|
+
wasm = async (wasmMsg: WasmMsg): Promise<WasmResponse> => {
|
217
217
|
return this.client.queryContractSmart(this.contractAddress, {
|
218
|
-
wasm:
|
218
|
+
wasm: wasmMsg
|
219
219
|
});
|
220
220
|
};
|
221
221
|
}"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { createExecuteClass, createExecuteInterface } from '../client';
|
2
|
+
import { expectCode, makeContext } from '../../../test-utils';
|
3
|
+
import ownership from '../../../../../__fixtures__/basic/ownership.json';
|
4
|
+
|
5
|
+
|
6
|
+
// it('query classes', () => {
|
7
|
+
// const ctx = makeContext(cosmos_msg_for__empty);
|
8
|
+
// expectCode(createQueryClass(
|
9
|
+
// ctx,
|
10
|
+
// 'SG721QueryClient',
|
11
|
+
// 'SG721ReadOnlyInstance',
|
12
|
+
// cosmos_msg_for__empty
|
13
|
+
// ))
|
14
|
+
// });
|
15
|
+
|
16
|
+
it('execute interfaces no extends', () => {
|
17
|
+
const ctx = makeContext(ownership);
|
18
|
+
expectCode(createExecuteInterface(
|
19
|
+
ctx,
|
20
|
+
'OwnershipInstance',
|
21
|
+
null,
|
22
|
+
ownership
|
23
|
+
))
|
24
|
+
});
|
25
|
+
|
26
|
+
it('ownership client with tuple', () => {
|
27
|
+
const ctx = makeContext(ownership);
|
28
|
+
expectCode(createExecuteClass(
|
29
|
+
ctx,
|
30
|
+
'OwnershipClient',
|
31
|
+
'OwnershipInstance',
|
32
|
+
null,
|
33
|
+
ownership
|
34
|
+
))
|
35
|
+
});
|
36
|
+
|
37
|
+
|
@@ -269,3 +269,63 @@ exports[`execute classes 1`] = `
|
|
269
269
|
};
|
270
270
|
}"
|
271
271
|
`;
|
272
|
+
|
273
|
+
exports[`ownershipClass 1`] = `
|
274
|
+
"export class OwnershipMessageComposer implements OwnershipMessage {
|
275
|
+
sender: string;
|
276
|
+
contractAddress: string;
|
277
|
+
|
278
|
+
constructor(sender: string, contractAddress: string) {
|
279
|
+
this.sender = sender;
|
280
|
+
this.contractAddress = contractAddress;
|
281
|
+
this.setFactory = this.setFactory.bind(this);
|
282
|
+
this.updateOwnership = this.updateOwnership.bind(this);
|
283
|
+
}
|
284
|
+
|
285
|
+
setFactory = ({
|
286
|
+
newFactory
|
287
|
+
}: {
|
288
|
+
newFactory: string;
|
289
|
+
}, funds?: Coin[]): MsgExecuteContractEncodeObject => {
|
290
|
+
return {
|
291
|
+
typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
|
292
|
+
value: MsgExecuteContract.fromPartial({
|
293
|
+
sender: this.sender,
|
294
|
+
contract: this.contractAddress,
|
295
|
+
msg: toUtf8(JSON.stringify({
|
296
|
+
set_factory: {
|
297
|
+
new_factory: newFactory
|
298
|
+
}
|
299
|
+
})),
|
300
|
+
funds
|
301
|
+
})
|
302
|
+
};
|
303
|
+
};
|
304
|
+
updateOwnership = (action: Action, funds?: Coin[]): MsgExecuteContractEncodeObject => {
|
305
|
+
return {
|
306
|
+
typeUrl: \\"/cosmwasm.wasm.v1.MsgExecuteContract\\",
|
307
|
+
value: MsgExecuteContract.fromPartial({
|
308
|
+
sender: this.sender,
|
309
|
+
contract: this.contractAddress,
|
310
|
+
msg: toUtf8(JSON.stringify({
|
311
|
+
update_ownership: action
|
312
|
+
})),
|
313
|
+
funds
|
314
|
+
})
|
315
|
+
};
|
316
|
+
};
|
317
|
+
}"
|
318
|
+
`;
|
319
|
+
|
320
|
+
exports[`ownershipInterface 1`] = `
|
321
|
+
"export interface OwnershipMessage {
|
322
|
+
contractAddress: string;
|
323
|
+
sender: string;
|
324
|
+
setFactory: ({
|
325
|
+
newFactory
|
326
|
+
}: {
|
327
|
+
newFactory: string;
|
328
|
+
}, funds?: Coin[]) => MsgExecuteContractEncodeObject;
|
329
|
+
updateOwnership: (action: Action, funds?: Coin[]) => MsgExecuteContractEncodeObject;
|
330
|
+
}"
|
331
|
+
`;
|
@@ -1,25 +1,46 @@
|
|
1
|
-
import execute_msg from
|
1
|
+
import execute_msg from "../../../../__fixtures__/basic/execute_msg_for__empty.json";
|
2
|
+
import ownership from "../../../../__fixtures__/basic/ownership.json";
|
3
|
+
|
2
4
|
import {
|
3
|
-
|
4
|
-
|
5
|
-
} from
|
6
|
-
import { expectCode, makeContext } from
|
5
|
+
createMessageComposerClass,
|
6
|
+
createMessageComposerInterface,
|
7
|
+
} from "./message-composer";
|
8
|
+
import { expectCode, makeContext } from "../../test-utils";
|
9
|
+
import * as t from "@babel/types";
|
10
|
+
import { createReactQueryMutationHooks } from "../react-query";
|
11
|
+
|
12
|
+
it("execute classes", () => {
|
13
|
+
const ctx = makeContext(execute_msg);
|
14
|
+
expectCode(
|
15
|
+
createMessageComposerClass(
|
16
|
+
ctx,
|
17
|
+
"SG721MessageComposer",
|
18
|
+
"SG721Message",
|
19
|
+
execute_msg
|
20
|
+
)
|
21
|
+
);
|
22
|
+
});
|
23
|
+
|
24
|
+
it("createMessageComposerInterface", () => {
|
25
|
+
const ctx = makeContext(execute_msg);
|
26
|
+
expectCode(createMessageComposerInterface(ctx, "SG721Message", execute_msg));
|
27
|
+
});
|
7
28
|
|
8
|
-
it(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
29
|
+
it("ownershipClass", () => {
|
30
|
+
const ctx = makeContext(ownership);
|
31
|
+
expectCode(
|
32
|
+
createMessageComposerClass(
|
33
|
+
ctx,
|
34
|
+
"OwnershipMessageComposer",
|
35
|
+
"OwnershipMessage",
|
36
|
+
ownership
|
37
|
+
)
|
38
|
+
);
|
16
39
|
});
|
17
40
|
|
18
|
-
it(
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
execute_msg
|
24
|
-
))
|
41
|
+
it("ownershipInterface", () => {
|
42
|
+
const ownershipCtx = makeContext(ownership);
|
43
|
+
expectCode(
|
44
|
+
createMessageComposerInterface(ownershipCtx, "OwnershipMessage", ownership)
|
45
|
+
);
|
25
46
|
});
|
@@ -14,6 +14,7 @@ import { JSONSchema } from '../types';
|
|
14
14
|
import { RenderContext } from '../context';
|
15
15
|
import { identifier } from '../utils/babel';
|
16
16
|
import { getWasmMethodArgs } from '../client/client';
|
17
|
+
import { Expression } from '@babel/types';
|
17
18
|
|
18
19
|
const createWasmExecMethodMessageComposer = (
|
19
20
|
context: RenderContext,
|
@@ -27,12 +28,20 @@ const createWasmExecMethodMessageComposer = (
|
|
27
28
|
|
28
29
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
29
30
|
const methodName = camel(underscoreName);
|
30
|
-
const
|
31
|
+
const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
31
32
|
const args = getWasmMethodArgs(
|
32
33
|
context,
|
33
34
|
jsonschema.properties[underscoreName]
|
34
35
|
);
|
35
36
|
|
37
|
+
// what the underscore named property in the message is assigned to
|
38
|
+
let actionValue: Expression
|
39
|
+
if (param?.type === 'Identifier') {
|
40
|
+
actionValue = t.identifier(param.name);
|
41
|
+
} else {
|
42
|
+
actionValue = t.objectExpression(args)
|
43
|
+
}
|
44
|
+
|
36
45
|
const constantParams = [
|
37
46
|
identifier('funds', t.tsTypeAnnotation(
|
38
47
|
t.tsArrayType(
|
@@ -46,9 +55,9 @@ const createWasmExecMethodMessageComposer = (
|
|
46
55
|
return t.classProperty(
|
47
56
|
t.identifier(methodName),
|
48
57
|
arrowFunctionExpression(
|
49
|
-
|
58
|
+
param ? [
|
50
59
|
// props
|
51
|
-
|
60
|
+
param,
|
52
61
|
...constantParams
|
53
62
|
] : constantParams,
|
54
63
|
t.blockStatement(
|
@@ -95,10 +104,9 @@ const createWasmExecMethodMessageComposer = (
|
|
95
104
|
),
|
96
105
|
[
|
97
106
|
t.objectExpression(
|
98
|
-
|
99
107
|
[
|
100
108
|
t.objectProperty(
|
101
|
-
t.identifier(underscoreName),
|
109
|
+
t.identifier(underscoreName), actionValue
|
102
110
|
)
|
103
111
|
]
|
104
112
|
|
@@ -113,6 +113,27 @@ exports[`execute class 1`] = `
|
|
113
113
|
}"
|
114
114
|
`;
|
115
115
|
|
116
|
+
exports[`ownership 1`] = `
|
117
|
+
"export abstract class Ownership {
|
118
|
+
static setFactory = ({
|
119
|
+
newFactory
|
120
|
+
}: CamelCasedProperties<Extract<ExecuteMsg, {
|
121
|
+
set_factory: unknown;
|
122
|
+
}>[\\"set_factory\\"]>): ExecuteMsg => {
|
123
|
+
return {
|
124
|
+
set_factory: ({
|
125
|
+
new_factory: newFactory
|
126
|
+
} as const)
|
127
|
+
};
|
128
|
+
};
|
129
|
+
static updateOwnership = (action: Action): ExecuteMsg => {
|
130
|
+
return {
|
131
|
+
update_ownership: action
|
132
|
+
};
|
133
|
+
};
|
134
|
+
}"
|
135
|
+
`;
|
136
|
+
|
116
137
|
exports[`query class 1`] = `
|
117
138
|
"export abstract class SG721MsgBuilder {
|
118
139
|
static ownerOf = ({
|
@@ -1,9 +1,12 @@
|
|
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';
|
4
|
+
|
3
5
|
import {
|
4
6
|
createMsgBuilderClass,
|
5
7
|
} from './msg-builder'
|
6
8
|
import { expectCode, makeContext } from '../../test-utils';
|
9
|
+
import { findExecuteMsg } from '@cosmwasm/ts-codegen/src';
|
7
10
|
|
8
11
|
it('execute class', () => {
|
9
12
|
const ctx = makeContext(execute_msg);
|
@@ -12,6 +15,11 @@ it('execute class', () => {
|
|
12
15
|
|
13
16
|
|
14
17
|
it('query class', () => {
|
15
|
-
const ctx = makeContext(
|
18
|
+
const ctx = makeContext(query_msg);
|
16
19
|
expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', query_msg))
|
17
20
|
});
|
21
|
+
|
22
|
+
it('ownership', () => {
|
23
|
+
const ctx = makeContext(ownership);
|
24
|
+
expectCode(createMsgBuilderClass(ctx, 'Ownership', ownership))
|
25
|
+
});
|
@@ -1,16 +1,11 @@
|
|
1
|
-
import * as t from
|
2
|
-
import { camel } from
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
} from "../utils";
|
10
|
-
import { ExecuteMsg, QueryMsg } from "../types";
|
11
|
-
import { createTypedObjectParams } from "../utils/types";
|
12
|
-
import { RenderContext } from "../context";
|
13
|
-
import { getWasmMethodArgs } from "../client/client";
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel } from 'case';
|
3
|
+
import { abstractClassDeclaration, arrowFunctionExpression, getMessageProperties } from '../utils';
|
4
|
+
import { ExecuteMsg, QueryMsg } from '../types';
|
5
|
+
import { createTypedObjectParams } from '../utils/types';
|
6
|
+
import { RenderContext } from '../context';
|
7
|
+
import { getWasmMethodArgs } from '../client/client';
|
8
|
+
import { Expression, Identifier, PatternLike, TSAsExpression } from '@babel/types';
|
14
9
|
|
15
10
|
export const createMsgBuilderClass = (
|
16
11
|
context: RenderContext,
|
@@ -34,10 +29,10 @@ export const createMsgBuilderClass = (
|
|
34
29
|
function createExtractTypeAnnotation(underscoreName: string, msgTitle: string) {
|
35
30
|
return t.tsTypeAnnotation(
|
36
31
|
t.tsTypeReference(
|
37
|
-
t.identifier(
|
32
|
+
t.identifier('CamelCasedProperties'),
|
38
33
|
t.tsTypeParameterInstantiation([
|
39
34
|
t.tsIndexedAccessType(
|
40
|
-
t.tsTypeReference(t.identifier(
|
35
|
+
t.tsTypeReference(t.identifier('Extract'),
|
41
36
|
t.tsTypeParameterInstantiation([
|
42
37
|
t.tsTypeReference(t.identifier(msgTitle)),
|
43
38
|
t.tsTypeLiteral([
|
@@ -62,7 +57,7 @@ const createStaticExecMethodMsgBuilder = (
|
|
62
57
|
) => {
|
63
58
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
64
59
|
const methodName = camel(underscoreName);
|
65
|
-
const
|
60
|
+
const param = createTypedObjectParams(
|
66
61
|
context,
|
67
62
|
jsonschema.properties[underscoreName]
|
68
63
|
);
|
@@ -71,17 +66,34 @@ const createStaticExecMethodMsgBuilder = (
|
|
71
66
|
jsonschema.properties[underscoreName]
|
72
67
|
);
|
73
68
|
|
74
|
-
|
69
|
+
// what the underscore named property in the message is assigned to
|
70
|
+
let actionValue: Expression
|
71
|
+
if (param?.type === 'Identifier') {
|
72
|
+
actionValue = t.identifier(param.name);
|
73
|
+
} else {
|
74
|
+
actionValue = t.tsAsExpression(t.objectExpression(args), t.tsTypeReference(t.identifier('const')));
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
// TODO: this is a hack to get the type annotation to work
|
79
|
+
// all type annotations in the future should be the extracted and camelized type
|
80
|
+
if (
|
81
|
+
param &&
|
82
|
+
param.typeAnnotation.type === 'TSTypeAnnotation' &&
|
83
|
+
param.typeAnnotation.typeAnnotation.type === 'TSTypeLiteral'
|
84
|
+
) {
|
85
|
+
param.typeAnnotation = createExtractTypeAnnotation(underscoreName, msgTitle);
|
86
|
+
}
|
75
87
|
|
76
88
|
return t.classProperty(
|
77
89
|
t.identifier(methodName),
|
78
90
|
arrowFunctionExpression(
|
79
91
|
// params
|
80
|
-
|
92
|
+
param
|
81
93
|
? [
|
82
|
-
|
83
|
-
|
84
|
-
|
94
|
+
// props
|
95
|
+
param
|
96
|
+
]
|
85
97
|
: [],
|
86
98
|
// body
|
87
99
|
t.blockStatement([
|
@@ -89,10 +101,10 @@ const createStaticExecMethodMsgBuilder = (
|
|
89
101
|
t.objectExpression([
|
90
102
|
t.objectProperty(
|
91
103
|
t.identifier(underscoreName),
|
92
|
-
|
93
|
-
)
|
104
|
+
actionValue
|
105
|
+
)
|
94
106
|
])
|
95
|
-
)
|
107
|
+
)
|
96
108
|
]),
|
97
109
|
// return type
|
98
110
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier(msgTitle))),
|
@@ -1312,3 +1312,48 @@ export function useSg721TransferNftMutation(options?: Omit<UseMutationOptions<Ex
|
|
1312
1312
|
}) => client.transferNft(msg, fee, memo, funds), options);
|
1313
1313
|
}"
|
1314
1314
|
`;
|
1315
|
+
|
1316
|
+
exports[`ownership 1`] = `
|
1317
|
+
"export interface OwnershipUpdateOwnershipMutation {
|
1318
|
+
client: OwnershipClient;
|
1319
|
+
msg: Action;
|
1320
|
+
args?: {
|
1321
|
+
fee?: number | StdFee | \\"auto\\";
|
1322
|
+
memo?: string;
|
1323
|
+
funds?: Coin[];
|
1324
|
+
};
|
1325
|
+
}
|
1326
|
+
export function useOwnershipUpdateOwnershipMutation(options?: Omit<UseMutationOptions<ExecuteResult, Error, OwnershipUpdateOwnershipMutation>, \\"mutationFn\\">) {
|
1327
|
+
return useMutation<ExecuteResult, Error, OwnershipUpdateOwnershipMutation>(({
|
1328
|
+
client,
|
1329
|
+
msg,
|
1330
|
+
args: {
|
1331
|
+
fee,
|
1332
|
+
memo,
|
1333
|
+
funds
|
1334
|
+
} = {}
|
1335
|
+
}) => client.updateOwnership(msg, fee, memo, funds), options);
|
1336
|
+
}
|
1337
|
+
export interface OwnershipSetFactoryMutation {
|
1338
|
+
client: OwnershipClient;
|
1339
|
+
msg: {
|
1340
|
+
newFactory: string;
|
1341
|
+
};
|
1342
|
+
args?: {
|
1343
|
+
fee?: number | StdFee | \\"auto\\";
|
1344
|
+
memo?: string;
|
1345
|
+
funds?: Coin[];
|
1346
|
+
};
|
1347
|
+
}
|
1348
|
+
export function useOwnershipSetFactoryMutation(options?: Omit<UseMutationOptions<ExecuteResult, Error, OwnershipSetFactoryMutation>, \\"mutationFn\\">) {
|
1349
|
+
return useMutation<ExecuteResult, Error, OwnershipSetFactoryMutation>(({
|
1350
|
+
client,
|
1351
|
+
msg,
|
1352
|
+
args: {
|
1353
|
+
fee,
|
1354
|
+
memo,
|
1355
|
+
funds
|
1356
|
+
} = {}
|
1357
|
+
}) => client.setFactory(msg, fee, memo, funds), options);
|
1358
|
+
}"
|
1359
|
+
`;
|
@@ -1,13 +1,15 @@
|
|
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
|
4
|
+
import ownership from '../../../../__fixtures__/basic/ownership.json';
|
5
|
+
|
5
6
|
|
6
7
|
import {
|
7
8
|
createReactQueryHooks,
|
8
9
|
createReactQueryMutationHooks,
|
9
10
|
} from './react-query'
|
10
11
|
import { expectCode, makeContext } from '../../test-utils';
|
12
|
+
import { createMsgBuilderClass } from '../msg-builder';
|
11
13
|
|
12
14
|
const execCtx = makeContext(execute_msg);
|
13
15
|
const queryCtx = makeContext(query_msg);
|
@@ -90,3 +92,17 @@ it('createReactQueryHooks', () => {
|
|
90
92
|
)))
|
91
93
|
});
|
92
94
|
|
95
|
+
it('ownership', () => {
|
96
|
+
const ownershipCtx = makeContext(ownership);
|
97
|
+
expectCode(t.program(
|
98
|
+
createReactQueryMutationHooks(
|
99
|
+
{
|
100
|
+
context: ownershipCtx,
|
101
|
+
execMsg: ownership,
|
102
|
+
contractName: 'Ownership',
|
103
|
+
ExecuteClient: 'OwnershipClient',
|
104
|
+
}
|
105
|
+
)))
|
106
|
+
});
|
107
|
+
|
108
|
+
|
package/src/utils/types.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
|
-
import { camel, pascal } from 'case';
|
2
|
+
import { camel, pascal, snake } from 'case';
|
3
3
|
import { propertySignature } from './babel';
|
4
4
|
import { TSTypeAnnotation } from '@babel/types';
|
5
5
|
import { RenderContext } from '../context';
|
@@ -425,15 +425,22 @@ export const createTypedObjectParams = (
|
|
425
425
|
context: RenderContext,
|
426
426
|
jsonschema: JSONSchema,
|
427
427
|
camelize: boolean = true
|
428
|
-
): t.
|
428
|
+
): (t.Identifier | t.Pattern | t.RestElement) => {
|
429
429
|
|
430
430
|
const keys = Object.keys(jsonschema.properties ?? {});
|
431
431
|
if (!keys.length) {
|
432
|
-
|
433
432
|
// is there a ref?
|
434
433
|
if (jsonschema.$ref) {
|
435
434
|
const obj = context.refLookup(jsonschema.$ref);
|
436
|
-
|
435
|
+
// If there is a oneOf, then we need to create a type for it
|
436
|
+
if (obj?.oneOf) {
|
437
|
+
// the actual type of the ref
|
438
|
+
const refType = jsonschema.$ref.split('/').pop();
|
439
|
+
const refName = camel(refType);
|
440
|
+
const id = t.identifier(refName);
|
441
|
+
id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
|
442
|
+
return id
|
443
|
+
} else if (obj) {
|
437
444
|
return createTypedObjectParams(
|
438
445
|
context,
|
439
446
|
obj,
|