From 3ebc79624c4982e98daa2ee00c630f81a27df996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Rodriguez=20Ve=CC=81lez?= Date: Fri, 21 Dec 2018 22:23:52 -0500 Subject: [PATCH] FIX: Currency Unit change --- App.js | 11 ++++- BlueComponents.js | 3 +- class/abstract-hd-wallet.js | 5 --- class/abstract-wallet.js | 7 --- class/lightning-custodian-wallet.js | 5 --- ios/BlueWallet/Info.plist | 2 +- loc/index.js | 69 ++++++++++++++++------------- package-lock.json | 13 ++++++ package.json | 1 + screen/wallets/transactions.js | 41 ++++++++++------- 10 files changed, 90 insertions(+), 67 deletions(-) diff --git a/App.js b/App.js index 390e8ea82..07ae0f0ec 100644 --- a/App.js +++ b/App.js @@ -20,7 +20,16 @@ export default class App extends React.Component { } handleOpenURL = event => { - if (event.url && event.url.toLowerCase().indexOf('bitcoin:') === 0) { + if (typeof event !== 'undefined') { + return; + } + if (event === null) { + return; + } + if (!event.hasOwnProperty('url')) { + return; + } + if (event.url.indexOf('bitcoin:') === 0 || event.url.indexOf('BITCOIN:') === 0) { this.navigator && this.navigator.dispatch( NavigationActions.navigate({ diff --git a/BlueComponents.js b/BlueComponents.js index 9c1ac3f7e..3d6ba5c71 100644 --- a/BlueComponents.js +++ b/BlueComponents.js @@ -23,6 +23,7 @@ import { HDLegacyP2PKHWallet } from './class/hd-legacy-p2pkh-wallet'; import { HDLegacyBreadwalletWallet } from './class/hd-legacy-breadwallet-wallet'; import { HDSegwitP2SHWallet } from './class/hd-segwit-p2sh-wallet'; import { LightningCustodianWallet } from './class/lightning-custodian-wallet'; +import { BitcoinUnit } from './models/bitcoinUnits'; let loc = require('./loc/'); /** @type {AppStorage} */ let BlueApp = require('./BlueApp'); @@ -1047,7 +1048,7 @@ export class WalletsCarousel extends Component { color: '#fff', }} > - {loc.formatBalance(item.getBalance(), item.getPreferredBalanceUnit())} + {loc.formatBalance(item.getBalance(), BitcoinUnit.BTC, item.getPreferredBalanceUnit())} CFBundleVersion - 154 + 155 LSRequiresIPhoneOS NSAppTransportSecurity diff --git a/loc/index.js b/loc/index.js index 9c73a124d..db1fa9145 100644 --- a/loc/index.js +++ b/loc/index.js @@ -2,8 +2,8 @@ import Localization from 'react-localization'; import { AsyncStorage } from 'react-native'; import { AppStorage } from '../class'; import { BitcoinUnit } from '../models/bitcoinUnits'; -let currency = require('../currency'); -let BigNumber = require('bignumber.js'); +const BTCUnits = require('bitcoin-units'); +const currency = require('../currency'); let strings; // first-time loading sequence @@ -69,43 +69,48 @@ strings.transactionTimeToReadable = function(time) { * @param unit {String} Value from models/bitcoinUnits.js * @returns {string} */ -strings.formatBalance = (balance, unit) => { - const conversion = 100000000; - if (unit === undefined) { +strings.formatBalance = (balance, fromUnit, toUnit) => { + if (toUnit === undefined || fromUnit === toUnit) { return balance + ' ' + BitcoinUnit.BTC; - } else { - if (balance !== 0) { - let b = new BigNumber(balance); - if (unit === BitcoinUnit.BTC) { - if (!Number.isInteger(balance)) { - return balance + ' ' + BitcoinUnit.BTC; - } - return b.div(conversion).toString() + ' ' + BitcoinUnit.BTC; - } else if (unit === BitcoinUnit.SATS) { - return (b.times(conversion).toString() + ' ' + BitcoinUnit.SATS).replace(/\./g, ''); - } else if (unit === BitcoinUnit.LOCAL_CURRENCY) { - return currency.satoshiToLocalCurrency(b.times(conversion).toNumber()); - } + } + if (balance !== 0) { + if (toUnit === BitcoinUnit.BTC) { + return BTCUnits(balance, fromUnit) + .to(BitcoinUnit.BTC) + .format(); + } else if (toUnit === BitcoinUnit.SATS) { + return BTCUnits(balance, fromUnit) + .to(BitcoinUnit.SATS) + .format(); + } else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) { + return currency.satoshiToLocalCurrency( + BTCUnits(balance, BitcoinUnit.BTC) + .to(BitcoinUnit.SATS) + .toString(), + ); } } return balance + ' ' + BitcoinUnit.BTC; }; -strings.formatBalanceWithoutSuffix = (balance, unit) => { - let conversion = 100000000; +strings.formatBalanceWithoutSuffix = (balance, fromUnit, toUnit) => { + if (toUnit === undefined) { + return balance; + } if (balance !== 0) { - let b = new BigNumber(balance); - if (unit === BitcoinUnit.BTC) { - return b.dividedBy(conversion).toString(); - } else if (unit === BitcoinUnit.SATS) { - conversion = 1; - return b - .times(conversion) - .toString() - .replace(/\./g, ''); - } else if (unit === BitcoinUnit.LOCAL_CURRENCY) { - conversion = 1; - return currency.satoshiToLocalCurrency(b.times(conversion).toNumber()).toString(); + if (fromUnit === BitcoinUnit.LOCAL_CURRENCY || toUnit === BitcoinUnit.LOCAL_CURRENCY) { + BTCUnits.setFiat(BitcoinUnit.LOCAL_CURRENCY, 3600); + } + if (toUnit === BitcoinUnit.BTC || toUnit === undefined) { + return BTCUnits(balance, fromUnit) + .to(BitcoinUnit.BTC) + .toString(); + } else if (toUnit === BitcoinUnit.SATS) { + return BTCUnits(balance, fromUnit) + .to(BitcoinUnit.SATS) + .toString(); + } else if (toUnit === BitcoinUnit.LOCAL_CURRENCY) { + return currency.satoshiToLocalCurrency(BTCUnits(balance, fromUnit).to(BitcoinUnit.SATS)); } } return balance; diff --git a/package-lock.json b/package-lock.json index d9d6af9bb..2b1851d4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2774,6 +2774,11 @@ "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.36.tgz", "integrity": "sha512-t70bfa7HYEA1D9idDbmuv7YbsbVkQ+Hp+8KFSul4aE5e/i1bjCNIRYJZlA8Q8p0r9T8cF/RVvwUgRA//FydEyg==" }, + "big.js": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz", + "integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ==" + }, "bigi": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/bigi/-/bigi-1.4.2.tgz", @@ -2817,6 +2822,14 @@ "resolved": "https://registry.npmjs.org/bitcoin-ops/-/bitcoin-ops-1.4.1.tgz", "integrity": "sha512-pef6gxZFztEhaE9RY9HmWVmiIHqCb2OyS4HPKkpc6CIiiOa3Qmuoylxc5P2EkU3w+5eTSifI9SEZC88idAIGow==" }, + "bitcoin-units": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/bitcoin-units/-/bitcoin-units-0.3.0.tgz", + "integrity": "sha512-CxmCxfoYDKQhKXMvT1HnShVL1E6rT5IUkVenhrFDphrRGKQHJUTGtK68UY7JnAQKqJA4510cC5MO6aEd/8v1EQ==", + "requires": { + "big.js": "^5.2.2" + } + }, "bitcoinjs-lib": { "version": "3.3.2", "resolved": "https://registry.npmjs.org/bitcoinjs-lib/-/bitcoinjs-lib-3.3.2.tgz", diff --git a/package.json b/package.json index f860a1b1b..620bc2dc7 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "bignumber.js": "^7.0.0", "bip21": "^2.0.2", "bip39": "^2.5.0", + "bitcoin-units": "^0.3.0", "bitcoinjs-lib": "^3.3.2", "buffer": "^5.2.1", "buffer-reverse": "^1.0.1", diff --git a/screen/wallets/transactions.js b/screen/wallets/transactions.js index 37950a885..a5190206c 100644 --- a/screen/wallets/transactions.js +++ b/screen/wallets/transactions.js @@ -3,7 +3,6 @@ import { Text, View, Image, FlatList, RefreshControl, TouchableOpacity } from 'r import LinearGradient from 'react-native-linear-gradient'; import PropTypes from 'prop-types'; import { NavigationEvents } from 'react-navigation'; -import { LightningCustodianWallet } from '../../class'; import { BlueText, BlueTransactionOnchainIcon, @@ -18,9 +17,10 @@ import { } from '../../BlueComponents'; import { Icon } from 'react-native-elements'; import { BitcoinUnit } from '../../models/bitcoinUnits'; +import { LightningCustodianWallet } from '../../class'; /** @type {AppStorage} */ - let BlueApp = require('../../BlueApp'); + let loc = require('../../loc'); let EV = require('../../events'); @@ -56,13 +56,13 @@ export default class WalletTransactions extends Component { // here, when we receive REMOTE_TRANSACTIONS_COUNT_CHANGED we fetch TXs and balance for current wallet EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED, this.refreshTransactionsFunction.bind(this)); const wallet = props.navigation.getParam('wallet'); - this.props.navigation.setParams({ wallet: wallet }); this.state = { isLoading: true, isTransactionsLoading: false, wallet: wallet, dataSource: wallet.getTransactions(), + walletPreviousPreferredUnit: wallet.getPreferredBalanceUnit(), }; } @@ -173,19 +173,23 @@ export default class WalletTransactions extends Component { } changeWalletBalanceUnit() { - const { wallet } = this.state; - if (wallet.getPreferredBalanceUnit() === undefined || wallet.getPreferredBalanceUnit() === BitcoinUnit.BTC) { - wallet.setPreferredBalanceUnit(BitcoinUnit.SATS); - } else if (wallet.getPreferredBalanceUnit() === BitcoinUnit.SATS) { - wallet.setPreferredBalanceUnit(BitcoinUnit.LOCAL_CURRENCY); - } else if (wallet.getPreferredBalanceUnit() === BitcoinUnit.LOCAL_CURRENCY) { - wallet.setPreferredBalanceUnit(BitcoinUnit.BTC); + let walletPreviousPreferredUnit = this.state.wallet.getPreferredBalanceUnit(); + const wallet = this.state.wallet; + if (walletPreviousPreferredUnit === BitcoinUnit.BTC) { + wallet.preferredBalanceUnit = BitcoinUnit.SATS; + walletPreviousPreferredUnit = BitcoinUnit.BTC; + } else if (walletPreviousPreferredUnit === BitcoinUnit.SATS) { + wallet.preferredBalanceUnit = BitcoinUnit.LOCAL_CURRENCY; + walletPreviousPreferredUnit = BitcoinUnit.SATS; + } else if (walletPreviousPreferredUnit === BitcoinUnit.LOCAL_CURRENCY) { + wallet.preferredBalanceUnit = BitcoinUnit.BTC; + walletPreviousPreferredUnit = BitcoinUnit.LOCAL_CURRENCY; } else { - wallet.setPreferredBalanceUnit(BitcoinUnit.BTC); + wallet.preferredBalanceUnit = BitcoinUnit.BTC; + walletPreviousPreferredUnit = BitcoinUnit.BTC; } - this.setState({ wallet: wallet }, () => { - BlueApp.saveToDisk(); - }); + + this.setState({ wallet: wallet, walletPreviousPreferredUnit: walletPreviousPreferredUnit }); } renderWalletHeader = () => { @@ -228,7 +232,13 @@ export default class WalletTransactions extends Component { color: '#fff', }} > - {loc.formatBalance(this.state.wallet.getBalance(), this.state.wallet.getPreferredBalanceUnit())} + {loc + .formatBalance( + this.state.wallet.getBalance(), + this.state.walletPreviousPreferredUnit, + this.state.wallet.getPreferredBalanceUnit(), + ) + .toString()} @@ -442,6 +452,7 @@ export default class WalletTransactions extends Component { rightTitle={loc .formatBalanceWithoutSuffix( (rowData.item.value && rowData.item.value) || 0, + BitcoinUnit.SATS, this.state.wallet.getPreferredBalanceUnit(), ) .toString()}