Browse Source

REF: currency

localNotifications
Overtorment 6 years ago
parent
commit
d001b7eca3
  1. 17
      App.test.js
  2. 4
      class/app-storage.js
  3. 81
      currency.js
  4. 10
      screen/settings/currency.js

17
App.test.js

@ -6,6 +6,7 @@ import Settings from './screen/settings/settings';
import Selftest from './screen/selftest'; import Selftest from './screen/selftest';
import { BlueHeader } from './BlueComponents'; import { BlueHeader } from './BlueComponents';
import MockStorage from './MockStorage'; import MockStorage from './MockStorage';
import { FiatUnit } from './models/fiatUnit';
global.crypto = require('crypto'); // shall be used by tests under nodejs CLI, but not in RN environment global.crypto = require('crypto'); // shall be used by tests under nodejs CLI, but not in RN environment
let assert = require('assert'); let assert = require('assert');
jest.mock('react-native-custom-qr-codes', () => 'Video'); jest.mock('react-native-custom-qr-codes', () => 'Video');
@ -302,10 +303,24 @@ describe('currency', () => {
AsyncStorage.storageCache = {}; // cleanup from other tests AsyncStorage.storageCache = {}; // cleanup from other tests
let currency = require('./currency'); let currency = require('./currency');
await currency.startUpdater(); await currency.startUpdater();
let cur = AsyncStorage.storageCache[AppStorage.CURRENCY]; let cur = AsyncStorage.storageCache[AppStorage.EXCHANGE_RATES];
cur = JSON.parse(cur); cur = JSON.parse(cur);
assert.ok(Number.isInteger(cur[currency.STRUCT.LAST_UPDATED])); assert.ok(Number.isInteger(cur[currency.STRUCT.LAST_UPDATED]));
assert.ok(cur[currency.STRUCT.LAST_UPDATED] > 0); assert.ok(cur[currency.STRUCT.LAST_UPDATED] > 0);
assert.ok(cur['BTC_USD'] > 0); assert.ok(cur['BTC_USD'] > 0);
// now, setting other currency as default
AsyncStorage.storageCache[AppStorage.PREFERRED_CURRENCY] = JSON.stringify(FiatUnit.JPY);
await currency.startUpdater();
cur = JSON.parse(AsyncStorage.storageCache[AppStorage.EXCHANGE_RATES]);
assert.ok(cur['BTC_JPY'] > 0);
// now setting with a proper setter
await currency.setPrefferedCurrency(FiatUnit.EUR);
await currency.startUpdater();
let preferred = await currency.getPreferredCurrency();
assert.equal(preferred.endPointKey, 'EUR');
cur = JSON.parse(AsyncStorage.storageCache[AppStorage.EXCHANGE_RATES]);
assert.ok(cur['BTC_EUR'] > 0);
}); });
}); });

4
class/app-storage.js

