diff --git a/App.test.js b/App.test.js index 299ca16a..2772b374 100644 --- a/App.test.js +++ b/App.test.js @@ -250,3 +250,16 @@ it('Wallet can fetch balance', async () => { assert.ok(w.getUnconfirmedBalance() === 0); assert.ok(w._lastBalanceFetch > 0); }); + +describe('currency', () => { + it.only('fetches exchange rate and saves to AsyncStorage', async () => { + AsyncStorage.storageCache = {}; // cleanup from other tests + let currency = require('./currency'); + await currency.startUpdater(true); + let cur = AsyncStorage.storageCache[AppStorage.CURRENCY]; + cur = JSON.parse(cur); + assert.ok(Number.isInteger(cur[currency.STRUCT.LAST_UPDATED])); + assert.ok(cur[currency.STRUCT.LAST_UPDATED] > 0); + assert.ok(cur[currency.STRUCT.BTC_USD] > 0); + }); +}); diff --git a/currency.js b/currency.js new file mode 100644 index 00000000..89a82dab --- /dev/null +++ b/currency.js @@ -0,0 +1,63 @@ +import Frisbee from 'frisbee'; +import { AsyncStorage } from 'react-native'; +import { AppStorage } from './class'; + +let lang = {}; +// let btcusd = 6500; // default + +const STRUCT = { + LAST_UPDATED: 'LAST_UPDATED', + BTC_USD: 'BTC_USD', +}; + +async function updateExchangeRate() { + if (+new Date() - lang[STRUCT.LAST_UPDATED] <= 30 * 60 * 1000) { + // not updating too often + return; + } + + let json; + try { + const api = new Frisbee({ + baseURI: 'https://www.bitstamp.net', + }); + + let response = await api.get('/api/v2/ticker/btcusd'); + json = response.body; + if (typeof json === 'undefined' || typeof json.last === 'undefined') { + throw new Error('Could not update currency rate: ' + response.err); + } + } catch (Err) { + console.warn(Err); + } + + lang[STRUCT.LAST_UPDATED] = +new Date(); + lang[STRUCT.BTC_USD] = json.last * 1; + await AsyncStorage.setItem(AppStorage.CURRENCY, JSON.stringify(lang)); + console.log('updated currency exchange:', lang); +} + +async function startUpdater(doNotSetInterval) { + lang = await AsyncStorage.getItem(AppStorage.CURRENCY); + try { + lang = JSON.parse(lang); + } catch (Err) { + lang = {}; + } + if (lang) { + console.log('lang is true'); + } + lang = lang || {}; + + lang[STRUCT.LAST_UPDATED] = lang[STRUCT.LAST_UPDATED] || 0; + lang[STRUCT.BTC_USD] = lang[STRUCT.BTC_USD] || 6500; + + if (!doNotSetInterval) { + setInterval(() => updateExchangeRate(), 2 * 60 * 100); + } + return updateExchangeRate(); +} + +module.exports.updateExchangeRate = updateExchangeRate; +module.exports.startUpdater = startUpdater; +module.exports.STRUCT = STRUCT;