From e66d3714e5e4fd6a8e3f5980a463b3bb61f40c4b Mon Sep 17 00:00:00 2001 From: Thomas Brillard Date: Fri, 18 Oct 2024 21:50:22 +0200 Subject: [PATCH] feat: thorswap erc20 swap (#8146) --- .changeset/modern-wolves-pretend.md | 5 ++++ .../src/exchange/swap/completeExchange.ts | 25 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .changeset/modern-wolves-pretend.md diff --git a/.changeset/modern-wolves-pretend.md b/.changeset/modern-wolves-pretend.md new file mode 100644 index 000000000000..7a09be7ef3ed --- /dev/null +++ b/.changeset/modern-wolves-pretend.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": minor +--- + +swap Thorswap ERC20 token exception diff --git a/libs/ledger-live-common/src/exchange/swap/completeExchange.ts b/libs/ledger-live-common/src/exchange/swap/completeExchange.ts index 54659f02b70e..4f4e0fd15692 100644 --- a/libs/ledger-live-common/src/exchange/swap/completeExchange.ts +++ b/libs/ledger-live-common/src/exchange/swap/completeExchange.ts @@ -24,6 +24,7 @@ import { getSwapProvider } from "../providers"; import { convertToAppExchangePartnerKey } from "../providers"; import { CompleteExchangeStep, convertTransportError } from "../error"; import { getDefaultAccountName } from "@ledgerhq/live-wallet/accountName"; +import BigNumber from "bignumber.js"; const withDevicePromise = (deviceId, fn) => firstValueFrom(withDevice(deviceId)(transport => from(fn(transport)))); @@ -65,7 +66,29 @@ const completeExchange = ( if (mainRefundCurrency.type !== "CryptoCurrency") throw new Error("This should be a cryptocurrency"); - transaction = await accountBridge.prepareTransaction(refundAccount, transaction); + // Thorswap ERC20 token exception hack: + // - We remove subAccountId to prevent EVM calldata swap during prepareTransaction. + // - Set amount to 0 to ensure correct handling of the transaction + // (this is adjusted during prepareTransaction before signing the actual EVM transaction for tokens but we skip it). + // - Since it's an ERC20 token transaction (not ETH), amount is set to 0 ETH + // because no ETH is being sent, only tokens. + // - This workaround can't be applied earlier in the flow as the amount is used for display purposes and checks. + // We must set the amount to 0 at this stage to avoid issues during the transaction. + // - This ensures proper handling of Thorswap-ERC20-specific transactions. + if ( + provider.toLocaleLowerCase() === "thorswap" && + transaction.subAccountId && + transaction.family === "evm" + ) { + const transactionFixed = { + ...transaction, + subAccountId: undefined, + amount: BigNumber(0), + }; + transaction = await accountBridge.prepareTransaction(refundAccount, transactionFixed); + } else { + transaction = await accountBridge.prepareTransaction(refundAccount, transaction); + } if (unsubscribed) return; const { errors, estimatedFees } = await accountBridge.getTransactionStatus(