Skip to content

Commit

Permalink
FiatApi: assign enum values and types based on the provider
Browse files Browse the repository at this point in the history
This was already the case before updating typescript to the latest v3 version in 35859ed,
which temporarily removed that handling as is was not compatible with newer typescript
versions. This commit reimplements this behavior in a different fashion.
  • Loading branch information
danimoh committed Sep 6, 2024
1 parent 911729a commit 5f0f702
Showing 1 changed file with 88 additions and 76 deletions.
164 changes: 88 additions & 76 deletions src/lib/FiatApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,82 +50,84 @@ FiatApi.SupportedProvider = Object.freeze({
});

/**
* @type {FiatApi.SupportedProvider}
* The effective provider used by the FiatApi. As this is not meant to be changeable at runtime, all code for other
* providers can be commented out, to avoid type confusion.
* @typedef {'CoinGecko'} FiatApi.Provider
* @type {FiatApi.Provider}
* The effective provider used by the FiatApi. As this is not meant to be changeable at runtime, a constant value and
* type are assigned which trigger the correct assignments of enum values and types below.
*/
FiatApi.Provider = FiatApi.SupportedProvider.CoinGecko;

// switch (FiatApi.Provider) { // eslint-disable-line default-case, -- no default to let ts warn us on missing providers
// case FiatApi.SupportedProvider.CoinGecko: {
/* eslint-disable indent */
/**
* @enum { 'nim' | 'btc' }
* Crypto currencies supported by the coingecko api that are currently of interest to us.
*/
FiatApi.SupportedCryptoCurrency = Object.freeze({
NIM: /** @type {'nim'} */ ('nim'),
BTC: /** @type {'btc'} */ ('btc'),
});
/**
* @enum { 'nim' | 'btc' }
* Crypto currencies supported by the coingecko api that are currently of interest to us.
*/
FiatApi.SupportedCryptoCurrencyCoinGecko = Object.freeze({
NIM: /** @type {'nim'} */ ('nim'),
BTC: /** @type {'btc'} */ ('btc'),
});

