Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 815 Lines • ▼ Show 20 Lines | export default function useBCH() { | ||||
const sendBch = async ( | const sendBch = async ( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
destinationAddress, | destinationAddress, | ||||
sendAmount, | sendAmount, | ||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
callbackTxId, | |||||
encodedOpReturn, | |||||
) => { | ) => { | ||||
// Note: callbackTxId is a callback function that accepts a txid as its only parameter | |||||
try { | try { | ||||
if (!sendAmount) { | if (!sendAmount) { | ||||
return null; | return null; | ||||
} | } | ||||
const value = new BigNumber(sendAmount); | const value = new BigNumber(sendAmount); | ||||
// 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 | ||||
Show All 31 Lines | ) => { | ||||
const utxo = utxos[i]; | const utxo = utxos[i]; | ||||
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 = calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | ||||
? calcFee(BCH, inputUtxos, 3, feeInSatsPerByte) | |||||
: calcFee(BCH, inputUtxos, 2, feeInSatsPerByte); | |||||
if (originalAmount.minus(satoshisToSend).minus(txFee).gte(0)) { | if (originalAmount.minus(satoshisToSend).minus(txFee).gte(0)) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// Get change address from sending utxos | // Get change address from sending utxos | ||||
// fall back to what is stored in wallet | // fall back to what is stored in wallet | ||||
Show All 17 Lines | ) => { | ||||
const remainder = originalAmount.minus(satoshisToSend).minus(txFee); | const remainder = originalAmount.minus(satoshisToSend).minus(txFee); | ||||
if (remainder.lt(0)) { | if (remainder.lt(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) { | |||||
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 (remainder.gte(new BigNumber(currency.dustSats))) { | if (remainder.gte(new BigNumber(currency.dustSats))) { | ||||
transactionBuilder.addOutput( | transactionBuilder.addOutput( | ||||
Show All 21 Lines | ) => { | ||||
// Broadcast transaction to the network | // Broadcast transaction to the network | ||||
const txidStr = await BCH.RawTransactions.sendRawTransaction([hex]); | const txidStr = await BCH.RawTransactions.sendRawTransaction([hex]); | ||||
if (txidStr && txidStr[0]) { | if (txidStr && txidStr[0]) { | ||||
console.log(`${currency.ticker} txid`, txidStr[0]); | console.log(`${currency.ticker} txid`, txidStr[0]); | ||||
} | } | ||||
let link; | let link; | ||||
if (callbackTxId) { | |||||
callbackTxId(txidStr); | |||||
} | |||||
if (process.env.REACT_APP_NETWORK === `mainnet`) { | if (process.env.REACT_APP_NETWORK === `mainnet`) { | ||||
link = `${currency.blockExplorerUrl}/tx/${txidStr}`; | link = `${currency.blockExplorerUrl}/tx/${txidStr}`; | ||||
} else { | } else { | ||||
link = `${currency.blockExplorerUrlTestnet}/tx/${txidStr}`; | link = `${currency.blockExplorerUrlTestnet}/tx/${txidStr}`; | ||||
} | } | ||||
//console.log(`link`, link); | //console.log(`link`, link); | ||||
return link; | return link; | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |