From 425e7a4795278040078407de7ec994fbc2e9e600 Mon Sep 17 00:00:00 2001 From: Gennadiy Popov Date: Thu, 8 Aug 2024 20:26:26 +0300 Subject: [PATCH] Added the ability to convert numbers using the fromNano and toNano functions. --- src/utils/Utils.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/utils/Utils.js b/src/utils/Utils.js index 315b7989..ef7409d4 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -26,10 +26,15 @@ function sha256(bytes) { /** * from coins to nanocoins - * @param amount {BN | string} + * @param amount {BN | string | number} * @return {BN} */ function toNano(amount) { + const isNumber = typeof amount === 'number' && !isNaN(amount) && isFinite(amount) + if(isNumber) { + amount = amount.toString() + } + if (!BN.isBN(amount) && !(typeof amount === 'string')) { throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); } @@ -39,10 +44,15 @@ function toNano(amount) { /** * from nanocoins to coins - * @param amount {BN | string} + * @param amount {BN | string | number} * @return {string} */ function fromNano(amount) { + const isNumber = typeof amount === 'number' && !isNaN(amount) && isFinite(amount) + if(isNumber) { + amount = amount.toString() + } + if (!BN.isBN(amount) && !(typeof amount === 'string')) { throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.'); }