/**
* @enum {'aed' | 'ars' | 'aud' | 'bdt' | 'bhd' | 'bmd' | 'brl' | 'cad' | 'chf' | 'clp' | 'cny' | 'czk' | 'dkk'
* | 'eur' | 'gbp' | 'gel' | 'hkd' | 'huf' | 'idr' | 'ils' | 'inr' | 'jpy' | 'krw' | 'kwd' | 'lkr' | 'mmk'
* | 'mxn' | 'myr' | 'ngn' | 'nok' | 'nzd' | 'php' | 'pkr' | 'pln' | 'rub' | 'sar' | 'sek' | 'sgd' | 'thb'
* | 'try' | 'twd' | 'uah' | 'usd' | 'vnd' | 'zar'}
* Fiat currencies supported by the coingecko api. Note that coingecko supports more vs_currencies (see
* https://api.coingecko.com/api/v3/simple/supported_vs_currencies) but also includes crypto currencies and
* ounces of gold amongst others that are not fiat currencies. See FiatApi in @nimiq/utils for how this list was
* assembled.
*/
FiatApi.SupportedFiatCurrency = Object.freeze({
AED: /** @type {'aed'} */ ('aed'), // Arab Emirates Dirham
ARS: /** @type {'ars'} */ ('ars'), // Argentine Peso
AUD: /** @type {'aud'} */ ('aud'), // Australian Dollar
BDT: /** @type {'bdt'} */ ('bdt'), // Bangladeshi Taka
BHD: /** @type {'bhd'} */ ('bhd'), // Bahraini Dinar
BMD: /** @type {'bmd'} */ ('bmd'), // Bermudan Dollar
BRL: /** @type {'brl'} */ ('brl'), // Brazilian Real
CAD: /** @type {'cad'} */ ('cad'), // Canadian Dollar
CHF: /** @type {'chf'} */ ('chf'), // Swiss Franc
CLP: /** @type {'clp'} */ ('clp'), // Chilean Peso
CNY: /** @type {'cny'} */ ('cny'), // Chinese Yuan
CZK: /** @type {'czk'} */ ('czk'), // Czech Koruna
DKK: /** @type {'dkk'} */ ('dkk'), // Danish Krone
EUR: /** @type {'eur'} */ ('eur'), // Euro
GBP: /** @type {'gbp'} */ ('gbp'), // British Pound
GEL: /** @type {'gel'} */ ('gel'), // Georgian Lari
HKD: /** @type {'hkd'} */ ('hkd'), // Hong Kong Dollar
HUF: /** @type {'huf'} */ ('huf'), // Hungarian Forint
IDR: /** @type {'idr'} */ ('idr'), // Indonesian Rupiah
ILS: /** @type {'ils'} */ ('ils'), // Israeli New Shekel
INR: /** @type {'inr'} */ ('inr'), // Indian Rupee
JPY: /** @type {'jpy'} */ ('jpy'), // Japanese Yen
KRW: /** @type {'krw'} */ ('krw'), // South Korean Won
KWD: /** @type {'kwd'} */ ('kwd'), // Kuwaiti Dinar
LKR: /** @type {'lkr'} */ ('lkr'), // Sri Lankan Rupee
MMK: /** @type {'mmk'} */ ('mmk'), // Burmese Kyat
MXN: /** @type {'mxn'} */ ('mxn'), // Mexican Peso
MYR: /** @type {'myr'} */ ('myr'), // Malaysian Ringgit
NGN: /** @type {'ngn'} */ ('ngn'), // Nigerian Naira
NOK: /** @type {'nok'} */ ('nok'), // Norwegian Krone
NZD: /** @type {'nzd'} */ ('nzd'), // New Zealand Dollar
PHP: /** @type {'php'} */ ('php'), // Philippine Peso
PKR: /** @type {'pkr'} */ ('pkr'), // Pakistani Rupee
PLN: /** @type {'pln'} */ ('pln'), // Poland Złoty
RUB: /** @type {'rub'} */ ('rub'), // Russian Ruble
SAR: /** @type {'sar'} */ ('sar'), // Saudi Riyal
SEK: /** @type {'sek'} */ ('sek'), // Swedish Krona
SGD: /** @type {'sgd'} */ ('sgd'), // Singapore Dollar
THB: /** @type {'thb'} */ ('thb'), // Thai Baht
TRY: /** @type {'try'} */ ('try'), // Turkish Lira
TWD: /** @type {'twd'} */ ('twd'), // New Taiwan Dollar
UAH: /** @type {'uah'} */ ('uah'), // Ukrainian Hryvnia
USD: /** @type {'usd'} */ ('usd'), // United States Dollar
// VEF: /** @type {'vef'} */ ('vef'), // Retired Venezuelan Bolívar Fuerte replaced by VES. Rates are off.
VND: /** @type {'vnd'} */ ('vnd'), // Vietnamese Đồng
ZAR: /** @type {'zar'} */ ('zar'), // South African Rand
});
/**
* @enum {'aed' | 'ars' | 'aud' | 'bdt' | 'bhd' | 'bmd' | 'brl' | 'cad' | 'chf' | 'clp' | 'cny' | 'czk' | 'dkk' | 'eur'
* | 'gbp' | 'gel' | 'hkd' | 'huf' | 'idr' | 'ils' | 'inr' | 'jpy' | 'krw' | 'kwd' | 'lkr' | 'mmk' | 'mxn' | 'myr'
* | 'ngn' | 'nok' | 'nzd' | 'php' | 'pkr' | 'pln' | 'rub' | 'sar' | 'sek' | 'sgd' | 'thb' | 'try' | 'twd' | 'uah'
* | 'usd' | 'vnd' | 'zar'}
* Fiat currencies supported by the coingecko api, see https://api.coingecko.com/api/v3/simple/supported_vs_currencies.
* More vs_currencies are supported, but those also include crypto currencies and ounces of gold amongst others which
* are not fiat currencies. See FiatApi in @nimiq/utils for how this list was assembled.
*/
FiatApi.SupportedFiatCurrencyCoinGecko = Object.freeze({
AED: /** @type {'aed'} */ ('aed'), // Arab Emirates Dirham
ARS: /** @type {'ars'} */ ('ars'), // Argentine Peso
AUD: /** @type {'aud'} */ ('aud'), // Australian Dollar
BDT: /** @type {'bdt'} */ ('bdt'), // Bangladeshi Taka
BHD: /** @type {'bhd'} */ ('bhd'), // Bahraini Dinar
BMD: /** @type {'bmd'} */ ('bmd'), // Bermudan Dollar
BRL: /** @type {'brl'} */ ('brl'), // Brazilian Real
CAD: /** @type {'cad'} */ ('cad'), // Canadian Dollar
CHF: /** @type {'chf'} */ ('chf'), // Swiss Franc
CLP: /** @type {'clp'} */ ('clp'), // Chilean Peso
CNY: /** @type {'cny'} */ ('cny'), // Chinese Yuan
CZK: /** @type {'czk'} */ ('czk'), // Czech Koruna
DKK: /** @type {'dkk'} */ ('dkk'), // Danish Krone
EUR: /** @type {'eur'} */ ('eur'), // Euro
GBP: /** @type {'gbp'} */ ('gbp'), // British Pound
GEL: /** @type {'gel'} */ ('gel'), // Georgian Lari
HKD: /** @type {'hkd'} */ ('hkd'), // Hong Kong Dollar
HUF: /** @type {'huf'} */ ('huf'), // Hungarian Forint
IDR: /** @type {'idr'} */ ('idr'), // Indonesian Rupiah
ILS: /** @type {'ils'} */ ('ils'), // Israeli New Shekel
INR: /** @type {'inr'} */ ('inr'), // Indian Rupee
JPY: /** @type {'jpy'} */ ('jpy'), // Japanese Yen
KRW: /** @type {'krw'} */ ('krw'), // South Korean Won
KWD: /** @type {'kwd'} */ ('kwd'), // Kuwaiti Dinar
LKR: /** @type {'lkr'} */ ('lkr'), // Sri Lankan Rupee
MMK: /** @type {'mmk'} */ ('mmk'), // Burmese Kyat
MXN: /** @type {'mxn'} */ ('mxn'), // Mexican Peso
MYR: /** @type {'myr'} */ ('myr'), // Malaysian Ringgit
NGN: /** @type {'ngn'} */ ('ngn'), // Nigerian Naira
NOK: /** @type {'nok'} */ ('nok'), // Norwegian Krone
NZD: /** @type {'nzd'} */ ('nzd'), // New Zealand Dollar
PHP: /** @type {'php'} */ ('php'), // Philippine Peso
PKR: /** @type {'pkr'} */ ('pkr'), // Pakistani Rupee
PLN: /** @type {'pln'} */ ('pln'), // Poland Złoty
RUB: /** @type {'rub'} */ ('rub'), // Russian Ruble
SAR: /** @type {'sar'} */ ('sar'), // Saudi Riyal
SEK: /** @type {'sek'} */ ('sek'), // Swedish Krona
SGD: /** @type {'sgd'} */ ('sgd'), // Singapore Dollar
THB: /** @type {'thb'} */ ('thb'), // Thai Baht
TRY: /** @type {'try'} */ ('try'), // Turkish Lira
TWD: /** @type {'twd'} */ ('twd'), // New Taiwan Dollar
UAH: /** @type {'uah'} */ ('uah'), // Ukrainian Hryvnia
USD: /** @type {'usd'} */ ('usd'), // United States Dollar
// VEF: /** @type {'vef'} */ ('vef'), // Retired Venezuelan Bolívar Fuerte replaced by VES. Rates are off.
VND: /** @type {'vnd'} */ ('vnd'), // Vietnamese Đồng
ZAR: /** @type {'zar'} */ ('zar'), // South African Rand
});

switch (FiatApi.Provider) { // eslint-disable-line default-case, -- no default to let ts warn us on missing providers
case FiatApi.SupportedProvider.CoinGecko: {
FiatApi.SupportedCryptoCurrency = FiatApi.SupportedCryptoCurrencyCoinGecko;
FiatApi.SupportedFiatCurrency = FiatApi.SupportedFiatCurrencyCoinGecko;

/**
* Coingecko api url. Note that the origin must be whitelisted in the csp.
Expand All @@ -140,7 +142,17 @@ FiatApi.Provider = FiatApi.SupportedProvider.CoinGecko;
[FiatApi.SupportedCryptoCurrency.BTC]: 'bitcoin',
});

/* eslint-enable indent */
// break;
// }
// }
break;
}
}

/**
* @typedef {FiatApi.Provider extends 'CoinGecko'
* ? FiatApi.SupportedCryptoCurrencyCoinGecko
* : never
* } FiatApi.SupportedCryptoCurrency
* @typedef {FiatApi.Provider extends 'CoinGecko'
* ? FiatApi.SupportedFiatCurrencyCoinGecko
* : never
* } FiatApi.SupportedFiatCurrency
*/

0 comments on commit 5f0f702

Please sign in to comment.