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