1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Conversion util can invert conversion rate

This commit is contained in:
Dan 2017-10-06 12:51:02 -02:30 committed by Chi Kei Chan
parent 49f76d27a9
commit db1258f3de
2 changed files with 18 additions and 9 deletions

View File

@ -36,6 +36,7 @@ const BIG_NUMBER_WEI_MULTIPLIER = new BigNumber('1000000000000000000')
// Individual Setters
const convert = R.invoker(1, 'times')
const round = R.invoker(2, 'toFormat')(R.__, BigNumber.ROUND_DOWN)
const invertConversionRate = conversionRate => () => new BigNumber(1.0).div(conversionRate)
// Setter Maps
const toBigNumber = {
@ -63,13 +64,23 @@ const fromAndToCurrencyPropsNotEqual = R.compose(
)
// Lens
const valuePropertyLense = R.over(R.lensProp('value'))
const valuePropertyLens = R.over(R.lensProp('value'))
const conversionRateLens = R.over(R.lensProp('conversionRate'))
// conditional conversionRate setting wrapper
const whenPredSetCRWithPropAndSetter = (pred, prop, setter) => R.when(
pred,
R.converge(
conversionRateLens,
[R.pipe(R.prop(prop), setter), R.identity]
)
)
// conditional 'value' setting wrappers
const whenPredSetWithPropAndSetter = (pred, prop, setter) => R.when(
pred,
R.converge(
valuePropertyLense,
valuePropertyLens,
[R.pipe(R.prop(prop), setter), R.identity]
)
)
@ -81,6 +92,7 @@ const whenPropApplySetterMap = (prop, setterMap) => whenPredSetWithPropAndSetter
// Conversion utility function
const converter = R.pipe(
whenPredSetCRWithPropAndSetter(R.prop('invertConversionRate'), 'conversionRate', invertConversionRate),
whenPropApplySetterMap('fromNumericBase', toBigNumber),
whenPropApplySetterMap('fromDenomination', toNormalizedDenomination),
whenPredSetWithPropAndSetter(fromAndToCurrencyPropsNotEqual, 'conversionRate', convert),
@ -101,6 +113,7 @@ const conversionUtil = (value, {
numberOfDecimals,
conversionRate,
ethToUSDRate,
invertConversionRate,
}) => converter({
fromCurrency,
toCurrency,
@ -111,6 +124,7 @@ const conversionUtil = (value, {
numberOfDecimals,
conversionRate,
ethToUSDRate,
invertConversionRate,
value,
});

View File

@ -22,7 +22,6 @@ const {
const { stripHexPrefix, addHexPrefix } = require('ethereumjs-util')
const { isHex, numericBalance, isValidAddress, allNull } = require('./util')
const { conversionUtil, conversionGreaterThan } = require('./conversion-util')
const BigNumber = require('bignumber.js')
module.exports = connect(mapStateToProps)(SendTransactionScreen)
@ -470,18 +469,14 @@ SendTransactionScreen.prototype.getAmountToSend = function (amount) {
const { activeCurrency } = this.state
const { conversionRate } = this.props
// TODO: need a clean way to integrate this into conversionUtil
const sendConversionRate = activeCurrency === 'ETH'
? conversionRate
: new BigNumber(1.0).div(conversionRate)
return conversionUtil(amount, {
fromNumericBase: 'dec',
toNumericBase: 'hex',
fromCurrency: activeCurrency,
toCurrency: 'ETH',
toDenomination: 'WEI',
conversionRate: sendConversionRate,
conversionRate,
invertConversionRate: activeCurrency !== 'ETH',
})
}