import { isEmpty } from 'ramda' ; import { useDispatch } from 'react-redux' ; import { useSettings } from '@services/Store' ; import { getRates , getTrackedAssets , trackAsset , useSelector } from '@store' ; import { Asset , ExtendedAsset , ReserveAsset , TUuid } from '@types' ; export interface IRatesContext { getAssetRate ( asset ) : number | undefined ; getAssetRateInCurrency ( asset , currency ) : number | undefined ; getPoolAssetReserveRate ( defiPoolTokenUUID , assets ) : ReserveAsset [ ] ; } const DEFAULT_FIAT_RATE = 0 ; function useRates ( ) { const rates = useSelector ( getRates ) ; const trackedAssets = useSelector ( getTrackedAssets ) ; const { settings } = useSettings ( ) ; const dispatch = useDispatch ( ) ; const getAssetRate = ( asset ) => { const uuid = asset . uuid ; if ( ! isEmpty ( rates ) && ! rates [ uuid ] && ! trackedAssets [ uuid ] ) { dispatch ( trackAsset ( asset ) ) ; return DEFAULT_FIAT_RATE ; } return rates [ uuid ] ? rates [ uuid ] [ ( settings . fiatCurrency as string ) . toLowerCase ( ) ] : DEFAULT_FIAT_RATE ; } ; const getAssetChange = ( asset ) => { const uuid = asset . uuid ; if ( ! isEmpty ( rates ) && ! rates [ uuid ] && ! trackedAssets [ uuid ] ) { dispatch ( trackAsset ( asset ) ) ; return DEFAULT_FIAT_RATE ; } return rates [ uuid ] ? rates [ uuid ] [ `${ ( settings . fiatCurrency as string ) . toLowerCase ( ) }_24h_change` ] : DEFAULT_FIAT_RATE ; } ; const getAssetRateInCurrency = ( asset , currency ) => { const uuid = asset . uuid ; if ( ! isEmpty ( rates ) && ! rates [ uuid ] && ! trackedAssets [ uuid ] ) { dispatch ( trackAsset ( asset ) ) ; return DEFAULT_FIAT_RATE ; } return rates [ uuid ] ? rates [ uuid ] [ currency . toLowerCase ( ) ] : DEFAULT_FIAT_RATE ; } ; const getPoolAssetReserveRate = ( _uuid , _assets ) => [ ] as ReserveAsset [ ] ; return { getAssetRate , getAssetRateInCurrency , getPoolAssetReserveRate , getAssetChange } ; } export default useRates ;