diff --git a/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/Body.tsx b/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/Body.tsx index 508971652f94..e35455d04325 100644 --- a/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/Body.tsx +++ b/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/Body.tsx @@ -6,7 +6,7 @@ import { ExchangeSwap } from "@ledgerhq/live-common/exchange/swap/types"; import { getUpdateAccountWithUpdaterParams } from "@ledgerhq/live-common/exchange/swap/getUpdateAccountWithUpdaterParams"; import { useBroadcast } from "@ledgerhq/live-common/hooks/useBroadcast"; import { Transaction } from "@ledgerhq/live-common/generated/types"; -import { Currency, TokenCurrency } from "@ledgerhq/types-cryptoassets"; +import { CryptoCurrency, Currency, TokenCurrency } from "@ledgerhq/types-cryptoassets"; import { updateAccountWithUpdater } from "~/renderer/actions/accounts"; import { BodyContent } from "./BodyContent"; import { BigNumber } from "bignumber.js"; @@ -32,8 +32,15 @@ export type Data = { magnitudeAwareRate?: BigNumber; }; +export enum ExchangeModeEnum { + Sell = "sell", + Swap = "swap", +} + +export type ExchangeMode = "sell" | "swap"; + type ResultsState = { - isSell?: boolean; + mode: ExchangeMode; swapId?: string; provider: string; sourceCurrency: Currency; @@ -147,44 +154,73 @@ const Body = ({ data, onClose }: { data: Data; onClose?: () => void | undefined [dispatch, exchange, transactionParams, provider], ); - const onBroadcastSuccess = useCallback( - (operation: Operation) => { - // If swap we save to swap history and keep open the drawer - if (swapId && toAccount && magnitudeAwareRate && sourceCurrency && targetCurrency) { - const newResult = { - operation, - swapId, - }; - updateAccount({ - result: newResult, - magnitudeAwareRate, - }); - setResult({ + const getResultByTransactionType = ( + isSwapTransaction: "" | CryptoCurrency | TokenCurrency | null | undefined, + ) => { + return isSwapTransaction + ? { swapId, + mode: ExchangeModeEnum.Swap, provider, - sourceCurrency, - targetCurrency, - }); - - if (getEnv("DISABLE_TRANSACTION_BROADCAST")) { - return onCancel(new DisabledTransactionBroadcastError()); + sourceCurrency: sourceCurrency as Currency, + targetCurrency: targetCurrency as Currency, } - onResult(operation); - } else if ( + : { + provider, + mode: ExchangeModeEnum.Sell, + sourceCurrency: sourceCurrency as Currency, + }; + }; + + const handleSwapTransaction = (operation: Operation, result: ResultsState) => { + const newResult = { + operation, + swapId: swapId as string, + }; + + updateAccount({ + result: newResult, + magnitudeAwareRate: magnitudeAwareRate as BigNumber, + }); + + setResult(result); + + onResult(operation); + }; + + const handleSellTransaction = (operation: Operation, result: ResultsState) => { + setResult(result); + + onResult(operation); + }; + + const onBroadcastSuccess = useCallback( + (operation: Operation) => { + if (getEnv("DISABLE_TRANSACTION_BROADCAST")) { + return onCancel(new DisabledTransactionBroadcastError()); + } + + const isSwapTransaction = + swapId && toAccount && magnitudeAwareRate && sourceCurrency && targetCurrency; + + const isSellTransaction = (data.exchangeType === ExchangeType.SELL || data.exchangeType === ExchangeType.SELL_NG) && - sourceCurrency - ) { - if (getEnv("DISABLE_TRANSACTION_BROADCAST")) { - return onCancel(new DisabledTransactionBroadcastError()); - } - onResult(operation); - setResult({ provider, isSell: true, sourceCurrency }); + sourceCurrency; + + const result = getResultByTransactionType(isSwapTransaction); + + if (isSwapTransaction) { + handleSwapTransaction(operation, result); + } else if (isSellTransaction) { + handleSellTransaction(operation, result); } else { - // else not swap i.e card we close the drawer + // old platform exchange flow onResult(operation); onClose?.(); } }, + // Disabling exhaustive-deps because adding handleSellTransaction and handleSwapTransaction would make it change on every render + // eslint-disable-next-line react-hooks/exhaustive-deps [ swapId, toAccount, diff --git a/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/BodyContent.tsx b/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/BodyContent.tsx index 7e1527b0b0c7..6b62f9069d07 100644 --- a/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/BodyContent.tsx +++ b/apps/ledger-live-desktop/src/renderer/modals/Platform/Exchange/CompleteExchange/BodyContent.tsx @@ -11,6 +11,7 @@ import DeviceAction from "~/renderer/components/DeviceAction"; import BigSpinner from "~/renderer/components/BigSpinner"; import ErrorDisplay from "~/renderer/components/ErrorDisplay"; import { TransactionBroadcastedContent } from "./TransactionBroadcastedContent"; +import { ExchangeMode } from "./Body"; const exchangeAction = createAction(completeExchange); const sendAction = txCreateAction(connectApp); @@ -37,8 +38,7 @@ export type BodyContentProps = { }; result?: { swapId?: string; - // The isSell will probably be replaced with a sellId similar with swapId. - isSell?: boolean; + mode: ExchangeMode; provider: string; sourceCurrency: Currency; targetCurrency?: Currency; @@ -59,7 +59,7 @@ export const BodyContent = (props: BodyContentProps) => { return ( @@ -32,9 +31,9 @@ export function TransactionBroadcastedContent(props: TransactionBroadcastedConte sourceCurrency={sourceCurrency?.name} targetCurrency={targetCurrency?.name} provider={provider} - {...(swapId ? swapDefaultTrack : sellDefaultTrack)} + {...(mode === ExchangeModeEnum.Swap && swapId && swapDefaultTrack)} /> - {swapId && targetCurrency && ( + {mode === ExchangeModeEnum.Swap && swapId && targetCurrency && ( <> )} - {isSell && sourceCurrency && ( + {mode === ExchangeModeEnum.Sell && sourceCurrency && ( <> diff --git a/apps/ledger-live-desktop/src/renderer/screens/exchange/Sell/utils/index.ts b/apps/ledger-live-desktop/src/renderer/screens/exchange/Sell/utils/index.ts deleted file mode 100644 index d1f002008f5b..000000000000 --- a/apps/ledger-live-desktop/src/renderer/screens/exchange/Sell/utils/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -export const SELL_VERSION = "1.00"; - -const SELL_TRACKING_PROPERTIES = { - swapVersion: SELL_VERSION, - flow: "sell", -}; - -export const useGetSellTrackingProperties = () => { - return SELL_TRACKING_PROPERTIES; -}; diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts index 1f6e2b28f4cd..dbd1d9a476ff 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts @@ -22,7 +22,6 @@ import { ExchangeType, ExchangeStartSellParams, SwapLiveError, - ExchangeCompleteSwapParams, } from "@ledgerhq/wallet-api-exchange-module"; import { decodePayloadProtobuf } from "@ledgerhq/hw-app-exchange"; import { TrackingAPI } from "./tracking";