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 @@ -89,4 +89,30 @@ new BigNumber(testAmountInSatoshis), ); }); + 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'), + ); + // Assume total BCHA available in utxos is 500 sats higher than 0.123456578 BCHA + let originalAmountTest = satoshisToSendTest.plus(500); + // Assume 229 byte tx fee + let txFeeTest = 229; + + expect( + Math.floor( + originalAmountTest.minus(satoshisToSendTest).minus(txFeeTest), + ), + ).toStrictEqual( + parseInt( + originalAmountTest.minus(satoshisToSendTest).minus(txFeeTest), + ), + ); + }); + it(`Using parseInt on a BigNumber returns output type required for Transaction Builder`, () => { + const remainder = new BigNumber('12345678'); + expect(parseInt(remainder)).toStrictEqual(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 @@ -493,10 +493,9 @@ } // amount to send back to the remainder address. - const remainder = Math.floor( - originalAmount.minus(satoshisToSend).minus(txFee), - ); - if (remainder < 0) { + const remainder = originalAmount.minus(satoshisToSend).minus(txFee); + + if (remainder.lt(0)) { const error = new Error(`Insufficient funds`); error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; throw error; @@ -507,17 +506,20 @@ } // add output w/ address and amount to send - transactionBuilder.addOutput( BCH.Address.toCashAddress(destinationAddress), parseInt(toSmallestDenomination(value)), ); if ( - remainder >= - parseFloat(toSmallestDenomination(new BigNumber(currency.dust))) + remainder.gte( + toSmallestDenomination(new BigNumber(currency.dust)), + ) ) { - transactionBuilder.addOutput(REMAINDER_ADDR, remainder); + transactionBuilder.addOutput( + REMAINDER_ADDR, + parseInt(remainder), + ); } // Sign the transactions with the HD node.