Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import { currency } from '@components/Common/Ticker'; | import { currency } from '@components/Common/Ticker'; | ||||
import SlpWallet from 'minimal-slp-wallet'; | import SlpWallet from 'minimal-slp-wallet'; | ||||
import { toSmallestDenomination } from '@utils/cashMethods'; | |||||
export default function useBCH() { | export default function useBCH() { | ||||
const SEND_BCH_ERRORS = { | const SEND_BCH_ERRORS = { | ||||
INSUFFICIENT_FUNDS: 0, | INSUFFICIENT_FUNDS: 0, | ||||
NETWORK_ERROR: 1, | NETWORK_ERROR: 1, | ||||
INSUFFICIENT_PRIORITY: 66, // ~insufficient fee | INSUFFICIENT_PRIORITY: 66, // ~insufficient fee | ||||
DOUBLE_SPENDING: 18, | DOUBLE_SPENDING: 18, | ||||
MAX_UNCONFIRMED_TXS: 64, | MAX_UNCONFIRMED_TXS: 64, | ||||
▲ Show 20 Lines • Show All 389 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, | |||||
cashDecimals = currency.cashDecimals, | |||||
) => { | |||||
// 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, or false if input is invalid | |||||
// Validate | |||||
// Input should be a BigNumber with no more decimal places than cashDecimals | |||||
const isValidSendAmount = | |||||
BigNumber.isBigNumber(sendAmount) && | |||||
sendAmount.dp() <= cashDecimals; | |||||
if (!isValidSendAmount) { | |||||
return false; | |||||
} | |||||
const conversionFactor = new BigNumber(10 ** cashDecimals); | |||||
const sendAmountSmallestDenomination = sendAmount.times( | |||||
conversionFactor, | |||||
); | |||||
return sendAmountSmallestDenomination; | |||||
}; | |||||
const sendBch = async ( | const sendBch = async ( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
destinationAddress, | destinationAddress, | ||||
sendAmount, | sendAmount, | ||||
feeInSatsPerByte, | feeInSatsPerByte, | ||||
callbackTxId, | callbackTxId, | ||||
▲ Show 20 Lines • Show All 147 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, | ||||
}; | }; | ||||
} | } |