Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

BigNumber.js -> bn.js #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/// required to define ETH_BIGNUMBER_ROUNDING_MODE
if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js'); // jshint ignore:line
var BigNumber = require('bn.js'); // jshint ignore:line
}

var ETH_UNITS = [
Expand Down Expand Up @@ -51,7 +51,7 @@ module.exports = {
ETH_PADDING: 32,
ETH_SIGNATURE_LENGTH: 4,
ETH_UNITS: ETH_UNITS,
ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN },
//ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN },
ETH_POLLING_TIMEOUT: 1000
};

30 changes: 17 additions & 13 deletions lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js'); // jshint ignore:line
var BigNumber = require('bn.js'); // jshint ignore:line
}

var utils = require('./utils');
Expand All @@ -45,17 +45,21 @@ var formatInputInt = function (value) {
if (value instanceof BigNumber || typeof value === 'number') {
if (typeof value === 'number')
value = new BigNumber(value);
BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
value = value.round();
//BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
//value = value.round();

if (value.lessThan(0))
value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1);
if (value.cmpn(0) === -1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be value.cmpn(new BN(0))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, I will fix this

value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).add(value).add(new BigNumber(1));
value = value.toString(16);
}
else if (value.indexOf('0x') === 0)
value = value.substr(2);
else if (typeof value === 'string')
value = formatInputInt(new BigNumber(value));
else if (typeof value === 'string') {
if (value.indexOf('0x') === 0) {
value = value.substr(2);
} else {
value = value.indexOf('.') === -1 ? value : value.substr(0, value.indexOf('.'));
value = formatInputInt(new BigNumber(value));
}
}
else
value = (+value).toString(16);
return padLeft(value, padding);
Expand All @@ -77,7 +81,7 @@ var formatInputBool = function (value) {
/// Values are multiplied by 2^m and encoded as integers
/// @returns byte representation of real
var formatInputReal = function (value) {
return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128)));
return formatInputInt(new BigNumber(value).mul(new BigNumber(2).toRed(BigNumber.red('k256')).redPow(new BigNumber(128)).fromRed()));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is k256 ok for the max int? Do you have a max int that ethereum.js can handle defined somewhere?

};


Expand All @@ -95,7 +99,7 @@ var formatOutputInt = function (value) {
// check if it's negative number
// it it is, return two's complement
if (signedIsNegative(value)) {
return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1);
return new BigNumber(value, 16).sub(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).sub(new BigNumber(1));
}
return new BigNumber(value, 16);
};
Expand All @@ -109,12 +113,12 @@ var formatOutputUInt = function (value) {

/// @returns input bytes formatted to real
var formatOutputReal = function (value) {
return formatOutputInt(value).dividedBy(new BigNumber(2).pow(128));
return formatOutputInt(value).div(new BigNumber(2).toRed(BigNumber.red('k256')).redPow(new BigNumber(128)).fromRed());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe define const TWO_P256 = '0100000000000000000000000000000000' somewhere. Then use like new BN(TWO_P256, 16) . Sidenote, pows are the most expensive operation in the library. I assume that why you can't use them if your not in reduction context

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also see this issue on a brief explention of reduction context indutny/bn.js#40

};

/// @returns input bytes formatted to ureal
var formatOutputUReal = function (value) {
return formatOutputUInt(value).dividedBy(new BigNumber(2).pow(128));
return formatOutputUInt(value).div(new BigNumber(2).toRed(BigNumber.red('k256')).redPow(new BigNumber(128)).fromRed());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

};

/// @returns right-aligned input bytes formatted to hex
Expand Down
2 changes: 1 addition & 1 deletion lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

if (process.env.NODE_ENV !== 'build') {
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
}

var eth = require('./eth');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lib": "./lib"
},
"dependencies": {
"bignumber.js": ">=2.0.0",
"bn.js": "^2.0.1",
"xmlhttprequest": "*"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion test/abi.inputParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert');
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var abi = require('../lib/abi.js');
var clone = function (object) { return JSON.parse(JSON.stringify(object)); };

Expand Down Expand Up @@ -546,3 +546,4 @@ describe('abi', function() {

});
});

2 changes: 1 addition & 1 deletion test/abi.outputParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert');
var BigNumber = require('bignumber.js');
var BigNumber = require('bn.js');
var abi = require('../lib/abi.js');
var clone = function (object) { return JSON.parse(JSON.stringify(object)); };

Expand Down