Skip to content

Commit

Permalink
fix(live-12785): 429 cloudflare swap rates limit (#6987)
Browse files Browse the repository at this point in the history
* fix: currency change

* chore: changeset
  • Loading branch information
liviuciulinaru committed Jun 3, 2024
1 parent 8f840f0 commit dc409f0
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .changeset/forty-oranges-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"ledger-live-desktop": patch
"@ledgerhq/live-common": patch
---

[swap] disabled the call to /rates endpoint in LL when Demo1 FF is ON
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const useSwapLiveAppHook = (props: UseSwapLiveAppHookProps) => {
getExchangeSDKParams,
getProviderRedirectURLSearch,
swapTransaction.swap.from,
swapTransaction.swap.from.amount,
swapError,
swapTransaction.bridgePending,
exchangeRatesState.status,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SwapDataType } from "@ledgerhq/live-common/exchange/swap/types";
import React from "react";
import styled from "styled-components";
import SectionRate from "./SectionRate";
import { SwapDataType } from "@ledgerhq/live-common/exchange/swap/types";

const Form = styled.section`
display: grid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from "react";
import styled from "styled-components";
import { useTranslation } from "react-i18next";
import styled from "styled-components";

import { Box } from "@ledgerhq/react-ui";
import { usePageState } from "@ledgerhq/live-common/exchange/swap/hooks/index";
import { SwapTransactionType } from "@ledgerhq/live-common/exchange/swap/types";
import { Box } from "@ledgerhq/react-ui";
import ButtonBase from "~/renderer/components/Button";
import SwapFormRates from "../FormRates";
import { SwapWebManifestIDs } from "../SwapWebView";
import LoadingState from "../Rates/LoadingState";
import SwapFormSummary from "../FormSummary";
import LoadingState from "../Rates/LoadingState";
import { SwapWebManifestIDs } from "../SwapWebView";

const Button = styled(ButtonBase)`
width: 100%;
Expand Down Expand Up @@ -49,7 +49,7 @@ export const SwapMigrationUI = (props: SwapMigrationUIProps) => {
) : null;

const nativeQuotesUI =
pageState === "loaded" ? (
pageState === "loaded" && !manifestID?.startsWith(SwapWebManifestIDs.Demo1) ? (
<SwapFormRates
swap={swapTransaction.swap}
provider={provider}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
usePageState,
useSwapTransaction,
} from "@ledgerhq/live-common/exchange/swap/hooks/index";
import { maybeTezosAccountUnrevealedAccount } from "@ledgerhq/live-common/exchange/swap/index";
import { OnNoRatesCallback } from "@ledgerhq/live-common/exchange/swap/types";
import { getProviderName } from "@ledgerhq/live-common/exchange/swap/utils/index";
import {
Expand Down Expand Up @@ -36,8 +37,11 @@ import ExchangeDrawer from "./ExchangeDrawer/index";
import SwapFormSelectors from "./FormSelectors";
import { SwapMigrationUI } from "./Migrations/SwapMigrationUI";
import EmptyState from "./Rates/EmptyState";
import SwapWebView, { SwapWebProps, useSwapLiveAppManifestID } from "./SwapWebView";
import { maybeTezosAccountUnrevealedAccount } from "@ledgerhq/live-common/exchange/swap/index";
import SwapWebView, {
SwapWebManifestIDs,
SwapWebProps,
useSwapLiveAppManifestID,
} from "./SwapWebView";

const DAPP_PROVIDERS = ["paraswap", "oneinch", "moonpay"];

Expand Down Expand Up @@ -81,11 +85,13 @@ const SwapForm = () => {
},
[swapDefaultTrack],
);
const swapLiveAppManifestID = useSwapLiveAppManifestID();

const swapTransaction = useSwapTransaction({
accounts,
setExchangeRate,
onNoRates,
isEnabled: !swapLiveAppManifestID?.startsWith(SwapWebManifestIDs.Demo1),
...(locationState as object),
});

Expand Down Expand Up @@ -165,6 +171,7 @@ const SwapForm = () => {
swapTransaction.swap.from.account?.id,
swapTransaction.swap.to.currency?.id,
swapTransaction.swap.to.account?.id,
swapTransaction.swap.from.amount,
exchangeRate?.providerType,
exchangeRate?.tradeMethod,
exchangeRate?.providerURL,
Expand Down Expand Up @@ -400,9 +407,12 @@ const SwapForm = () => {
swapTransaction.setFromAccount(account);
};

const setFromAmount = (amount: BigNumber) => {
swapTransaction.setFromAmount(amount);
};
const setFromAmount = useCallback(
(amount: BigNumber) => {
swapTransaction.setFromAmount(amount);
},
[swapTransaction],
);

const setToCurrency = (currency: TokenCurrency | CryptoCurrency | undefined) => {
swapTransaction.setToCurrency(currency);
Expand All @@ -417,7 +427,6 @@ const SwapForm = () => {
swapTransaction.reverseSwap();
};

const swapLiveAppManifestID = useSwapLiveAppManifestID();
const localManifest = useLocalLiveAppManifest(swapLiveAppManifestID || undefined);
const remoteManifest = useRemoteLiveAppManifest(swapLiveAppManifestID || undefined);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { getAccountCurrency, getFeesUnit } from "@ledgerhq/coin-framework/account/index";
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
import {
AmountRequired,
FeeNotLoaded,
FeeNotLoadedSwap,
NotEnoughGas,
NotEnoughGasSwap,
} from "@ledgerhq/errors";
import { Account } from "@ledgerhq/types-live";
import { useMemo } from "react";
import useBridgeTransaction, { Result } from "../../../bridge/useBridgeTransaction";
import { Transaction } from "../../../generated/types";
import {
SwapSelectorStateType,
ExchangeRate,
OnNoRatesCallback,
SwapSelectorStateType,
SwapTransactionType,
ExchangeRate,
} from "../types";
import useBridgeTransaction, { Result } from "../../../bridge/useBridgeTransaction";
import { useFromState } from "./useFromState";
import { useToState } from "./useToState";
import { useReverseAccounts } from "./useReverseAccounts";
import { Account } from "@ledgerhq/types-live";
import { useToState } from "./useToState";
import { useUpdateMaxAmount } from "./useUpdateMaxAmount";
import { Transaction } from "../../../generated/types";
import { getAccountCurrency, getFeesUnit } from "@ledgerhq/coin-framework/account/index";
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/index";
import { useProviderRates } from "./v5/useProviderRates";

export const selectorStateDefaultValues = {
Expand Down Expand Up @@ -98,6 +98,7 @@ type UseSwapTransactionProps = {
excludeFixedRates?: boolean;
refreshRate?: number;
allowRefresh?: boolean;
isEnabled?: boolean;
};

export const useSwapTransaction = ({
Expand All @@ -110,7 +111,8 @@ export const useSwapTransaction = ({
excludeFixedRates,
refreshRate,
allowRefresh,
}: UseSwapTransactionProps = {}): SwapTransactionType => {
isEnabled,
}: UseSwapTransactionProps): SwapTransactionType => {
const bridgeTransaction = useBridgeTransaction(() => ({
account: defaultAccount,
parentAccount: defaultParentAccount,
Expand Down Expand Up @@ -165,6 +167,7 @@ export const useSwapTransaction = ({
setExchangeRate,
countdown: refreshRate,
allowRefresh,
isEnabled,
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { AccountLike } from "@ledgerhq/types-live";
import { getAccountCurrency } from "@ledgerhq/coin-framework/account/helpers";
import { CryptoOrTokenCurrency } from "@ledgerhq/types-cryptoassets";
import { AccountLike } from "@ledgerhq/types-live";
import BigNumber from "bignumber.js";
import { fetchRates } from "../../api/v5/fetchRates";
import { useFeature } from "../../../../featureFlags";
import { useAPI } from "../../../../hooks/useAPI";
import { fetchRates } from "../../api/v5/fetchRates";
import { ExchangeRate } from "../../types";
import { useFeature } from "../../../../featureFlags";

type Props = {
fromCurrencyAccount: AccountLike | undefined;
toCurrency: CryptoOrTokenCurrency | undefined;
fromCurrencyAmount: BigNumber;
onSuccess?(data: ExchangeRate[]): void;
isEnabled?: boolean;
};

export function useFetchRates({
fromCurrencyAccount,
toCurrency,
fromCurrencyAmount,
onSuccess,
isEnabled = true,
}: Props) {
const currencyFrom = fromCurrencyAccount ? getAccountCurrency(fromCurrencyAccount).id : undefined;
const unitFrom = fromCurrencyAccount
Expand All @@ -45,7 +47,13 @@ export function useFetchRates({
fromCurrencyAmount: formattedCurrencyAmount,
},
staleTimeout: 20000,
enabled: !!toCurrencyId && !!currencyFrom && fromCurrencyAmount.gt(0) && !!unitFrom && !!unitTo,
enabled:
!!toCurrencyId &&
!!currencyFrom &&
fromCurrencyAmount.gt(0) &&
!!unitFrom &&
!!unitTo &&
isEnabled,
onSuccess,
});
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import BigNumber from "bignumber.js";
import { OnNoRatesCallback, RatesReducerState, SwapSelectorStateType } from "../../types";
import { useFetchRates } from "./useFetchRates";
import { SetExchangeRateCallback } from "../useSwapTransaction";
import { useFeature } from "../../../../featureFlags";
import { useCallback, useEffect } from "react";
import { useCountdown } from "usehooks-ts";
import { useFeature } from "../../../../featureFlags";
import { DEFAULT_SWAP_RATES_INTERVAL_MS } from "../../const/timeout";
import { OnNoRatesCallback, RatesReducerState, SwapSelectorStateType } from "../../types";
import { SetExchangeRateCallback } from "../useSwapTransaction";
import { useFetchRates } from "./useFetchRates";

type Props = {
fromState: SwapSelectorStateType;
Expand All @@ -14,6 +14,7 @@ type Props = {
setExchangeRate?: SetExchangeRateCallback | null | undefined;
countdown?: number;
allowRefresh?: boolean;
isEnabled?: boolean;
};

export type UseProviderRatesResponse = {
Expand All @@ -29,6 +30,7 @@ export function useProviderRates({
onNoRates,
setExchangeRate,
allowRefresh = true,
isEnabled = true,
...props
}: Props): UseProviderRatesResponse {
const [countdown, { startCountdown, resetCountdown, stopCountdown }] = useCountdown({
Expand Down Expand Up @@ -59,6 +61,7 @@ export function useProviderRates({
setExchangeRate?.(rates[0]);
}
},
isEnabled,
});

useEffect(() => {
Expand Down

0 comments on commit dc409f0

Please sign in to comment.