Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 400 Lines • ▼ Show 20 Lines | ) => { | ||||
link = `${currency.blockExplorerUrlTestnet}/tx/${txidStr}`; | link = `${currency.blockExplorerUrlTestnet}/tx/${txidStr}`; | ||||
} | } | ||||
//console.log(`link`, link); | //console.log(`link`, link); | ||||
return link; | return link; | ||||
}; | }; | ||||
const toSmallestDenomination = sendAmount => { | |||||
// Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places | |||||
// Example, for an 8 decimal place currency like Bitcoin | |||||
// Input: a BigNumber of the amount of Bitcoin to be sent | |||||
// Output: a BigNumber of the amount of satoshis to be sent | |||||
const conversionFactor = new BigNumber(10 ** currency.cashDecimals); | |||||
const sendAmountSmallestDenomination = sendAmount.times( | |||||
conversionFactor, | |||||
); | |||||
return sendAmountSmallestDenomination; | |||||
}; | |||||
const sendBch = async ( | const sendBch = async ( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
{ addresses, values, encodedOpReturn }, | { addresses, values, encodedOpReturn }, | ||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
callbackTxId, | callbackTxId, | ||||
) => { | ) => { | ||||
Show All 18 Lines | ) => { | ||||
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'); | ||||
const satoshisToSend = BCH.BitcoinCash.toSatoshi(value.toFixed(8)); | const satoshisToSend = toSmallestDenomination(value); | ||||
let originalAmount = new BigNumber(0); | let originalAmount = new BigNumber(0); | ||||
let txFee = 0; | let txFee = 0; | ||||
for (let i = 0; i < utxos.length; i++) { | for (let i = 0; i < utxos.length; i++) { | ||||
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 | ||||
▲ Show 20 Lines • Show All 117 Lines • ▼ Show 20 Lines | export default function useBCH() { | ||||
return { | return { | ||||
getBCH, | getBCH, | ||||
calcFee, | calcFee, | ||||
getUtxos, | getUtxos, | ||||
getHydratedUtxoDetails, | getHydratedUtxoDetails, | ||||
getSlpBalancesAndUtxos, | getSlpBalancesAndUtxos, | ||||
getTxHistory, | getTxHistory, | ||||
getRestUrl, | getRestUrl, | ||||
toSmallestDenomination, | |||||
sendBch, | sendBch, | ||||
sendToken, | sendToken, | ||||
}; | }; | ||||
} | } |