Skip to content

Commit

Permalink
Fix fromWei / toWei param and return type documentation (#3115)
Browse files Browse the repository at this point in the history
* Fixing Documentation of fromWei and toWei

* Add unit tests for fromWei/toWei return types
  • Loading branch information
cgewecke authored and nivida committed Oct 8, 2019
1 parent 9a13b32 commit a790dd6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/web3-utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ Converts any `ether value <http://ethdocs.org/en/latest/ether.html>`_ value into
Parameters
----------

1. ``number`` - ``String|Number|BN``: The value.
1. ``number`` - ``String|BN``: The value.
2. ``unit`` - ``String`` (optional, defaults to ``"ether"``): The ether to convert from. Possible units are:
- ``noether``: '0'
- ``wei``: '1'
Expand Down Expand Up @@ -1009,7 +1009,7 @@ Parameters
Returns
-------

``String|BN``: If a number, or string is given it returns a number string, otherwise a `BN.js <https://github.com/indutny/bn.js/>`_ instance.
``String|BN``: If a string is given it returns a number string, otherwise a `BN.js <https://github.com/indutny/bn.js/>`_ instance.

-------
Example
Expand Down Expand Up @@ -1049,7 +1049,7 @@ Converts any `wei <http://ethereum.stackexchange.com/questions/253/the-ether-den
Parameters
----------

1. ``number`` - ``String|Number|BN``: The value in wei.
1. ``number`` - ``String|BN``: The value in wei.
2. ``unit`` - ``String`` (optional, defaults to ``"ether"``): The ether to convert to. Possible units are:
- ``noether``: '0'
- ``wei``: '1'
Expand Down Expand Up @@ -1083,7 +1083,7 @@ Parameters
Returns
-------

``String|BN``: If a number, or string is given it returns a number string, otherwise a `BN.js <https://github.com/indutny/bn.js/>`_ instance.
``String``: It always returns a string number.

-------
Example
Expand Down
8 changes: 8 additions & 0 deletions test/utils.fromWei.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,13 @@ describe('lib/utils/utils', function () {
assert(error.message.includes('Please pass numbers as strings or BN objects'))
}
})
// fromWei always returns string
it('should return the correct type', function(){
var weiString = '100000000000000000';
var weiBN = utils.toBN(weiString);

assert(typeof utils.fromWei(weiString) === 'string');
assert(typeof utils.fromWei(weiBN) === 'string');
})
});
});
12 changes: 12 additions & 0 deletions test/utils.toWei.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,25 @@ describe('lib/utils/utils', function () {
assert.throws(function () {utils.toWei(1, 'wei1');}, Error);
});


it('should verify "number" arg is string or BN', function () {
try {
utils.toWei(1, 'wei')
assert.fail();
} catch (error) {
assert(error.message.includes('Please pass numbers as strings or BN objects'))
}
});

// toWei returns string when given string, BN when given BN
it('should return the correct type', function(){
var weiString = '1';
var weiBN = utils.toBN(weiString);

var bn = utils.toWei(weiBN);

assert(utils.isBN(bn));
assert(typeof utils.toWei(weiString) === 'string');
})
});
});

0 comments on commit a790dd6

Please sign in to comment.