-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
bignumber.js
149 lines (107 loc) · 4.15 KB
/
bignumber.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* BigNumber
*
* A wrapper around the BN.js object. In the future we can swap out
* the underlying BN.js library for something smaller.
*/
var BN = require('bn.js');
var defineProperty = require('./properties').defineProperty;
var convert = require('./convert');
var throwError = require('./throw-error');
function BigNumber(value) {
if (!(this instanceof BigNumber)) { throw new Error('missing new'); }
if (convert.isHexString(value)) {
if (value == '0x') { value = '0x0'; }
value = new BN(value.substring(2), 16);
} else if (typeof(value) === 'string' && value[0] === '-' && convert.isHexString(value.substring(1))) {
value = (new BN(value.substring(3), 16)).mul(BigNumber.constantNegativeOne._bn);
} else if (typeof(value) === 'string' && value.match(/^-?[0-9]*$/)) {
if (value == '') { value = '0'; }
value = new BN(value);
} else if (typeof(value) === 'number' && parseInt(value) == value) {
value = new BN(value);
} else if (BN.isBN(value)) {
//value = value
} else if (isBigNumber(value)) {
value = value._bn;
} else if (convert.isArrayish(value)) {
value = new BN(convert.hexlify(value).substring(2), 16);
} else {
throwError('invalid BigNumber value', { input: value });
}
defineProperty(this, '_bn', value);
}
defineProperty(BigNumber, 'constantNegativeOne', bigNumberify(-1));
defineProperty(BigNumber, 'constantZero', bigNumberify(0));
defineProperty(BigNumber, 'constantOne', bigNumberify(1));
defineProperty(BigNumber, 'constantTwo', bigNumberify(2));
defineProperty(BigNumber, 'constantWeiPerEther', bigNumberify(new BN('1000000000000000000')));
defineProperty(BigNumber.prototype, 'fromTwos', function(value) {
return new BigNumber(this._bn.fromTwos(value));
});
defineProperty(BigNumber.prototype, 'toTwos', function(value) {
return new BigNumber(this._bn.toTwos(value));
});
defineProperty(BigNumber.prototype, 'add', function(other) {
return new BigNumber(this._bn.add(bigNumberify(other)._bn));
});
defineProperty(BigNumber.prototype, 'sub', function(other) {
return new BigNumber(this._bn.sub(bigNumberify(other)._bn));
});
defineProperty(BigNumber.prototype, 'div', function(other) {
return new BigNumber(this._bn.div(bigNumberify(other)._bn));
});
defineProperty(BigNumber.prototype, 'mul', function(other) {
return new BigNumber(this._bn.mul(bigNumberify(other)._bn));
});
defineProperty(BigNumber.prototype, 'mod', function(other) {
return new BigNumber(this._bn.mod(bigNumberify(other)._bn));
});
defineProperty(BigNumber.prototype, 'pow', function(other) {
return new BigNumber(this._bn.pow(bigNumberify(other)._bn));
});
defineProperty(BigNumber.prototype, 'maskn', function(value) {
return new BigNumber(this._bn.maskn(value));
});
defineProperty(BigNumber.prototype, 'eq', function(other) {
return this._bn.eq(bigNumberify(other)._bn);
});
defineProperty(BigNumber.prototype, 'lt', function(other) {
return this._bn.lt(bigNumberify(other)._bn);
});
defineProperty(BigNumber.prototype, 'lte', function(other) {
return this._bn.lte(bigNumberify(other)._bn);
});
defineProperty(BigNumber.prototype, 'gt', function(other) {
return this._bn.gt(bigNumberify(other)._bn);
});
defineProperty(BigNumber.prototype, 'gte', function(other) {
return this._bn.gte(bigNumberify(other)._bn);
});
defineProperty(BigNumber.prototype, 'isZero', function() {
return this._bn.isZero();
});
defineProperty(BigNumber.prototype, 'toNumber', function(base) {
return this._bn.toNumber();
});
defineProperty(BigNumber.prototype, 'toString', function() {
//return this._bn.toString(base || 10);
return this._bn.toString(10);
});
defineProperty(BigNumber.prototype, 'toHexString', function() {
var hex = this._bn.toString(16);
if (hex.length % 2) { hex = '0' + hex; }
return '0x' + hex;
});
function isBigNumber(value) {
return (value._bn && value._bn.mod);
}
function bigNumberify(value) {
if (isBigNumber(value)) { return value; }
return new BigNumber(value);
}
module.exports = {
isBigNumber: isBigNumber,
bigNumberify: bigNumberify,
BigNumber: BigNumber
};