Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(realtime): add country codes to supported currencies list #67

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions realtime/src/config/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import mergeWith from "lodash.mergewith"
import * as cron from "node-cron"
import Ajv from "ajv"

import countryToCurrency from "@domain/country-to-currency"

import { baseLogger } from "@services/logger"

import { ConfigError } from "./error"
Expand Down Expand Up @@ -55,6 +57,15 @@ export const getFractionDigits = ({

export const supportedCurrencies: FiatCurrency[] = yamlConfig.quotes.map((q) => {
const code = q.code.toUpperCase()

const countryCodes: CountryCode[] = []
let key: CountryCode
for (key in countryToCurrency) {
if (countryToCurrency[key] === code) {
countryCodes.push(key)
}
}

return {
code,
symbol: q.symbol,
Expand All @@ -64,6 +75,7 @@ export const supportedCurrencies: FiatCurrency[] = yamlConfig.quotes.map((q) =>
currency: code,
fractionDigits: q.fractionDigits,
}),
countryCodes,
}
})
export const defaultBaseCurrency: CurrencyCode = yamlConfig.base
Expand Down
255 changes: 255 additions & 0 deletions realtime/src/domain/country-to-currency/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// This is a generated file taken from:
// https://github.com/thiagodp/country-to-currency/blob/v1.1.5/index.ts

export default {
AD: "EUR",
AE: "AED",
AF: "AFN",
AG: "XCD",
AI: "XCD",
AL: "ALL",
AM: "AMD",
AN: "ANG",
AO: "AOA",
AQ: "USD",
AR: "ARS",
AS: "USD",
AT: "EUR",
AU: "AUD",
AW: "AWG",
AX: "EUR",
AZ: "AZN",
BA: "BAM",
BB: "BBD",
BD: "BDT",
BE: "EUR",
BF: "XOF",
BG: "BGN",
BH: "BHD",
BI: "BIF",
BJ: "XOF",
BL: "EUR",
BM: "BMD",
BN: "BND",
BO: "BOB",
BQ: "USD",
BR: "BRL",
BS: "BSD",
BT: "BTN",
BV: "NOK",
BW: "BWP",
BY: "BYN",
BZ: "BZD",
CA: "CAD",
CC: "AUD",
CD: "CDF",
CF: "XAF",
CG: "XAF",
CH: "CHF",
CI: "XOF",
CK: "NZD",
CL: "CLP",
CM: "XAF",
CN: "CNY",
CO: "COP",
CR: "CRC",
CU: "CUP",
CV: "CVE",
CW: "ANG",
CX: "AUD",
CY: "EUR",
CZ: "CZK",
DE: "EUR",
DJ: "DJF",
DK: "DKK",
DM: "XCD",
DO: "DOP",
DZ: "DZD",
EC: "USD",
EE: "EUR",
EG: "EGP",
EH: "MAD",
ER: "ERN",
ES: "EUR",
ET: "ETB",
FI: "EUR",
FJ: "FJD",
FK: "FKP",
FM: "USD",
FO: "DKK",
FR: "EUR",
GA: "XAF",
GB: "GBP",
GD: "XCD",
GE: "GEL",
GF: "EUR",
GG: "GBP",
GH: "GHS",
GI: "GIP",
GL: "DKK",
GM: "GMD",
GN: "GNF",
GP: "EUR",
GQ: "XAF",
GR: "EUR",
GS: "FKP",
GT: "GTQ",
GU: "USD",
GW: "XOF",
GY: "GYD",
HK: "HKD",
HM: "AUD",
HN: "HNL",
HR: "EUR",
HT: "HTG",
HU: "HUF",
ID: "IDR",
IE: "EUR",
IL: "ILS",
IM: "GBP",
IN: "INR",
IO: "USD",
IQ: "IQD",
IR: "IRR",
IS: "ISK",
IT: "EUR",
JE: "GBP",
JM: "JMD",
JO: "JOD",
JP: "JPY",
KE: "KES",
KG: "KGS",
KH: "KHR",
KI: "AUD",
KM: "KMF",
KN: "XCD",
KP: "KPW",
KR: "KRW",
KW: "KWD",
KY: "KYD",
KZ: "KZT",
LA: "LAK",
LB: "LBP",
LC: "XCD",
LI: "CHF",
LK: "LKR",
LR: "LRD",
LS: "LSL",
LT: "EUR",
LU: "EUR",
LV: "EUR",
LY: "LYD",
MA: "MAD",
MC: "EUR",
MD: "MDL",
ME: "EUR",
MF: "EUR",
MG: "MGA",
MH: "USD",
MK: "MKD",
ML: "XOF",
MM: "MMK",
MN: "MNT",
MO: "MOP",
MP: "USD",
MQ: "EUR",
MR: "MRU",
MS: "XCD",
MT: "EUR",
MU: "MUR",
MV: "MVR",
MW: "MWK",
MX: "MXN",
MY: "MYR",
MZ: "MZN",
NA: "NAD",
NC: "XPF",
NE: "XOF",
NF: "AUD",
NG: "NGN",
NI: "NIO",
NL: "EUR",
NO: "NOK",
NP: "NPR",
NR: "AUD",
NU: "NZD",
NZ: "NZD",
OM: "OMR",
PA: "PAB",
PE: "PEN",
PF: "XPF",
PG: "PGK",
PH: "PHP",
PK: "PKR",
PL: "PLN",
PM: "EUR",
PN: "NZD",
PR: "USD",
PS: "ILS",
PT: "EUR",
PW: "USD",
PY: "PYG",
QA: "QAR",
RE: "EUR",
RO: "RON",
RS: "RSD",
RU: "RUB",
RW: "RWF",
SA: "SAR",
SB: "SBD",
SC: "SCR",
SD: "SDG",
SE: "SEK",
SG: "SGD",
SH: "SHP",
SI: "EUR",
SJ: "NOK",
SK: "EUR",
SL: "SLE",
SM: "EUR",
SN: "XOF",
SO: "SOS",
SR: "SRD",
SS: "SSP",
ST: "STN",
SV: "USD",
SX: "ANG",
SY: "SYP",
SZ: "SZL",
TC: "USD",
TD: "XAF",
TF: "EUR",
TG: "XOF",
TH: "THB",
TJ: "TJS",
TK: "NZD",
TL: "USD",
TM: "TMT",
TN: "TND",
TO: "TOP",
TR: "TRY",
TT: "TTD",
TV: "AUD",
TW: "TWD",
TZ: "TZS",
UA: "UAH",
UG: "UGX",
UM: "USD",
US: "USD",
UY: "UYU",
UZ: "UZS",
VA: "EUR",
VC: "XCD",
VE: "VES",
VG: "USD",
VI: "USD",
VN: "VND",
VU: "VUV",
WF: "XPF",
WS: "WST",
YE: "YER",
YT: "EUR",
ZA: "ZAR",
ZM: "ZMW",
ZW: "ZWL",
} as const
1 change: 1 addition & 0 deletions realtime/src/domain/country-to-currency/index.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type CountryCode = keyof typeof import("./index").default
1 change: 1 addition & 0 deletions realtime/src/domain/primitives/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type FiatCurrency = {
readonly name: string
readonly flag: string
readonly fractionDigits: number
readonly countryCodes: CountryCode[]
}
1 change: 1 addition & 0 deletions realtime/src/servers/protos/price.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ message Currency {
string name = 3;
string flag = 4;
int32 fractionDigits = 5;
repeated string countryCodes = 6;
}
78 changes: 77 additions & 1 deletion realtime/test/unit/config/yaml.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { coerceToStringArray, ConfigError, getFractionDigits } from "@config"
import {
coerceToStringArray,
ConfigError,
getFractionDigits,
supportedCurrencies,
} from "@config"

describe("coerceToStringArray", () => {
it("returns an empty array if the input is falsy", () => {
Expand Down Expand Up @@ -55,3 +60,74 @@ describe("getFractionDigits", () => {
expect(() => getFractionDigits({ currency })).toThrowError(expectedErrorMessage)
})
})

describe("supportedCurrencies", () => {
test("returns correct country codes for a valid currency", () => {
const expectedUsdCountries = [
"AQ",
"AS",
"BQ",
"EC",
"FM",
"GU",
"IO",
"MH",
"MP",
"PR",
"PW",
"SV",
"TC",
"TL",
"UM",
"US",
"VG",
"VI",
]

const expectedEurCountries = [
"AD",
"AT",
"AX",
"BE",
"BL",
"CY",
"DE",
"EE",
"ES",
"FI",
"FR",
"GF",
"GP",
"GR",
"HR",
"IE",
"IT",
"LT",
"LU",
"LV",
"MC",
"ME",
"MF",
"MQ",
"MT",
"NL",
"PM",
"PT",
"RE",
"SI",
"SK",
"SM",
"TF",
"VA",
"YT",
]

const usdCurrency = supportedCurrencies.find((c) => c.code === "USD")
if (usdCurrency === undefined) throw new Error()
expect(usdCurrency.countryCodes).toEqual(expectedUsdCountries)

const eurCurrency = supportedCurrencies.find((c) => c.code === "EUR")
if (eurCurrency === undefined) throw new Error()
expect(eurCurrency.countryCodes).toEqual(expectedEurCountries)
})
})
3 changes: 2 additions & 1 deletion typos.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
files.extend-exclude = [
"dev",
"test/postman/*.json"
"test/postman/*.json",
"realtime/src/domain/country-to-currency/index.ts"
]

[default.extend-words]
Expand Down
Loading