Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 410 Lines • ▼ Show 20 Lines | const toSmallestDenomination = ( | ||||
cashDecimals = currency.cashDecimals, | cashDecimals = currency.cashDecimals, | ||||
) => { | ) => { | ||||
// Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places | // Replace the BCH.toSatoshi method with an equivalent function that works for arbitrary decimal places | ||||
// Example, for an 8 decimal place currency like Bitcoin | // Example, for an 8 decimal place currency like Bitcoin | ||||
// Input: a BigNumber of the amount of Bitcoin to be sent | // 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 | // Output: a BigNumber of the amount of satoshis to be sent, or false if input is invalid | ||||
// Validate | // Validate | ||||
// Input should be a BigNumber with cashDecimals decimal places | // Input should be a BigNumber with no more decimal places than cashDecimals | ||||
const isValidSendAmount = | const isValidSendAmount = | ||||
BigNumber.isBigNumber(sendAmount) && | BigNumber.isBigNumber(sendAmount) && | ||||
sendAmount.dp() === cashDecimals; | sendAmount.dp() <= cashDecimals; | ||||
if (!isValidSendAmount) { | if (!isValidSendAmount) { | ||||
return false; | return false; | ||||
} | } | ||||
const conversionFactor = new BigNumber(10 ** cashDecimals); | const conversionFactor = new BigNumber(10 ** cashDecimals); | ||||
const sendAmountSmallestDenomination = sendAmount.times( | const sendAmountSmallestDenomination = sendAmount.times( | ||||
conversionFactor, | conversionFactor, | ||||
); | ); | ||||
return sendAmountSmallestDenomination; | return sendAmountSmallestDenomination; | ||||
▲ Show 20 Lines • Show All 183 Lines • Show Last 20 Lines |