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

Commit a790dd6

Browse files
cgeweckenivida
authored andcommitted
Fix fromWei / toWei param and return type documentation (#3115)
* Fixing Documentation of fromWei and toWei * Add unit tests for fromWei/toWei return types
1 parent 9a13b32 commit a790dd6

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

docs/web3-utils.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ Converts any `ether value <http://ethdocs.org/en/latest/ether.html>`_ value into
975975
Parameters
976976
----------
977977

978-
1. ``number`` - ``String|Number|BN``: The value.
978+
1. ``number`` - ``String|BN``: The value.
979979
2. ``unit`` - ``String`` (optional, defaults to ``"ether"``): The ether to convert from. Possible units are:
980980
- ``noether``: '0'
981981
- ``wei``: '1'
@@ -1009,7 +1009,7 @@ Parameters
10091009
Returns
10101010
-------
10111011

1012-
``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.
1012+
``String|BN``: If a string is given it returns a number string, otherwise a `BN.js <https://github.com/indutny/bn.js/>`_ instance.
10131013

10141014
-------
10151015
Example
@@ -1049,7 +1049,7 @@ Converts any `wei <http://ethereum.stackexchange.com/questions/253/the-ether-den
10491049
Parameters
10501050
----------
10511051

1052-
1. ``number`` - ``String|Number|BN``: The value in wei.
1052+
1. ``number`` - ``String|BN``: The value in wei.
10531053
2. ``unit`` - ``String`` (optional, defaults to ``"ether"``): The ether to convert to. Possible units are:
10541054
- ``noether``: '0'
10551055
- ``wei``: '1'
@@ -1083,7 +1083,7 @@ Parameters
10831083
Returns
10841084
-------
10851085

1086-
``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.
1086+
``String``: It always returns a string number.
10871087

10881088
-------
10891089
Example

test/utils.fromWei.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,13 @@ describe('lib/utils/utils', function () {
2727
assert(error.message.includes('Please pass numbers as strings or BN objects'))
2828
}
2929
})
30+
// fromWei always returns string
31+
it('should return the correct type', function(){
32+
var weiString = '100000000000000000';
33+
var weiBN = utils.toBN(weiString);
34+
35+
assert(typeof utils.fromWei(weiString) === 'string');
36+
assert(typeof utils.fromWei(weiBN) === 'string');
37+
})
3038
});
3139
});

test/utils.toWei.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ describe('lib/utils/utils', function () {
3535
assert.throws(function () {utils.toWei(1, 'wei1');}, Error);
3636
});
3737

38+
3839
it('should verify "number" arg is string or BN', function () {
3940
try {
4041
utils.toWei(1, 'wei')
4142
assert.fail();
4243
} catch (error) {
4344
assert(error.message.includes('Please pass numbers as strings or BN objects'))
4445
}
46+
});
47+
48+
// toWei returns string when given string, BN when given BN
49+
it('should return the correct type', function(){
50+
var weiString = '1';
51+
var weiBN = utils.toBN(weiString);
52+
53+
var bn = utils.toWei(weiBN);
54+
55+
assert(utils.isBN(bn));
56+
assert(typeof utils.toWei(weiString) === 'string');
4557
})
4658
});
4759
});

0 commit comments

Comments
 (0)