@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
36 lines • 1.46 kB
JavaScript
import { getAccountCurrency } from "@ledgerhq/ledger-wallet-framework/account/helpers";
import { fetchRates } from "../../api/v5/fetchRates";
import { useAPI } from "../../../../hooks/useAPI";
import { useFilteredProviders } from "./useFilteredProviders";
export function useFetchRates({ fromCurrencyAccount, toCurrency, fromCurrencyAmount, onSuccess, isEnabled = true, }) {
const currencyFrom = fromCurrencyAccount ? getAccountCurrency(fromCurrencyAccount).id : undefined;
const unitFrom = fromCurrencyAccount
? getAccountCurrency(fromCurrencyAccount).units[0]
: undefined;
const unitTo = toCurrency?.units[0];
const formattedCurrencyAmount = (unitFrom && `${fromCurrencyAmount.shiftedBy(-unitFrom.magnitude)}`) ?? "0";
const { providers, loading, error } = useFilteredProviders();
const toCurrencyId = toCurrency?.id;
return useAPI({
queryFn: fetchRates,
queryProps: {
providers,
unitTo: unitTo,
unitFrom: unitFrom,
currencyFrom,
toCurrencyId,
fromCurrencyAmount: formattedCurrencyAmount,
},
staleTimeout: 20000,
enabled: !!toCurrencyId &&
!!currencyFrom &&
fromCurrencyAmount.gt(0) &&
!!unitFrom &&
!!unitTo &&
!loading &&
isEnabled &&
!error,
onSuccess,
});
}
//# sourceMappingURL=useFetchRates.js.map