FIX: Currency Unit change

This commit is contained in:
Marcos Rodriguez Vélez 2018-12-21 22:23:52 -05:00
parent fb1f862e82
commit 3ebc79624c
10 changed files with 90 additions and 67 deletions

11
App.js
View File

@ -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({

View File

@ -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())}
</Text>
<Text style={{ backgroundColor: 'transparent' }} />
<Text

View File

@ -88,11 +88,6 @@ export class AbstractHDWallet extends LegacyWallet {
return this.preferredBalanceUnit || BitcoinUnit.BTC;
}
setPreferredBalanceUnit(unit) {
this.preferredBalanceUnit = unit;
return this;
}
/**
* @return {Boolean} is mnemonic in `this.secret` valid
*/

View File

@ -39,13 +39,6 @@ export class AbstractWallet {
return this.preferredBalanceUnit || BitcoinUnit.BTC;
}
setPreferredBalanceUnit(unit) {
const indexOf = Object.values(BitcoinUnit).indexOf(unit);
const currentPreferredUnit = Object.entries(BitcoinUnit)[indexOf];
this.preferredBalanceUnit = currentPreferredUnit;
return this;
}
allowReceive() {
return true;
}

View File

@ -505,11 +505,6 @@ export class LightningCustodianWallet extends LegacyWallet {
getPreferredBalanceUnit() {
return this.preferredBalanceUnit || BitcoinUnit.SATS;
}
setPreferredBalanceUnit(unit) {
this.preferredBalanceUnit = unit;
return this;
}
}
/*

View File

@ -34,7 +34,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>154</string>
<string>155</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>

View File

@ -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;

13
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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()}
</Text>
</TouchableOpacity>
<Text style={{ backgroundColor: 'transparent' }} />
@ -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()}