Skip to content

Commit

Permalink
fix: fix confirmation message swap web app (#5073)
Browse files Browse the repository at this point in the history
* fix: fix confirmation message swap web app

* chore: add changeset

* fix: add webApp utils
  • Loading branch information
sarneijim authored Oct 17, 2023
1 parent bfb567c commit 9d9f8bb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .changeset/giant-wasps-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"ledger-live-desktop": patch
"@ledgerhq/live-common": patch
---

Fix confirmation message for swap web app
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, Component } from "react";
import BigNumber from "bignumber.js";
import { Trans, useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";
import { Action } from "@ledgerhq/live-common/hw/actions/types";
Expand Down Expand Up @@ -324,15 +325,41 @@ export const DeviceActionDefaultRendering = <R, H extends States, P>({
return renderListingApps();
}

if (completeExchangeStarted && !completeExchangeResult && !completeExchangeError) {
if (completeExchangeStarted && !completeExchangeResult && !completeExchangeError && !isLoading) {
const { exchangeType } = request as { exchangeType: number };

// FIXME: could use a TS enum (when LLD will be in TS) or a JS object instead of raw numbers for switch values for clarity
switch (exchangeType) {
// swap
case 0x00: {
// FIXME: should use `renderSwapDeviceConfirmationV2` but all params not available in hookState for this SDK exchange flow
return <div>{"Confirm swap on your device"}</div>;
const {
transaction,
exchange,
provider,
rate = 1,
amountExpectedTo = 0,
} = request as {
transaction: Transaction;
exchange: Exchange;
provider: string;
rate: number;
amountExpectedTo: number;
};
const { estimatedFees } = hookState;

return renderSwapDeviceConfirmation({
modelId: device.modelId,
type,
transaction,
exchangeRate: {
provider,
rate: new BigNumber(rate),
} as ExchangeRate,
exchange,
swapDefaultTrack,
amountExpectedTo: amountExpectedTo.toString() ?? undefined,
estimatedFees: estimatedFees?.toString() ?? undefined,
});
}

case 0x01: // sell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Exchange, ExchangeSwap } from "@ledgerhq/live-common/exchange/platform/
import { Exchange as SwapExchange } from "@ledgerhq/live-common/exchange/swap/types";
import { setBroadcastTransaction } from "@ledgerhq/live-common/exchange/swap/setBroadcastTransaction";
import { getUpdateAccountWithUpdaterParams } from "@ledgerhq/live-common/exchange/swap/getUpdateAccountWithUpdaterParams";
import { convertParametersToValidFormat } from "@ledgerhq/live-common/exchange/swap/webApp/index";
import { Operation, SignedOperation } from "@ledgerhq/types-live";
import { Transaction } from "@ledgerhq/live-common/generated/types";
import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
Expand All @@ -13,6 +12,7 @@ import { ModalBody } from "~/renderer/components/Modal";
import Box from "~/renderer/components/Box";
import { useBroadcast } from "~/renderer/hooks/useBroadcast";
import { BodyContent } from "./BodyContent";
import { getMagnitudeAwareRate } from "@ledgerhq/live-common/exchange/swap/webApp/index";

export type Data = {
provider: string;
Expand All @@ -32,12 +32,21 @@ const Body = ({ data, onClose }: { data: Data; onClose?: () => void | undefined
const dispatch = useDispatch();
const { onResult, onCancel, swapId, rate, ...exchangeParams } = data;
const { exchange, provider, transaction: transactionParams } = exchangeParams;
const { amount } = transactionParams;
const {
fromAccount: account,
fromParentAccount: parentAccount,
toAccount,
} = exchange as ExchangeSwap;
const request = { ...exchangeParams };

const magnitudeAwareRate = getMagnitudeAwareRate({
fromAccount: account,
toAccount,
rate,
});
const amountExpectedTo = +amount * +magnitudeAwareRate;

const request = { ...exchangeParams, amountExpectedTo };

const tokenCurrency: TokenCurrency | undefined =
account.type === "TokenAccount" ? account.token : undefined;
Expand Down Expand Up @@ -72,13 +81,10 @@ const Body = ({ data, onClose }: { data: Data; onClose?: () => void | undefined
broadcast(signedOperation).then(operation => {
// Save swap history
if (swapId && rate && toAccount) {
const { result, magnitudeAwareRate } = convertParametersToValidFormat({
const result = {
operation,
swapId,
fromAccount: account,
toAccount,
rate,
});
};
setBroadcastTransaction({
result,
provider,
Expand Down Expand Up @@ -112,6 +118,7 @@ const Body = ({ data, onClose }: { data: Data; onClose?: () => void | undefined
onResult,
signedOperation,
transaction,
magnitudeAwareRate,
]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const completeExchange = (

o.next({
type: "complete-exchange-requested",
estimatedFees,
estimatedFees: estimatedFees.toString(),
});

if (unsubscribed) return;
Expand Down
3 changes: 1 addition & 2 deletions libs/ledger-live-common/src/exchange/platform/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { Account, AccountLike, AccountRaw, AccountRawLike } from "@ledgerhq/types-live";
import { BigNumber } from "bignumber.js";
import type { Transaction } from "../../generated/types";
import { ExchangeTypes, RateTypes } from "@ledgerhq/hw-app-exchange";

export type CompleteExchangeRequestEvent =
| { type: "complete-exchange" }
| {
type: "complete-exchange-requested";
estimatedFees: BigNumber;
estimatedFees: string;
}
| {
type: "complete-exchange-error";
Expand Down
10 changes: 5 additions & 5 deletions libs/ledger-live-common/src/exchange/swap/completeExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ const completeExchange = (
throw convertTransportError(currentStep, e);
}

o.next({
type: "complete-exchange-requested",
estimatedFees: estimatedFees.toString(),
});

// Swap specific checks to confirm the refund address is correct.
if (unsubscribed) return;
const refundAddressParameters = await perFamily[
Expand Down Expand Up @@ -182,11 +187,6 @@ const completeExchange = (
throw convertTransportError(currentStep, e);
}

o.next({
type: "complete-exchange-requested",
estimatedFees,
});

if (unsubscribed) return;
ignoreTransportError = true;
currentStep = "SIGN_COIN_TRANSACTION";
Expand Down
18 changes: 3 additions & 15 deletions libs/ledger-live-common/src/exchange/swap/webApp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,9 @@ export const convertToNonAtomicUnit = (amount, account) => {
return amount.shiftedBy(-fromMagnitude);
};

export const convertParametersToValidFormat = ({
operation,
swapId,
fromAccount,
toAccount,
rate,
}) => {
const result = { operation, swapId };
export const getMagnitudeAwareRate = ({ fromAccount, toAccount, rate }): BigNumber => {
const unitFrom = getAccountUnit(fromAccount);
const unitTo = getAccountUnit(toAccount);
const magnitudeAwareRate = new BigNumber(rate).div(
new BigNumber(10).pow(unitFrom.magnitude - unitTo.magnitude),
);
return {
result,
magnitudeAwareRate,
};
const magnitudeAwareRate = new BigNumber(rate).shiftedBy(unitTo.magnitude - unitFrom.magnitude);
return magnitudeAwareRate;
};
6 changes: 4 additions & 2 deletions libs/ledger-live-common/src/hw/actions/completeExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type State = {
freezeReduxDevice: boolean;
completeExchangeRequested: boolean;
isLoading: boolean;
estimatedFees: string | undefined;
};

type CompleteExchangeState = AppState & State;
Expand All @@ -39,7 +40,7 @@ type Result =
type CompleteExchangeAction = Action<CompleteExchangeRequest, CompleteExchangeState, Result>;
export type ExchangeRequestEvent =
| { type: "complete-exchange" }
| { type: "complete-exchange-requested" }
| { type: "complete-exchange-requested"; estimatedFees: string }
| { type: "complete-exchange-error"; error: Error }
| { type: "complete-exchange-result"; completeExchangeResult: Transaction };

Expand All @@ -63,6 +64,7 @@ const initialState: State = {
completeExchangeRequested: false,
freezeReduxDevice: false,
isLoading: true,
estimatedFees: undefined,
};

const reducer = (state: State, e: ExchangeRequestEvent) => {
Expand All @@ -84,7 +86,7 @@ const reducer = (state: State, e: ExchangeRequestEvent) => {
case "complete-exchange-requested":
return {
...state,
completeExchangeRequested: true,
estimatedFees: e.estimatedFees,
isLoading: false,
};
case "complete-exchange-result":
Expand Down

1 comment on commit 9d9f8bb

@vercel
Copy link

@vercel vercel bot commented on 9d9f8bb Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.