openchain-nodejs-ts-yxl 1.0.8 → 1.1.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.
- package/index.d.ts +20 -0
- package/lib/common/util.js +693 -685
- package/lib/util/index.js +57 -119
- package/package.json +1 -1
package/lib/util/index.js
CHANGED
@@ -1,148 +1,86 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
const
|
3
|
+
const wrap = require('co-wrap-all');
|
4
|
+
const merge = require('merge-descriptors');
|
4
5
|
const BigNumber = require('bignumber.js');
|
6
|
+
const is = require('is-type-of');
|
5
7
|
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
|
-
};
|
20
8
|
|
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
|
-
};
|
9
|
+
module.exports = Util;
|
29
10
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
*/
|
35
|
-
util.utfToHex = function (str) {
|
36
|
-
if (is.string(str) || is.number(str)) {
|
37
|
-
let hex = '';
|
11
|
+
function Util(options) {
|
12
|
+
if (!(this instanceof Util)) {
|
13
|
+
return new Util(options);
|
14
|
+
}
|
38
15
|
|
39
|
-
|
40
|
-
|
41
|
-
}
|
16
|
+
this.options = options;
|
17
|
+
}
|
42
18
|
|
43
|
-
|
44
|
-
}
|
19
|
+
const proto = Util.prototype;
|
45
20
|
|
46
|
-
|
47
|
-
};
|
21
|
+
merge(proto, require('../common/util'));
|
48
22
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
* @return {String}
|
53
|
-
*/
|
54
|
-
util.hexToUtf = function (str) {
|
55
|
-
if (is.string(str) || is.number(str)) {
|
56
|
-
let utf = '';
|
23
|
+
proto.isBigNumber = function (object) {
|
24
|
+
return this._isBigNumber(object);
|
25
|
+
};
|
57
26
|
|
58
|
-
|
59
|
-
|
60
|
-
|
27
|
+
proto.toBigNumber = function(data) {
|
28
|
+
return this._toBigNumber(data);
|
29
|
+
};
|
61
30
|
|
62
|
-
|
31
|
+
proto.utfToHex = function(str) {
|
32
|
+
if (!is.string(str)) {
|
33
|
+
return;
|
63
34
|
}
|
64
35
|
|
65
|
-
return str;
|
36
|
+
return Buffer.from(str, 'utf8').toString('hex');
|
66
37
|
};
|
67
38
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
39
|
+
proto.hexToUtf = function(str) {
|
40
|
+
if (!is.string(str) ||
|
41
|
+
str === '' ||
|
42
|
+
!this._isHexString(str)) {
|
43
|
+
return;
|
44
|
+
}
|
45
|
+
|
46
|
+
return Buffer.from(str, 'hex').toString('utf8');
|
75
47
|
};
|
76
48
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
49
|
+
proto.buToMo = function(bu) {
|
50
|
+
if (!this._isAvailableBu(bu)) {
|
51
|
+
return '';
|
52
|
+
}
|
53
|
+
|
54
|
+
const oneMo = Math.pow(10, 8);
|
55
|
+
const mo = new BigNumber(bu).times(oneMo);
|
56
|
+
return mo.toString();
|
84
57
|
};
|
85
58
|
|
86
|
-
|
87
|
-
|
88
|
-
|
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;
|
59
|
+
proto.moToBu = function(mo) {
|
60
|
+
if (!this._isAvailableValue(mo)) {
|
61
|
+
return '';
|
95
62
|
}
|
96
63
|
|
97
|
-
const
|
98
|
-
const
|
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]+$/;
|
99
71
|
|
100
|
-
if (
|
72
|
+
if (!reg.test(amount) || !reg.test(decimals)) {
|
101
73
|
return false;
|
102
74
|
}
|
103
75
|
|
104
|
-
|
105
|
-
|
76
|
+
amount = new BigNumber(amount);
|
77
|
+
decimals = new BigNumber(Math.pow(10, decimals));
|
78
|
+
const amountWithDecimals = amount.times(decimals);
|
106
79
|
|
107
|
-
|
108
|
-
|
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;
|
80
|
+
if (amountWithDecimals.isGreaterThanOrEqualTo(0) && amountWithDecimals.isLessThanOrEqualTo(long.MAX_VALUE.toString())) {
|
81
|
+
return amountWithDecimals.toString();
|
145
82
|
}
|
146
|
-
|
83
|
+
return false;
|
84
|
+
}
|
147
85
|
|
148
|
-
|
86
|
+
wrap(proto);
|