FIX: Bug: CNY price is wrong #5705

This commit is contained in:
Marcos Rodriguez Velez 2023-10-22 10:05:35 -04:00
parent d90de3efab
commit 2fbf8b42c9
No known key found for this signature in database
GPG Key ID: 6030B2F48CCE86D7
3 changed files with 26 additions and 1 deletions

View File

@ -37,6 +37,8 @@ class WidgetAPI {
urlString = "https://api.wazirx.com/api/v2/tickers/btcinr"
case "Bitstamp":
urlString = "https://www.bitstamp.net/api/v2/ticker/btc\(endPointKey.lowercased())"
case "Coinbase":
urlString = "https://api.coinbase.com/v2/prices/BTC-\(endPointKey.uppercased())/buy"
case "CoinGecko":
urlString = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=\(endPointKey.lowercased())"
default:
@ -95,6 +97,13 @@ class WidgetAPI {
else { break }
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
case "Coinbase":
guard let data = json["data"] as? Dictionary<String, Any>,
let rateString = data["amount"] as? String,
let rateDouble = Double(rateString)
else { break }
let lastUpdatedString = ISO8601DateFormatter().string(from: Date())
latestRateDataStore = WidgetDataStore(rate: rateString, lastUpdate: lastUpdatedString, rateDouble: rateDouble)
default:
guard let bpi = json["bpi"] as? Dictionary<String, Any>,
let preferredCurrency = bpi[endPointKey] as? Dictionary<String, Any>,

View File

@ -1,6 +1,7 @@
import untypedFiatUnit from './fiatUnits.json';
export const FiatUnitSource = {
Coinbase: 'Coinbase',
CoinDesk: 'CoinDesk',
CoinGecko: 'CoinGecko',
Yadio: 'Yadio',
@ -11,6 +12,21 @@ export const FiatUnitSource = {
} as const;
const RateExtractors = {
Coinbase: async (ticker: string): Promise<number> => {
let json;
try {
const res = await fetch(`https://api.coinbase.com/v2/prices/BTC-${ticker.toUpperCase()}/buy`);
json = await res.json();
} catch (e: any) {
throw new Error(`Could not update rate for ${ticker}: ${e.message}`);
}
let rate = json?.data?.amount;
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;
},
CoinDesk: async (ticker: string): Promise<number> => {
let json;
try {

View File

@ -68,7 +68,7 @@
"CNY": {
"endPointKey": "CNY",
"locale": "zh-CN",
"source": "CoinDesk",
"source": "Coinbase",
"symbol": "¥"
},
"COP": {