Skip to content

Commit

Permalink
refactor: MarketCurrency to FiatCurrency & CryptoCurrency (#2702)
Browse files Browse the repository at this point in the history
* refactor: MarketCurrency to FiatCurrency & CryptoCurrency

* chore: adds profile migration

* fix: mark suggestions
  • Loading branch information
jeeanribeiro authored Jul 3, 2024
1 parent 76017cd commit ca31adb
Show file tree
Hide file tree
Showing 23 changed files with 126 additions and 85 deletions.
4 changes: 2 additions & 2 deletions packages/desktop/lib/electron/managers/transak.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ITransakManager, ITransakWindowData } from '@core/app'
import path from 'path'
import { TRANSAK_WIDGET_URL } from '@auxiliary/transak/constants'
import { buildUrl } from '@core/utils/url'
import { MarketCurrency } from '@core/market/enums/market-currency.enum'
import { FiatCurrency } from '@core/market/enums/fiat-currency.enum'
import fs from 'fs'
import { IError } from '@core/error'
import { QueryParameters } from '@core/utils'
Expand Down Expand Up @@ -226,7 +226,7 @@ export default class TransakManager implements ITransakManager {
throw new Error('Undefined Transak API key')
}

if (!Object.values(MarketCurrency).includes(currency as MarketCurrency)) {
if (!Object.values(FiatCurrency).includes(currency as FiatCurrency)) {
throw new Error('Invalid Transak currency')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
TransakWindowPlaceholder,
} from '../components'
import { isDashboardSideBarExpanded } from '@core/ui'
import { MarketCoinId, MarketCurrency } from '@core/market/enums'
import { FiatCurrency, MarketCoinId } from '@core/market/enums'
import { MarketCurrency } from '@core/market/types'
import { marketCoinPrices } from '@core/market/stores'
import { DrawerState } from '@desktop/auxiliary/drawer/types'
import { drawerState } from '@desktop/auxiliary/drawer/stores'
Expand Down Expand Up @@ -67,14 +68,14 @@
function getDefaultFiatAmount(currency: MarketCurrency): number {
const DEFAULT_FIAT_AMOUNT = 1000
switch (currency) {
case MarketCurrency.Usd:
case MarketCurrency.Eur:
case MarketCurrency.Gbp:
case FiatCurrency.USD:
case FiatCurrency.EUR:
case FiatCurrency.GBP:
return DEFAULT_FIAT_AMOUNT
default: {
const conversionRate =
$marketCoinPrices[MarketCoinId.Iota]?.[currency] /
$marketCoinPrices[MarketCoinId.Iota]?.[MarketCurrency.Usd]
$marketCoinPrices[MarketCoinId.Iota]?.[FiatCurrency.USD]
const fiatAmount = DEFAULT_FIAT_AMOUNT * conversionRate
const roundedAmount = customRound(fiatAmount)
return roundedAmount
Expand All @@ -97,7 +98,7 @@
currency: $activeProfile?.settings.marketCurrency,
address: $selectedAccount.depositAddress,
service: 'BUY',
amount: getDefaultFiatAmount($activeProfile?.settings.marketCurrency ?? MarketCurrency.Usd),
amount: getDefaultFiatAmount($activeProfile?.settings.marketCurrency ?? FiatCurrency.USD),
})
isTransakOpen = true
await updateTransakBounds()
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/views/settings/views/profile/Currency.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script lang="ts">
import { IOption, SelectInput } from '@bloomwalletio/ui'
import { localize } from '@core/i18n'
import { MarketCurrency } from '@core/market'
import { CryptoCurrency, FiatCurrency, MarketCurrency } from '@core/market'
import { activeProfile, updateActiveProfileSettings } from '@core/profile/stores'
import SettingsSection from '../SettingsSection.svelte'
const options: IOption[] = Object.values(MarketCurrency)
const options: IOption[] = [...Object.values(FiatCurrency), ...Object.values(CryptoCurrency)]
.map((currency) => ({ value: currency, label: currency.toUpperCase() }))
.sort()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IPersistedAccountData } from '@core/account'
import { APP_STAGE } from '@core/app'
import { MarketCurrency } from '@core/market'
import { FiatCurrency } from '@core/market'
import {
DEFAULT_EVM_NETWORK_CONFIGURATIONS_FOR_STARDUST_NETWORK,
DEFAULT_EXPLORER_URLS,
Expand Down Expand Up @@ -111,7 +111,7 @@ function buildEvmNetworksFromThirdPartyPersistedNetwork(

function buildSettingsFromThirdPartyPersistedSettings(settings: IThirdPartyPersistedSettings): IProfileSettings {
return {
marketCurrency: settings.marketCurrency ?? MarketCurrency.Usd,
marketCurrency: settings.marketCurrency ?? FiatCurrency.USD,
lockScreenTimeoutInMinutes: settings.lockScreenTimeoutInMinutes ?? DEFAULT_LOCK_SCREEN_TIMEOUT_IN_MINUTES,
strongholdPasswordTimeoutInMinutes:
settings.strongholdPasswordTimeoutInMinutes ?? DEFAULT_STRONGHOLD_PASSWORD_TIMEOUT_IN_MINUTES,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MarketCurrency } from '@core/market/enums/market-currency.enum'
import { MarketCurrency } from '@core/market/types'

export interface ITransakWindowData {
currency: MarketCurrency
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/lib/core/i18n/utils/formatCurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export function formatCurrency(
convertedValue = Number(value.toFixed(2))
}

const isCurrencyNotSupported = currency?.length > 3
if (isCurrencyNotSupported) {
const formattedValue = convertedValue.toLocaleString(appLanguage, {
minimumFractionDigits: 2,
maximumFractionDigits: 20,
useGrouping: grouped,
})
return `${formattedValue} ${currency.toUpperCase()}`
}

const formatter = Intl.NumberFormat(appLanguage, {
style: 'currency',
currency: currency ?? 'USD',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export function getDecimalSeparator(currency: string | undefined = undefined): s
currency = get(activeProfile)?.settings?.marketCurrency
}

const isCurrencySupportedByIntl = currency?.length <= 3
return (
Intl.NumberFormat(appLanguage, {
style: 'currency',
currency: currency ?? 'USD',
currency: isCurrencySupportedByIntl ? currency : 'USD',
})
.formatToParts(1.1)
.find((part) => part.type === 'decimal')?.value ?? '.'
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/lib/core/i18n/utils/getGroupSeparator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export function getGroupSeparator(currency: string | undefined = undefined): str
currency = get(activeProfile)?.settings?.marketCurrency
}

const isCurrencySupportedByIntl = currency?.length <= 3
return (
Intl.NumberFormat(appLanguage, {
style: 'currency',
currency: currency ?? 'USD',
currency: isCurrencySupportedByIntl ? currency : 'USD',
})
.formatToParts(1111111)
.find((part) => part.type === 'group')?.value ?? ','
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get } from 'svelte/store'
import { CoinGeckoApi } from '../apis'
import { MarketCoinId, MarketCurrency } from '../enums'
import { CryptoCurrency, FiatCurrency, MarketCoinId } from '../enums'
import { CoinGeckoCoin } from '../interfaces'
import { coinGeckoTokensMetadata, updateMarketCoinPrices } from '../stores'

Expand All @@ -12,7 +12,7 @@ export async function getAndUpdateMarketPrices(): Promise<void> {

const marketPricesResponse = await CoinGeckoApi.getSimplePrices(
[MarketCoinId.Iota, MarketCoinId.Shimmer, MarketCoinId.Ethereum, ...storedTokenIds],
Object.values(MarketCurrency)
[...Object.values(FiatCurrency), ...Object.values(CryptoCurrency)]
)
updateMarketCoinPrices(marketPricesResponse)
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/lib/core/market/apis/coingecko.api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DEFAULT_APPLICATION_JSON_REQUEST_OPTIONS, QueryParameters, buildUrl } from '@core/utils'
import { MARKET_API_BASE_URL } from '../constants'
import { CoinGeckoApiEndpoint, CoinGeckoNetworkId, MarketCoinId, MarketCurrency } from '../enums'
import { MarketCoinPrices } from '../types'
import { CoinGeckoApiEndpoint, CoinGeckoNetworkId, MarketCoinId } from '../enums'
import { MarketCoinPrices, MarketCurrency } from '../types'
import { CoinGeckoCoin } from '../interfaces'
import { localize } from '@core/i18n'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MarketCurrency } from '../enums'
import { FiatCurrency } from '../enums'

export const DEFAULT_MARKET_CURRENCY = MarketCurrency.Usd
export const DEFAULT_MARKET_CURRENCY = FiatCurrency.USD
16 changes: 16 additions & 0 deletions packages/shared/src/lib/core/market/enums/crypto-currency.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Coingecko supported crypto currencies prices
export enum CryptoCurrency {
BTC = 'btc',
ETH = 'eth',
LTC = 'ltc',
BCH = 'bch',
BNB = 'bnb',
EOS = 'eos',
XRP = 'xrp',
XLM = 'xlm',
LINK = 'link',
DOT = 'dot',
YFI = 'yfi',
BITS = 'bits',
SATS = 'sats',
}
35 changes: 35 additions & 0 deletions packages/shared/src/lib/core/market/enums/fiat-currency.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Transak and Coingecko supported fiat currencies intersection
export enum FiatCurrency {
USD = 'usd',
EUR = 'eur',
GBP = 'gbp',
AED = 'aed',
ARS = 'ars',
AUD = 'aud',
BHD = 'bhd',
BMD = 'bmd',
BRL = 'brl',
CAD = 'cad',
CHF = 'chf',
CLP = 'clp',
CZK = 'czk',
DKK = 'dkk',
GEL = 'gel',
HKD = 'hkd',
HUF = 'huf',
IDR = 'idr',
ILS = 'ils',
INR = 'inr',
JPY = 'jpy',
KRW = 'krw',
KWD = 'kwd',
MXN = 'mxn',
MYR = 'myr',
NOK = 'nok',
NZD = 'nzd',
PHP = 'php',
PLN = 'pln',
SEK = 'sek',
SGD = 'sgd',
TWD = 'twd',
}
3 changes: 2 additions & 1 deletion packages/shared/src/lib/core/market/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './coingecko-api-endpoint.enum'
export * from './coingecko-network-id.enum'
export * from './crypto-currency.enum'
export * from './fiat-currency.enum'
export * from './market-coin-id.enum'
export * from './market-currency.enum'
60 changes: 0 additions & 60 deletions packages/shared/src/lib/core/market/enums/market-currency.enum.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/shared/src/lib/core/market/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './market-currency.type'
export * from './market-prices.type'
export * from './market-coin-prices.type'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CryptoCurrency, FiatCurrency } from '../enums'

export type MarketCurrency = CryptoCurrency | FiatCurrency
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { MarketCurrency } from '../enums'
import { MarketCurrency } from './market-currency.type'

export type MarketPrices = { [key in MarketCurrency]?: number }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AppStage } from '@core/app/enums'

export const PROFILE_VERSION: Record<AppStage, number> = {
[AppStage.ALPHA]: 22,
[AppStage.ALPHA]: 23,
[AppStage.BETA]: 1,
[AppStage.PROD]: 12,
[AppStage.PROD]: 13,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CryptoCurrency, FiatCurrency } from '@core/market'
import { IPersistedProfile } from '@core/profile/interfaces'

export function alphaProfileMigration22To23(existingProfile: unknown): Promise<void> {
const profile = existingProfile as IPersistedProfile
const marketCurrencies = [...Object.values(FiatCurrency), ...Object.values(CryptoCurrency)]

if (!marketCurrencies.includes(profile?.settings?.marketCurrency)) {
profile.settings.marketCurrency = FiatCurrency.USD
}

return Promise.resolve()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { alphaProfileMigration19To20 } from './alpha-profile-migration-19-to-20'
import { alphaProfileMigration2To3 } from './alpha-profile-migration-2-to-3'
import { alphaProfileMigration20To21 } from './alpha-profile-migration-20-to-21'
import { alphaProfileMigration21To22 } from './alpha-profile-migration-21-to-22'
import { alphaProfileMigration22To23 } from './alpha-profile-migration-22-to-23'
import { alphaProfileMigration3To4 } from './alpha-profile-migration-3-to-4'
import { alphaProfileMigration4To5 } from './alpha-profile-migration-4-to-5'
import { alphaProfileMigration5To6 } from './alpha-profile-migration-5-to-6'
Expand Down Expand Up @@ -49,4 +50,6 @@ export const ALPHA_PROFILE_MIGRATION_MAP: ProfileMigrationMap = {
// ^^^ release 1.0.4 ^^^
21: alphaProfileMigration21To22,
// ^^^ release 1.0.8 ^^^
22: alphaProfileMigration22To23,
// ^^^ release 1.1.2 ^^^
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CryptoCurrency, FiatCurrency } from '@core/market'
import { IPersistedProfile } from '@core/profile/interfaces'

export function prodProfileMigration12To13(existingProfile: unknown): Promise<void> {
const profile = existingProfile as IPersistedProfile
const marketCurrencies = [...Object.values(FiatCurrency), ...Object.values(CryptoCurrency)]

if (!marketCurrencies.includes(profile?.settings?.marketCurrency)) {
profile.settings.marketCurrency = FiatCurrency.USD
}

return Promise.resolve()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { prodProfileMigration9To10 } from './prod-profile-migration-9-to-10'
import { prodProfileMigration8To9 } from './prod-profile-migration-8-to-9'
import { prodProfileMigration10To11 } from './prod-profile-migration-10-to-11'
import { prodProfileMigration11To12 } from './prod-profile-migration-11-to-12'
import { prodProfileMigration12To13 } from './prod-profile-migration-12-to-13'

export const PROD_PROFILE_MIGRATION_MAP: ProfileMigrationMap = {
0: prodProfileMigration0To1,
Expand All @@ -30,4 +31,6 @@ export const PROD_PROFILE_MIGRATION_MAP: ProfileMigrationMap = {
// ^^^ release 1.0.4 ^^^
11: prodProfileMigration11To12,
// ^^^ release 1.0.8 ^^^
12: prodProfileMigration12To13,
// ^^^ release 1.1.2 ^^^
}

0 comments on commit ca31adb

Please sign in to comment.