diff --git a/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx b/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx index 9ab312f51730..c13368098179 100644 --- a/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx +++ b/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx @@ -7,7 +7,7 @@ import { getCustomFeesPerFamily, convertToNonAtomicUnit, } from "@ledgerhq/live-common/exchange/swap/webApp/index"; -import { getProviderName, getCustomDappUrl } from "@ledgerhq/live-common/exchange/swap/utils/index"; +import { getProviderName } from "@ledgerhq/live-common/exchange/swap/utils/index"; import React, { useCallback, useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDispatch, useSelector } from "react-redux"; @@ -227,14 +227,7 @@ const SwapForm = () => { if (providerType === "DEX") { const from = swapTransaction.swap.from; const fromAccountId = from.parentAccount?.id || from.account?.id; - const customParams = { - provider, - providerURL, - } as { - provider: string; - providerURL?: string; - }; - const customDappUrl = getCustomDappUrl(customParams); + const pathname = `/platform/${getProviderName(provider).toLowerCase()}`; const getAccountId = ({ accountId, @@ -262,7 +255,7 @@ const SwapForm = () => { // This looks like an issue, the proper signature is: push(path, [state]) - (function) Pushes a new entry onto the history stack // It seems possible to also pass a LocationDescriptorObject but it does not expect extra properties // @ts-expect-error so customDappUrl is not expected to be here - customDappUrl, + customDappUrl: providerURL, pathname, state: { returnTo: "/swap", diff --git a/apps/ledger-live-desktop/tests/specs/services/services-api-mocks/getRates.mock.ts b/apps/ledger-live-desktop/tests/specs/services/services-api-mocks/getRates.mock.ts index 0cb44df36467..4da07ec73352 100644 --- a/apps/ledger-live-desktop/tests/specs/services/services-api-mocks/getRates.mock.ts +++ b/apps/ledger-live-desktop/tests/specs/services/services-api-mocks/getRates.mock.ts @@ -42,7 +42,7 @@ export const getEthereumToTetherRatesMock = () => { provider: "oneinch", providerType: "DEX", providerURL: - "/platform/1inch/#/1/unified/swap/eth/usdt?sourceTokenAmount=10.135255432293584185", + "https://app.1inch.io/#/1/unified/swap/eth/usdt?https://app.1inch.isourceTokenAmount=10.135255432293584185", from: "ethereum", to: "ethereum/erc20/usd_tether__erc20_", amountFrom: "10.135255432293584185", @@ -56,7 +56,7 @@ export const getEthereumToTetherRatesMock = () => { provider: "paraswap", providerType: "DEX", providerURL: - "/platform/paraswap/#/0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-0xdac17f958d2ee523a2206206994597c13d831ec7/10.135255432293584185?network=1", + "https://embedded.paraswap.io?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false&network=1#/0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-0xdac17f958d2ee523a2206206994597c13d831ec7/10.135255432293584185", from: "ethereum", to: "ethereum/erc20/usd_tether__erc20_", amountFrom: "10.135255432293584185", diff --git a/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx b/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx index 1baad9af3347..f1e3a0edace2 100644 --- a/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx +++ b/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx @@ -4,7 +4,6 @@ import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view import { Button, Flex } from "@ledgerhq/native-ui"; import { OnNoRatesCallback } from "@ledgerhq/live-common/exchange/swap/types"; import { useSwapTransaction, usePageState } from "@ledgerhq/live-common/exchange/swap/hooks/index"; -import { getCustomDappUrl } from "@ledgerhq/live-common/exchange/swap/utils/index"; import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; import { useDispatch, useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; @@ -216,15 +215,6 @@ export function SwapForm({ if (providerType === "DEX") { const from = swapTransaction.swap.from; const fromAccountId = from.parentAccount?.id || from.account?.id; - const customParams = { - provider, - providerURL, - } as { - provider: string; - providerURL?: string; - }; - - const customDappUrl = getCustomDappUrl(customParams); const getAccountId = ({ accountId, @@ -253,7 +243,7 @@ export function SwapForm({ platform: getProviderName(provider).toLowerCase(), name: getProviderName(provider), accountId, - customDappURL: customDappUrl, + customDappURL: providerURL, }); } else { setConfirmed(true); diff --git a/libs/ledger-live-common/src/exchange/swap/api/v5/__mocks__/fetchRates.mocks.ts b/libs/ledger-live-common/src/exchange/swap/api/v5/__mocks__/fetchRates.mocks.ts index b23d717e99cf..f026dc261f74 100644 --- a/libs/ledger-live-common/src/exchange/swap/api/v5/__mocks__/fetchRates.mocks.ts +++ b/libs/ledger-live-common/src/exchange/swap/api/v5/__mocks__/fetchRates.mocks.ts @@ -58,7 +58,8 @@ export const fetchRatesMock = ( { provider: "oneinch", providerType: "DEX", - providerURL: "/platform/1inch/#/1/unified/swap/usdt/eth?sourceTokenAmount=100", + providerURL: + "https://app.1inch.io/#/1/unified/swap/usdt/eth?ledgerLive=true&sourceTokenAmount=100", from: "ethereum/erc20/usd_tether__erc20_", to: "ethereum", amountFrom: "100", diff --git a/libs/ledger-live-common/src/exchange/swap/mock.ts b/libs/ledger-live-common/src/exchange/swap/mock.ts index 3f7923b758f3..ebeae62b72f2 100644 --- a/libs/ledger-live-common/src/exchange/swap/mock.ts +++ b/libs/ledger-live-common/src/exchange/swap/mock.ts @@ -103,7 +103,7 @@ export const mockGetExchangeRates = async ( providerType: "DEX", expirationDate: new Date(), tradeMethod: "float", - providerURL: `/platform/1inch/#/1/unified/swap/eth/usdt?sourceTokenAmount=${transaction.amount}`, + providerURL: `https://app.1inch.io/#/1/unified/swap/eth/usdt?ledgerLive=true&sourceTokenAmount=${transaction.amount}`, }, ]; }; diff --git a/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts b/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts index 0ecb5c0cd3bf..b37514b61812 100644 --- a/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts +++ b/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts @@ -10,7 +10,6 @@ import { isRegistrationRequired, getProviderName, getNoticeType, - getCustomDappUrl, } from "./index"; /* TODO: Refacto these two function and move them to mock/account.ts if needed */ @@ -221,32 +220,3 @@ describe("swap/utils/getNoticeType", function () { expect(result).toEqual(expectedResult); }); }); - -describe("swap/utils/getCustomDappUrl", () => { - it("should convert correct paraswap URL", async () => { - const customDappUrl = getCustomDappUrl({ - provider: "paraswap", - providerURL: - "/platform/paraswap/#/0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-0xdac17f958d2ee523a2206206994597c13d831ec7/0.004?network=1", - }); - expect(customDappUrl).toBe( - "https://embedded.paraswap.io?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false&network=1#/0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-0xdac17f958d2ee523a2206206994597c13d831ec7/0.004", - ); - }); - it("should convert correct 1inch URL", async () => { - const customDappUrl = getCustomDappUrl({ - provider: "oneinch", - providerURL: "/platform/1inch/#/1/unified/swap/eth/usdt?sourceTokenAmount=0.04", - }); - expect(customDappUrl).toBe( - "https://app.1inch.io/#/1/simple/swap/eth/usdt?ledgerLive=true&sourceTokenAmount=0.04", - ); - }); - it("should not update URL when complete URL is provided", async () => { - const customDappUrl = getCustomDappUrl({ - provider: "oneinch", - providerURL: "https://app.1inch.io/#/test", - }); - expect(customDappUrl).toBe("https://app.1inch.io/#/test"); - }); -}); diff --git a/libs/ledger-live-common/src/exchange/swap/utils/index.ts b/libs/ledger-live-common/src/exchange/swap/utils/index.ts index 33b4a8daf26f..6697399c67dc 100644 --- a/libs/ledger-live-common/src/exchange/swap/utils/index.ts +++ b/libs/ledger-live-common/src/exchange/swap/utils/index.ts @@ -84,73 +84,3 @@ export const getNoticeType = (provider: string): { message: string; learnMore: b return { message: "default", learnMore: true }; } }; - -const isValidUrl = (urlString: string) => { - try { - return Boolean(new URL(urlString)); - } catch (e) { - return false; - } -}; - -/** - * Get complete DAPP URL - * @param provider - * @param providerURL - * - * This Func is to ensure a complete DAPP URL is generated if partial & incorrect path is provided. - * - * Example 1: - * actual: /platform/paraswap/#/0xdac17f958d2ee523a2206206994597c13d831ec7-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599/21.3?network=1 - * expected: https://embedded.paraswap.io?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false&network=1#/0xdac17f958d2ee523a2206206994597c13d831ec7-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599/21.3 - * - * Example 2: - * expected: /platform/1inch/#/1/unified/swap/usdt/shib?sourceTokenAmount=24.6 - * actual: https://app.1inch.io/#/1/simple/swap/usdt/shib?ledgerLive=true&sourceTokenAmount=24.6 - */ -export const getCustomDappUrl = ({ - provider, - providerURL = "", -}: { - provider: string; - providerURL?: string; -}): string => { - if (isValidUrl(providerURL)) { - return providerURL; - } - - const dappUrl = - provider === "paraswap" - ? "https://embedded.paraswap.io/?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false" - : "https://app.1inch.io/?ledgerLive=true"; - const newUrl = `https://www.prefix.com/${providerURL}`; - const isValidNewdUrl = isValidUrl(newUrl); - - if (isValidNewdUrl) { - const { origin, search } = new URL(dappUrl); - const { hash: fragment, searchParams } = new URL(newUrl); - const [realFragment, query] = fragment.split("?"); - const urlSearchParams = new URLSearchParams(query); - const allParams = { - ...Object.fromEntries(new URLSearchParams(search)), - ...Object.fromEntries(urlSearchParams.entries()), - ...Object.fromEntries(searchParams), - }; - - /** - * Providers should use the standard structure: query + fragment - * - * 1inch is currently not using the standard (fragment + query). To be refactored once providers follow the standard structure. - * @see https://www.rfc-editor.org/rfc/rfc3986#section-4.2 - */ - const newDappUrl = - provider === "oneinch" - ? `${origin}/${realFragment}?${new URLSearchParams(allParams).toString()}`.replace( - "/unified/", - "/simple/", - ) - : `${origin}?${new URLSearchParams(allParams).toString()}${realFragment}`; - return newDappUrl; - } - return ""; -};