mm_contract 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/mm_eth.js +51 -26
  2. package/package.json +46 -44
package/mm_eth.js CHANGED
@@ -1,4 +1,3 @@
1
- require("mm_logs");
2
1
  const Web3 = require("web3");
3
2
  const BigNumber = require("bignumber.js");
4
3
 
@@ -59,6 +58,32 @@ class MM_eth {
59
58
  * 当前账户余额
60
59
  */
61
60
  this.balance = 0;
61
+
62
+ /**
63
+ * 日志记录器(可经 config.logger 注入,缺省使用 console)
64
+ * @type {Object}
65
+ */
66
+ this._logger = this.config.logger || console;
67
+ }
68
+
69
+ /**
70
+ * 设置日志记录器
71
+ * @param {Object} logger 日志记录器(需实现 debug/info/warn/error)
72
+ * @returns {MM_eth} 当前实例
73
+ */
74
+ setLogger(logger) {
75
+ if (logger) {
76
+ this._logger = logger;
77
+ }
78
+ return this;
79
+ }
80
+
81
+ /**
82
+ * 获取日志记录器
83
+ * @returns {Object} 日志记录器
84
+ */
85
+ getLogger() {
86
+ return this._logger;
62
87
  }
63
88
  }
64
89
 
@@ -143,18 +168,18 @@ MM_eth.prototype.init = async function(way) {
143
168
  * @return {String} 返回地址
144
169
  */
145
170
  MM_eth.prototype.send = async function(type, name, method, value, ...param) {
146
- $.log.debug("send req", type, name, method, value, ...param);
171
+ this._logger.debug("send req", type, name, method, value, ...param);
147
172
  try {
148
173
  var res = await this[type][name].contract.methods[method](...param).send({
149
174
  from: this.address,
150
175
  gasLimit: this.config.gas,
151
176
  value: value
152
177
  });
153
- $.log.debug("send res", type, name, method, res);
178
+ this._logger.debug("send res", type, name, method, res);
154
179
  } catch (err) {
155
- $.log.error(method, err);
180
+ this._logger.error(method, err);
156
181
  }
157
- $.log.debug("send res", name, method, res);
182
+ this._logger.debug("send res", name, method, res);
158
183
  return res;
159
184
  };
160
185
 
@@ -170,15 +195,15 @@ MM_eth.prototype.call = async function(type, name, method, value, ...param) {
170
195
  if (!this.address) {
171
196
  await this.init();
172
197
  }
173
- $.log.debug("call req", type, name, method, ...param);
198
+ this._logger.debug("call req", type, name, method, ...param);
174
199
  try {
175
200
  var res = await this[type][name].contract.methods[method](...param).call({
176
201
  from: this.address
177
202
  });
178
203
  } catch (err) {
179
- $.log.error(method, err);
204
+ this._logger.error(method, err);
180
205
  }
181
- // $.log.debug("call res", name, method, res);
206
+ // this._logger.debug("call res", name, method, res);
182
207
  return res;
183
208
  };
184
209
 
@@ -190,7 +215,7 @@ MM_eth.prototype.call = async function(type, name, method, value, ...param) {
190
215
  * @param {Number} chainId 区块链频道
191
216
  */
192
217
  MM_eth.prototype.eventAfter = function(name, accounts, chainId) {
193
- // $.log.debug(name);
218
+ // this._logger.debug(name);
194
219
  };
195
220
 
196
221
  /**
@@ -200,7 +225,7 @@ MM_eth.prototype.eventAfter = function(name, accounts, chainId) {
200
225
  * @param {Number} chainId 区块链频道
201
226
  */
202
227
  MM_eth.prototype.event = function(name, accounts, chainId) {
203
- $.log.debug("event:", name, accounts, chainId);
228
+ this._logger.debug("event:", name, accounts, chainId);
204
229
  if (accounts && accounts.length) {
205
230
  this.address = pdr.selectedAddress || accounts[0];
206
231
  }
@@ -240,7 +265,7 @@ MM_eth.prototype.event = function(name, accounts, chainId) {
240
265
  try {
241
266
  o(name, this.address, this.chainId);
242
267
  } catch (e) {
243
- $.log.debug(e);
268
+ this._logger.debug(e);
244
269
  }
245
270
  }
246
271
  this.eventAfter(name, accounts, this.chainId);
@@ -289,9 +314,9 @@ MM_eth.prototype.subscribeEvent = function(provider) {
289
314
  chainId,
290
315
  accounts
291
316
  } = payload;
292
- $.log.debug("连接成功");
293
- $.log.debug(chainId, accounts);
294
- $.log.debug(Object.keys(payload));
317
+ this._logger.debug("连接成功");
318
+ this._logger.debug(chainId, accounts);
319
+ this._logger.debug(Object.keys(payload));
295
320
  this.event('connect', accounts, this.toChage10(chainId, 1));
296
321
  });
297
322
 
@@ -421,15 +446,15 @@ MM_eth.prototype.sendS = async function(type, name, method, value, ...param) {
421
446
  let address_my = this.web3.eth.defaultAccount || this.address;
422
447
 
423
448
  if (!this[type]) {
424
- $.log.error("合约类型不正确");
449
+ this._logger.error("合约类型不正确");
425
450
  return
426
451
  }
427
452
  if (!this[type][name]) {
428
- $.log.error("合约不正确");
453
+ this._logger.error("合约不正确");
429
454
  return
430
455
  }
431
456
  if (!this[type][name].contract.methods[method]) {
432
- $.log.error("函数不存在!");
457
+ this._logger.error("函数不存在!");
433
458
  return
434
459
  }
435
460
 
@@ -455,7 +480,7 @@ MM_eth.prototype.sendS = async function(type, name, method, value, ...param) {
455
480
  // $.log.info("数据", rawTx);
456
481
  var sign_data = await this.web3.eth.accounts.signTransaction(rawTx, this.config.private_key);
457
482
  if (!sign_data) {
458
- $.log.error("签名失败", rawTx, err);
483
+ this._logger.error("签名失败", rawTx, err);
459
484
  return null;
460
485
  }
461
486
  // $.log.info("签名", sign_data);
@@ -464,7 +489,7 @@ MM_eth.prototype.sendS = async function(type, name, method, value, ...param) {
464
489
  try {
465
490
  tx = await this.web3.eth.sendSignedTransaction(sign_data.rawTransaction);
466
491
  } catch (err) {
467
- $.log.error("链上交易超时", rawTx, err);
492
+ this._logger.error("链上交易超时", rawTx, err);
468
493
  }
469
494
  // $.log.info("广播", tx);
470
495
  if (tx) {
@@ -476,7 +501,7 @@ MM_eth.prototype.sendS = async function(type, name, method, value, ...param) {
476
501
  this.nonce -= 1
477
502
  }
478
503
 
479
- $.log.debug('send res', name, tx);
504
+ this._logger.debug('send res', name, tx);
480
505
  return tx;
481
506
  };
482
507
 
@@ -492,20 +517,20 @@ MM_eth.prototype.sendS = async function(type, name, method, value, ...param) {
492
517
  * @return {String} return address
493
518
  */
494
519
  MM_eth.prototype.callObj = async function(type, name, method, keys, ...param) {
495
- $.log.debug("call req", type, name, method, ...param);
520
+ this._logger.debug("call req", type, name, method, ...param);
496
521
  try {
497
522
  var res = await this[type][name].contract.methods[method](...param).call({
498
523
  from: this.address
499
524
  });
500
525
  } catch (err) {
501
- $.log.error(method, err)
526
+ this._logger.error(method, err)
502
527
  }
503
528
  var arr = res.split('_');
504
529
  var obj = {};
505
530
  for(var i = 0; i < keys.length; i++){
506
531
  obj[keys[i]] = arr.length > i ? arr[i] : 0;
507
532
  }
508
- $.log.debug("call res", name, method, obj);
533
+ this._logger.debug("call res", name, method, obj);
509
534
  return obj;
510
535
  };
511
536
 
@@ -518,13 +543,13 @@ MM_eth.prototype.callObj = async function(type, name, method, keys, ...param) {
518
543
  * @return {String} return address
519
544
  */
520
545
  MM_eth.prototype.callList = async function(type, name, method, keys, ...param) {
521
- $.log.debug("call req", type, name, method, ...param);
546
+ this._logger.debug("call req", type, name, method, ...param);
522
547
  try {
523
548
  var res = await this[type][name].contract.methods[method](...param).call({
524
549
  from: this.address
525
550
  });
526
551
  } catch (err) {
527
- $.log.error(method, err)
552
+ this._logger.error(method, err)
528
553
  }
529
554
  var list = res.map((o) => {
530
555
  var arr = o.split('_');
@@ -534,7 +559,7 @@ MM_eth.prototype.callList = async function(type, name, method, keys, ...param) {
534
559
  }
535
560
  return obj
536
561
  });
537
- $.log.debug("call res", name, method, list);
562
+ this._logger.debug("call res", name, method, list);
538
563
  return list;
539
564
  };
540
565
 
package/package.json CHANGED
@@ -1,44 +1,46 @@
1
- {
2
- "name": "mm_contract",
3
- "version": "1.0.0",
4
- "description": "这是超级美眉智能合约调用辅助模块,用于快速发布合约和调用区块链ABI方法,适用于eth和tron。",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js",
8
- "dev": "nodemon ./demo/index.js"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://gitee.com/qiuwenwu91/mm_contract.git"
13
- },
14
- "keywords": [
15
- "mm_mod",
16
- "mm_adapter",
17
- "mm_ext_service",
18
- "MM",
19
- "超级美眉",
20
- "区块链",
21
- "智能合约",
22
- "浏览器钱包",
23
- "ETH",
24
- "TRON",
25
- "以太坊",
26
- "波场",
27
- "发布",
28
- "调用ABI",
29
- "Contract"
30
- ],
31
- "layer": "adapter",
32
- "group": "ext_service",
33
- "author": "邱文武(qiuwenwu)",
34
- "license": "ISC",
35
- "bugs": {
36
- "url": "https://gitee.com/qiuwenwu91/mm_contract/issues"
37
- },
38
- "homepage": "https://gitee.com/qiuwenwu91/mm_contract#readme",
39
- "dependencies": {
40
- "bignumber": "^1.1.0",
41
- "mm_logs": "^1.0.9",
42
- "web3": "^1.3.0"
43
- }
44
- }
1
+ {
2
+ "name": "mm_contract",
3
+ "version": "1.0.1",
4
+ "description": "这是超级美眉智能合约调用辅助模块,用于快速发布合约和调用区块链ABI方法,适用于eth和tron。",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js",
8
+ "dev": "nodemon ./demo/index.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://gitee.com/qiuwenwu91/mm_contract.git"
13
+ },
14
+ "keywords": [
15
+ "mm_mod",
16
+ "mm_adapter",
17
+ "mm_ext_service",
18
+ "MM",
19
+ "超级美眉",
20
+ "区块链",
21
+ "智能合约",
22
+ "浏览器钱包",
23
+ "ETH",
24
+ "TRON",
25
+ "以太坊",
26
+ "波场",
27
+ "发布",
28
+ "调用ABI",
29
+ "Contract"
30
+ ],
31
+ "layer": "adapter",
32
+ "group": "ext_service",
33
+ "author": "邱文武(qiuwenwu)",
34
+ "license": "ISC",
35
+ "bugs": {
36
+ "url": "https://gitee.com/qiuwenwu91/mm_contract/issues"
37
+ },
38
+ "homepage": "https://gitee.com/qiuwenwu91/mm_contract#readme",
39
+ "dependencies": {
40
+ "bignumber": "^1.1.0",
41
+ "web3": "^1.3.0"
42
+ },
43
+ "devDependencies": {
44
+ "mm_logs": "^1.0.9"
45
+ }
46
+ }