openchain-nodejs-ts-yxl 1.0.6 → 1.0.8

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/index.d.ts CHANGED
@@ -992,6 +992,51 @@ export interface ContractInvokeResult {
992
992
  logs: string;
993
993
  }
994
994
 
995
+ /**
996
+ * 合约调用参数
997
+ * @interface
998
+ * @property {string} [contractAddress] 合约账户地址
999
+ * @property {string} [sourceAddress] 调用者账户地址
1000
+ * @property {string} [code] 合约代码
1001
+ * @property {string} [input] 合约调用输入数据
1002
+ * @property {string} [contractBalance] 合约账户余额
1003
+ * @property {number} optType 操作类型(1-查询,2-调用)
1004
+ * @property {string} [feeLimit] 交易费用上限
1005
+ * @property {string} [gasPrice] Gas单价
1006
+ */
1007
+ export interface ContractCallParams {
1008
+ contractAddress?: string;
1009
+ sourceAddress?: string;
1010
+ code?: string;
1011
+ input?: string;
1012
+ contractBalance?: string;
1013
+ optType: number;
1014
+ feeLimit?: string;
1015
+ gasPrice?: string;
1016
+ }
1017
+
1018
+ /**
1019
+ * 合约调用结果
1020
+ * @interface
1021
+ * @property {string[]} queryRets 查询返回结果列表
1022
+ * @property {string} logs 合约执行日志
1023
+ * @property {Object} stat 合约执行统计信息
1024
+ * @property {number} stat.apply_time 执行时间(毫秒)
1025
+ * @property {number} stat.memory_usage 内存使用量(字节)
1026
+ * @property {number} stat.stack_usage 堆栈使用量(字节)
1027
+ * @property {number} stat.step 执行步数
1028
+ */
1029
+ export interface ContractCallResult {
1030
+ queryRets: string[];
1031
+ logs: string;
1032
+ stat: {
1033
+ apply_time: number;
1034
+ memory_usage: number;
1035
+ stack_usage: number;
1036
+ step: number;
1037
+ };
1038
+ }
1039
+
995
1040
  export interface OperationResult {
996
1041
  type: string;
997
1042
  data: Record<string, any>;
@@ -1,10 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const request = require('request-promise');
3
+ const axios = require('axios');
4
4
  const is = require('is-type-of');
5
5
  const long = require('long');
6
6
  const JSONbig = require('json-bigint');
7
- // const bigNumberToString = require('bignumber-to-string')
8
7
  const BigNumber = require('bignumber.js');
9
8
  const protobuf = require("protobufjs");
10
9
  const tou8 = require('buffer-to-uint8array');
@@ -12,10 +11,8 @@ const humps = require('humps');
12
11
  const { keypair, signature } = require('yxchain-encryption-nodejs');
13
12
  const errors = require('../exception');
14
13
 
15
-
16
14
  const proto = exports;
17
15
 
18
-
19
16
  /**
20
17
  * GET/POST request
21
18
  *
@@ -24,7 +21,7 @@ const proto = exports;
24
21
  * @param {Object} data
25
22
  * @return {Object}
26
23
  */
27
- proto._request = function* (method, path, data = {}) {
24
+ proto._request = async function (method, path, data = {}) {
28
25
  try {
29
26
  const protocol = this.options.secure ? 'https://' : 'http://';
30
27
  const uri = `${protocol}${this.options.host}/${path}`;
@@ -37,7 +34,7 @@ proto._request = function* (method, path, data = {}) {
37
34
  throw new Error('path must be a non-empty string');
38
35
  }
39
36
 
40
- const methods = [ 'get', 'post' ];
37
+ const methods = ['get', 'post'];
41
38
 
42
39
  if (!methods.includes(method.toLowerCase())) {
43
40
  throw new Error(`${method} http method is not supported`);
@@ -45,21 +42,23 @@ proto._request = function* (method, path, data = {}) {
45
42
 
46
43
  const options = {
47
44
  method,
48
- uri,
45
+ url: uri,
49
46
  timeout: this.options.timeout,
50
47
  };
51
48
 
52
- if (method === 'get') {
53
- options.qs = data;
49
+ if (method.toLowerCase() === 'get') {
50
+ options.params = data;
54
51
  }
55
52
 
56
- if (method === 'post') {
57
- options.body = data;
53
+ if (method.toLowerCase() === 'post') {
54
+ options.data = data;
58
55
  }
59
- const result = yield request(options);
60
- const obj = JSONbig.parse(result);
56
+
57
+ const response = await axios(options);
58
+ const result = response.data;
59
+ const obj = typeof result === 'string' ? JSONbig.parse(result) : result;
61
60
  const error_code = obj.error_code;
62
- const final = this._bigNumberToString(obj);
61
+ const final = this._bigNumberToString(obj);
63
62
  final.error_code = error_code;
64
63
  return final;
65
64
  } catch (err) {
@@ -877,4 +876,4 @@ proto._isAvailableBu = function (str) {
877
876
  long.fromValue(str).greaterThanOrEqual(0) &&
878
877
  long.fromValue(str).lessThanOrEqual(long.MAX_VALUE.divide(Math.pow(10, 8)))
879
878
  );
880
- }
879
+ }
package/lib/util/index.js CHANGED
@@ -1,86 +1,148 @@
1
1
  'use strict';
2
2
 
3
- const wrap = require('co-wrap-all');
4
- const merge = require('merge-descriptors');
5
- const BigNumber = require('bignumber.js');
6
3
  const is = require('is-type-of');
4
+ const BigNumber = require('bignumber.js');
7
5
  const long = require('long');
6
+ const axios = require('axios');
7
+ const JSONbig = require('json-bigint');
8
+ const errors = require('../exception');
9
+
10
+ const util = exports;
11
+
12
+ /**
13
+ * check if the object is BigNumber
14
+ * @param {Object} object
15
+ * @return {Boolean}
16
+ */
17
+ util.isBigNumber = function (object) {
18
+ return object instanceof BigNumber;
19
+ };
8
20
 
9
- module.exports = Util;
10
-
11
- function Util(options) {
12
- if (!(this instanceof Util)) {
13
- return new Util(options);
14
- }
15
-
16
- this.options = options;
17
- }
21
+ /**
22
+ * create BigNumber object
23
+ * @param {String | Number} value
24
+ * @return {BigNumber}
25
+ */
26
+ util.toBigNumber = function (value) {
27
+ return new BigNumber(value);
28
+ };
18
29
 
19
- const proto = Util.prototype;
30
+ /**
31
+ * encode string to hex string
32
+ * @param {String} str
33
+ * @return {String} hex string
34
+ */
35
+ util.utfToHex = function (str) {
36
+ if (is.string(str) || is.number(str)) {
37
+ let hex = '';
20
38
 
21
- merge(proto, require('../common/util'));
39
+ for (let i = 0; i < str.length; i++) {
40
+ hex += str.charCodeAt(i).toString(16);
41
+ }
22
42
 
23
- proto.isBigNumber = function (object) {
24
- return this._isBigNumber(object);
25
- };
43
+ return hex;
44
+ }
26
45
 
27
- proto.toBigNumber = function(data) {
28
- return this._toBigNumber(data);
46
+ return str;
29
47
  };
30
48
 
31
- proto.utfToHex = function(str) {
32
- if (!is.string(str)) {
33
- return;
34
- }
49
+ /**
50
+ * decode hex string to string
51
+ * @param {String} str hex string
52
+ * @return {String}
53
+ */
54
+ util.hexToUtf = function (str) {
55
+ if (is.string(str) || is.number(str)) {
56
+ let utf = '';
35
57
 
36
- return Buffer.from(str, 'utf8').toString('hex');
37
- };
58
+ for (let i = 0; i < str.length; i += 2) {
59
+ utf += String.fromCharCode(parseInt(str.substr(i, 2), 16));
60
+ }
38
61
 
39
- proto.hexToUtf = function(str) {
40
- if (!is.string(str) ||
41
- str === '' ||
42
- !this._isHexString(str)) {
43
- return;
62
+ return utf;
44
63
  }
45
64
 
46
- return Buffer.from(str, 'hex').toString('utf8');
65
+ return str;
47
66
  };
48
67
 
49
- proto.buToMo = function(bu) {
50
- if (!this._isAvailableBu(bu)) {
51
- return '';
52
- }
68
+ /**
69
+ * convert bu to mo
70
+ * @param {String} str
71
+ * @return {String}
72
+ */
73
+ util.buToMo = function (str) {
74
+ return new BigNumber(str).multipliedBy(100000000).toString(10);
75
+ };
53
76
 
54
- const oneMo = Math.pow(10, 8);
55
- const mo = new BigNumber(bu).times(oneMo);
56
- return mo.toString();
77
+ /**
78
+ * convert mo to bu
79
+ * @param {String} str
80
+ * @return {String}
81
+ */
82
+ util.moToBu = function (str) {
83
+ return new BigNumber(str).dividedBy(100000000).toString(10);
57
84
  };
58
85
 
59
- proto.moToBu = function(mo) {
60
- if (!this._isAvailableValue(mo)) {
61
- return '';
86
+ /**
87
+ * calculate amount with decimals
88
+ * @param {String} amount
89
+ * @param {String} decimals
90
+ * @return {String | false}
91
+ */
92
+ util.unitWithDecimals = function (amount, decimals) {
93
+ if (!is.string(decimals) || !is.string(amount)) {
94
+ return false;
62
95
  }
63
96
 
64
- const oneMo = Math.pow(10, 8);
65
- const bu = new BigNumber(mo).dividedBy(oneMo);
66
- return bu.toString();
67
- };
68
-
69
- proto.unitWithDecimals = function(amount, decimals) {
70
- const reg = /^[0-9]+$/;
97
+ const decimalsBN = new BigNumber(decimals);
98
+ const amountBN = new BigNumber(amount);
71
99
 
72
- if (!reg.test(amount) || !reg.test(decimals)) {
100
+ if (decimalsBN.isNaN() || amountBN.isNaN()) {
73
101
  return false;
74
102
  }
75
103
 
76
- amount = new BigNumber(amount);
77
- decimals = new BigNumber(Math.pow(10, decimals));
78
- const amountWithDecimals = amount.times(decimals);
104
+ return amountBN.multipliedBy(Math.pow(10, decimals)).toString(10);
105
+ };
79
106
 
80
- if (amountWithDecimals.isGreaterThanOrEqualTo(0) && amountWithDecimals.isLessThanOrEqualTo(long.MAX_VALUE.toString())) {
81
- return amountWithDecimals.toString();
107
+ /**
108
+ * http request
109
+ * @param {String} method
110
+ * @param {String} url
111
+ * @param {Object} data
112
+ * @return {Object}
113
+ */
114
+ util.httpRequest = async function (method, url, data = {}) {
115
+ try {
116
+ if (!is.string(method) || !is.string(url)) {
117
+ throw new Error('method and url must be string');
118
+ }
119
+
120
+ const methods = ['get', 'post'];
121
+
122
+ if (!methods.includes(method.toLowerCase())) {
123
+ throw new Error(`${method} http method is not supported`);
124
+ }
125
+
126
+ const options = {
127
+ method,
128
+ url,
129
+ timeout: 3000,
130
+ };
131
+
132
+ if (method.toLowerCase() === 'get') {
133
+ options.params = data;
134
+ }
135
+
136
+ if (method.toLowerCase() === 'post') {
137
+ options.data = data;
138
+ }
139
+
140
+ const response = await axios(options);
141
+ const result = response.data;
142
+ return typeof result === 'string' ? JSONbig.parse(result) : result;
143
+ } catch (err) {
144
+ throw err;
82
145
  }
83
- return false;
84
- }
146
+ };
85
147
 
86
- wrap(proto);
148
+ module.exports = util;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openchain-nodejs-ts-yxl",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "openchain sdk",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -14,6 +14,7 @@
14
14
  "author": "OpenChain",
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
+ "axios": "^1.6.2",
17
18
  "bignumber.js": "^7.2.1",
18
19
  "buffer-to-uint8array": "^1.1.0",
19
20
  "co-wrap-all": "^1.0.0",
@@ -23,8 +24,6 @@
23
24
  "long": "^4.0.0",
24
25
  "merge-descriptors": "^1.0.1",
25
26
  "protobufjs": "^6.8.8",
26
- "request": "^2.87.0",
27
- "request-promise": "^4.2.2",
28
27
  "yxchain-encryption-nodejs": "^1.0.2"
29
28
  },
30
29
  "devDependencies": {