From c8bb77d8af85d2f9f9df82f1afbe7516ab296e98 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sun, 22 Nov 2020 16:34:07 -0500 Subject: [PATCH] Allow base-10 to be passed into BigNumbner.toString and improve errors for other radices (#1164). --- packages/bignumber/src.ts/bignumber.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/bignumber/src.ts/bignumber.ts b/packages/bignumber/src.ts/bignumber.ts index ab3c59a95e..beb095f884 100644 --- a/packages/bignumber/src.ts/bignumber.ts +++ b/packages/bignumber/src.ts/bignumber.ts @@ -35,6 +35,9 @@ export function isBigNumberish(value: any): value is BigNumberish { ); } +// Only warn about passing 10 into radix once +let _warnedToStringRadix = false; + export class BigNumber implements Hexable { readonly _hex: string; readonly _isBigNumber: boolean; @@ -188,9 +191,18 @@ export class BigNumber implements Hexable { } toString(): string { - // Lots of people expect this, which we do not support, so check - if (arguments.length !== 0) { - logger.throwError("bigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, { }); + // Lots of people expect this, which we do not support, so check (See: #889) + if (arguments.length > 0) { + if (arguments[0] === 10) { + if (!_warnedToStringRadix) { + _warnedToStringRadix = true; + logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed"); + } + } else if (arguments[0] === 16) { + logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, { }); + } else { + logger.throwError("BigNumber.toString does not accept parametes", Logger.errors.UNEXPECTED_ARGUMENT, { }); + } } return toBN(this).toString(10); }