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:
parent
49f76d27a9
commit
db1258f3de
@ -36,6 +36,7 @@ const BIG_NUMBER_WEI_MULTIPLIER = new BigNumber('1000000000000000000')
|
|||||||
// Individual Setters
|
// Individual Setters
|
||||||
const convert = R.invoker(1, 'times')
|
const convert = R.invoker(1, 'times')
|
||||||
const round = R.invoker(2, 'toFormat')(R.__, BigNumber.ROUND_DOWN)
|
const round = R.invoker(2, 'toFormat')(R.__, BigNumber.ROUND_DOWN)
|
||||||
|
const invertConversionRate = conversionRate => () => new BigNumber(1.0).div(conversionRate)
|
||||||
|
|
||||||
// Setter Maps
|
// Setter Maps
|
||||||
const toBigNumber = {
|
const toBigNumber = {
|
||||||
@ -63,13 +64,23 @@ const fromAndToCurrencyPropsNotEqual = R.compose(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Lens
|
// 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
|
// conditional 'value' setting wrappers
|
||||||
const whenPredSetWithPropAndSetter = (pred, prop, setter) => R.when(
|
const whenPredSetWithPropAndSetter = (pred, prop, setter) => R.when(
|
||||||
pred,
|
pred,
|
||||||
R.converge(
|
R.converge(
|
||||||
valuePropertyLense,
|
valuePropertyLens,
|
||||||
[R.pipe(R.prop(prop), setter), R.identity]
|
[R.pipe(R.prop(prop), setter), R.identity]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -81,6 +92,7 @@ const whenPropApplySetterMap = (prop, setterMap) => whenPredSetWithPropAndSetter
|
|||||||
|
|
||||||
// Conversion utility function
|
// Conversion utility function
|
||||||
const converter = R.pipe(
|
const converter = R.pipe(
|
||||||
|
whenPredSetCRWithPropAndSetter(R.prop('invertConversionRate'), 'conversionRate', invertConversionRate),
|
||||||
whenPropApplySetterMap('fromNumericBase', toBigNumber),
|
whenPropApplySetterMap('fromNumericBase', toBigNumber),
|
||||||
whenPropApplySetterMap('fromDenomination', toNormalizedDenomination),
|
whenPropApplySetterMap('fromDenomination', toNormalizedDenomination),
|
||||||
whenPredSetWithPropAndSetter(fromAndToCurrencyPropsNotEqual, 'conversionRate', convert),
|
whenPredSetWithPropAndSetter(fromAndToCurrencyPropsNotEqual, 'conversionRate', convert),
|
||||||
@ -101,6 +113,7 @@ const conversionUtil = (value, {
|
|||||||
numberOfDecimals,
|
numberOfDecimals,
|
||||||
conversionRate,
|
conversionRate,
|
||||||
ethToUSDRate,
|
ethToUSDRate,
|
||||||
|
invertConversionRate,
|
||||||
}) => converter({
|
}) => converter({
|
||||||
fromCurrency,
|
fromCurrency,
|
||||||
toCurrency,
|
toCurrency,
|
||||||
@ -111,6 +124,7 @@ const conversionUtil = (value, {
|
|||||||
numberOfDecimals,
|
numberOfDecimals,
|
||||||
conversionRate,
|
conversionRate,
|
||||||
ethToUSDRate,
|
ethToUSDRate,
|
||||||
|
invertConversionRate,
|
||||||
value,
|
value,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ const {
|
|||||||
const { stripHexPrefix, addHexPrefix } = require('ethereumjs-util')
|
const { stripHexPrefix, addHexPrefix } = require('ethereumjs-util')
|
||||||
const { isHex, numericBalance, isValidAddress, allNull } = require('./util')
|
const { isHex, numericBalance, isValidAddress, allNull } = require('./util')
|
||||||
const { conversionUtil, conversionGreaterThan } = require('./conversion-util')
|
const { conversionUtil, conversionGreaterThan } = require('./conversion-util')
|
||||||
const BigNumber = require('bignumber.js')
|
|
||||||
|
|
||||||
module.exports = connect(mapStateToProps)(SendTransactionScreen)
|
module.exports = connect(mapStateToProps)(SendTransactionScreen)
|
||||||
|
|
||||||
@ -470,18 +469,14 @@ SendTransactionScreen.prototype.getAmountToSend = function (amount) {
|
|||||||
const { activeCurrency } = this.state
|
const { activeCurrency } = this.state
|
||||||
const { conversionRate } = this.props
|
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, {
|
return conversionUtil(amount, {
|
||||||
fromNumericBase: 'dec',
|
fromNumericBase: 'dec',
|
||||||
toNumericBase: 'hex',
|
toNumericBase: 'hex',
|
||||||
fromCurrency: activeCurrency,
|
fromCurrency: activeCurrency,
|
||||||
toCurrency: 'ETH',
|
toCurrency: 'ETH',
|
||||||
toDenomination: 'WEI',
|
toDenomination: 'WEI',
|
||||||
conversionRate: sendConversionRate,
|
conversionRate,
|
||||||
|
invertConversionRate: activeCurrency !== 'ETH',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user