Skip to content

Commit

Permalink
feat: support customFees in swap web app mode (#4174)
Browse files Browse the repository at this point in the history
* feat: support customFees in swap web app mode

* fix: add other custom params

* feat: update wallet-api dependencies

* fix: fix async call getCustomFeesPerFamily

* fix: fix typecheck

* refactor: move getCustomFeesPerFamily to common

* fix: fix lint

* fix: remove feesStrategy as mandatory to open swap web app

* fix: add convertToNonAtomicUnit utils

* chore: update wallet-api server call after upgrade

* fix: fix lint
  • Loading branch information
sarneijim authored Aug 29, 2023
1 parent 1e5c2e5 commit 5d20c32
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .changeset/mighty-fireants-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"ledger-live-desktop": patch
"@ledgerhq/live-common": patch
---

Support customFees in swap web app mode
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
useSwapTransaction,
usePageState,
} from "@ledgerhq/live-common/exchange/swap/hooks/index";
import {
getCustomFeesPerFamily,
convertToNonAtomicUnit,
} from "@ledgerhq/live-common/exchange/swap/webApp/index";
import { getProviderName, getCustomDappUrl } from "@ledgerhq/live-common/exchange/swap/utils/index";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -39,7 +43,6 @@ import EmptyState from "./Rates/EmptyState";
import { AccountLike, Feature } from "@ledgerhq/types-live";
import BigNumber from "bignumber.js";
import { CryptoCurrency, TokenCurrency } from "@ledgerhq/types-cryptoassets";
import { SwapSelectorStateType } from "@ledgerhq/live-common/exchange/swap/types";
import { SWAP_RATES_TIMEOUT } from "../../config";

const Wrapper = styled(Box).attrs({
Expand Down Expand Up @@ -133,6 +136,7 @@ const SwapForm = () => {
const refreshTime = useRefreshRates(swapTransaction.swap, {
pause: pauseRefreshing,
});

const refreshIdle = useCallback(() => {
idleState && setIdleState(false);
idleTimeout.current && clearInterval(idleTimeout.current);
Expand All @@ -141,19 +145,20 @@ const SwapForm = () => {
}, idleTime);
}, [idleState]);

const swapWebAppRedirection = useCallback(() => {
const swapWebAppRedirection = useCallback(async () => {
const { to, from } = swapTransaction.swap;
const transaction = swapTransaction.transaction;
const { account: fromAccount, parentAccount: fromParentAccount } = from;
const { account: toAccount, parentAccount: toParentAccount } = to;
const feesStrategy = transaction?.feesStrategy;
const rateId = exchangeRate?.rateId || "1234";
if (fromAccount && toAccount && feesStrategy) {
const { feesStrategy } = transaction || {};

const rateId = exchangeRate?.rateId || "12345";
if (fromAccount && toAccount) {
const fromAccountId = accountToWalletAPIAccount(fromAccount, fromParentAccount)?.id;
const toAccountId = accountToWalletAPIAccount(toAccount, toParentAccount)?.id;
const fromMagnitude =
(fromAccount as unknown as SwapSelectorStateType)?.currency?.units[0].magnitude || 0;
const fromAmount = transaction?.amount.shiftedBy(-fromMagnitude);
const fromAmount = convertToNonAtomicUnit(transaction?.amount, fromAccount);

const customFeesParams = feesStrategy === "custom" ? getCustomFeesPerFamily(transaction) : {};

history.push({
pathname: "/swap-web",
Expand All @@ -163,11 +168,12 @@ const SwapForm = () => {
toAccountId,
fromAmount,
quoteId: encodeURIComponent(rateId),
feeStrategy: feesStrategy.toUpperCase(), // Custom fee is not supported yet
feeStrategy: feesStrategy?.toUpperCase(),
...customFeesParams,
},
});
}
}, [history, swapTransaction, provider, exchangeRate?.rateId]);
}, [swapTransaction.swap, swapTransaction.transaction, exchangeRate?.rateId, history, provider]);

useEffect(() => {
if (swapTransaction.swap.rates.status === "success") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const completeExchange = (
const payoutAccount = getMainAccount(toAccount, toParentAccount);
const accountBridge = getAccountBridge(refundAccount);
const mainPayoutCurrency = getAccountCurrency(payoutAccount);
const payoutCurrency = getAccountCurrency(toAccount);
const refundCurrency = getAccountCurrency(fromAccount);
const mainRefundCurrency = getAccountCurrency(refundAccount);
if (mainPayoutCurrency.type !== "CryptoCurrency")
Expand Down Expand Up @@ -126,7 +127,7 @@ const completeExchange = (
if (unsubscribed) return;

const { config: payoutAddressConfig, signature: payoutAddressConfigSignature } =
getCurrencyExchangeConfig(mainPayoutCurrency);
getCurrencyExchangeConfig(payoutCurrency);

try {
currentStep = "CHECK_PAYOUT_ADDRESS";
Expand Down
1 change: 1 addition & 0 deletions libs/ledger-live-common/src/exchange/swap/webApp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./utils";
41 changes: 41 additions & 0 deletions libs/ledger-live-common/src/exchange/swap/webApp/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getGasLimit as getEthGasLimit } from "../../../families/ethereum/transaction";
import { getGasLimit as getEvmGasLimit } from "@ledgerhq/coin-evm/logic";

export const getCustomFeesPerFamily = transaction => {
const { family, maxFeePerGas, maxPriorityFeePerGas, userGasLimit, customGasLimit, feePerByte } =
transaction;

switch (family) {
case "ethereum": {
return {
maxFeePerGas,
maxPriorityFeePerGas,
userGasLimit,
gasLimit: getEthGasLimit(transaction),
};
}
case "evm": {
return {
maxFeePerGas,
maxPriorityFeePerGas,
gasLimit: getEvmGasLimit(transaction),
customGasLimit,
};
}
case "bitcoin": {
return {
feePerByte,
};
}
default:
return {};
}
};

export const convertToNonAtomicUnit = (amount, account) => {
const fromMagnitude =
account.type === "TokenAccount"
? account.token.units[0].magnitude || 0
: account.currency?.units[0].magnitude || 0;
return amount.shiftedBy(-fromMagnitude);
};
32 changes: 12 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 5d20c32

@vercel
Copy link

@vercel vercel bot commented on 5d20c32 Aug 29, 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.