mm_contract 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,2 +1,11 @@
1
- # mm_contract
2
- 这是超级美眉智能合约调用辅助模块,用于快速发布合约和调用区块链ABI方法,适用于eth链及其衍生链
1
+ # mm_contract
2
+ 这是超级美眉智能合约调用辅助模块,用于快速发布合约和调用区块链ABI方法,适用于eth链及其衍生链
3
+
4
+ ## 工具契约(LLM 可调用)
5
+
6
+ 本模块 `tool/` 下登记以下工具契约,实现位于 `lib/tools/<name>/`,`tool/<name>/index.js` 仅为薄适配层。
7
+ 其它模块可直接 `require('<pkg>').tools.<name>` 复用实现,无需经过工具注册面。
8
+
9
+ | 工具名 | 标题 | 说明 |
10
+ | --- | --- | --- |
11
+ | `contract` | 链上合约账户与调用 | 默认账户、余额、签名、只读调用 |
package/index.js CHANGED
@@ -1,22 +1,4 @@
1
- var Web3 = require('web3');
2
- var mm_eth = require('./mm_eth.js');
3
- var mm_tron = require('./mm_tron.js');
4
-
5
- /**
6
- * 方法
7
- */
8
- class mm_contract {
9
- /**
10
- * 构造函数
11
- * @param {Object} config 配置参数
12
- */
13
- constructor(config, type) {
14
- if (type == "tron") {
15
- return new mm_tron(config);
16
- } else {
17
- return new mm_eth(config);
18
- }
19
- }
20
- }
21
-
22
- module.exports = mm_contract;
1
+ 'use strict';
2
+ const mm_contract = require('./lib/mm_contract');
3
+
4
+ module.exports = { mm_contract };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+ let mm_eth = require('../mm_eth.js');
3
+ let mm_tron = require('../mm_tron.js');
4
+
5
+ /**
6
+ * 合约分发类,按链类型返回对应实现实例。
7
+ */
8
+ class mm_contract {
9
+ /**
10
+ * 按链类型创建合约实例
11
+ * @param {Object} config 配置参数
12
+ * @param {string} [type] 链类型,取 tron 时返回 tron 实现
13
+ * @returns {Object} 对应链的合约实例
14
+ */
15
+ constructor(config, type) {
16
+ if (type == "tron") {
17
+ return new mm_tron(config);
18
+ } else {
19
+ return new mm_eth(config);
20
+ }
21
+ }
22
+ }
23
+
24
+ module.exports = mm_contract;
@@ -0,0 +1,290 @@
1
+ "use strict";
2
+ const MM_eth = require("../mm_eth.js");
3
+
4
+
5
+ /**
6
+ * 转账
7
+ * @param {String} type 类型
8
+ * @param {String} name 名称
9
+ * @param {String} method 合约方法
10
+ * @param {Number} value 转账数量
11
+ * @param {Array} 合约传参
12
+ * @return {Object} 成功返回转账结果, 否则返回空
13
+ */
14
+ MM_eth.prototype.sendS = async function(type, name, method, value, ...param) {
15
+ let address_my = this.web3.eth.defaultAccount || this.address;
16
+
17
+ if (!this[type]) {
18
+ this._logger.error("合约类型不正确");
19
+ return
20
+ }
21
+ if (!this[type][name]) {
22
+ this._logger.error("合约不正确");
23
+ return
24
+ }
25
+ if (!this[type][name].contract.methods[method]) {
26
+ this._logger.error("函数不存在!");
27
+ return
28
+ }
29
+
30
+ let data = await this[type][name].contract.methods[method](...param).encodeABI();
31
+ let contract_address = this[type][name].address;
32
+
33
+
34
+ let nonce = await this.getNonce();
35
+
36
+ let rawTx = {
37
+ // from: address_my,
38
+ nonce,
39
+ // eth用 0x14F46B0400, bsc用 0x12A05F200
40
+ gasPrice: "0x12A05F200",
41
+ gasLimit: '0x249F0',
42
+ to: contract_address,
43
+ // 调用receiveEth 给合约转入0.1eth ,如果调用合约方法不给合约转入eth,这个值是0
44
+ value: value ? this.toChage16(value) : '0x0',
45
+ // 这个数据需要自己拼接,在gitee/gkai/metamask里面有方法(通过encodeABI可以不用自己拼接了)
46
+ data
47
+ };
48
+
49
+ // $.log.info("数据", rawTx);
50
+ let sign_data = await this.web3.eth.accounts.signTransaction(rawTx, this.config.private_key);
51
+ if (!sign_data) {
52
+ this._logger.error("签名失败", rawTx, err);
53
+ return null;
54
+ }
55
+ // $.log.info("签名", sign_data);
56
+ // $.sleep(1000);
57
+ let tx = null;
58
+ try {
59
+ tx = await this.web3.eth.sendSignedTransaction(sign_data.rawTransaction);
60
+ } catch (err) {
61
+ this._logger.error("链上交易超时", rawTx, err);
62
+ }
63
+ // $.log.info("广播", tx);
64
+ if (tx) {
65
+ if (!tx.status) {
66
+ this.nonce = 0;
67
+ return null;
68
+ }
69
+ } else {
70
+ this.nonce -= 1
71
+ }
72
+
73
+ this._logger.debug('send res', name, tx);
74
+ return tx;
75
+ };
76
+
77
+
78
+
79
+ /** 追加 **/
80
+ /**
81
+ * call obj
82
+ * @param {String} name Login Method
83
+ * @param {String} method ABI method
84
+ * @param {Array} keys object prop
85
+ * @param {Array} param parameter set
86
+ * @return {String} return address
87
+ */
88
+ MM_eth.prototype.callObj = async function(type, name, method, keys, ...param) {
89
+ this._logger.debug("call req", type, name, method, ...param);
90
+ try {
91
+ let res = await this[type][name].contract.methods[method](...param).call({
92
+ from: this.address
93
+ });
94
+ } catch (err) {
95
+ this._logger.error(method, err)
96
+ }
97
+ let arr = res.split('_');
98
+ let obj = {};
99
+ for(let i = 0; i < keys.length; i++){
100
+ obj[keys[i]] = arr.length > i ? arr[i] : 0;
101
+ }
102
+ this._logger.debug("call res", name, method, obj);
103
+ return obj;
104
+ };
105
+
106
+ /**
107
+ * call list
108
+ * @param {String} name Login Method
109
+ * @param {String} method ABI method
110
+ * @param {Array} keys object prop
111
+ * @param {Array} param parameter set
112
+ * @return {String} return address
113
+ */
114
+ MM_eth.prototype.callList = async function(type, name, method, keys, ...param) {
115
+ this._logger.debug("call req", type, name, method, ...param);
116
+ try {
117
+ let res = await this[type][name].contract.methods[method](...param).call({
118
+ from: this.address
119
+ });
120
+ } catch (err) {
121
+ this._logger.error(method, err)
122
+ }
123
+ let list = res.map((o) => {
124
+ let arr = o.split('_');
125
+ let obj = {};
126
+ for(let i = 0; i < keys.length; i++){
127
+ obj[keys[i]] = arr.length > i ? arr[i] : 0;
128
+ }
129
+ return obj
130
+ });
131
+ this._logger.debug("call res", name, method, list);
132
+ return list;
133
+ };
134
+
135
+ /**
136
+ * Add event
137
+ * @param {Function} name event name
138
+ * @param {Function} func event method
139
+ * @returns {Funtion} this event function key
140
+ */
141
+ MM_eth.prototype.addEvent = function(func, name) {
142
+ if (!name) {
143
+ this.list_count++;
144
+ name = this.list_count;
145
+ }
146
+ this.list_event[name] = func;
147
+ return name;
148
+ };
149
+
150
+ /**
151
+ * Delete event
152
+ * @param {Function} name event name
153
+ */
154
+ MM_eth.prototype.delEvent = function(name) {
155
+ delete this.list_event[name];
156
+ };
157
+
158
+ /**
159
+ * execute provider
160
+ * @param {Object} provider
161
+ */
162
+ MM_eth.prototype.subscribeEvent = function(provider) {
163
+ if (!provider) {
164
+ return;
165
+ }
166
+ // Subscribe to accounts change
167
+ provider.on("accountsChanged", (accounts) => {
168
+ this.event("accountsChanged", accounts)
169
+ });
170
+
171
+ // Subscribe to chainId change
172
+ provider.on("chainChanged", (chainId) => {
173
+ this.event("chainChanged", null, this.toChage10(chainId, 1))
174
+ });
175
+
176
+ /**
177
+ * This event is fired when a transaction is initiated
178
+ */
179
+ provider.on("session_update", async (error, payload) => {
180
+ if (error) {
181
+ throw error;
182
+ }
183
+ let {
184
+ chainId,
185
+ accounts
186
+ } = payload.params[0];
187
+ this.event("session_update", accounts, this.toChage10(chainId, 1));
188
+ });
189
+
190
+ /**
191
+ * when connected
192
+ */
193
+ provider.on("connect", (error, payload) => {
194
+ if (error) {
195
+ throw error;
196
+ }
197
+ if (!payload) {
198
+ return;
199
+ }
200
+ let {
201
+ chainId,
202
+ accounts
203
+ } = payload;
204
+ this.event('connect', accounts, this.toChage10(chainId, 1));
205
+ });
206
+
207
+ /**
208
+ * when disconnected
209
+ */
210
+ provider.on("disconnect", (error, payload) => {
211
+ if (error) {
212
+ throw error;
213
+ }
214
+ this.event('disconnect');
215
+ });
216
+ };
217
+
218
+ /**
219
+ * Web3 签名加密
220
+ * @param {String} content 签名内容
221
+ * @param {String} address 签名地址
222
+ * @param {Function} func 回调函数(可选)
223
+ */
224
+ MM_eth.prototype.sign = async function(content, address, func) {
225
+ let ret = null;
226
+ if (func) {
227
+ this.web3.eth.sign(content, address, func);
228
+ } else {
229
+ ret = await this.web3.eth.sign(content, address);
230
+ }
231
+ return ret;
232
+ }
233
+
234
+ MM_eth.prototype.getBalance = async function(address) {
235
+ let {
236
+ chain_accuracy
237
+ } = this.config;
238
+ let y = Math.pow(10, chain_accuracy);
239
+
240
+ if (!this.default_address) {
241
+ await this.getAccounts();
242
+ }
243
+
244
+ if (!address) {
245
+ address = this.default_address;
246
+ }
247
+ // 查询余额
248
+ let num_str = await this.web3.eth.getBalance(address);
249
+ // 转为大数字类型
250
+ let num_b = new BigNumber(num_str);
251
+ // 除以小数位
252
+ let balance = num_b.div(y).toNumber();
253
+ // 保存余额
254
+ this.balance = balance;
255
+ return balance;
256
+ };
257
+
258
+ /**
259
+ * 获取以太网上的账户
260
+ * @return {Array} 账户数组
261
+ */
262
+ MM_eth.prototype.getAccount = async function() {
263
+ this.default_address = this.web3.eth.defaultAccount;
264
+ return this.default_address;
265
+ };
266
+
267
+ /**
268
+ * 获取以太网上的账户
269
+ * @return {Array} 账户数组
270
+ */
271
+ MM_eth.prototype.getAccounts = async function() {
272
+ let accounts = await this.web3.eth.getAccounts();
273
+ if (accounts.length) {
274
+ this.web3.eth.defaultAccount = accounts[0];
275
+ this.default_address = this.web3.eth.defaultAccount;
276
+ }
277
+ return accounts;
278
+ };
279
+
280
+ /**
281
+ * 加载ABI
282
+ * @param {String} path 地址
283
+ * @return {String} 加载成功返回true
284
+ */
285
+ MM_eth.prototype.loadAbi = function(abi) {
286
+
287
+ }
288
+
289
+
290
+ module.exports = {};