Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 428 Lines • ▼ Show 20 Lines | ) => { | ||||
); | ); | ||||
return sendAmountSmallestDenomination; | return sendAmountSmallestDenomination; | ||||
}; | }; | ||||
const sendBch = async ( | const sendBch = async ( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
{ addresses, values, encodedOpReturn }, | destinationAddress, | ||||
sendAmount, | |||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
callbackTxId, | callbackTxId, | ||||
encodedOpReturn, | |||||
) => { | ) => { | ||||
// Note: callbackTxId is a callback function that accepts a txid as its only parameter | // Note: callbackTxId is a callback function that accepts a txid as its only parameter | ||||
try { | try { | ||||
if (!values || values.length === 0) { | if (!sendAmount) { | ||||
return null; | return null; | ||||
} | } | ||||
const value = values.reduce( | const value = new BigNumber(sendAmount); | ||||
(previous, current) => new BigNumber(current).plus(previous), | |||||
new BigNumber(0), | |||||
); | |||||
// If user is attempting to send less than minimum accepted by the backend | // If user is attempting to send less than minimum accepted by the backend | ||||
if (value.lt(new BigNumber(currency.dust))) { | if (value.lt(new BigNumber(currency.dust))) { | ||||
// Throw the same error given by the backend attempting to broadcast such a tx | // Throw the same error given by the backend attempting to broadcast such a tx | ||||
throw new Error('dust'); | throw new Error('dust'); | ||||
} | } | ||||
const REMAINDER_ADDR = wallet.Path1899.cashAddress; | const REMAINDER_ADDR = wallet.Path1899.cashAddress; | ||||
const inputUtxos = []; | const inputUtxos = []; | ||||
Show All 20 Lines | ) => { | ||||
originalAmount = originalAmount.plus(utxo.value); | originalAmount = originalAmount.plus(utxo.value); | ||||
const vout = utxo.vout; | const vout = utxo.vout; | ||||
const txid = utxo.txid; | const txid = utxo.txid; | ||||
// add input with txid and index of vout | // add input with txid and index of vout | ||||
transactionBuilder.addInput(txid, vout); | transactionBuilder.addInput(txid, vout); | ||||
inputUtxos.push(utxo); | inputUtxos.push(utxo); | ||||
txFee = encodedOpReturn | txFee = encodedOpReturn | ||||
? calcFee( | ? calcFee(BCH, inputUtxos, 3, feeInSatsPerByte) | ||||
BCH, | : calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | ||||
inputUtxos, | |||||
addresses.length + 2, | |||||
feeInSatsPerByte, | |||||
) | |||||
: calcFee( | |||||
BCH, | |||||
inputUtxos, | |||||
addresses.length + 1, | |||||
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 = Math.floor( | ||||
originalAmount.minus(satoshisToSend).minus(txFee), | originalAmount.minus(satoshisToSend).minus(txFee), | ||||
); | ); | ||||
if (remainder < 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 | ||||
for (let i = 0; i < addresses.length; i++) { | |||||
const address = addresses[i]; | |||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
BCH.Address.toCashAddress(address), | BCH.Address.toCashAddress(destinationAddress), | ||||
BCH.BitcoinCash.toSatoshi(Number(values[i]).toFixed(8)), | BCH.BitcoinCash.toSatoshi(Number(sendAmount).toFixed(8)), | ||||
); | ); | ||||
} | |||||
if ( | if ( | ||||
remainder >= | remainder >= | ||||
BCH.BitcoinCash.toSatoshi( | BCH.BitcoinCash.toSatoshi( | ||||
parseFloat(new BigNumber(currency.dust).toFixed(8)), | parseFloat(new BigNumber(currency.dust).toFixed(8)), | ||||
) | ) | ||||
) { | ) { | ||||
transactionBuilder.addOutput(REMAINDER_ADDR, remainder); | transactionBuilder.addOutput(REMAINDER_ADDR, remainder); | ||||
▲ Show 20 Lines • Show All 78 Lines • Show Last 20 Lines |