Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduma-ledger committed Sep 3, 2024
1 parent 10f99fd commit 817f7e2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -59,7 +59,7 @@ export const BodyContent = (props: BodyContentProps) => {
return (
<TransactionBroadcastedContent
swapId={props.result.swapId}
isSell={props.result.isSell}
mode={props.result.mode}
provider={props.result.provider}
sourceCurrency={props.result.sourceCurrency}
targetCurrency={props.result.targetCurrency}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ import { Separator } from "~/renderer/screens/exchange/Swap2/Form/Separator";
import Button from "~/renderer/components/Button";
import SwapCompleted from "~/renderer/screens/exchange/Swap2/Form/ExchangeDrawer/SwapCompleted";
import { useGetSwapTrackingProperties } from "~/renderer/screens/exchange/Swap2/utils";
import { useGetSellTrackingProperties } from "~/renderer/screens/exchange/Sell/utils";
import TrackPage from "~/renderer/analytics/TrackPage";
import { Currency } from "@ledgerhq/types-cryptoassets";
import SellCompleted from "~/renderer/screens/exchange/Sell/SellCompleted";
import { ExchangeMode, ExchangeModeEnum } from "./Body";

type TransactionBroadcastedContentProps = {
swapId?: string;
isSell?: boolean;
mode: ExchangeMode;
provider: string;
sourceCurrency: Currency;
targetCurrency?: Currency;
onViewDetails: (id: string) => void;
};

export function TransactionBroadcastedContent(props: TransactionBroadcastedContentProps) {
const { swapId, provider, sourceCurrency, targetCurrency, onViewDetails, isSell } = props;
const { swapId, provider, sourceCurrency, targetCurrency, onViewDetails, mode } = props;
const swapDefaultTrack = useGetSwapTrackingProperties();
const sellDefaultTrack = useGetSellTrackingProperties();

return (
<Box height="100%" justifyContent="space-between" paddingTop={62} paddingBottom={15}>
Expand All @@ -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 && (
<>
<Box justifyContent="center" flex={1}>
<SwapCompleted
Expand All @@ -53,7 +52,7 @@ export function TransactionBroadcastedContent(props: TransactionBroadcastedConte
</Box>
</>
)}
{isSell && sourceCurrency && (
{mode === ExchangeModeEnum.Sell && sourceCurrency && (
<>
<Box>
<SellCompleted />
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion libs/ledger-live-common/src/wallet-api/Exchange/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 817f7e2

Please sign in to comment.