@ -14,9 +14,9 @@ let encryption = require('../encryption');
export class AppStorage { export class AppStorage {
static FLAG_ENCRYPTED = 'data_encrypted'; static FLAG_ENCRYPTED = 'data_encrypted';
static LANG = 'lang'; static LANG = 'lang';
static CURRENCY = 'currency'; static EXCHANGE_RATES = 'currency';
static LNDHUB = 'lndhub'; static LNDHUB = 'lndhub';
static PREFERREDCURRENCY = 'preferredCurrency'; static PREFERRED_CURRENCY = 'preferredCurrency';
constructor() { constructor() {
/** {Array.<AbstractWallet>} */ /** {Array.<AbstractWallet>} */

81
currency.js

@ -4,26 +4,41 @@ import { AppStorage } from './class';
import { FiatUnit } from './models/fiatUnit'; import { FiatUnit } from './models/fiatUnit';
let BigNumber = require('bignumber.js'); let BigNumber = require('bignumber.js');
let preferredFiatCurrency = FiatUnit.USD; let preferredFiatCurrency = FiatUnit.USD;
let lang = {}; let exchangeRates = {};
// let btcusd = 6500; // default
const STRUCT = { const STRUCT = {
LAST_UPDATED: 'LAST_UPDATED', LAST_UPDATED: 'LAST_UPDATED',
}; };
/**
* Saves to storage preferred currency, whole object
* from `./models/fiatUnit`
*
* @param item {Object} one of the values in `./models/fiatUnit`
* @returns {Promise<void>}
*/
async function setPrefferedCurrency(item) {
await AsyncStorage.setItem(AppStorage.PREFERRED_CURRENCY, JSON.stringify(item));
}
async function getPreferredCurrency() {
return JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERRED_CURRENCY));
}
async function updateExchangeRate() { async function updateExchangeRate() {
try { if (+new Date() - exchangeRates[STRUCT.LAST_UPDATED] <= 30 * 60 * 1000) {
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY));
if (preferredFiatCurrency === null) {
throw Error();
}
} catch (_error) {
preferredFiatCurrency = FiatUnit.USD;
}
if (+new Date() - lang[STRUCT.LAST_UPDATED] <= 30 * 60 * 1000) {
// not updating too often // not updating too often
console.log('not updating too often');
return; return;
} }
try {
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERRED_CURRENCY));
} catch (_) {
console.log('error :-( default will be used');
}
preferredFiatCurrency = preferredFiatCurrency || FiatUnit.USD;
let json; let json;
try { try {
const api = new Frisbee({ const api = new Frisbee({
@ -31,7 +46,7 @@ async function updateExchangeRate() {
}); });
let response = await api.get('/v1/bpi/currentprice/' + preferredFiatCurrency.endPointKey + '.json'); let response = await api.get('/v1/bpi/currentprice/' + preferredFiatCurrency.endPointKey + '.json');
json = JSON.parse(response.body); json = JSON.parse(response.body);
if (typeof json === 'undefined' || typeof json.bpi[preferredFiatCurrency.endPointKey].rate_float === 'undefined') { if (!json || !json.bpi || !json.bpi[preferredFiatCurrency.endPointKey] || !json.bpi[preferredFiatCurrency.endPointKey].rate_float) {
throw new Error('Could not update currency rate: ' + response.err); throw new Error('Could not update currency rate: ' + response.err);
} }
} catch (Err) { } catch (Err) {
@ -39,43 +54,31 @@ async function updateExchangeRate() {
return; return;
} }
lang[STRUCT.LAST_UPDATED] = +new Date(); exchangeRates[STRUCT.LAST_UPDATED] = +new Date();
lang['BTC_' + preferredFiatCurrency.endPointKey] = json.bpi[preferredFiatCurrency.endPointKey].rate_float * 1; exchangeRates['BTC_' + preferredFiatCurrency.endPointKey] = json.bpi[preferredFiatCurrency.endPointKey].rate_float * 1;
await AsyncStorage.setItem(AppStorage.CURRENCY, JSON.stringify(lang)); await AsyncStorage.setItem(AppStorage.EXCHANGE_RATES, JSON.stringify(exchangeRates));
await AsyncStorage.setItem(AppStorage.PREFERRED_CURRENCY, JSON.stringify(preferredFiatCurrency));
} }
async function startUpdater(force = false) { let interval = false;
if (force) { async function startUpdater() {
await AsyncStorage.removeItem(AppStorage.CURRENCY); if (interval) {
} console.log('clear interval');
lang = await AsyncStorage.getItem(AppStorage.CURRENCY); clearInterval(interval);
try { exchangeRates[STRUCT.LAST_UPDATED] = 0;
preferredFiatCurrency = JSON.parse(await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY));
if (preferredFiatCurrency === null) {
throw Error();
}
} catch (_error) {
preferredFiatCurrency = FiatUnit.USD;
} }
try {
lang = JSON.parse(lang); interval = setInterval(() => updateExchangeRate(), 2 * 60 * 100);
} catch (Err) {
lang = {};
}
lang = lang || {};
lang[STRUCT.LAST_UPDATED] = lang[STRUCT.LAST_UPDATED] || 0;
lang['BTC_' + preferredFiatCurrency.endPointKey] = lang['BTC_' + preferredFiatCurrency.endPointKey] || 0;
setInterval(() => updateExchangeRate(), 2 * 60 * 100);
return updateExchangeRate(); return updateExchangeRate();
} }
function satoshiToLocalCurrency(satoshi) { function satoshiToLocalCurrency(satoshi) {
if (!lang['BTC_' + preferredFiatCurrency.endPointKey]) return satoshi; if (!exchangeRates['BTC_' + preferredFiatCurrency.endPointKey]) return satoshi;
let b = new BigNumber(satoshi); let b = new BigNumber(satoshi);
b = b b = b
.dividedBy(100000000) .dividedBy(100000000)
.multipliedBy(lang['BTC_' + preferredFiatCurrency.endPointKey]) .multipliedBy(exchangeRates['BTC_' + preferredFiatCurrency.endPointKey])
.toString(10); .toString(10);
b = parseFloat(b).toFixed(2); b = parseFloat(b).toFixed(2);
@ -106,3 +109,5 @@ module.exports.STRUCT = STRUCT;
module.exports.satoshiToLocalCurrency = satoshiToLocalCurrency; module.exports.satoshiToLocalCurrency = satoshiToLocalCurrency;
module.exports.satoshiToBTC = satoshiToBTC; module.exports.satoshiToBTC = satoshiToBTC;
module.exports.BTCToLocalCurrency = BTCToLocalCurrency; module.exports.BTCToLocalCurrency = BTCToLocalCurrency;
module.exports.setPrefferedCurrency = setPrefferedCurrency;
module.exports.getPreferredCurrency = getPreferredCurrency;

10
screen/settings/currency.js

@ -1,11 +1,9 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { FlatList, TouchableOpacity, AsyncStorage, ActivityIndicator, View } from 'react-native'; import { FlatList, TouchableOpacity, ActivityIndicator, View } from 'react-native';
import { SafeBlueArea, BlueNavigationStyle, BlueListItem } from '../../BlueComponents'; import { SafeBlueArea, BlueNavigationStyle, BlueListItem } from '../../BlueComponents';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Icon } from 'react-native-elements'; import { Icon } from 'react-native-elements';
import { AppStorage } from '../../class';
import { FiatUnit } from '../../models/fiatUnit'; import { FiatUnit } from '../../models/fiatUnit';
/** @type {AppStorage} */
let loc = require('../../loc'); let loc = require('../../loc');
let currency = require('../../currency'); let currency = require('../../currency');
@ -22,7 +20,7 @@ export default class Currency extends Component {
async componentDidMount() { async componentDidMount() {
try { try {
const preferredCurrency = await AsyncStorage.getItem(AppStorage.PREFERREDCURRENCY); const preferredCurrency = await currency.getPreferredCurrency();
if (preferredCurrency === null) { if (preferredCurrency === null) {
throw Error(); throw Error();
} }
@ -37,8 +35,8 @@ export default class Currency extends Component {
<TouchableOpacity <TouchableOpacity
onPress={() => { onPress={() => {
this.setState({ isSavingNewPreferredCurrency: true, selectedCurrency: item }, async () => { this.setState({ isSavingNewPreferredCurrency: true, selectedCurrency: item }, async () => {
await AsyncStorage.setItem(AppStorage.PREFERREDCURRENCY, JSON.stringify(item)); await currency.setPrefferedCurrency(item);
await currency.startUpdater(true); await currency.startUpdater();
this.setState({ isSavingNewPreferredCurrency: false }); this.setState({ isSavingNewPreferredCurrency: false });
}); });
}} }}

Loading…
Cancel
Save