-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5295 from leather-wallet/release/rate-limiter
Release/rate limiter
- Loading branch information
Showing
93 changed files
with
740 additions
and
636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { MarketData } from '@shared/models/market.model'; | ||
import type { Money } from '@shared/models/money.model'; | ||
|
||
import { baseCurrencyAmountInQuote } from './money/calculate-money'; | ||
import { i18nFormatCurrency } from './money/format-money'; | ||
import { isMoneyGreaterThanZero } from './money/money.utils'; | ||
|
||
export function sortAssetsByName<T extends { name: string }[]>(assets: T) { | ||
return assets | ||
.sort((a, b) => { | ||
if (a.name < b.name) return -1; | ||
if (a.name > b.name) return 1; | ||
return 0; | ||
}) | ||
.sort((a, b) => { | ||
if (a.name === 'STX') return -1; | ||
if (b.name !== 'STX') return 1; | ||
return 0; | ||
}) | ||
.sort((a, b) => { | ||
if (a.name === 'BTC') return -1; | ||
if (b.name !== 'BTC') return 1; | ||
return 0; | ||
}); | ||
} | ||
|
||
export function migratePositiveAssetBalancesToTop<T extends { balance: Money }[]>(assets: T) { | ||
const assetsWithPositiveBalance = assets.filter(asset => asset.balance.amount.isGreaterThan(0)); | ||
const assetsWithZeroBalance = assets.filter(asset => asset.balance.amount.isEqualTo(0)); | ||
return [...assetsWithPositiveBalance, ...assetsWithZeroBalance] as T; | ||
} | ||
|
||
export function convertAssetBalanceToFiat< | ||
T extends { balance: Money | null; marketData: MarketData | null }, | ||
>(asset: T) { | ||
if (!asset.marketData || !asset.balance || !isMoneyGreaterThanZero(asset.marketData.price)) | ||
return ''; | ||
return i18nFormatCurrency(baseCurrencyAmountInQuote(asset.balance, asset.marketData)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,17 @@ | ||
import { useCallback, useMemo } from 'react'; | ||
import { useCallback } from 'react'; | ||
|
||
import { CryptoCurrencies } from '@shared/models/currencies.model'; | ||
import { createMarketData, createMarketPair } from '@shared/models/market.model'; | ||
import type { Money } from '@shared/models/money.model'; | ||
import { type Money } from '@shared/models/money.model'; | ||
|
||
import { useCryptoCurrencyMarketData } from '@app/query/common/market-data/market-data.hooks'; | ||
import { useCryptoCurrencyMarketDataMeanAverage } from '@app/query/common/market-data/market-data.hooks'; | ||
|
||
import { baseCurrencyAmountInQuote } from '../money/calculate-money'; | ||
|
||
export function useConvertCryptoCurrencyToFiatAmount(currency: CryptoCurrencies) { | ||
const cryptoCurrencyMarketData = useCryptoCurrencyMarketData(currency); | ||
const cryptoCurrencyMarketData = useCryptoCurrencyMarketDataMeanAverage(currency); | ||
|
||
return useCallback( | ||
(value: Money) => baseCurrencyAmountInQuote(value, cryptoCurrencyMarketData), | ||
[cryptoCurrencyMarketData] | ||
); | ||
} | ||
|
||
export function useConvertAlexSdkCurrencyToFiatAmount(currency: CryptoCurrencies, price: Money) { | ||
const alexCurrencyMarketData = useMemo( | ||
() => createMarketData(createMarketPair(currency, 'USD'), price), | ||
[currency, price] | ||
); | ||
|
||
return useCallback( | ||
(value: Money) => baseCurrencyAmountInQuote(value, alexCurrencyMarketData), | ||
[alexCurrencyMarketData] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { BreakpointToken, token } from 'leather-styles/tokens'; | ||
|
||
function useMediaQuery(query: string) { | ||
const [matches, setMatches] = useState(false); | ||
|
||
useEffect(() => { | ||
const media = window.matchMedia(query); | ||
if (media.matches !== matches) { | ||
setMatches(media.matches); | ||
} | ||
const listener = () => setMatches(media.matches); | ||
window.addEventListener('resize', listener); | ||
return () => window.removeEventListener('resize', listener); | ||
}, [matches, query]); | ||
|
||
return matches; | ||
} | ||
|
||
export function useViewportMinWidth(viewport: BreakpointToken) { | ||
return useMediaQuery(`(min-width: ${token(`breakpoints.${viewport}`)})`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/app/common/money/is-money.ts → src/app/common/money/money.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Money } from '@shared/models/money.model'; | ||
import { isObject } from '@shared/utils'; | ||
|
||
export function isMoney(val: unknown): val is Money { | ||
if (!isObject(val)) return false; | ||
return 'amount' in val && 'symbol' in val && 'decimals' in val; | ||
} | ||
|
||
export function isMoneyGreaterThanZero(money: Money) { | ||
if (!BigNumber.isBigNumber(money.amount)) return; | ||
return !(money.amount.isNaN() || money.amount.isZero()); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.