Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 953 Lines • ▼ Show 20 Lines | const signPkMessage = async (BCH, pk, message) => { | ||||
); | ); | ||||
return signature; | return signature; | ||||
} catch (err) { | } catch (err) { | ||||
console.log(`useBCH.signPkMessage() error: `, err); | console.log(`useBCH.signPkMessage() error: `, err); | ||||
throw err; | throw err; | ||||
} | } | ||||
}; | }; | ||||
const sendBch = async ( | const sendXec = async ( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
destinationAddress, | |||||
sendAmount, | |||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
optionalOpReturnMsg, | optionalOpReturnMsg, | ||||
isOneToMany, | |||||
destinationAddressAndValueArray, | |||||
destinationAddress, | |||||
sendAmount, | |||||
) => { | ) => { | ||||
try { | try { | ||||
let value = new BigNumber(0); | |||||
if (isOneToMany) { | |||||
// this is a one to many XEC transaction | |||||
if ( | |||||
!destinationAddressAndValueArray || | |||||
!destinationAddressAndValueArray.length | |||||
) { | |||||
throw new Error('Invalid destinationAddressAndValueArray'); | |||||
} | |||||
const arrayLength = destinationAddressAndValueArray.length; | |||||
for (let i = 0; i < arrayLength; i++) { | |||||
// add the total value being sent in this array of recipients | |||||
value = BigNumber.sum( | |||||
value, | |||||
new BigNumber( | |||||
destinationAddressAndValueArray[i].split(',')[1], | |||||
), | |||||
); | |||||
} | |||||
// If user is attempting to send an aggregate value that is less than minimum accepted by the backend | |||||
if ( | |||||
value.lt( | |||||
new BigNumber( | |||||
fromSmallestDenomination( | |||||
currency.dustSats, | |||||
).toString(), | |||||
), | |||||
) | |||||
) { | |||||
// Throw the same error given by the backend attempting to broadcast such a tx | |||||
throw new Error('dust'); | |||||
} | |||||
} else { | |||||
// this is a one to one XEC transaction then check sendAmount | |||||
// note: one to many transactions won't be sending a single sendAmount | |||||
if (!sendAmount) { | if (!sendAmount) { | ||||
return null; | return null; | ||||
} | } | ||||
const value = new BigNumber(sendAmount); | 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 | ||||
if ( | if ( | ||||
value.lt( | value.lt( | ||||
new BigNumber( | new BigNumber( | ||||
fromSmallestDenomination(currency.dustSats).toString(), | fromSmallestDenomination( | ||||
currency.dustSats, | |||||
).toString(), | |||||
), | ), | ||||
) | ) | ||||
) { | ) { | ||||
// 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 inputUtxos = []; | const inputUtxos = []; | ||||
let transactionBuilder; | let transactionBuilder; | ||||
// instance of transaction builder | // instance of transaction builder | ||||
if (process.env.REACT_APP_NETWORK === `mainnet`) | if (process.env.REACT_APP_NETWORK === `mainnet`) | ||||
transactionBuilder = new BCH.TransactionBuilder(); | transactionBuilder = new BCH.TransactionBuilder(); | ||||
else transactionBuilder = new BCH.TransactionBuilder('testnet'); | else transactionBuilder = new BCH.TransactionBuilder('testnet'); | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 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; | ||||
} | } | ||||
// add output w/ address and amount to send | if (isOneToMany) { | ||||
// for one to many mode, add the multiple outputs from the array | |||||
let arrayLength = destinationAddressAndValueArray.length; | |||||
for (let i = 0; i < arrayLength; i++) { | |||||
// add each send tx from the array as an output | |||||
let outputAddress = | |||||
destinationAddressAndValueArray[i].split(',')[0]; | |||||
let outputValue = new BigNumber( | |||||
destinationAddressAndValueArray[i].split(',')[1], | |||||
); | |||||
transactionBuilder.addOutput( | |||||
BCH.Address.toCashAddress(outputAddress), | |||||
parseInt(toSmallestDenomination(outputValue)), | |||||
); | |||||
} | |||||
} else { | |||||
// for one to one mode, add output w/ single 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( | ||||
REMAINDER_ADDR, | REMAINDER_ADDR, | ||||
parseInt(remainder), | parseInt(remainder), | ||||
); | ); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | return { | ||||
getTxHistory, | getTxHistory, | ||||
flattenTransactions, | flattenTransactions, | ||||
parseTxData, | parseTxData, | ||||
addTokenTxData, | addTokenTxData, | ||||
parseTokenInfoForTxHistory, | parseTokenInfoForTxHistory, | ||||
getTxData, | getTxData, | ||||
getRestUrl, | getRestUrl, | ||||
signPkMessage, | signPkMessage, | ||||
sendBch, | sendXec, | ||||
sendToken, | sendToken, | ||||
createToken, | createToken, | ||||
getTokenStats, | getTokenStats, | ||||
}; | }; | ||||
} | } |