Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/cashMethods.js
import { currency } from '@components/Common/Ticker'; | import { currency } from '@components/Common/Ticker'; | ||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
export const fromSmallestDenomination = ( | export const fromSmallestDenomination = ( | ||||
amount, | amount, | ||||
cashDecimals = currency.cashDecimals, | cashDecimals = currency.cashDecimals, | ||||
) => { | ) => { | ||||
const amountBig = new BigNumber(amount); | const amountBig = new BigNumber(amount); | ||||
const multiplier = new BigNumber(10 ** (-1 * cashDecimals)); | const multiplier = new BigNumber(10 ** (-1 * cashDecimals)); | ||||
const amountInBaseUnits = amountBig.times(multiplier); | const amountInBaseUnits = amountBig.times(multiplier); | ||||
return amountInBaseUnits.toNumber(); | return amountInBaseUnits.toNumber(); | ||||
}; | }; | ||||
export 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; | |||||
}; |