Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 487 Lines • ▼ Show 20 Lines | ) => { | ||||
: calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | : calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | ||||
if (originalAmount.minus(satoshisToSend).minus(txFee).gte(0)) { | if (originalAmount.minus(satoshisToSend).minus(txFee).gte(0)) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// amount to send back to the remainder address. | // amount to send back to the remainder address. | ||||
const remainder = Math.floor( | const remainder = originalAmount.minus(satoshisToSend).minus(txFee); | ||||
originalAmount.minus(satoshisToSend).minus(txFee), | |||||
); | if (remainder.lt(0)) { | ||||
if (remainder < 0) { | |||||
const error = new Error(`Insufficient funds`); | const error = new Error(`Insufficient funds`); | ||||
error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; | error.code = SEND_BCH_ERRORS.INSUFFICIENT_FUNDS; | ||||
throw error; | throw error; | ||||
} | } | ||||
if (encodedOpReturn) { | if (encodedOpReturn) { | ||||
transactionBuilder.addOutput(encodedOpReturn, 0); | transactionBuilder.addOutput(encodedOpReturn, 0); | ||||
} | } | ||||
// add output w/ address and amount to send | // add output w/ address and amount to send | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toCashAddress(destinationAddress), | BCH.Address.toCashAddress(destinationAddress), | ||||
parseInt(toSmallestDenomination(value)), | parseInt(toSmallestDenomination(value)), | ||||
); | ); | ||||
if ( | if ( | ||||
remainder >= | remainder.gte( | ||||
parseFloat(toSmallestDenomination(new BigNumber(currency.dust))) | toSmallestDenomination(new BigNumber(currency.dust)), | ||||
) | |||||
) { | ) { | ||||
transactionBuilder.addOutput(REMAINDER_ADDR, remainder); | transactionBuilder.addOutput( | ||||
REMAINDER_ADDR, | |||||
parseInt(remainder), | |||||
); | |||||
} | } | ||||
// Sign the transactions with the HD node. | // Sign the transactions with the HD node. | ||||
for (let i = 0; i < inputUtxos.length; i++) { | for (let i = 0; i < inputUtxos.length; i++) { | ||||
const utxo = inputUtxos[i]; | const utxo = inputUtxos[i]; | ||||
transactionBuilder.sign( | transactionBuilder.sign( | ||||
i, | i, | ||||
BCH.ECPair.fromWIF(utxo.wif), | BCH.ECPair.fromWIF(utxo.wif), | ||||
▲ Show 20 Lines • Show All 70 Lines • Show Last 20 Lines |