@cosmwasm/ts-codegen 0.30.1 → 0.31.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +5 -2
- package/main/builder/builder.js +41 -4
- package/main/generators/create-helpers.js +38 -0
- package/main/helpers/contractContextBase.js +8 -0
- package/main/helpers/contractsContextTSX.js +8 -0
- package/main/helpers/index.js +31 -0
- package/main/plugins/client.js +11 -6
- package/main/plugins/message-composer.js +10 -6
- package/main/plugins/msg-builder.js +1 -1
- package/main/plugins/plugin-base.js +9 -2
- package/main/plugins/provider-bundle.js +146 -0
- package/main/plugins/provider.js +170 -0
- package/main/plugins/react-query.js +1 -1
- package/main/plugins/recoil.js +1 -1
- package/main/plugins/types.js +1 -1
- package/main/utils/files.js +77 -0
- package/main/utils/unused.js +68 -0
- package/module/builder/builder.js +28 -2
- package/module/generators/create-helpers.js +25 -0
- package/module/helpers/contractContextBase.js +92 -0
- package/module/helpers/contractsContextTSX.js +73 -0
- package/module/helpers/index.js +2 -0
- package/module/plugins/client.js +13 -10
- package/module/plugins/message-composer.js +12 -10
- package/module/plugins/msg-builder.js +1 -1
- package/module/plugins/plugin-base.js +15 -8
- package/module/plugins/provider-bundle.js +65 -0
- package/module/plugins/provider.js +81 -0
- package/module/plugins/react-query.js +1 -1
- package/module/plugins/recoil.js +1 -1
- package/module/plugins/types.js +1 -1
- package/module/utils/files.js +44 -0
- package/module/utils/unused.js +45 -0
- package/package.json +3 -3
- package/src/builder/builder.ts +36 -1
- package/src/generators/create-helpers.ts +28 -0
- package/src/helpers/contractContextBase.ts +92 -0
- package/src/helpers/contractsContextTSX.ts +73 -0
- package/src/helpers/index.ts +2 -0
- package/src/plugins/client.ts +30 -14
- package/src/plugins/message-composer.ts +23 -14
- package/src/plugins/msg-builder.ts +1 -1
- package/src/plugins/plugin-base.ts +30 -20
- package/src/plugins/provider-bundle.ts +97 -0
- package/src/plugins/provider.ts +114 -0
- package/src/plugins/react-query.ts +1 -1
- package/src/plugins/recoil.ts +1 -1
- package/src/plugins/types.ts +1 -1
- package/src/utils/files.ts +73 -0
- package/src/utils/unused.ts +52 -0
- package/types/src/builder/builder.d.ts +7 -1
- package/types/src/generators/create-helpers.d.ts +3 -0
- package/types/src/helpers/contractContextBase.d.ts +1 -0
- package/types/src/helpers/contractsContextTSX.d.ts +1 -0
- package/types/src/helpers/index.d.ts +2 -0
- package/types/src/plugins/client.d.ts +4 -3
- package/types/src/plugins/message-composer.d.ts +4 -3
- package/types/src/plugins/plugin-base.d.ts +7 -3
- package/types/src/plugins/provider-bundle.d.ts +13 -0
- package/types/src/plugins/provider.d.ts +15 -0
- package/types/src/plugins/use-contracts.d.ts +12 -0
- package/types/src/utils/files.d.ts +3 -0
- package/types/src/utils/unused.d.ts +5 -0
@@ -0,0 +1,170 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
6
|
+
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
8
|
+
value: true
|
9
|
+
});
|
10
|
+
exports.GetLocalNameByContractName = exports.GetLocalBaseNameByContractName = exports.ContractsContextProviderPlugin = void 0;
|
11
|
+
|
12
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
13
|
+
|
14
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
15
|
+
|
16
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
17
|
+
|
18
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
19
|
+
|
20
|
+
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
21
|
+
|
22
|
+
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
23
|
+
|
24
|
+
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
25
|
+
|
26
|
+
var _case = require("case");
|
27
|
+
|
28
|
+
var w = _interopRequireWildcard(require("wasm-ast-types"));
|
29
|
+
|
30
|
+
var _pluginBase = require("./plugin-base");
|
31
|
+
|
32
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
33
|
+
|
34
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
35
|
+
|
36
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
37
|
+
|
38
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
39
|
+
|
40
|
+
var GetLocalNameByContractName = function GetLocalNameByContractName(name) {
|
41
|
+
return "".concat((0, _case.pascal)(name), ".provider.ts");
|
42
|
+
};
|
43
|
+
|
44
|
+
exports.GetLocalNameByContractName = GetLocalNameByContractName;
|
45
|
+
|
46
|
+
var GetLocalBaseNameByContractName = function GetLocalBaseNameByContractName(name) {
|
47
|
+
return "".concat((0, _case.pascal)(name), ".provider");
|
48
|
+
};
|
49
|
+
|
50
|
+
exports.GetLocalBaseNameByContractName = GetLocalBaseNameByContractName;
|
51
|
+
|
52
|
+
var ContractsContextProviderPlugin = /*#__PURE__*/function (_BuilderPluginBase) {
|
53
|
+
(0, _inherits2["default"])(ContractsContextProviderPlugin, _BuilderPluginBase);
|
54
|
+
|
55
|
+
var _super = _createSuper(ContractsContextProviderPlugin);
|
56
|
+
|
57
|
+
function ContractsContextProviderPlugin(opt) {
|
58
|
+
var _this;
|
59
|
+
|
60
|
+
(0, _classCallCheck2["default"])(this, ContractsContextProviderPlugin);
|
61
|
+
_this = _super.call(this, opt);
|
62
|
+
_this.utils = {
|
63
|
+
ContractBase: "__contractContextBase__",
|
64
|
+
IContractConstructor: "__contractContextBase__",
|
65
|
+
IEmptyClient: "__contractContextBase__"
|
66
|
+
};
|
67
|
+
return _this;
|
68
|
+
}
|
69
|
+
|
70
|
+
(0, _createClass2["default"])(ContractsContextProviderPlugin, [{
|
71
|
+
key: "initContext",
|
72
|
+
value: function initContext(contract, options) {
|
73
|
+
return new w.RenderContext(contract, options, this.builder.builderContext);
|
74
|
+
}
|
75
|
+
}, {
|
76
|
+
key: "doRender",
|
77
|
+
value: function () {
|
78
|
+
var _doRender = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(name, context) {
|
79
|
+
var _this$option, _this$option$useContr, _Object$keys;
|
80
|
+
|
81
|
+
var providerInfo, localname, needEmptyClientType, clientFile, clientClasses, body, signClientProviderInfo, queryClientProviderInfo, messageComposerProviderInfo;
|
82
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
83
|
+
while (1) {
|
84
|
+
switch (_context.prev = _context.next) {
|
85
|
+
case 0:
|
86
|
+
if ((_this$option = this.option) !== null && _this$option !== void 0 && (_this$option$useContr = _this$option.useContracts) !== null && _this$option$useContr !== void 0 && _this$option$useContr.enabled) {
|
87
|
+
_context.next = 2;
|
88
|
+
break;
|
89
|
+
}
|
90
|
+
|
91
|
+
return _context.abrupt("return");
|
92
|
+
|
93
|
+
case 2:
|
94
|
+
providerInfo = context.getProviderInfos()[name];
|
95
|
+
|
96
|
+
if ((_Object$keys = Object.keys(providerInfo)) !== null && _Object$keys !== void 0 && _Object$keys.length) {
|
97
|
+
_context.next = 5;
|
98
|
+
break;
|
99
|
+
}
|
100
|
+
|
101
|
+
return _context.abrupt("return");
|
102
|
+
|
103
|
+
case 5:
|
104
|
+
context.addUtil("ContractBase");
|
105
|
+
context.addUtil("IContractConstructor");
|
106
|
+
localname = GetLocalNameByContractName(name);
|
107
|
+
needEmptyClientType = false;
|
108
|
+
clientFile = null;
|
109
|
+
clientClasses = [];
|
110
|
+
body = [];
|
111
|
+
signClientProviderInfo = providerInfo[w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE];
|
112
|
+
|
113
|
+
if (signClientProviderInfo) {
|
114
|
+
clientFile = "./".concat(signClientProviderInfo.basename);
|
115
|
+
clientClasses.push(signClientProviderInfo.classname);
|
116
|
+
} else {
|
117
|
+
needEmptyClientType = true;
|
118
|
+
}
|
119
|
+
|
120
|
+
queryClientProviderInfo = providerInfo[w.PROVIDER_TYPES.QUERY_CLIENT_TYPE];
|
121
|
+
|
122
|
+
if (queryClientProviderInfo) {
|
123
|
+
clientFile = "./".concat(queryClientProviderInfo.basename);
|
124
|
+
clientClasses.push(queryClientProviderInfo.classname);
|
125
|
+
} else {
|
126
|
+
needEmptyClientType = true;
|
127
|
+
}
|
128
|
+
|
129
|
+
if (clientFile) {
|
130
|
+
body.push(w.importStmt(clientClasses, clientFile));
|
131
|
+
}
|
132
|
+
|
133
|
+
messageComposerProviderInfo = providerInfo[w.PROVIDER_TYPES.MESSAGE_COMPOSER_TYPE];
|
134
|
+
|
135
|
+
if (messageComposerProviderInfo) {
|
136
|
+
body.push(w.importStmt([messageComposerProviderInfo.classname], "./".concat(messageComposerProviderInfo.basename)));
|
137
|
+
} else {
|
138
|
+
needEmptyClientType = true;
|
139
|
+
}
|
140
|
+
|
141
|
+
if (needEmptyClientType) {
|
142
|
+
context.addUtil("IEmptyClient");
|
143
|
+
}
|
144
|
+
|
145
|
+
body.push(w.createProvider(name, providerInfo));
|
146
|
+
return _context.abrupt("return", [{
|
147
|
+
type: "plugin",
|
148
|
+
localname: localname,
|
149
|
+
body: body
|
150
|
+
}]);
|
151
|
+
|
152
|
+
case 22:
|
153
|
+
case "end":
|
154
|
+
return _context.stop();
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}, _callee, this);
|
158
|
+
}));
|
159
|
+
|
160
|
+
function doRender(_x, _x2) {
|
161
|
+
return _doRender.apply(this, arguments);
|
162
|
+
}
|
163
|
+
|
164
|
+
return doRender;
|
165
|
+
}()
|
166
|
+
}]);
|
167
|
+
return ContractsContextProviderPlugin;
|
168
|
+
}(_pluginBase.BuilderPluginBase);
|
169
|
+
|
170
|
+
exports.ContractsContextProviderPlugin = ContractsContextProviderPlugin;
|
@@ -52,7 +52,7 @@ var ReactQueryPlugin = /*#__PURE__*/function (_BuilderPluginBase) {
|
|
52
52
|
(0, _createClass2["default"])(ReactQueryPlugin, [{
|
53
53
|
key: "initContext",
|
54
54
|
value: function initContext(contract, options) {
|
55
|
-
return new w.RenderContext(contract, options);
|
55
|
+
return new w.RenderContext(contract, options, this.builder.builderContext);
|
56
56
|
}
|
57
57
|
}, {
|
58
58
|
key: "doRender",
|
package/main/plugins/recoil.js
CHANGED
@@ -67,7 +67,7 @@ var RecoilPlugin = /*#__PURE__*/function (_BuilderPluginBase) {
|
|
67
67
|
(0, _createClass2["default"])(RecoilPlugin, [{
|
68
68
|
key: "initContext",
|
69
69
|
value: function initContext(contract, options) {
|
70
|
-
return new w.RenderContext(contract, options);
|
70
|
+
return new w.RenderContext(contract, options, this.builder.builderContext);
|
71
71
|
}
|
72
72
|
}, {
|
73
73
|
key: "doRender",
|
package/main/plugins/types.js
CHANGED
@@ -56,7 +56,7 @@ var TypesPlugin = /*#__PURE__*/function (_BuilderPluginBase) {
|
|
56
56
|
(0, _createClass2["default"])(TypesPlugin, [{
|
57
57
|
key: "initContext",
|
58
58
|
value: function initContext(contract, options) {
|
59
|
-
return new _wasmAstTypes.RenderContext(contract, options);
|
59
|
+
return new _wasmAstTypes.RenderContext(contract, options, this.builder.builderContext);
|
60
60
|
}
|
61
61
|
}, {
|
62
62
|
key: "doRender",
|
@@ -0,0 +1,77 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
6
|
+
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
8
|
+
value: true
|
9
|
+
});
|
10
|
+
exports.writeContentToFile = exports.writeAstToFile = void 0;
|
11
|
+
|
12
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
13
|
+
|
14
|
+
var _parser = require("@babel/parser");
|
15
|
+
|
16
|
+
var _mkdirp = require("mkdirp");
|
17
|
+
|
18
|
+
var _fs = require("fs");
|
19
|
+
|
20
|
+
var _path = require("path");
|
21
|
+
|
22
|
+
var _generator = _interopRequireDefault(require("@babel/generator"));
|
23
|
+
|
24
|
+
var _unused = require("./unused");
|
25
|
+
|
26
|
+
var _traverse = _interopRequireDefault(require("@babel/traverse"));
|
27
|
+
|
28
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
29
|
+
|
30
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
31
|
+
|
32
|
+
var writeAstToFile = function writeAstToFile(outPath, program, filename) {
|
33
|
+
var removeUnusedImports = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
34
|
+
var isTsDisable = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
35
|
+
var isEslintDisable = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
|
36
|
+
var ast = t.program(program);
|
37
|
+
var content = (0, _generator["default"])(ast).code;
|
38
|
+
|
39
|
+
if (removeUnusedImports) {
|
40
|
+
var plugins = ["typescript"];
|
41
|
+
var newAst = (0, _parser.parse)(content, {
|
42
|
+
sourceType: "module",
|
43
|
+
plugins: plugins
|
44
|
+
});
|
45
|
+
(0, _traverse["default"])(newAst, _unused.unused);
|
46
|
+
var content2 = (0, _generator["default"])(newAst).code;
|
47
|
+
writeContentToFile(outPath, content2, filename, isTsDisable, isEslintDisable);
|
48
|
+
} else {
|
49
|
+
writeContentToFile(outPath, content, filename, isTsDisable, isEslintDisable);
|
50
|
+
}
|
51
|
+
};
|
52
|
+
|
53
|
+
exports.writeAstToFile = writeAstToFile;
|
54
|
+
|
55
|
+
var writeContentToFile = function writeContentToFile(outPath, content, filename) {
|
56
|
+
var isTsDisable = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
57
|
+
var isEslintDisable = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
58
|
+
var esLintPrefix = "";
|
59
|
+
var tsLintPrefix = "";
|
60
|
+
var nameWithoutPath = filename.replace(outPath, ""); // strip off leading slash
|
61
|
+
|
62
|
+
if (nameWithoutPath.startsWith("/")) nameWithoutPath = nameWithoutPath.replace(/^\//, "");
|
63
|
+
|
64
|
+
if (isTsDisable) {
|
65
|
+
tsLintPrefix = "//@ts-nocheck\n";
|
66
|
+
}
|
67
|
+
|
68
|
+
if (isEslintDisable) {
|
69
|
+
esLintPrefix = "/* eslint-disable */\n";
|
70
|
+
}
|
71
|
+
|
72
|
+
var text = tsLintPrefix + esLintPrefix + content;
|
73
|
+
(0, _mkdirp.sync)((0, _path.dirname)(filename));
|
74
|
+
(0, _fs.writeFileSync)(filename, text);
|
75
|
+
};
|
76
|
+
|
77
|
+
exports.writeContentToFile = writeContentToFile;
|
@@ -0,0 +1,68 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4
|
+
|
5
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
6
|
+
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
8
|
+
value: true
|
9
|
+
});
|
10
|
+
exports.unused = void 0;
|
11
|
+
|
12
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
13
|
+
|
14
|
+
var t = _interopRequireWildcard(require("@babel/types"));
|
15
|
+
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
17
|
+
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
19
|
+
|
20
|
+
//@ts-nocheck
|
21
|
+
// https://github.com/chuyik/babel-plugin-danger-remove-unused-import
|
22
|
+
// https://github.com/chuyik/babel-plugin-danger-remove-unused-import/blob/c5454c21e94698a2464a12baa5590761932a71a8/License#L1
|
23
|
+
var unused = {
|
24
|
+
Program: {
|
25
|
+
exit: function exit(path) {
|
26
|
+
var UnRefBindings = new Map();
|
27
|
+
|
28
|
+
for (var _i = 0, _Object$entries = Object.entries(path.scope.bindings); _i < _Object$entries.length; _i++) {
|
29
|
+
var _Object$entries$_i = (0, _slicedToArray2["default"])(_Object$entries[_i], 2),
|
30
|
+
name = _Object$entries$_i[0],
|
31
|
+
binding = _Object$entries$_i[1];
|
32
|
+
|
33
|
+
if (!binding.path.parentPath || binding.kind !== 'module') continue;
|
34
|
+
var source = binding.path.parentPath.get('source');
|
35
|
+
var importName = source.node.value;
|
36
|
+
if (!t.isStringLiteral(source)) continue;
|
37
|
+
var key = "".concat(importName, "(").concat(source.node.loc && source.node.loc.start.line, ")");
|
38
|
+
|
39
|
+
if (!UnRefBindings.has(key)) {
|
40
|
+
UnRefBindings.set(key, binding);
|
41
|
+
}
|
42
|
+
|
43
|
+
if (binding.referenced) {
|
44
|
+
UnRefBindings.set(key, null);
|
45
|
+
} else {
|
46
|
+
var nodeType = binding.path.node.type;
|
47
|
+
|
48
|
+
if (nodeType === 'ImportSpecifier') {
|
49
|
+
binding.path.remove();
|
50
|
+
} else if (nodeType === 'ImportDefaultSpecifier') {
|
51
|
+
binding.path.remove();
|
52
|
+
} else if (nodeType === 'ImportNamespaceSpecifier') {
|
53
|
+
binding.path.remove();
|
54
|
+
} else if (binding.path.parentPath) {
|
55
|
+
binding.path.parentPath.remove();
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
UnRefBindings.forEach(function (binding, key) {
|
61
|
+
if (binding && binding.path.parentPath) {
|
62
|
+
binding.path.parentPath.remove();
|
63
|
+
}
|
64
|
+
});
|
65
|
+
}
|
66
|
+
}
|
67
|
+
};
|
68
|
+
exports.unused = unused;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
-
import { defaultOptions } from "wasm-ast-types";
|
2
|
+
import { defaultOptions, BuilderContext } from "wasm-ast-types";
|
3
3
|
import { header } from '../utils/header';
|
4
4
|
import { join } from "path";
|
5
5
|
import { writeFileSync } from 'fs';
|
@@ -17,6 +17,9 @@ import { MsgBuilderPlugin } from "../plugins/msg-builder";
|
|
17
17
|
import { MessageComposerPlugin } from "../plugins/message-composer";
|
18
18
|
import { ClientPlugin } from "../plugins/client";
|
19
19
|
import { TypesPlugin } from "../plugins/types";
|
20
|
+
import { ContractsContextProviderPlugin } from "../plugins/provider";
|
21
|
+
import { createHelpers } from "../generators/create-helpers";
|
22
|
+
import { ContractsProviderBundlePlugin } from "../plugins/provider-bundle";
|
20
23
|
const defaultOpts = {
|
21
24
|
bundle: {
|
22
25
|
enabled: true,
|
@@ -27,6 +30,7 @@ const defaultOpts = {
|
|
27
30
|
;
|
28
31
|
;
|
29
32
|
;
|
33
|
+
;
|
30
34
|
|
31
35
|
function getContract(contractOpt) {
|
32
36
|
if (typeof contractOpt === 'string') {
|
@@ -46,7 +50,7 @@ function getContract(contractOpt) {
|
|
46
50
|
|
47
51
|
export class TSBuilder {
|
48
52
|
loadDefaultPlugins() {
|
49
|
-
[].push.apply(this.plugins, [new TypesPlugin(this.options), new ClientPlugin(this.options), new MessageComposerPlugin(this.options), new ReactQueryPlugin(this.options), new RecoilPlugin(this.options), new MsgBuilderPlugin(this.options)]);
|
53
|
+
[].push.apply(this.plugins, [new TypesPlugin(this.options), new ClientPlugin(this.options), new MessageComposerPlugin(this.options), new ReactQueryPlugin(this.options), new RecoilPlugin(this.options), new MsgBuilderPlugin(this.options), new ContractsContextProviderPlugin(this.options)]);
|
50
54
|
}
|
51
55
|
|
52
56
|
constructor({
|
@@ -63,6 +67,8 @@ export class TSBuilder {
|
|
63
67
|
|
64
68
|
_defineProperty(this, "plugins", []);
|
65
69
|
|
70
|
+
_defineProperty(this, "builderContext", new BuilderContext());
|
71
|
+
|
66
72
|
_defineProperty(this, "files", []);
|
67
73
|
|
68
74
|
this.contracts = contracts;
|
@@ -73,6 +79,8 @@ export class TSBuilder {
|
|
73
79
|
if (plugins && plugins.length) {
|
74
80
|
[].push.apply(this.plugins, plugins);
|
75
81
|
}
|
82
|
+
|
83
|
+
this.plugins.forEach(plugin => plugin.setBuilder(this));
|
76
84
|
}
|
77
85
|
|
78
86
|
async build() {
|
@@ -106,7 +114,25 @@ export class TSBuilder {
|
|
106
114
|
async after() {
|
107
115
|
if (this.options.bundle.enabled) {
|
108
116
|
this.bundle();
|
117
|
+
} //create useContracts bundle file
|
118
|
+
|
119
|
+
|
120
|
+
const contractsProviderBundlePlugin = new ContractsProviderBundlePlugin(this.options);
|
121
|
+
contractsProviderBundlePlugin.setBuilder(this);
|
122
|
+
let files = await contractsProviderBundlePlugin.render("", {
|
123
|
+
schemas: []
|
124
|
+
}, this.outPath);
|
125
|
+
|
126
|
+
if (files && files.length) {
|
127
|
+
[].push.apply(this.files, files);
|
109
128
|
}
|
129
|
+
|
130
|
+
createHelpers({
|
131
|
+
outPath: this.outPath,
|
132
|
+
contracts: this.contracts,
|
133
|
+
options: this.options,
|
134
|
+
plugins: this.plugins
|
135
|
+
}, this.builderContext);
|
110
136
|
}
|
111
137
|
|
112
138
|
async bundle() {
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { join, dirname } from "path";
|
2
|
+
import { sync as mkdirp } from "mkdirp";
|
3
|
+
import pkg from "../../package.json";
|
4
|
+
import { writeContentToFile } from "../utils/files";
|
5
|
+
import { contractContextBase, contractsContextTSX } from "../helpers";
|
6
|
+
const version = process.env.NODE_ENV === "test" ? "latest" : pkg.version;
|
7
|
+
const header = `/**
|
8
|
+
* This file and any referenced files were automatically generated by ${pkg.name}@${version}
|
9
|
+
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
|
10
|
+
* and run the transpile command or yarn proto command to regenerate this bundle.
|
11
|
+
*/
|
12
|
+
\n`;
|
13
|
+
|
14
|
+
const write = (outPath, file, content) => {
|
15
|
+
const outFile = join(outPath, file);
|
16
|
+
mkdirp(dirname(outFile));
|
17
|
+
writeContentToFile(outPath, header + content, outFile);
|
18
|
+
};
|
19
|
+
|
20
|
+
export const createHelpers = (input, builderContext) => {
|
21
|
+
if (input.options?.useContracts?.enabled && Object.keys(builderContext.providers)?.length) {
|
22
|
+
write(input.outPath, "contractContextBase.ts", contractContextBase);
|
23
|
+
write(input.outPath, "contracts-context.tsx", contractsContextTSX);
|
24
|
+
}
|
25
|
+
};
|
@@ -0,0 +1,92 @@
|
|
1
|
+
export const contractContextBase = `
|
2
|
+
import {
|
3
|
+
CosmWasmClient,
|
4
|
+
SigningCosmWasmClient,
|
5
|
+
} from '@cosmjs/cosmwasm-stargate';
|
6
|
+
|
7
|
+
export interface IContractConstructor {
|
8
|
+
address: string | undefined;
|
9
|
+
cosmWasmClient: CosmWasmClient | undefined;
|
10
|
+
signingCosmWasmClient: SigningCosmWasmClient | undefined;
|
11
|
+
}
|
12
|
+
|
13
|
+
export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
|
14
|
+
|
15
|
+
export const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';
|
16
|
+
|
17
|
+
export const NO_ADDRESS_ERROR_MESSAGE = "address doesn't exist";
|
18
|
+
|
19
|
+
export const NO_SIGNING_CLIENT_ERROR_MESSAGE =
|
20
|
+
'Signing client is not generated. Please check ts-codegen config';
|
21
|
+
|
22
|
+
export const NO_QUERY_CLIENT_ERROR_MESSAGE =
|
23
|
+
'Query client is not generated. Please check ts-codegen config';
|
24
|
+
|
25
|
+
export const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =
|
26
|
+
'Message composer client is not generated. Please check ts-codegen config';
|
27
|
+
|
28
|
+
/**
|
29
|
+
* a placeholder for non-generated classes
|
30
|
+
*/
|
31
|
+
export interface IEmptyClient {}
|
32
|
+
|
33
|
+
export interface ISigningClientProvider<T> {
|
34
|
+
getSigningClient(contractAddr: string): T;
|
35
|
+
}
|
36
|
+
|
37
|
+
export interface IQueryClientProvider<T> {
|
38
|
+
getQueryClient(contractAddr: string): T;
|
39
|
+
}
|
40
|
+
|
41
|
+
export interface IMessageComposerProvider<T> {
|
42
|
+
getMessageComposer(contractAddr: string): T;
|
43
|
+
}
|
44
|
+
|
45
|
+
export class ContractBase<
|
46
|
+
TSign = IEmptyClient,
|
47
|
+
TQuery = IEmptyClient,
|
48
|
+
TMsgComposer = IEmptyClient
|
49
|
+
> {
|
50
|
+
constructor(
|
51
|
+
protected address: string | undefined,
|
52
|
+
protected cosmWasmClient: CosmWasmClient | undefined,
|
53
|
+
protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
|
54
|
+
private TSign?: new (
|
55
|
+
client: SigningCosmWasmClient,
|
56
|
+
sender: string,
|
57
|
+
contractAddress: string
|
58
|
+
) => TSign,
|
59
|
+
private TQuery?: new (
|
60
|
+
client: CosmWasmClient,
|
61
|
+
contractAddress: string
|
62
|
+
) => TQuery,
|
63
|
+
private TMsgComposer?: new (
|
64
|
+
sender: string,
|
65
|
+
contractAddress: string
|
66
|
+
) => TMsgComposer
|
67
|
+
) {}
|
68
|
+
|
69
|
+
public getSigningClient(contractAddr: string): TSign {
|
70
|
+
if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);
|
71
|
+
if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
|
72
|
+
if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);
|
73
|
+
return new this.TSign(
|
74
|
+
this.signingCosmWasmClient,
|
75
|
+
this.address,
|
76
|
+
contractAddr
|
77
|
+
);
|
78
|
+
}
|
79
|
+
|
80
|
+
public getQueryClient(contractAddr: string): TQuery {
|
81
|
+
if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);
|
82
|
+
if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);
|
83
|
+
return new this.TQuery(this.cosmWasmClient, contractAddr);
|
84
|
+
}
|
85
|
+
|
86
|
+
public getMessageComposer(contractAddr: string): TMsgComposer {
|
87
|
+
if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
|
88
|
+
if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);
|
89
|
+
return new this.TMsgComposer(this.address, contractAddr);
|
90
|
+
}
|
91
|
+
}
|
92
|
+
`;
|
@@ -0,0 +1,73 @@
|
|
1
|
+
export const contractsContextTSX = `
|
2
|
+
import React, { useEffect, useMemo, useRef, useState, useContext } from 'react';
|
3
|
+
import {
|
4
|
+
CosmWasmClient,
|
5
|
+
SigningCosmWasmClient,
|
6
|
+
} from '@cosmjs/cosmwasm-stargate';
|
7
|
+
|
8
|
+
import { IContractsContext, getProviders } from './contractContextProviders';
|
9
|
+
|
10
|
+
interface ContractsConfig {
|
11
|
+
address: string | undefined;
|
12
|
+
getCosmWasmClient: () => Promise<CosmWasmClient>;
|
13
|
+
getSigningCosmWasmClient: () => Promise<SigningCosmWasmClient>;
|
14
|
+
}
|
15
|
+
|
16
|
+
const ContractsContext = React.createContext<IContractsContext | null>(null);
|
17
|
+
|
18
|
+
export const ContractsProvider = ({
|
19
|
+
children,
|
20
|
+
contractsConfig,
|
21
|
+
}: {
|
22
|
+
children: React.ReactNode;
|
23
|
+
contractsConfig: ContractsConfig;
|
24
|
+
}) => {
|
25
|
+
const [cosmWasmClient, setCosmWasmClient] = useState<CosmWasmClient>();
|
26
|
+
const [signingCosmWasmClient, setSigningCosmWasmClient] =
|
27
|
+
useState<SigningCosmWasmClient>();
|
28
|
+
|
29
|
+
const { address, getCosmWasmClient, getSigningCosmWasmClient } =
|
30
|
+
contractsConfig;
|
31
|
+
|
32
|
+
const prevAddressRef = useRef<string | undefined>(address);
|
33
|
+
|
34
|
+
const contracts: IContractsContext = useMemo(() => {
|
35
|
+
return getProviders(address, cosmWasmClient, signingCosmWasmClient);
|
36
|
+
}, [address, cosmWasmClient, signingCosmWasmClient]);
|
37
|
+
|
38
|
+
useEffect(() => {
|
39
|
+
const connectSigningCwClient = async () => {
|
40
|
+
if (address && prevAddressRef.current !== address) {
|
41
|
+
const signingCosmWasmClient = await getSigningCosmWasmClient();
|
42
|
+
setSigningCosmWasmClient(signingCosmWasmClient);
|
43
|
+
} else if (!address) {
|
44
|
+
setSigningCosmWasmClient(undefined);
|
45
|
+
}
|
46
|
+
prevAddressRef.current = address;
|
47
|
+
};
|
48
|
+
connectSigningCwClient();
|
49
|
+
}, [address, getSigningCosmWasmClient]);
|
50
|
+
|
51
|
+
useEffect(() => {
|
52
|
+
const connectCosmWasmClient = async () => {
|
53
|
+
const cosmWasmClient = await getCosmWasmClient();
|
54
|
+
setCosmWasmClient(cosmWasmClient);
|
55
|
+
};
|
56
|
+
connectCosmWasmClient();
|
57
|
+
}, [getCosmWasmClient]);
|
58
|
+
|
59
|
+
return (
|
60
|
+
<ContractsContext.Provider value={contracts}>
|
61
|
+
{children}
|
62
|
+
</ContractsContext.Provider>
|
63
|
+
);
|
64
|
+
};
|
65
|
+
|
66
|
+
export const useContracts = () => {
|
67
|
+
const contracts = useContext(ContractsContext);
|
68
|
+
if (contracts === null) {
|
69
|
+
throw new Error('useContracts must be used within a ContractsProvider');
|
70
|
+
}
|
71
|
+
return contracts;
|
72
|
+
};
|
73
|
+
`;
|
package/module/plugins/client.js
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
import { pascal } from
|
2
|
-
import * as w from
|
3
|
-
import { findExecuteMsg, findAndParseTypes, findQueryMsg } from
|
4
|
-
import { RenderContext, getMessageProperties } from
|
5
|
-
import { BuilderPluginBase } from
|
1
|
+
import { pascal } from "case";
|
2
|
+
import * as w from "wasm-ast-types";
|
3
|
+
import { findExecuteMsg, findAndParseTypes, findQueryMsg } from "../utils";
|
4
|
+
import { RenderContext, getMessageProperties } from "wasm-ast-types";
|
5
|
+
import { BuilderPluginBase } from "./plugin-base";
|
6
|
+
export const TYPE = "client";
|
6
7
|
export class ClientPlugin extends BuilderPluginBase {
|
7
8
|
initContext(contract, options) {
|
8
|
-
return new RenderContext(contract, options);
|
9
|
+
return new RenderContext(contract, options, this.builder.builderContext);
|
9
10
|
}
|
10
11
|
|
11
12
|
async doRender(name, context) {
|
@@ -20,8 +21,8 @@ export class ClientPlugin extends BuilderPluginBase {
|
|
20
21
|
const {
|
21
22
|
schemas
|
22
23
|
} = context.contract;
|
23
|
-
const localname = pascal(name) +
|
24
|
-
const TypesFile = pascal(name) +
|
24
|
+
const localname = pascal(name) + ".client.ts";
|
25
|
+
const TypesFile = pascal(name) + ".types";
|
25
26
|
const QueryMsg = findQueryMsg(schemas);
|
26
27
|
const ExecuteMsg = findExecuteMsg(schemas);
|
27
28
|
const typeHash = await findAndParseTypes(schemas);
|
@@ -37,6 +38,7 @@ export class ClientPlugin extends BuilderPluginBase {
|
|
37
38
|
ReadOnlyInstance = pascal(`${name}ReadOnlyInterface`);
|
38
39
|
body.push(w.createQueryInterface(context, ReadOnlyInstance, QueryMsg));
|
39
40
|
body.push(w.createQueryClass(context, QueryClient, ReadOnlyInstance, QueryMsg));
|
41
|
+
context.addProviderInfo(name, w.PROVIDER_TYPES.QUERY_CLIENT_TYPE, QueryClient, localname);
|
40
42
|
} // execute messages
|
41
43
|
|
42
44
|
|
@@ -48,16 +50,17 @@ export class ClientPlugin extends BuilderPluginBase {
|
|
48
50
|
Instance = pascal(`${name}Interface`);
|
49
51
|
body.push(w.createExecuteInterface(context, Instance, this.option.client.execExtendsQuery ? ReadOnlyInstance : null, ExecuteMsg));
|
50
52
|
body.push(w.createExecuteClass(context, Client, Instance, this.option.client.execExtendsQuery ? QueryClient : null, ExecuteMsg));
|
53
|
+
context.addProviderInfo(name, w.PROVIDER_TYPES.SIGNING_CLIENT_TYPE, Client, localname);
|
51
54
|
}
|
52
55
|
}
|
53
56
|
|
54
|
-
if (typeHash.hasOwnProperty(
|
57
|
+
if (typeHash.hasOwnProperty("Coin")) {
|
55
58
|
// @ts-ignore
|
56
59
|
delete context.utils.Coin;
|
57
60
|
}
|
58
61
|
|
59
62
|
return [{
|
60
|
-
type:
|
63
|
+
type: TYPE,
|
61
64
|
localname,
|
62
65
|
body
|
63
66
|
}];
|