aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/conversion-util.js
diff options
context:
space:
mode:
authorThomas Huang <tmashuang@users.noreply.github.com>2017-11-03 01:02:09 +0800
committerGitHub <noreply@github.com>2017-11-03 01:02:09 +0800
commit86e88dc8135e9729a86c4b8ce3c00a9d4cad0a3e (patch)
tree1da4b27d85cb0e155c7709d7b3a73d75645726a5 /ui/app/conversion-util.js
parentc1d2a1226c5ba4e5cfe1eeae2f52a86b67fc7404 (diff)
parent56e9f98bd05de8ae26f653d15eec4304f0c72155 (diff)
downloadtangerine-wallet-browser-86e88dc8135e9729a86c4b8ce3c00a9d4cad0a3e.tar.gz
tangerine-wallet-browser-86e88dc8135e9729a86c4b8ce3c00a9d4cad0a3e.tar.zst
tangerine-wallet-browser-86e88dc8135e9729a86c4b8ce3c00a9d4cad0a3e.zip
Merge pull request #2519 from danjm/NewUI-flat-lintfixes
[NewUI-flat] New ui flat lintfixes
Diffstat (limited to 'ui/app/conversion-util.js')
-rw-r--r--ui/app/conversion-util.js54
1 files changed, 41 insertions, 13 deletions
diff --git a/ui/app/conversion-util.js b/ui/app/conversion-util.js
index 2f3fb1678..9359d7c90 100644
--- a/ui/app/conversion-util.js
+++ b/ui/app/conversion-util.js
@@ -13,7 +13,6 @@
* @param {string} [options.fromDenomination = 'WEI'] The denomination of the passed value
* @param {number} [options.numberOfDecimals] The desired number of in the result
* @param {number} [options.conversionRate] The rate to use to make the fromCurrency -> toCurrency conversion
-* @param {number} [options.ethToUSDRate] If present, a second conversion - at ethToUSDRate - happens after conversionRate is applied.
* @returns {(number | string | BN)}
*
* The utility passes value along with the options as a single object to the `converter` function.
@@ -23,6 +22,8 @@
*/
const BigNumber = require('bignumber.js')
+const ethUtil = require('ethereumjs-util')
+const BN = ethUtil.BN
const R = require('ramda')
const { stripHexPrefix } = require('ethereumjs-util')
@@ -38,6 +39,7 @@ const BIG_NUMBER_GWEI_MULTIPLIER = new BigNumber('1000000000')
const convert = R.invoker(1, 'times')
const round = R.invoker(2, 'round')(R.__, BigNumber.ROUND_DOWN)
const invertConversionRate = conversionRate => () => new BigNumber(1.0).div(conversionRate)
+const decToBigNumberViaString = n => R.pipe(String, toBigNumber['dec'])
// Setter Maps
const toBigNumber = {
@@ -51,7 +53,7 @@ const toNormalizedDenomination = {
}
const toSpecifiedDenomination = {
WEI: bigNumber => bigNumber.times(BIG_NUMBER_WEI_MULTIPLIER).round(),
- GWEI: bigNumber => bigNumber.times(BIG_NUMBER_GWEI_MULTIPLIER).round(),
+ GWEI: bigNumber => bigNumber.times(BIG_NUMBER_GWEI_MULTIPLIER).round(1),
}
const baseChange = {
hex: n => n.toString(16),
@@ -95,16 +97,16 @@ const whenPropApplySetterMap = (prop, setterMap) => whenPredSetWithPropAndSetter
// Conversion utility function
const converter = R.pipe(
+ whenPredSetCRWithPropAndSetter(R.prop('conversionRate'), 'conversionRate', decToBigNumberViaString),
whenPredSetCRWithPropAndSetter(R.prop('invertConversionRate'), 'conversionRate', invertConversionRate),
whenPropApplySetterMap('fromNumericBase', toBigNumber),
whenPropApplySetterMap('fromDenomination', toNormalizedDenomination),
whenPredSetWithPropAndSetter(fromAndToCurrencyPropsNotEqual, 'conversionRate', convert),
whenPropApplySetterMap('toDenomination', toSpecifiedDenomination),
- whenPredSetWithPropAndSetter(R.prop('ethToUSDRate'), 'ethToUSDRate', convert),
whenPredSetWithPropAndSetter(R.prop('numberOfDecimals'), 'numberOfDecimals', round),
whenPropApplySetterMap('toNumericBase', baseChange),
R.view(R.lensProp('value'))
-);
+)
const conversionUtil = (value, {
fromCurrency = null,
@@ -115,7 +117,6 @@ const conversionUtil = (value, {
toDenomination,
numberOfDecimals,
conversionRate,
- ethToUSDRate,
invertConversionRate,
}) => converter({
fromCurrency,
@@ -126,18 +127,17 @@ const conversionUtil = (value, {
toDenomination,
numberOfDecimals,
conversionRate,
- ethToUSDRate,
invertConversionRate,
value: value || '0',
-});
+})
const addCurrencies = (a, b, options = {}) => {
const {
aBase,
bBase,
- ...conversionOptions,
+ ...conversionOptions
} = options
- const value = (new BigNumber(a, aBase)).add(b, bBase);
+ const value = (new BigNumber(a, aBase)).add(b, bBase)
return converter({
value,
@@ -149,10 +149,13 @@ const multiplyCurrencies = (a, b, options = {}) => {
const {
multiplicandBase,
multiplierBase,
- ...conversionOptions,
+ ...conversionOptions
} = options
- const value = (new BigNumber(a, multiplicandBase)).times(b, multiplierBase);
+ const bigNumberA = new BigNumber(String(a), multiplicandBase)
+ const bigNumberB = new BigNumber(String(b), multiplierBase)
+
+ const value = bigNumberA.times(bigNumberB)
return converter({
value,
@@ -162,16 +165,41 @@ const multiplyCurrencies = (a, b, options = {}) => {
const conversionGreaterThan = (
{ ...firstProps },
- { ...secondProps },
+ { ...secondProps },
) => {
const firstValue = converter({ ...firstProps })
const secondValue = converter({ ...secondProps })
return firstValue.gt(secondValue)
}
+const conversionGTE = (
+ { ...firstProps },
+ { ...secondProps },
+) => {
+ const firstValue = converter({ ...firstProps })
+ const secondValue = converter({ ...secondProps })
+ return firstValue.greaterThanOrEqualTo(secondValue)
+}
+
+const conversionLTE = (
+ { ...firstProps },
+ { ...secondProps },
+) => {
+ const firstValue = converter({ ...firstProps })
+ const secondValue = converter({ ...secondProps })
+ return firstValue.lessThanOrEqualTo(secondValue)
+}
+
+const toNegative = (n, options = {}) => {
+ return multiplyCurrencies(n, -1, options)
+}
+
module.exports = {
conversionUtil,
addCurrencies,
multiplyCurrencies,
conversionGreaterThan,
-} \ No newline at end of file
+ conversionGTE,
+ conversionLTE,
+ toNegative,
+}