@0x/web3-wrapper: Let toBaseUnitAmount() accept a number as well as a BigNumber

This commit is contained in:
Lawrence Forman 2019-05-18 02:04:19 -04:00 committed by Amir Bandeali
parent 339fc9ff14
commit d480f8d82a
2 changed files with 12 additions and 4 deletions

View File

@ -1,4 +1,13 @@
[ [
{
"version": "6.0.9",
"changes": [
{
"note": "Let `toBaseUnitAmount()` accept a `number`",
"pr": 1819
}
]
},
{ {
"version": "6.0.8", "version": "6.0.8",
"changes": [ "changes": [

View File

@ -88,14 +88,13 @@ export class Web3Wrapper {
* @param decimals The number of decimal places the unit amount has. * @param decimals The number of decimal places the unit amount has.
* @return The amount in baseUnits. * @return The amount in baseUnits.
*/ */
public static toBaseUnitAmount(amount: BigNumber, decimals: number): BigNumber { public static toBaseUnitAmount(amount: BigNumber | number, decimals: number): BigNumber {
assert.isBigNumber('amount', amount);
assert.isNumber('decimals', decimals); assert.isNumber('decimals', decimals);
const unit = new BigNumber(BASE_TEN).pow(decimals); const unit = new BigNumber(BASE_TEN).pow(decimals);
const baseUnitAmount = amount.times(unit); const baseUnitAmount = unit.times(amount);
const hasDecimals = baseUnitAmount.decimalPlaces() !== 0; const hasDecimals = baseUnitAmount.decimalPlaces() !== 0;
if (hasDecimals) { if (hasDecimals) {
throw new Error(`Invalid unit amount: ${amount.toString()} - Too many decimal places`); throw new Error(`Invalid unit amount: ${amount.toString(BASE_TEN)} - Too many decimal places`);
} }
return baseUnitAmount; return baseUnitAmount;
} }