Skip to content

Commit

Permalink
[Bugfix/LIVE-11850]: Add additional properties to the accepted & canc…
Browse files Browse the repository at this point in the history
…elled swap API calls. (#6612)

* fix: add LLD and common updates

* fix: add properties to accepted swap on LLM

* chore: add changeset

* fix: use knownDevicesSelector on LLM

* fix: add further properties to swap api calls
  • Loading branch information
kallen-ledger committed Apr 5, 2024
1 parent 433bfec commit 12daa05
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/sharp-camels-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"ledger-live-desktop": patch
"live-mobile": patch
"@ledgerhq/live-common": patch
---

Add additional properties to the accepted and cancelled swap api calls
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { postSwapCancelled } from "@ledgerhq/live-common/exchange/swap/index";
import { setBroadcastTransaction } from "@ledgerhq/live-common/exchange/swap/setBroadcastTransaction";
import { getUpdateAccountWithUpdaterParams } from "@ledgerhq/live-common/exchange/swap/getUpdateAccountWithUpdaterParams";
import { CompleteExchangeError } from "@ledgerhq/live-common/exchange/error";
import {
ExchangeSwap,
SwapTransactionType,
ExchangeRate,
} from "@ledgerhq/live-common/exchange/swap/types";
import React, { useCallback, useMemo, useState } from "react";
import { Trans } from "react-i18next";
import { useDispatch } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
import styled from "styled-components";
import TrackPage from "~/renderer/analytics/TrackPage";
import { track } from "~/renderer/analytics/segment";
Expand All @@ -28,6 +29,8 @@ import SwapAction from "./SwapAction";
import SwapCompleted from "./SwapCompleted";
import { Operation } from "@ledgerhq/types-live";
import { BigNumber } from "bignumber.js";
import { getCurrentDevice } from "~/renderer/reducers/devices";
import { rateSelector } from "~/renderer/actions/swap";

const ContentBox = styled(Box)`
${DeviceActionHeader} {
Expand All @@ -52,6 +55,8 @@ export default function ExchangeDrawer({ swapTransaction, exchangeRate, onComple
swapId: string;
} | null>(null);
const swapDefaultTrack = useGetSwapTrackingProperties();
const device = useSelector(getCurrentDevice);
const selectedExchangeRate = useSelector(rateSelector);
const redirectToHistory = useRedirectToSwapHistory();
const {
transaction,
Expand Down Expand Up @@ -79,6 +84,13 @@ export default function ExchangeDrawer({ swapTransaction, exchangeRate, onComple
postSwapCancelled({
provider: exchangeRate.provider,
swapId: swapId ?? "",
...(error instanceof CompleteExchangeError ? { swapStep: error.step } : {}),
statusCode: error.name,
errorMessage: error.message,
sourceCurrencyId: swapTransaction.swap.from.currency?.id,
targetCurrencyId: swapTransaction.swap.to.currency?.id,
hardwareWalletType: device?.modelId,
swapType: selectedExchangeRate?.tradeMethod,
});
track("error_message", {
message: "drawer_error",
Expand All @@ -88,6 +100,7 @@ export default function ExchangeDrawer({ swapTransaction, exchangeRate, onComple
});
setError(error);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[exchangeRate.provider, swapDefaultTrack],
);

Expand All @@ -105,6 +118,10 @@ export default function ExchangeDrawer({ swapTransaction, exchangeRate, onComple
setBroadcastTransaction({
result,
provider,
sourceCurrencyId: swapTransaction.swap.from.currency?.id,
targetCurrencyId: swapTransaction.swap.to.currency?.id,
hardwareWalletType: device?.modelId,
swapType: selectedExchangeRate?.tradeMethod,
});
const params = getUpdateAccountWithUpdaterParams({
result,
Expand All @@ -119,6 +136,7 @@ export default function ExchangeDrawer({ swapTransaction, exchangeRate, onComple
setResult(result);
onCompleteSwap && onCompleteSwap();
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[dispatch, exchange, exchangeRate, transaction, onCompleteSwap],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,26 @@ export function Confirmation({
* since the partner will never receive the funds
*/
if (getEnv("DISABLE_TRANSACTION_BROADCAST")) {
postSwapCancelled({ provider, swapId });
postSwapCancelled({
provider,
swapId,
swapStep: "SIGN_COIN_TRANSACTION",
statusCode: "DISABLE_TRANSACTION_BROADCAST",
errorMessage: "DISABLE_TRANSACTION_BROADCAST",
sourceCurrencyId: swapTx.current.swap.from.account?.id,
targetCurrencyId: swapTx.current.swap.to.account?.id,
hardwareWalletType: deviceMeta.device.modelId,
swapType: exchangeRate.current.tradeMethod,
});
} else {
postSwapAccepted({
provider,
swapId,
transactionId: operation.hash,
sourceCurrencyId: swapTx.current.swap.from.account?.id,
targetCurrencyId: swapTx.current.swap.to.account?.id,
hardwareWalletType: deviceMeta.device.modelId,
swapType: exchangeRate.current.tradeMethod,
});
}

Expand Down Expand Up @@ -156,6 +170,7 @@ export function Confirmation({
});
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[
toAccount,
fromAccount,
Expand Down
18 changes: 16 additions & 2 deletions apps/ledger-live-mobile/src/screens/Swap/Form/Modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { useMemo, useCallback, useState } from "react";
import { SwapTransactionType, ExchangeRate } from "@ledgerhq/live-common/exchange/swap/types";
import { postSwapCancelled } from "@ledgerhq/live-common/exchange/swap/index";
import { useDispatch } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
import GenericErrorBottomModal from "~/components/GenericErrorBottomModal";
import { Confirmation, DeviceMeta } from "./Confirmation";
import { Terms } from "./Terms";
import { swapAcceptProvider } from "~/actions/settings";
import { useAnalytics } from "~/analytics";
import { sharedSwapTracking } from "../../utils";
import { CompleteExchangeError } from "@ledgerhq/live-common/exchange/error";
import { knownDevicesSelector } from "~/reducers/ble";

export function Modal({
confirmed,
Expand All @@ -25,6 +27,7 @@ export function Modal({
exchangeRate?: ExchangeRate;
}) {
const { track } = useAnalytics();
const [device] = useSelector(knownDevicesSelector);
const dispatch = useDispatch();
const [error, setError] = useState<Error>();
const provider = exchangeRate?.provider;
Expand Down Expand Up @@ -63,10 +66,21 @@ export function Modal({
return;
}
// Consider the swap as cancelled (on provider perspective) in case of error
postSwapCancelled({ provider: exchangeRate.provider, swapId });
postSwapCancelled({
provider: exchangeRate.provider,
swapId: swapId ?? "",
...(error instanceof CompleteExchangeError ? { swapStep: error.step } : {}),
statusCode: error?.name,
errorMessage: error?.message,
sourceCurrencyId: swapTx.swap.from.currency?.id,
targetCurrencyId: swapTx.swap.to.currency?.id,
...(device ? { hardwareWalletType: device.modelId } : {}),
swapType: exchangeRate?.tradeMethod,
});

setError(error);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[exchangeRate, track],
);

Expand Down
13 changes: 7 additions & 6 deletions libs/ledger-live-common/src/exchange/swap/postSwapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export const postSwapAccepted: PostSwapAccepted = async ({
provider,
swapId = "",
transactionId,
...rest
}) => {
if (isIntegrationTestEnv()) return mockPostSwapAccepted({ provider, swapId, transactionId });
if (isIntegrationTestEnv())
return mockPostSwapAccepted({ provider, swapId, transactionId, ...rest });

/**
* Since swapId is requiered by the endpoit, don't call it if we don't have
Expand All @@ -18,12 +20,11 @@ export const postSwapAccepted: PostSwapAccepted = async ({
if (!swapId) {
return null;
}

try {
await network({
method: "POST",
url: `${getSwapAPIBaseURL()}/swap/accepted`,
data: { provider, swapId, transactionId },
data: { provider, swapId, transactionId, ...rest },
});
} catch (error) {
console.error(error);
Expand All @@ -32,8 +33,8 @@ export const postSwapAccepted: PostSwapAccepted = async ({
return null;
};

export const postSwapCancelled: PostSwapCancelled = async ({ provider, swapId = "" }) => {
if (isIntegrationTestEnv()) return mockPostSwapCancelled({ provider, swapId });
export const postSwapCancelled: PostSwapCancelled = async ({ provider, swapId = "", ...rest }) => {
if (isIntegrationTestEnv()) return mockPostSwapCancelled({ provider, swapId, ...rest });

/**
* Since swapId is requiered by the endpoit, don't call it if we don't have
Expand All @@ -47,7 +48,7 @@ export const postSwapCancelled: PostSwapCancelled = async ({ provider, swapId =
await network({
method: "POST",
url: `${getSwapAPIBaseURL()}/swap/cancelled`,
data: { provider, swapId },
data: { provider, swapId, ...rest },
});
} catch (error) {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { getEnv } from "@ledgerhq/live-env";
import { Operation } from "@ledgerhq/types-live";
import { postSwapAccepted, postSwapCancelled } from "./index";
import { DeviceModelId } from "@ledgerhq/devices";
import { TradeMethod } from "./types";

export const setBroadcastTransaction = ({
result,
provider,
sourceCurrencyId,
targetCurrencyId,
hardwareWalletType,
swapType,
}: {
result: { operation: Operation; swapId: string };
provider: string;
sourceCurrencyId?: string;
targetCurrencyId?: string;
hardwareWalletType?: DeviceModelId;
swapType?: TradeMethod;
}) => {
const { operation, swapId } = result;

Expand All @@ -19,12 +29,23 @@ export const setBroadcastTransaction = ({
postSwapCancelled({
provider,
swapId,
swapStep: "SIGN_COIN_TRANSACTION",
statusCode: "DISABLE_TRANSACTION_BROADCAST",
errorMessage: "DISABLE_TRANSACTION_BROADCAST",
sourceCurrencyId,
targetCurrencyId,
hardwareWalletType,
swapType: swapType,
});
} else {
postSwapAccepted({
provider,
swapId,
transactionId: operation.hash,
sourceCurrencyId,
targetCurrencyId,
hardwareWalletType,
swapType,
});
}
};
11 changes: 10 additions & 1 deletion libs/ledger-live-common/src/exchange/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CryptoCurrency, TokenCurrency } from "@ledgerhq/types-cryptoassets";
import { Account, AccountLike, AccountRaw, AccountRawLike, Operation } from "@ledgerhq/types-live";
import { Transaction, TransactionRaw } from "../../generated/types";
import { Result as UseBridgeTransactionResult } from "../../bridge/useBridgeTransaction";
import { DeviceModelId } from "@ledgerhq/devices";

export type ExchangeSwap = {
fromParentAccount: Account | null | undefined;
Expand Down Expand Up @@ -184,7 +185,15 @@ export type SwapStatus = {
type SwapStateRequest = {
provider: string;
swapId: string;
};
} & Partial<{
swapStep: string;
statusCode: string;
errorMessage: string;
sourceCurrencyId: string;
targetCurrencyId: string;
hardwareWalletType: DeviceModelId;
swapType: TradeMethod;
}>;

export type SwapStateAcceptedRequest = SwapStateRequest & {
transactionId: string;
Expand Down

0 comments on commit 12daa05

Please sign in to comment.