diff --git a/web/cashtab/src/hooks/__tests__/migrations.test.js b/web/cashtab/src/hooks/__tests__/migrations.test.js --- a/web/cashtab/src/hooks/__tests__/migrations.test.js +++ b/web/cashtab/src/hooks/__tests__/migrations.test.js @@ -2,7 +2,10 @@ import BigNumber from 'bignumber.js'; import BCHJS from '@psf/bch-js'; import useBCH from '../useBCH'; -import { fromSmallestDenomination } from '@utils/cashMethods'; +import { + fromSmallestDenomination, + toSmallestDenomination, +} from '@utils/cashMethods'; describe('Testing functions for upgrading Cashtab', () => { it('Replacement currency.dust parameter parsing matches legacy DUST parameter', () => { @@ -12,7 +15,6 @@ }); it('Replicate 8-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { const BCH = new BCHJS(); - const { toSmallestDenomination } = useBCH(); const testSendAmount = '0.12345678'; expect( parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), @@ -20,7 +22,6 @@ }); it('Replicate 2-decimal return value from instance of toSatoshi in TransactionBuilder with toSmallestDenomination', () => { const BCH = new BCHJS(); - const { toSmallestDenomination } = useBCH(); const testSendAmount = '0.12'; expect( parseInt(toSmallestDenomination(new BigNumber(testSendAmount), 8)), @@ -28,7 +29,6 @@ }); it('Replicate 8-decimal return value from instance of toSatoshi in remainder comparison with toSmallestDenomination', () => { const BCH = new BCHJS(); - const { toSmallestDenomination } = useBCH(); // note: do not specify 8 decimals as this test SHOULD fail when currency.dust changes or cashDecimals changes if not updated expect( parseFloat(toSmallestDenomination(new BigNumber(currency.dust))), @@ -39,34 +39,26 @@ ); }); it('toSmallestDenomination() returns false if input is not a BigNumber', () => { - const { toSmallestDenomination } = useBCH(); const testInput = 132.12345678; expect(toSmallestDenomination(testInput)).toBe(false); }); it(`toSmallestDenomination() returns false if input is a BigNumber with more decimals than specified by cashDecimals parameter`, () => { - const { toSmallestDenomination } = useBCH(); const testInput = new BigNumber('132.123456789'); expect(toSmallestDenomination(testInput, 8)).toBe(false); }); it(`toSmallestDenomination() returns expected value if input is a BigNumber with 8 decimal places`, () => { - const { toSmallestDenomination } = useBCH(); - const testInput = new BigNumber('100.12345678'); expect(toSmallestDenomination(testInput, 8)).toStrictEqual( new BigNumber('10012345678'), ); }); it(`toSmallestDenomination() returns expected value if input is a BigNumber with 2 decimal places`, () => { - const { toSmallestDenomination } = useBCH(); - const testInput = new BigNumber('100.12'); expect(toSmallestDenomination(testInput, 2)).toStrictEqual( new BigNumber('10012'), ); }); it(`toSmallestDenomination() returns expected value if input is a BigNumber with 1 decimal place`, () => { - const { toSmallestDenomination } = useBCH(); - const testInput = new BigNumber('100.1'); expect(toSmallestDenomination(testInput, 8)).toStrictEqual( new BigNumber('10010000000'), @@ -74,8 +66,6 @@ }); it('toSmallestDenomination() returns exact result as toSatoshi but in BigNumber format', () => { const BCH = new BCHJS(); - const { toSmallestDenomination } = useBCH(); - const testAmount = new BigNumber('0.12345678'); // Match legacy implementation, inputting a BigNumber converted to a string by .toFixed(8) @@ -91,8 +81,6 @@ ); }); it(`BigNumber version of remainder variable is equivalent to Math.floor version`, () => { - const { toSmallestDenomination } = useBCH(); - // Test case for sending 0.12345678 BCHA let satoshisToSendTest = toSmallestDenomination( new BigNumber('0.12345678'), diff --git a/web/cashtab/src/hooks/useBCH.js b/web/cashtab/src/hooks/useBCH.js --- a/web/cashtab/src/hooks/useBCH.js +++ b/web/cashtab/src/hooks/useBCH.js @@ -1,6 +1,7 @@ import BigNumber from 'bignumber.js'; import { currency } from '@components/Common/Ticker'; import SlpWallet from 'minimal-slp-wallet'; +import { toSmallestDenomination } from '@utils/cashMethods'; export default function useBCH() { const SEND_BCH_ERRORS = { @@ -406,30 +407,6 @@ return link; }; - const toSmallestDenomination = ( - sendAmount, - cashDecimals = currency.cashDecimals, - ) => { - // Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places - // Example, for an 8 decimal place currency like Bitcoin - // Input: a BigNumber of the amount of Bitcoin to be sent - // Output: a BigNumber of the amount of satoshis to be sent, or false if input is invalid - - // Validate - // Input should be a BigNumber with no more decimal places than cashDecimals - const isValidSendAmount = - BigNumber.isBigNumber(sendAmount) && - sendAmount.dp() <= cashDecimals; - if (!isValidSendAmount) { - return false; - } - const conversionFactor = new BigNumber(10 ** cashDecimals); - const sendAmountSmallestDenomination = sendAmount.times( - conversionFactor, - ); - return sendAmountSmallestDenomination; - }; - const sendBch = async ( BCH, wallet, @@ -593,7 +570,6 @@ getSlpBalancesAndUtxos, getTxHistory, getRestUrl, - toSmallestDenomination, sendBch, sendToken, }; diff --git a/web/cashtab/src/utils/cashMethods.js b/web/cashtab/src/utils/cashMethods.js --- a/web/cashtab/src/utils/cashMethods.js +++ b/web/cashtab/src/utils/cashMethods.js @@ -10,3 +10,24 @@ const amountInBaseUnits = amountBig.times(multiplier); return amountInBaseUnits.toNumber(); }; + +export const toSmallestDenomination = ( + sendAmount, + cashDecimals = currency.cashDecimals, +) => { + // Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places + // Example, for an 8 decimal place currency like Bitcoin + // Input: a BigNumber of the amount of Bitcoin to be sent + // Output: a BigNumber of the amount of satoshis to be sent, or false if input is invalid + + // Validate + // Input should be a BigNumber with no more decimal places than cashDecimals + const isValidSendAmount = + BigNumber.isBigNumber(sendAmount) && sendAmount.dp() <= cashDecimals; + if (!isValidSendAmount) { + return false; + } + const conversionFactor = new BigNumber(10 ** cashDecimals); + const sendAmountSmallestDenomination = sendAmount.times(conversionFactor); + return sendAmountSmallestDenomination; +};