mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-22 06:52:41 +01:00
REF: convert fiatUnit to typescript
This commit is contained in:
parent
170e5a742f
commit
1d015d5a2d
6 changed files with 45 additions and 20 deletions
|
@ -32,7 +32,7 @@
|
|||
6DD410A1266CADF10087DE03 /* Widgets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD410A0266CADF10087DE03 /* Widgets.swift */; };
|
||||
6DD410A7266CADF40087DE03 /* WidgetsExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 6DD4109C266CADF10087DE03 /* WidgetsExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
|
||||
6DD410AC266CAE470087DE03 /* PriceWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6CA4BC255872E3009312A5 /* PriceWidget.swift */; };
|
||||
6DD410AE266CAF1F0087DE03 /* fiatUnit.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */; };
|
||||
6DD410AE266CAF1F0087DE03 /* fiatUnits.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */; };
|
||||
6DD410AF266CAF5C0087DE03 /* WalletInformationAndMarketWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D9A2E06254BA347007B5B82 /* WalletInformationAndMarketWidget.swift */; };
|
||||
6DD410B0266CAF5C0087DE03 /* WalletInformationWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DEB4AB1254FB59C00E9F9AA /* WalletInformationWidget.swift */; };
|
||||
6DD410B1266CAF5C0087DE03 /* WidgetAPI+Electrum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6CA5142558EBA3009312A5 /* WidgetAPI+Electrum.swift */; };
|
||||
|
@ -75,8 +75,8 @@
|
|||
B43D037C225847C500FBAA95 /* Wallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = B43D0376225847C500FBAA95 /* Wallet.swift */; };
|
||||
B43D037D225847C500FBAA95 /* WalletInformation.swift in Sources */ = {isa = PBXBuildFile; fileRef = B43D0377225847C500FBAA95 /* WalletInformation.swift */; };
|
||||
B4EE583C226703320003363C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B40D4E35225841ED00428FCC /* Assets.xcassets */; };
|
||||
E5D4794B26781FC0007838C1 /* fiatUnit.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */; };
|
||||
E5D4794C26781FC1007838C1 /* fiatUnit.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnit.json */; };
|
||||
E5D4794B26781FC0007838C1 /* fiatUnits.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */; };
|
||||
E5D4794C26781FC1007838C1 /* fiatUnits.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD410AD266CAF1F0087DE03 /* fiatUnits.json */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
@ -274,7 +274,7 @@
|
|||
6DD4109C266CADF10087DE03 /* WidgetsExtension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = WidgetsExtension.appex; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
6DD410A0266CADF10087DE03 /* Widgets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Widgets.swift; sourceTree = "<group>"; };
|
||||
6DD410A4266CADF40087DE03 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
6DD410AD266CAF1F0087DE03 /* fiatUnit.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = fiatUnit.json; path = ../../../../models/fiatUnit.json; sourceTree = "<group>"; };
|
||||
6DD410AD266CAF1F0087DE03 /* fiatUnits.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = fiatUnits.json; path = ../../../../models/fiatUnits.json; sourceTree = "<group>"; };
|
||||
6DD410C3266CCB780087DE03 /* WidgetsExtension.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = WidgetsExtension.entitlements; sourceTree = SOURCE_ROOT; };
|
||||
6DEB496F254E38DE00E9F9AA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/MainInterface.strings; sourceTree = "<group>"; };
|
||||
6DEB4AB1254FB59C00E9F9AA /* WalletInformationWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletInformationWidget.swift; sourceTree = "<group>"; };
|
||||
|
@ -512,7 +512,7 @@
|
|||
6D2AA8062568B8E50090B089 /* Fiat */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
6DD410AD266CAF1F0087DE03 /* fiatUnit.json */,
|
||||
6DD410AD266CAF1F0087DE03 /* fiatUnits.json */,
|
||||
6D2AA8072568B8F40090B089 /* FiatUnit.swift */,
|
||||
);
|
||||
path = Fiat;
|
||||
|
@ -968,7 +968,7 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
6DD410B7266CAF5C0087DE03 /* Assets.xcassets in Resources */,
|
||||
6DD410AE266CAF1F0087DE03 /* fiatUnit.json in Resources */,
|
||||
6DD410AE266CAF1F0087DE03 /* fiatUnits.json in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
@ -977,7 +977,7 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
B40D4E36225841ED00428FCC /* Assets.xcassets in Resources */,
|
||||
E5D4794C26781FC1007838C1 /* fiatUnit.json in Resources */,
|
||||
E5D4794C26781FC1007838C1 /* fiatUnits.json in Resources */,
|
||||
B40D4E34225841EC00428FCC /* Interface.storyboard in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -987,7 +987,7 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
B4EE583C226703320003363C /* Assets.xcassets in Resources */,
|
||||
E5D4794B26781FC0007838C1 /* fiatUnit.json in Resources */,
|
||||
E5D4794B26781FC0007838C1 /* fiatUnits.json in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
|
@ -16,7 +16,7 @@ struct FiatUnit: Codable {
|
|||
}
|
||||
|
||||
func fiatUnit(currency: String) -> FiatUnit? {
|
||||
return Bundle.main.decode([String: FiatUnit].self, from: "fiatUnit.json").first(where: {$1.endPointKey == currency})?.value
|
||||
return Bundle.main.decode([String: FiatUnit].self, from: "fiatUnits.json").first(where: {$1.endPointKey == currency})?.value
|
||||
}
|
||||
|
||||
extension Bundle {
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
import Frisbee from 'frisbee';
|
||||
import untypedFiatUnit from './fiatUnits.json';
|
||||
|
||||
export const FiatUnitSource = Object.freeze({
|
||||
export const FiatUnitSource = {
|
||||
CoinDesk: 'CoinDesk',
|
||||
Yadio: 'Yadio',
|
||||
BitcoinduLiban: 'BitcoinduLiban',
|
||||
Exir: 'Exir',
|
||||
});
|
||||
} as const;
|
||||
|
||||
const RateExtractors = Object.freeze({
|
||||
CoinDesk: async ticker => {
|
||||
const RateExtractors = {
|
||||
CoinDesk: async (ticker: string): Promise<number> => {
|
||||
const api = new Frisbee({ baseURI: 'https://api.coindesk.com' });
|
||||
const res = await api.get(`/v1/bpi/currentprice/${ticker}.json`);
|
||||
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
|
||||
|
@ -26,7 +27,7 @@ const RateExtractors = Object.freeze({
|
|||
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
|
||||
return rate;
|
||||
},
|
||||
Yadio: async ticker => {
|
||||
Yadio: async (ticker: string): Promise<number> => {
|
||||
const api = new Frisbee({ baseURI: 'https://api.yadio.io/json' });
|
||||
const res = await api.get(`/${ticker}`);
|
||||
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
|
||||
|
@ -44,7 +45,7 @@ const RateExtractors = Object.freeze({
|
|||
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
|
||||
return rate;
|
||||
},
|
||||
BitcoinduLiban: async ticker => {
|
||||
BitcoinduLiban: async (ticker: string): Promise<number> => {
|
||||
const api = new Frisbee({ baseURI: 'https://bitcoinduliban.org' });
|
||||
const res = await api.get('/api.php?key=lbpusd');
|
||||
if (res.err) throw new Error(`Could not update rate for ${ticker}: ${res.err}`);
|
||||
|
@ -62,7 +63,7 @@ const RateExtractors = Object.freeze({
|
|||
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
|
||||
return rate;
|
||||
},
|
||||
Exir: async ticker => {
|
||||
Exir: async (ticker: string): Promise<number> => {
|
||||
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}`);
|
||||
|
@ -80,10 +81,18 @@ const RateExtractors = Object.freeze({
|
|||
if (!(rate >= 0)) throw new Error(`Could not update rate for ${ticker}: data is wrong`);
|
||||
return rate;
|
||||
},
|
||||
});
|
||||
} as const;
|
||||
|
||||
export const FiatUnit = require('./fiatUnit.json');
|
||||
type FiatUnit = {
|
||||
[key: string]: {
|
||||
endPointKey: string;
|
||||
symbol: string;
|
||||
locale: string;
|
||||
source: 'CoinDesk' | 'Yadio' | 'Exir' | 'BitcoinduLiban';
|
||||
};
|
||||
};
|
||||
export const FiatUnit = untypedFiatUnit as FiatUnit;
|
||||
|
||||
export async function getFiatRate(ticker) {
|
||||
export async function getFiatRate(ticker: string): Promise<number> {
|
||||
return await RateExtractors[FiatUnit[ticker].source](ticker);
|
||||
}
|
|
@ -10,7 +10,8 @@
|
|||
"noEmit": true,
|
||||
"strict": true,
|
||||
"target": "esnext",
|
||||
"skipLibCheck": true
|
||||
"skipLibCheck": true,
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
|
||||
}
|
||||
|
|
15
typings/frisbee.d.ts
vendored
Normal file
15
typings/frisbee.d.ts
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
declare module 'frisbee' {
|
||||
// TODO: improve with more specific types
|
||||
type RequestOptions = Record<string, any>;
|
||||
type Response = any;
|
||||
|
||||
declare class Frisbee {
|
||||
constructor(options: { baseURI?: string; headers?: Record<string, string> });
|
||||
|
||||
get(path: string, options?: RequestOptions): Promise<Response>;
|
||||
post(path: string, options?: RequestOptions): Promise<Response>;
|
||||
// TODO: add missing methods
|
||||
}
|
||||
|
||||
export default Frisbee;
|
||||
}
|
Loading…
Add table
Reference in a new issue