-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathetherUnits.js
executable file
·56 lines (53 loc) · 1.8 KB
/
etherUnits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var etherUnits = function() {}
etherUnits.unitMap = {
'wei': '1',
'kwei': '1000',
'ada': '1000',
'femtoether': '1000',
'mwei': '1000000',
'babbage': '1000000',
'picoether': '1000000',
'gwei': '1000000000',
'shannon': '1000000000',
'nanoether': '1000000000',
'nano': '1000000000',
'szabo': '1000000000000',
'microether': '1000000000000',
'micro': '1000000000000',
'finney': '1000000000000000',
'milliether': '1000000000000000',
'milli': '1000000000000000',
'ether': '1000000000000000000',
'kether': '1000000000000000000000',
'grand': '1000000000000000000000',
'einstein': '1000000000000000000000',
'mether': '1000000000000000000000000',
'gether': '1000000000000000000000000000',
'tether': '1000000000000000000000000000000'
};
etherUnits.getValueOfUnit = function(unit) {
unit = unit ? unit.toLowerCase() : 'ether';
var unitValue = this.unitMap[unit];
if (unitValue === undefined) {
throw new Error("This unit doesn\'t exists, please use the one of the following units " + JSON.stringify(this.unitMap, null, 2));
}
return new BigNumber(unitValue, 10);
}
etherUnits.fiatToWei = function(number, pricePerEther) {
var returnValue = new BigNumber(String(number)).div(pricePerEther).times(this.getValueOfUnit('ether')).round(0);
return returnValue.toString(10);
}
etherUnits.toFiat = function(number, unit, multi) {
var returnValue = new BigNumber(this.toEther(number, unit)).times(multi).round(5);
return returnValue.toString(10);
}
etherUnits.toEther = function(number, unit) {
var returnValue = new BigNumber(this.toWei(number, unit)).div(this.getValueOfUnit('ether'));
return returnValue.toString(10);
}
etherUnits.toWei = function(number, unit) {
var returnValue = new BigNumber(String(number)).times(this.getValueOfUnit(unit));
return returnValue.toString(10);
}
module.exports = etherUnits;