Skip to content

Commit

Permalink
ADD: IRR currency
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Jul 6, 2021
1 parent cbe9523 commit cd135c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
9 changes: 8 additions & 1 deletion ios/Widgets/Shared/WidgetAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class WidgetAPI {
urlString = "https://api.yadio.io/json/\(endPointKey)"
case "BitcoinduLiban":
urlString = "https://bitcoinduliban.org/api.php?key=lbpusd"
case "Exir":
urlString = "https://api.exir.io/v1/ticker?symbol=btc-irt"
default:
urlString = "https://api.coindesk.com/v1/bpi/currentprice/\(endPointKey).json"
}
Expand Down Expand Up @@ -59,7 +61,12 @@ class WidgetAPI {
latestRateDataStore = WidgetDataStore(rate: String(rateDouble), lastUpdate: lastUpdatedString, rateDouble: rateDouble)
case "BitcoinduLiban":
guard let rateString = json["BTC/LBP"] as? String else { break }
guard let rateDouble = Double(rateString) else {return}
guard let rateDouble = Double(rateString) else { break }
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
case "Exir":
guard let rateDouble = json["last"] as? Double else { break }
let rateString = String(rateDouble)
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
default:
Expand Down
19 changes: 19 additions & 0 deletions models/fiatUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const FiatUnitSource = Object.freeze({
CoinDesk: 'CoinDesk',
Yadio: 'Yadio',
BitcoinduLiban: 'BitcoinduLiban',
Exir: 'Exir',
});

const RateExtractors = Object.freeze({
Expand Down Expand Up @@ -57,6 +58,24 @@ const RateExtractors = Object.freeze({
let rate = json?.[`BTC/${ticker}`];
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);

rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
},
Exir: async ticker => {
const api = new Frisbee({ baseURI: 'https://api.exir.io' });
const res = await api.get('/v1/ticker?symbol=btc-irt');
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);

let json;
try {
json = typeof res.body === 'string' ? JSON.parse(res.body) : res.body;
} catch (e) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.last;
if (!rate) throw new Error(`Could not update rate for ${ticker}: data is wrong`);

rate = Number(rate);
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
return rate;
Expand Down
6 changes: 6 additions & 0 deletions models/fiatUnit.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
"locale": "hi-HN",
"source": "CoinDesk"
},
"IRR": {
"endPointKey": "IRR",
"symbol": "",
"locale": "fa-IR",
"source": "Exir"
},
"JPY": {
"endPointKey": "JPY",
"symbol": "¥",
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/Currency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ describe('currency', () => {
// disabled, because it throws "Service Temporarily Unavailable" on circleci
// await currency.setPrefferedCurrency(FiatUnit.LBP);
// await currency.startUpdater();
// cur = JSON.parse(await AsyncStorage.getItem(AppStorage.EXCHANGE_RATES));
// cur = JSON.parse(await AsyncStorage.getItem(currency.EXCHANGE_RATES));
// assert.ok(cur.BTC_LBP > 0);

// test Exir rate source
await currency.setPrefferedCurrency(FiatUnit.IRR);
await currency.startUpdater();
cur = JSON.parse(await AsyncStorage.getItem(currency.EXCHANGE_RATES));
assert.ok(cur.BTC_IRR > 0);
});
});

0 comments on commit cd135c9

Please sign in to comment.