openchain-nodejs-ts-yxl 1.0.7 → 1.1.0

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,83 @@ 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
+ /**
1008
+ * 合约调用参数
1009
+ * @interface
1010
+ * @property {string} [contractAddress] 合约账户地址
1011
+ * @property {string} [sourceAddress] 调用者账户地址
1012
+ * @property {string} [code] 合约代码
1013
+ * @property {string} [input] 合约调用输入数据
1014
+ * @property {string} [contractBalance] 合约账户余额
1015
+ * @property {number} optType 操作类型(1-查询,2-调用)
1016
+ * @property {string} [feeLimit] 交易费用上限
1017
+ * @property {string} [gasPrice] Gas单价
1018
+ * @example
1019
+ * {
1020
+ * contractAddress: 'adx...',
1021
+ * sourceAddress: 'adx...',
1022
+ * input: 'method(param)',
1023
+ * optType: 1,
1024
+ * feeLimit: '1000000',
1025
+ * gasPrice: '100'
1026
+ * }
1027
+ */
1028
+ export interface ContractCallParams {
1029
+ contractAddress?: string;
1030
+ sourceAddress?: string;
1031
+ code?: string;
1032
+ input?: string;
1033
+ contractBalance?: string;
1034
+ optType: number;
1035
+ feeLimit?: string;
1036
+ gasPrice?: string;
1037
+ }
1038
+
1039
+ /**
1040
+ * 合约调用结果
1041
+ * @interface
1042
+ * @property {string[]} queryRets 查询返回结果列表
1043
+ * @property {string} logs 合约执行日志
1044
+ * @property {Object} stat 合约执行统计信息
1045
+ * @property {number} stat.apply_time 执行时间(毫秒)
1046
+ * @property {number} stat.memory_usage 内存使用量(字节)
1047
+ * @property {number} stat.stack_usage 堆栈使用量(字节)
1048
+ * @property {number} stat.step 执行步数
1049
+ * @example
1050
+ * {
1051
+ * queryRets: ['return value'],
1052
+ * logs: 'execution logs',
1053
+ * stat: {
1054
+ * apply_time: 100,
1055
+ * memory_usage: 1024,
1056
+ * stack_usage: 256,
1057
+ * step: 1000
1058
+ * }
1059
+ * }
1060
+ */
1061
+ export interface ContractCallResult {
1062
+ queryRets: string[];
1063
+ logs: string;
1064
+ stat: {
1065
+ apply_time: number;
1066
+ memory_usage: number;
1067
+ stack_usage: number;
1068
+ step: number;
1069
+ };
1070
+ }
1071
+
995
1072
  export interface OperationResult {
996
1073
  type: string;
997
1074
  data: Record<string, any>;
package/lib/util/index.js CHANGED
@@ -1,148 +1,86 @@
1
1
  'use strict';
2
2
 
3
- const is = require('is-type-of');
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
- * 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 = '';
11
+ function Util(options) {
12
+ if (!(this instanceof Util)) {
13
+ return new Util(options);
14
+ }
38
15
 
39
- for (let i = 0; i < str.length; i++) {
40
- hex += str.charCodeAt(i).toString(16);
41
- }
16
+ this.options = options;
17
+ }
42
18
 
43
- return hex;
44
- }
19
+ const proto = Util.prototype;
45
20
 
46
- return str;
47
- };
21
+ merge(proto, require('../common/util'));
48
22
 
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 = '';
23
+ proto.isBigNumber = function (object) {
24
+ return this._isBigNumber(object);
25
+ };
57
26
 
58
- for (let i = 0; i < str.length; i += 2) {
59
- utf += String.fromCharCode(parseInt(str.substr(i, 2), 16));
60
- }
27
+ proto.toBigNumber = function(data) {
28
+ return this._toBigNumber(data);
29
+ };
61
30
 
62
- return utf;
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
- * 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);
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
- * 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);
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
- * 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;
59
+ proto.moToBu = function(mo) {
60
+ if (!this._isAvailableValue(mo)) {
61
+ return '';
95
62
  }
96
63
 
97
- const decimalsBN = new BigNumber(decimals);
98
- const amountBN = new BigNumber(amount);
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 (decimalsBN.isNaN() || amountBN.isNaN()) {
72
+ if (!reg.test(amount) || !reg.test(decimals)) {
101
73
  return false;
102
74
  }
103
75
 
104
- return amountBN.multipliedBy(Math.pow(10, decimals)).toString(10);
105
- };
76
+ amount = new BigNumber(amount);
77
+ decimals = new BigNumber(Math.pow(10, decimals));
78
+ const amountWithDecimals = amount.times(decimals);
106
79
 
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;
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
- module.exports = util;
86
+ wrap(proto);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openchain-nodejs-ts-yxl",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "openchain sdk",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",